Shortest path between nodes in netlogo using nw extension

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Shortest path between nodes in netlogo using nw extension

Sara Vicario

 I want to find the shortest path between two nodes - slocation and new-location - in a network of links


 I have several slocation and new-location. One slocation for each citizen with some age, sex, and other characteristics, and one new-location for each node close to a work building.
 I could identify these nodes with colors - see image.
 However, I could not identify the shortest path between each slocation (for each citizen) and newlocation.
 I got the following error: BUT-FIRST expected input to be a string or list but got the TRUE/FALSE false instead. when I run these lines of code:
 ask slocation [set path but-first nw:turtles-on-weighted-path-to new-location weight] Is this because I have several slocation and new-location nodes? should I introduce and foreach variable to calculate the shortest path for each citizen?
 I tried already only with one citizen and still this part of the code is not capable to find the path
 Here, it is my code:

     breed [citizens citizen]
    breed [nodes node]

    links-own [weight]

    to go
    ask citizens

    to set-timekeeper
    if (counter = 2) [set timekeeper 2] ;From 7:00 AM to 9:00 AM

    to find-day-activities
    if (timekeeper = 2) [Do_7AM_9AM]

    to Do_7AM_9AM
    if (sex = 0 and age = 1 and employment = 0 and household-size = 0)

    to shortest-distance
    let slocation min-one-of nodes [distance myself]
    ask slocation [set color yellow]
    let new-location one-of nodes with-min [distance one-of workbuildings]
    ask new-location [set color blue]

    ask links [set weight link-length]
    let path nobody
    ask slocation
    [set path but-first nw:turtles-on-weighted-path-to new-location weight]
    ask turtle-set path [ set color yellow ]
Probably, the error is that the nodes slocation and new-location are not connected with a single link, if not with several links.
 The citizens have to select the shortest path created by the link network connecting the slocation
 and new-location nodes to reach this the new-location node.
 Here it is my code to define the link network, Do you find any issue here for which the code above cannot set the path?
 to make-road-network

  let first-node nobody
  let previous-node nobody
  foreach gis:feature-list-of roads [ polyline ->
    foreach gis:vertex-lists-of polyline [ segment ->
      foreach segment [ coordinate ->
        let location gis:location-of coordinate
        if not empty? location [
          create-nodes 1 [
            set color green
            set size 1
   set xcor item 0 location
            set ycor item 1 location
            if first-node = nobody [
              set first-node self
            if previous-node != nobody [
              create-link-with previous-node
            set previous-node self

      set previous-node nobody
ask links [set color red

           set thickness 1]

 Thank you in advance


Sin título.png (251K) Download Attachment