0
votes

In a model I've got patients moving to hospitals. These agents live in continuous space. More specifically, they are placed in polylines. Initial patient population is zero, but an event generates new patients continuously. Hospitals have a initial population of 10 and no new are generated thereafter. What I want to achieve is that, at the moment of generating a new patient, he selects the hospital that is nearest to the patient, and sets that hospital as myHospital in a parameter.

To select the nearest hospital I've tried to create a function in Patient, and used myHospital in the statechart to have the patient move to myHospital:

Hospital myHospital = this.getNearestAgent(main.hospitals);
return myHospital;

So far all patients have selected the same hospital as the nearest hospital, which is wrong, especially given that I can inspect the patient's X and Y coordinates.

What approach will fix this issue?

1

1 Answers

1
votes

It is possible that when you call the function to get the nearest hospital, your agents are still in x=0 y=0, so you will get the closest hospital to that point.

The order in which you should do things in your event is the following:

Patient p=add_patients(); //generate a patient
p.setXY(uniform(0,500),uniform(0,500)); //define your patient initial position FIRST
p.hospital=p.getHospital();// or just do p.getNearestAgent(hospitals);
p.moveTo(p.hospital); //

This should work for sure