海龟之间的距离(汽车)

Distance Between Turtles (car)

最近我一直在研究 netlogo,尤其是在 Traffic Basic。 我想修改代码并创建一个函数来计算每只乌龟(汽车)与前面的乌龟之间的距离。 我怎样才能做到这一点?

也许 to-report 会做你想做的事。如果将此程序添加到 Traffic Basic 中:

to-report distance-car-ahead 
  ; If there are any cars within 10 patches of me
  ifelse any? other turtles in-cone 10 1 [
    ; Report the distance to the nearest one
    report distance ( min-one-of ( other turtles in-cone 10 1 ) [distance myself] )
  ] [
    ; Otherwise, report that I am in the lead
    report "I am the lead car"
  ]
end

现在作为示例,您可以修改 go 来检查它是否正常工作,如下所示:

to go
  ;; if there is a car right ahead of you, match its speed then slow down
  ask turtles [
    let car-ahead one-of turtles-on patch-ahead 1
    ifelse car-ahead != nobody
      [ slow-down-car car-ahead ]
      [ speed-up-car ] ;; otherwise, speed up
    ;; don't slow down below speed minimum or speed up beyond speed limit
    if speed < speed-min [ set speed speed-min ]
    if speed > speed-limit [ set speed speed-limit ]
    fd speed
    show distance-car-ahead
  ]
  tick
end

我建议将汽车的数量减少到 3 或 4 来评估打印语句,以确保它符合您的预期。