How to Find the Closest Link to a Node in NetLogo

43 Views Asked by At

I'm working with a NetLogo model that consists of a network of nodes and links. I need assistance in calculating the shortest distance from a given node to the nearest point on any link in the network. This calculation should consider the entire length of the link, not just specific points like the endpoints or the midpoint.

Here's a simplified version of my model for context:

breed [nodes node]

to setup
  clear-all
  create-nodes 10 [
    setxy random-xcor random-ycor
    set color blue
    set shape "circle"
  ]
  create-links
    create-nodes 1 [
    setxy 0 0 
    set color red
    set shape "circle"
  ]
  reset-ticks
end


to create-links
  ask nodes [
    create-link-with one-of other nodes
  ]
end

; Need a method to find the closest point on a link
to-report find-closest-point-on-link [target-node]
  ; Implementation needed here
end

In this model, I have a set of nodes and links between them. I am looking for a way to accurately determine the closest point on any of these links to a given node.

Questions:

How can I compute the shortest distance from a node to any point along a link in NetLogo? What would be the best approach or algorithm to use for this calculation? I appreciate any suggestions or guidance on this problem.

example: green line indicating roughly shortest path to closest point on closest link

1

There are 1 best solutions below

0
On

distance cannot receive links as argument (it should, in my opinion!) but you have the coordinates of the red node and of the two nodes that are at the ends of each link. So you can use the appropriate geometric equations (not delving into that with this answer) to find the coordinates of the point where the link and its perpendicular passing through the red node meet.

What I would do would be:

  • Have a list of links created by using sort links (this is needed because, as opposed to the simple links agentset, it keeps links in the same order. Needed for the last one of these bullet points).
  • For each of said links, retrieve the list whose each item is the pair of link's ends by using end1+end2 or both-ends.
  • For each of said items, find the coordinates of the nodes by using xcor and ycor.
  • For each of said couple of coordinates pairs, find the coordinates of the point where the line passing through the two points and its perpendicular passing through the red node meet, by using the appropriate geometric equations.
  • For each of said points (i.e. coordinates), compute the distance between the point and the red node by using distancexy.
  • Now you have a list of distances that are still ordered exactly as sort links. Find the position (in the list) of the shortest distance by using min and position.
  • Retrieve from sort links the link that is placed in the same position as the shortest distance in the list of distances (as per point above) by using index.

Note

The steps above let you identify which link is the closes to a given node (as per the title of your question: "How to Find the Closest Link to a Node in NetLogo"). Based on how you word things in the body of the question, sometimes you rather seem interested in identifying the closes point on a link ("I am looking for a way to accurately determine the closest point on any of these links to a given node"). Some other times you rather seem interested in finding which is the shortest distance from a node to any point on the links ("How can I compute the shortest distance from a node to any point along a link in NetLogo?").

These are all different outputs but I trust that the end-point of the process I suggested can be easily adjusted for any of those cases