I am trying to move my citizens from one node (slocation) to another node (new-location) calculating the shortest path. I could only calculate the distance from slocation to new-location using set total-expected-path [distance [slocation] of myself] of new-location.
However, I am pretty sure that the following line after to set total-expected-path are not correct. I got the following error: this code can't be run by a turtle, only a link error while node 35 running LINK-LENGTH
How can I define this distance calculate in the total-expected-path as the minimum between nodes using the link connection between nodes? and after that, how can I move the turtles following this short path? to go set-timekeeper ask citizens [find-day-activities] end
to set-timekeeper
tick
let counter ticks
if (counter = 2)
[set timekeeper 2]
end
to find-day-activities
if (timekeeper = 2)
[Do_7AM_9AM]
end
to Do_7AM_9AM
if (sex = 0 and age = 1 and employment = 0 and household-size = 0 [move-work]
end
to move-work
to move-work
set slocation min-one-of nodes [distance myself]
let new-location min-one-of nodes [distance one-of workbuildings]
let llocation one-of [link-neighbors with-min [link-length]] of new-location
move-to llocation
end
You may want to use the
nw
extension to take advantage of thenw:turtles-on-path-to
ornw:turtles-on-weighted-path-to
primitives. With these extensions and variables:and this setup:
This creates a loop network- pretend that the green is the start node and the red is the destination.
Now, using
nw:turtles-on-path-to
you can identify the nodes in the path that gets to the destination by the fewest links:Or, using
link-length
as theweight
variable innw:turtles-on-weighted-path-to
, you can get the shortest distance:To actually have your people move along a particular path, you can use a combination of the path identifying code above and
foreach
. Example setup for that:And the movement itself: