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

I am modeling people commuting using different transportation methods: walking, biking, and driving. I

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.

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:

  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

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.

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:

  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
Share asked Mar 3 at 11:03 vihelehaviheleha 193 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The current approach where your turtles move-to the next node in your network is 'unitless'- the turtles are able to jump from one node to the next with no regard for distance. That can work, depending on your need, but here is an alternative that uses patch distance to allow for speed differences.

extensions [nw]

breed [locations location]
breed [movers mover]

movers-own [
  speed-class
  speed
  homeplace
  nodes-to-goal
  links-to-goal
]

to setup
  ca
  ask patches with [pxcor mod 5 = 0 and pycor mod 5 = 0] [
    sprout-locations 1 [
      set shape "circle"
      set color 65
      set size 1.5
    ]
  ]
  ask locations [
    let nearby-locs other locations in-radius 5
    create-links-with nearby-locs
  ]
  ask n-of 5 locations [
    hatch 1 [
      set breed movers 
      set color 15 + ( random 10 ) * 10   
      pd     
      set pen-size 5
      set speed-class one-of [ "bike" "walk" ]
      set shape ifelse-value speed-class = "bike" ["wheel"] ["person"]
      set speed ifelse-value speed-class = "bike" [4] [1]
      set size 1
      set homeplace one-of other locations
      ask homeplace [ set label myself set label-color 68]
      set nodes-to-goal [nw:turtles-on-path-to [homeplace] of myself] of one-of locations-here
    ]
  ]
  ; This is just for visualization to remove the other nodes
  let non-nodes remove-duplicates reduce sentence [nodes-to-goal] of movers
  ask locations with [ not member? self non-nodes ] [ die ]
  reset-ticks
end

to go
  if not any? movers with [ length nodes-to-goal > 0 ] [ stop ]
  ask movers [
    ; If there are nodes yet to travel towards
    if length nodes-to-goal > 0 [                 
      ; If distance is NOT greater than speed, move to the target
      ; and then potentially move the remainder of speed towards the next target.
      ; Repeat this as needed until no speed remains      
      let remain-speed speed
      while [remain-speed > 0 and (length nodes-to-goal) > 0] [
        let cur-targ item 0 nodes-to-goal
        let cur-dist distance cur-targ
        face cur-targ 
        if-else cur-dist > speed [
          fd speed
          set remain-speed remain-speed - speed
        ] [
          move-to cur-targ
          set remain-speed remain-speed - cur-dist
          set nodes-to-goal but-first nodes-to-goal
        ]
      ]
    ]
  ]  
  tick
end

Newbie here but maybe create different turtles so that they can follow a + 1 or + 2 for how many patches they are allowed to move per tick?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745097952a4611095.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信