Search code examples
nodesanylogic

Move to closest node


In my model I want the agent to move to a node based on the distance between the agents and some specific nodes in a collection. So in the moveto block the node is based on a specification function ( see first image), which should be based on a function in anylogic which calculates the distance. Now my idea is to create a function which loops through all nodes in the specific collection and choose the closest based on the getdistance() function (see picture 2). But I'm not sure how to finish it to let the function return the closest node (in the collection) when it is activated in the destination node part.

I also was thinking about using the function getNearestNode which may be helpfull(but the node should be in a specific collection? Can somebody help me, cause I was also not able to find any examples on the internet.

Thanks!

Node movetoblock

Function


Solution

  • you can do it like this, just place this on your node section that you circled:

    top(collectionOfNodes,n->-agent.getDistance(n.getX(),n.getY()))
    

    If you want to find the ones that are on the same z then:

    top(findAll(collectionOfNodes,a->a.getZ()==agent.getZ()),n->-agent.getDistance(n.getX(),n.getY()))