我试图在NetLogo中模拟特定的交叉点。这是代码(在开发的早期阶段):
breed [ trams tram ]
breed [ cars car ]
to setup
clear-all
set-default-shape trams "bug"
set-default-shape cars "car"
setup-patches
reset-ticks
end
to go
make-new-car frequency-SN-1 1 -20 0
make-new-car frequency-SN-2 2 -20 0
make-new-car frequency-NS-3 -3 20 180
setup-trams tram-frequency
move-trams
want-turn
move-cars
tick
end
to setup-trams [ freq ]
if (random-float 100 < freq) and not any? turtles-on patch 0 -20 [
create-trams 1 [
setxy 0 -20
set heading 0
set color black
]
]
end
to make-new-car [freq x y head ]
if (random-float 100 < freq) and not any? turtles-on patch x y [
create-cars 1 [
setxy x y
set heading head
set color one-of base-colors
]
]
end
to move-cars
ask cars [
ifelse not can-move? 1
[ die ]
[ fd 1 ]
]
end
to want-turn
ask cars-on patch 2 -7 [
ifelse (random-float 100 < 100) and not any? turtles-on patch 3 -7
[ rt 90 fd 1
lt 90 fd 4
rt 90 fd 2
lt 90 fd 2
rt 90 fd 1]
[ fd 1 ]
]
end
to move-trams
ask trams [
ifelse not can-move? 1
[ die ]
[ fd 1 ]
]
end
to setup-patches
ask patches [
ifelse abs pxcor <= 3 or abs pycor <= 3
[ set pcolor black ]
[ set pcolor green - 1 ]
ifelse abs pxcor <= 0.5
[ set pcolor red ]
[ ]
ask patch 3 -2 [ set pcolor red ]
ask patch -2 -1 [ set pcolor red ]
ask patch -1 -1 [ set pcolor red ]
if pycor = -3 and pxcor > 8
[ set pcolor green - 1 ]
if pycor <= 3 and pycor >= -1 and pxcor < -3
[ set pcolor green - 1 ]
if pxcor = 3 and pycor <= -8
[ set pcolor green - 1 ]
]
end
导致我问题的是转弯程序。我想要一些汽车向右移动并向东移动。然而,我不能,到目前为止(刚刚开始使用NetLogo)以不同的方式做到这一点 - 但只是为了让汽车在特定补丁上消失并重新出现在另一个上(这完全是由在转发程序中运行的转弯程序引起的)。每次打勾都会运行,但是在转弯里面我会移动许多补丁,所以看起来像是传送。)
我可以以某种方式在那里创造道路吗?因此,我不会手动将汽车移动到另一个补丁,而只是询问这辆汽车是否符合特定补丁的条件(在这种情况下:补丁2-7),如果是的话 - 它会移动替代道路。
我刚刚发现了NetLogo,欢迎任何想法
答案 0 :(得分:1)
这样做的一种方法是使用基于转弯车前面的补丁颜色的规则。对于一种严格适用于我认为您正在尝试重现的模式的方法,如果前方有绿色补丁,并且前方有红色补丁,则可以让汽车左转。这允许您在移动时根据补丁控制其标题。
我还建议建立他们的运动程序,如果前方有车(如模型库中的交通模型),他们就不会前进,就像你的转弯程序一样。这样,它们就永远不会进入已被另一辆车占用的补丁。
您还应该查看patch-set
以创建补丁代理集,这样您就不必重复set pcolor red
之类的命令;您也可以使用or
执行此操作。
查看我对您的代码所做的修改,以了解上述的一些实现示例:
breed [ cars car ]
to setup
ca
resize-world -20 20 -20 20
set-default-shape cars "car"
setup-patches
reset-ticks
end
to go
make-new-car 10 2 -20 0 blue
make-new-car 10 1 -20 0 yellow
make-new-car 10 -3 20 180 white
make-new-car 10 -1 20 180 pink
make-new-car 10 -20 -3 90 orange
want-turn
move-cars
tick
end
to make-new-car [freq x y head col ]
if (random-float 100 < freq) and not any? turtles-on patch x y [
create-cars 1 [
setxy x y
set heading head
set color col
]
]
end
to move-cars
ask cars [
ifelse not can-move? 1
[
die
]
[
if patch-ahead 1 != nobody [
if [pcolor] of patch-ahead 1 = green - 1 [
rt -90
]
if [pcolor] of patch-ahead 1 = red [
rt 90
]
if ( [count turtles-here] of patch-ahead 1 = 0 ) and
([pcolor] of patch-ahead 1 = black) [
fd 1
]
]
]
]
end
to want-turn
ask cars-on patch 2 -7 [
if (random-float 100 < 50) [
rt 90
]
]
end
to setup-patches
ask patches [
ifelse abs pxcor <= 3 or abs pycor <= 3
[ set pcolor black ]
[ set pcolor green - 1 ]
if abs pxcor <= 0.5
[ set pcolor red ]
ask ( patch-set patch 3 -2 patch -2 -1 patch -1 -1 patch 8 -1 ) [
set pcolor red
]
if ( pycor = -3 and pxcor > 8 ) or (pycor <= 3 and pycor >= -1 and pxcor < -3) or (pxcor = 3 and pycor <= -8) [
set pcolor green - 1
]
]
end