Search code examples
anylogicagent-based-modeling

how to find the nearest agent based on distance?


I am trying to make that when an agent is less than a specified distance from any other, they change color. But when I use getNearestAgent(this.getPopulation()) it gives me the Agent in the position 0 of the population list. I'm using an event to make the agents change color. this is the code of the event and the output of the console

and the color change depends of the rate of the event and sometimes the distance from the point (0,0,0)


Solution

  • You can use the distanceTo() function.

    Each agent will need to loop through all the other agents and then check their distance to the other agents.

    enter image description here