乌龟之间的距离(汽车)

时间:2018-10-07 06:46:55

标签: distance netlogo traffic

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

1 个答案:

答案 0 :(得分:1)

也许to-report会做您想要的事情。如果您将以下过程添加到“流量基础”中:

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,以评估打印报表,以确保其达到了预期的效果。