我刚开始在Netlogo上编写一个Masters项目的代码,并试图找出如何做的事情。
作为一个更大的模型的一部分,我试图编写一个可以遵循路线的“汽车”。汽车需要能够遵循一条有点复杂/不稳定的路线,因为我计划在GIS层中扫描道路网络,因此不能依赖漂亮的“整齐”线路。
到目前为止,'汽车'遵循这些程序来识别它将“驾驶”的下一个“道路”补丁。
breed [road]
breed [car]
patches-own [
road?
front-route
turn-route
reverse-route
nearest-front
nearest-turn
nearest-reverse]
to setup
clear-all
create-road 1 [
set color gray
setxy 0 0
]
ask road [
repeat 9 [
set pcolor gray
fd 1
]
repeat 25 [
set pcolor gray
lt 10
fd 1
]
repeat 15 [
set pcolor gray
fd 1
]
repeat 27 [
set pcolor gray
rt 9.8
fd 1
]
repeat 9 [
set pcolor gray
fd 1
]
]
ask road [
die
]
ask patches [set road? false]
ask patches with [
pcolor = gray][
set road? true]
create-car 1
ask car [
set color black]
end
to go
ask car [
follow-route]
end
to follow-route
scan
ifelse any? front-route [
find-nearest-front]
[ifelse any? turn-route [
find-nearest-turn]
[if any? reverse-route [
find-nearest-reverse]
]
]
end
to scan
set front-route other patches with [road?] in-cone 2 25
set turn-route other patches with [road?] in-cone 2 180
set reverse-route other patches with [road?] in-cone 1 360
end
to find-nearest-front
set nearest-front max-one-of front-route [distance myself]
face nearest-front
fd 0.5
end
to find-nearest-turn
set nearest-turn min-one-of turn-route [distance myself]
face nearest-turn
fd 0.5
end
to find-nearest-reverse
set nearest-reverse min-one-of reverse-route [distance myself]
face nearest-reverse
fd 0.5
end
上述代码产生了相对令人满意的结果,但是一些“形状”的道路导致它失火。
我希望汽车做的是将其标题设置为“扫描”操作中识别的补丁的平均标题变化。理想情况下,这将允许我的代码依赖于单个“扫描”和“路径”操作,而不是单独的“前路线”和“转弯路线”
我玩了几个不同的命令,但无法找到一个完美的解决方案。
to-report average-front-road-heading
let x-cord-front-road sum [pxcor] of front-route
let y-cord-front-road sum [pycor] of front-route
ifelse x-cord-front-road = 0 and y-cord-front-road = 0 [
report heading ][
report atan x-cord-front-road y-cord-front-road]
end
to-report average-turn-road-heading
let x-cord-turn-road sum [pxcor] of turn-route
let y-cord-turn-road sum [pycor] of turn-route
ifelse x-cord-turn-road = 0 and y-cord-turn-road = 0 [
report heading ][
report atan x-cord-turn-road y-cord-turn-road]
end
to find-nearest-front
set heading average-front-road-heading
fd 0.5
end
to find-nearest-turn
set heading average-turn-road-heading
fd 0.5
end
以上代码运行并且似乎适用于直线和逐渐倾斜的道路,但在拐角处出现故障。
谢谢,
哈里·