Search code examples
anylogic

How to transition from one state to another state based upon distance of agent in a network in Anylogic


I am trying to develop an Agent Based model where patient gets to hospital based on the nearest hospital distance. I have two agent population on Main,

  1. patients (Population of 10)
  2. hospitals (Population of 2)

This is my network. See Image

enter image description here

And here is the transition condition, enter image description here

distanceTo(getNearestAgent(main.hospitals(0))) < distanceTo(getNearestAgent(main.hospitals(1)))

This is returning the error, "The method getNearestAgent(Iterable) in the type Agent is not applicable for the arguments (Hospital)".


Solution

  • The function getNearestAgent() is used for finding the nearest agent in a population and you got the error because you're calling it on a single agent. Since you only have two hospitals that you need to calculate the distance from, you can either use

    distanceTo(main.hospitals(0)) < distanceTo(main.hospitals(1)) (is hospital(0) closer than hospital(1)?)

    or

    getNearestAgent(main.hospitals).equals(main.hospitals(0)) (is hospital(0) the nearest one?)

    (But if you need to extend your model to include more than two hospitals, you will need a slightly different approach)