我正在尝试在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的滑块。
答案 0 :(得分:3)
要检查崩溃,只要让乌龟找到最近的其他海龟。如果他们的距离小于1,那么他们会崩溃。就这样:
let closest min-one-of other turtles [distance myself]
if (distance closest < 1) [
;;we crashed
]
答案 1 :(得分:2)
“由于某种原因,不止一只海龟(汽车)被安装到同一个地方,当我检查时它们之间的距离大于1!这怎么可能呢?”,考虑两只海龟的对面角落相同的补丁。它们之间的距离是1.414 ...(对角线的长度),即使它们在同一个补丁上。