mivandev
mivandev

Reputation: 321

Connect to nearest agent in collection

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.

Upvotes: 0

Views: 694

Answers (1)

Felipe
Felipe

Reputation: 9421

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

Upvotes: 1

Related Questions