Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to make turtles follow a path at different speeds in Netlogo?

41 views
Skip to first unread message

Victoria Haskins

unread,
Mar 3, 2025, 1:36:56 PMMar 3
to netlogo-users
I am modeling people commuting using different transportation methods: walking, biking, and driving. I use nw:path-to to find the shortest path between their home and work and then move them along the path until they reach their goal. This works fine for people walking as they move one link in the path per tick, but cars and bikes should be able to travel faster than pedestrians.

I don't use fd 1 for this because they're not always going in one direction.

I tried to simulate this by repeating the movement procedure twice per tick for bikes and four times per tick for cars, expecting this would make them move twice as fast and four times as fast as pedestrians, but unfortunately, they still move at the same speed.

```people-own [
path-to-goal ;; list of links between the starting node and the goal destination
dist-goal ;; the length of the list of links between the starting node and the goal destination
mynode ;; min-one-of nodes distance from each person at the start
destination ;; the goal location, which is the same for every agent
actual-choice ;; what transportation method people choose (either bike, walk, or car)
]
```

This is how I set the path for them to follow:

```to set-path
  ask people [
    ask mynode [
      let shortest-path-to-goal nw:path-to ndestination

      ask myself [
        set path-to-goal shortest-path-to-goal
        set dist-goal length path-to-goal
    ]
  ]
end
```
This is how I get the people to follow the path:
```
to move-people
    ifelse length path-to-goal = 0 [
      ;; If the person has reached the destination, stop moving.
      if distance destination < 1 [
        set got_to_destination 1
        print "goal reached!"
      ]
    ]
    [
      let current item 0 path-to-goal
      let new-location 0

      ;; Determine the next location based on the current road segment
      ifelse ([end1] of current = mynode) [
        set new-location [end2] of current
      ] [
        set new-location [end1] of current
      ]

      ;; Move the person to the new location
      move-to new-location
      set mynode new-location

      ;; Remove the current node from the path
      set path-to-goal remove-item 0 path-to-goal

      ;; Recalculate the path from the new location to the destination
      set path-to-goal nw:path-to destination
      if path-to-goal = false [
        set path-to-goal []
      ]
    ]
end
```
This is my attempt to ask the people to move at different speeds:
```
to move-to-work
  ask people [
  if actual-choice = "walk" [
    move-people
  ]
  if actual-choice = "bike" [
      set shape "bike"
      set size 4
    repeat 2;;bike_speed
      [ move-people ]
  ]
  if actual-choice = "car" [
      set shape "car"
      set size 4
      repeat 4;;car_speed
      [ move-people ]
  ]
  ]
end
```

Bart de Bruin

unread,
Mar 3, 2025, 2:32:41 PMMar 3
to Victoria Haskins, netlogo-users
Hi Victoria,

I answered a similar question on stack overflow last month. Maybe it is helpful 



Bart

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/netlogo-users/f9cf184e-1f6c-4ee2-8cc9-2163d2d4ab9dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages