有没有办法检查海龟的 n 块半径内的海龟?
Is there a way to check for turtles in a n patch radius of a turtle?
我想制作一个得来速模拟器,其中汽车会自动在它们和前面的汽车之间留下一些 space。现在我可以通过检查相邻的补丁是否有另一辆车来确保它们不会紧挨着移动。麻烦的是,这仍然让它们有一点重叠。我如何使用 NetLogo 来确保如果另一辆车在汽车的 4 个补丁半径内,汽车不会移动?
我想让我的车向东移动,然后向南移动,然后向东移动,然后向北移动,然后消失。出于这个原因,我的代码到目前为止看起来像这样:
to move
ask turtles
[
if pycor > 12 and pxcor < -10 [
set heading 90 ; set the starting heading
if not any? turtles-on neighbors
[
if not (count turtles-on patches with [pxcor = -10] > 5) or pxcor != -14 [forward 1]
]
]
if pycor > -11 and count turtles-on patches with [pxcor < -8] < 13
[
if (pxcor = -10 and pycor < 15) or (pxcor = -14)
[
set heading 180 ; start moving downwards
if not any? turtles-on neighbors
[
if not (count turtles-on patches with [pycor = -11] > 5) or pycor != -8 [forward 1]
if num-dto = 2
[
if count turtles-on patches with [pxcor = -10] > count turtles-on patches with [pxcor = -14] and pxcor = -10
[
set heading 270
forward 4
set heading 180
]
]
]
]
]
if pycor = -11 and pxcor < 13 [
set heading 90 ; start moving to the right
if not any? turtles-on neighbors
[
if not (count turtles-on patches with [pxcor = 13] > 5) or pxcor != 12 [forward 1]
]
]
if pxcor = 13 and pycor < 12 [
; start moving up
if not any? turtles-on neighbors [set heading 0 forward 1]
]
;if pxcor > 12 and pycor > 11 [die]
]
end
如果有更简洁的方法来做到这一点,我真的很喜欢。感谢您的帮助,我对 NetLogo 编程还很陌生。
我想你想要 in-radius
or, more likely so that your turtles check only in front of them, in-cone
这样的东西。这两者都会检查一定距离内的某些代理,in-cone
添加 'cone' 度数定义的视野。
例如:
turtles-own [ speed ]
to setup
ca
ask patch -10 -10 [
sprout 1 [
set heading 0
repeat 4 [
repeat 20 [
set pcolor grey
fd 1
if random-float 1 < 0.15 [
hatch 1 [
set heading [heading] of myself
set speed 0.25 + random-float 0.5
set color 102 + ( speed * 5 )
]
]
]
rt 90
]
die
]
]
reset-ticks
end
to go
ask turtles [
if not any? other turtles in-cone 4 90 [
ifelse [pcolor] of patch-ahead 1 = black [
rt 90
set xcor pxcor
set ycor pycor
] [
fd speed
]
]
]
tick
end
我还要提到你说你的海龟 overlap
- 我只是想指出,尽管它们的图像可能重叠(例如,如果海龟形状大于补丁大小),turtles
实际上只占据一个点(它们的xcor
和ycor
值)。出于美学目的,重叠可能很重要,但出于建模目的,它可能并不重要。
我想制作一个得来速模拟器,其中汽车会自动在它们和前面的汽车之间留下一些 space。现在我可以通过检查相邻的补丁是否有另一辆车来确保它们不会紧挨着移动。麻烦的是,这仍然让它们有一点重叠。我如何使用 NetLogo 来确保如果另一辆车在汽车的 4 个补丁半径内,汽车不会移动?
我想让我的车向东移动,然后向南移动,然后向东移动,然后向北移动,然后消失。出于这个原因,我的代码到目前为止看起来像这样:
to move
ask turtles
[
if pycor > 12 and pxcor < -10 [
set heading 90 ; set the starting heading
if not any? turtles-on neighbors
[
if not (count turtles-on patches with [pxcor = -10] > 5) or pxcor != -14 [forward 1]
]
]
if pycor > -11 and count turtles-on patches with [pxcor < -8] < 13
[
if (pxcor = -10 and pycor < 15) or (pxcor = -14)
[
set heading 180 ; start moving downwards
if not any? turtles-on neighbors
[
if not (count turtles-on patches with [pycor = -11] > 5) or pycor != -8 [forward 1]
if num-dto = 2
[
if count turtles-on patches with [pxcor = -10] > count turtles-on patches with [pxcor = -14] and pxcor = -10
[
set heading 270
forward 4
set heading 180
]
]
]
]
]
if pycor = -11 and pxcor < 13 [
set heading 90 ; start moving to the right
if not any? turtles-on neighbors
[
if not (count turtles-on patches with [pxcor = 13] > 5) or pxcor != 12 [forward 1]
]
]
if pxcor = 13 and pycor < 12 [
; start moving up
if not any? turtles-on neighbors [set heading 0 forward 1]
]
;if pxcor > 12 and pycor > 11 [die]
]
end
如果有更简洁的方法来做到这一点,我真的很喜欢。感谢您的帮助,我对 NetLogo 编程还很陌生。
我想你想要 in-radius
or, more likely so that your turtles check only in front of them, in-cone
这样的东西。这两者都会检查一定距离内的某些代理,in-cone
添加 'cone' 度数定义的视野。
例如:
turtles-own [ speed ]
to setup
ca
ask patch -10 -10 [
sprout 1 [
set heading 0
repeat 4 [
repeat 20 [
set pcolor grey
fd 1
if random-float 1 < 0.15 [
hatch 1 [
set heading [heading] of myself
set speed 0.25 + random-float 0.5
set color 102 + ( speed * 5 )
]
]
]
rt 90
]
die
]
]
reset-ticks
end
to go
ask turtles [
if not any? other turtles in-cone 4 90 [
ifelse [pcolor] of patch-ahead 1 = black [
rt 90
set xcor pxcor
set ycor pycor
] [
fd speed
]
]
]
tick
end
我还要提到你说你的海龟 overlap
- 我只是想指出,尽管它们的图像可能重叠(例如,如果海龟形状大于补丁大小),turtles
实际上只占据一个点(它们的xcor
和ycor
值)。出于美学目的,重叠可能很重要,但出于建模目的,它可能并不重要。