Search code examples
anylogicagent-based-modeling

Anylogic, finding agents in a given radius


I have agents in a grid and for any arbitrary agent I want to somehow define a radius r and find all the other agents in that area given r. Is this possible, is there any implemented method for that in AnyLogic?


Solution

  • If by 'in a grid' you mean they are in continuous space but with integer (x,y) coordinates, you can use the standard function agentsInRange (which internally is using the standard function distanceTo).

    If the agents are in an actual grid (i.e., a discrete space with (row, column) coordinates), you can't use the standard continuous/GIS space functions like distanceTo or agentsInRange. (You get runtime exceptions.)

    In that case, you would have to write your own function to calculate distances (probably via Pythagoras' theorem given the row and column values) to each other agent, and return only those within the given threshold.

    Below is such a sample function which assumes your agents are of type Thing and live in a population called things (and uses the straight-line distance in number-of-cells between agents as the 'distance between them'). You supply the function with the 'source' agent (that you want to get other agents in range of) and the desired range. (There are lots of slightly different ways of coding this depending on, for example, whether you want it to work for any population of any agent type.)

    sample range function