0

So I have several different agent types: Person, Factory, Hospital, Home, Doctor. Now, all of these agents except Person are connected through a network, while the initial population size of Person = 0.

Now, when the model runs, Person agents will be generated with a given rate. What I want to accomplish is that each instance of Person determines which instance of Factory, Hospital or Home is the nearest, and then makes a connection to that particular agent.

How would I accomplish this?

I have so far been able to let instances of Person connect to the nearest Hospital, the nearest Factory, or the nearest Home. I wrote the following code in the entry action box in a Person's statechart:

Hospital nearestHospital = this.getNearestAgent(main.Hospital); this.connectTo(nearestHospital);

But I have been unable to let the instances of Person determine the nearest instance of Hospital, Factory, Home concurrently.

mivandev
  • 321
  • 2
  • 14

1 Answers1

1

In your Person agent you have to create 3 link to agents as you can see in the following figure: You will find them in the Agent palette.

Person Agent

After that, you have to create connections independently for hospital, home and factory. (since they are different agents)

Hospital nearestHospital = this.getNearestAgent(main.hospitals);
Home nearestHome = this.getNearestAgent(main.homes);
Factory nearestFactory = this.getNearestAgent(main.factories);
double distanceToHospital=distanceTo(nearestHospital);
double distanceToHome=distanceTo(nearestHome);
double distanceToFactory=distanceTo(nearestFactory);

hospitalLink.disconnectFromAll();
homeLink.disconnectFromAll();
factoryLink.disconnectFromAll();

if(distanceToHospital<distanceToHome && distanceToHospital<distanceToFactory)
   hospitalLink.connectTo(neareastHospital);
else if(distanceToHome < distanceToFactory)
   homeLink.connectTo(neareastHome);
else
   factoryLink.connectTo(nearestFactory);

That's the way it must be done... what you do later with it... I don't know

Felipe
  • 8,311
  • 2
  • 15
  • 31
  • Thank you, Felipe. However, this is not what I meant. I want the person agent to consider hospital, factory and home concurrently and connect to the nearest one, so that person is connected to only one of hospital, factory and home. – mivandev Feb 02 '18 at 06:26
  • How would I modify this if I would like to find out what the second nearest instance of Hospital is? So I have first defined the nearestHospital, and now I'd like to know the second nearest agent. So far my search has not found a clearcut solution to this problem. – mivandev Feb 12 '18 at 14:11
  • 2
    Something like this List hospitalList=sortAscending( hospitals, h -> h.distanceTo(yourAgent) ); Hospital nearest=hospitalList.get(0); Hospital secondNearest=hospitalList.get(1); – Felipe Feb 13 '18 at 03:47