Shortest path between nodes in netlogo using nw extension

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Shortest path between nodes in netlogo using nw extension

Sara Vicario
Hello,
 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]
 1. Is this because I have several slocation and new-location? 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. I think the reason is not behind the number of citizens.
 Here, it is my code:
     breed [citizens citizen]
    breed [nodes node]

    links-own [weight]

    to go
    set-timekeeper
    ask citizens
    [find-day-activities]
    end

    to set-timekeeper
    tick
    if (counter = 2) [set timekeeper 2] ;From 7:00 AM to 9:00 AM
    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)
    [shortest-distance]
    end

    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 ]
    end
 2. 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 links connecting slocation and new-location to reach this new-location.
 You can find the code just down about how I create the link network from a shapefile. Is there a problem in my link network that NetLogo cannot find the path?
 to make-road-network
  clear-links
  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]
end Thank you in advance

Sin título.png (251K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Shortest path between nodes in netlogo using nw extension

guerrin
Hi,

the error message you got: "*BUT-FIRST expected input to be a string or
list..."*

as it says, is due to the fact that the but-first's argument should be a
list or a string (but-first returns the list or string without its first
element).

Put it in a list format and it should disappear...

FG
**


Le 06/04/2018 à 19:19, [hidden email] [netlogo-users] a écrit :

>
> Hello,
>
> I want to find the shortest path between two nodes - slocationand
> 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] |
>
> *1. Is this because I have several slocation and new-location? 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. I think the reason
> is not behind the number of citizens.
>
> Here, it is my code:
>
> |breed [citizens citizen] breed [nodes node] links-own [weight] to go
> set-timekeeper ask citizens [find-day-activities] end to
> set-timekeeper tick if (counter = 2) [set timekeeper 2] ;From 7:00 AM
> to 9:00 AM 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) [shortest-distance] end 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 ] end |
>
> 2. 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 links
> connecting slocation and new-location to reach this new-location.
>
> You can find the code just down about how I create the link network
> from a shapefile. Is there a problem in my link network that NetLogo
> cannot find the path?
>
> |to make-road-network clear-links 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] end|
>
> Thank you in advance
>
>

--

SignatureMail-FG.png (33K) Download Attachment