我正在尝试在 NetLogo 中创建高速公路和驾驶员行为的模拟。
我有一些我正在努力解决的问题。
这是我的代码:
globals
[
selected-car ;; the currently selected car
average-speed ;; average speed of all the cars
look-ahead
]
turtles-own
[
speed ;; the current speed of the car
speed-limit ;; the maximum speed of the car (different for all cars)
lane ;; the current lane of the car
target-lane ;; the desired lane of the car
change? ;; true if the car wants to change lanes
patience ;; the driver's current patience
max-patience ;; the driver's maximum patience
]
to setup
ca
import-drawing "my_road3.png"
set-default-shape turtles "car"
crt number_of_cars
[ setup-cars ]
end
to setup-cars
set color blue
set size .9
set lane (random 3)
set target-lane (lane + 1)
setxy round random-xcor (lane + 1)
set heading 90
set speed 0.1 + random 9.9
set speed-limit (((random 11) / 10) + 1)
set change? false
set max-patience ((random 50) + 10)
set patience (max-patience - (random 10))
;; make sure no two cars are on the same patch
loop
[
ifelse any? other turtles-here
[ fd 1 ]
[ stop ]
;if count turtles-here > 1
; fd 0.1
;if
;
;ifelse (any? turtles-on neighbors) or (count turtles-here > 1)
;[
; ifelse (count turtles-here = 1)
; [ if any? turtles-on neighbors
; [
; if distance min-one-of turtles-on neighbors [distance myself] > 0.9
; [stop]
; ]
; ]
; [ fd 0.1 ]
;]
;[ stop ]
]
end
to go
drive
end
to drive
;; first determine average speed of the cars
set average-speed ((sum [speed] of turtles) / number_of_cars)
;set-current-plot "Car Speeds"
;set-current-plot-pen "average"
;plot average-speed
;set-current-plot-pen "max"
;plot (max [speed] of turtles)
;set-current-plot-pen "min"
;plot (abs (min [speed] of turtles) )
;set-current-plot-pen "selected-car"
;plot ([speed] of selected-car)
ask turtles
[
ifelse (any? turtles-at 1 0)
[
set speed ([speed] of (one-of (turtles-at 1 0)))
decelerate
]
[
ifelse (look-ahead = 2)
[
ifelse (any? turtles-at 2 0)
[
set speed ([speed] of (one-of turtles-at 2 0))
decelerate
]
[
accelerate
if count turtles-at 0 1 = 0 and ycor < 2.5
[lt 90
fd 1
rt 90]
]
]
[accelerate
if count turtles-at 0 1 = 0 and ycor < 2.5
[lt 90
fd 1
rt 90]
]
]
if (speed < 0.01)
[ set speed 0.01 ]
if (speed > speed-limit)
[ set speed speed-limit ]
ifelse (change? = false)
[ signal ]
[ change-lanes ]
;; Control for making sure no one crashes.
ifelse (any? turtles-at 1 0) and (xcor != min-pxcor - .5)
[ set speed [speed] of (one-of turtles-at 1 0) ]
[
ifelse ((any? turtles-at 2 0) and (speed > 1.0))
[
set speed ([speed] of (one-of turtles-at 2 0))
fd 1
]
[jump speed]
]
]
tick
end
;; increase speed of cars
to accelerate ;; turtle procedure
set speed (speed + (speed-up / 1000))
end
;; reduce speed of cars
to decelerate ;; turtle procedure
set speed (speed - (slow-down / 1000))
end
to signal
ifelse (any? turtles-at 1 0)
[
if ([speed] of (one-of (turtles-at 1 0))) < (speed)
[ set change? true ]
]
[ set change? false ]
end
;; undergoes search algorithms
to change-lanes ;; turtle procedure
show ycor
ifelse (patience <= 0)
[
ifelse (max-patience <= 1)
[ set max-patience (random 10) + 1 ]
[ set max-patience (max-patience - (random 5)) ]
set patience max-patience
ifelse (target-lane = 0)
[
set target-lane 1
set lane 0
]
[
set target-lane 0
set lane 1
]
]
[ set patience (patience - 1) ]
ifelse (target-lane = lane)
[
ifelse (target-lane = 0)
[
set target-lane 1
set change? false
]
[
set target-lane 0
set change? false
]
]
[
ifelse (target-lane = 1)
[
ifelse (pycor = 2)
[
set lane 1
set change? false
]
[
ifelse (not any? turtles-at 0 1)
[ set ycor (ycor + 1) ]
[
ifelse (not any? turtles-at 1 0)
[ set xcor (xcor + 1) ]
[
decelerate
if (speed <= 0)
[ set speed 0.1 ]
]
]
]
]
[
ifelse (pycor = -2)
[
set lane 0
set change? false
]
[
ifelse (not any? turtles-at 0 -1)
[ set ycor (ycor - 1) ]
[
ifelse (not any? turtles-at 1 0)
[ set xcor (xcor + 1) ]
[
decelerate
if (speed <= 0)
[ set speed 0.1 ]
]
]
]
]
]
end
我知道它有点乱,因为我使用的是库中其他模型的代码。
我想知道如何创建汽车的碰撞。我想不出任何想法。正如您所注意到的,我的代理与补丁的大小几乎相同(我将其设置为 0.9,以便您可以在两辆车彼此相邻时区分它们之间的空间,并且我将坐标四舍五入以便将它们设置为中心补丁)。
在我的加速过程中,我将我的代理设置为左转,移动 1,循环右转。我想知道是否有一个命令可以让我让代理从一个车道跳到另一个车道(到它左边旁边的补丁)而不让它转动和移动。
最后,如果你注意到我创建的代码,汽车会检查它左侧车道上旁边的补丁,以及它前面和后面的补丁。所以如果它左边的3个补丁是空的,那么它可以改变车道。模糊的部分是,当我运行设置并按 Go 键时,有时(并非总是)汽车会驶出 3 个基本车道。
为了理解这一点,我有 7 条车道。我不使用的中间是 0 号车道。然后在 0 号车道的顶部和 3 号车道下面有 3 条车道。所以我使用的代码是指我设置汽车的上 3 条车道,但由于某种原因,一些汽车改变车道并进入 -3 车道,然后进入 -2 车道,依此类推。
如果有人可以给我小费,我将不胜感激。
先感谢您。
提示:如果您想在 netlogo 中尝试此代码,请记住,在界面选项卡上,我有 2 个按钮,一个设置,一个执行以及 3 个滑块,名称为:number_of_cars、speed-up、slow-down。