我正在尝试创建一个模型,其中海龟随机行走(但有向前运动的趋势),直到它们落在代表诱饵物体的黄色斑块上。
当一只降落在其中一个黄色斑块上时,我希望它停在那个斑块上,在那里停留15个蜱虫,同时它"调查"诱饵。
经过15个蜱虫后,我希望海龟像往常一样继续移动,直到它们遇到另一个黄色斑块。
我试图在网络徽标建模公共空间中修改此停放卡片模型的部分内容,但无法真正理解它(我是netlogo的新手)http://modelingcommons.org/browse/one_model/3205#model_tabs_browse_procedures
我还尝试实现倒数计时器,如此线程中所述如何在NetLogo中创建倒数计时器?
但是,当我尝试运行模拟时,我收到运行时错误"只有观察者可以询问所有海龟的集合"。谁能告诉我哪里出错了?大概有几个地方!谢谢。
下面是导致运行时错误的代码:
turtles-own [count-down]
to setup
clear-all
ask patches with [count neighbors != 8]
[set pcolor blue]
create-turtles 20
ask turtles
[setxy random-xcor random-ycor
pen-down]
ask n-of 20 patches
[ set pcolor yellow ]
reset-ticks
end
to go
move-turtles
tick
if ticks >= 720 [stop]
end
to move-turtles
ask turtles
[ ifelse pcolor != yellow
[continue]
[stay]
]
end
to continue
ask turtles
[rt -90 + random 181]
ask turtles
[ifelse [pcolor] of patch-ahead 1 = blue [ lt random-float 360 ]
[fd 1]
]
end
to stay
ask turtles
[
setup-timer
decrement-timer
if timer-expired? [continue]
]
end
to setup-timer
set count-down 15
end
to decrement-timer
set count-down count-down - 1
end
to-report timer-expired?
report ( count-down <= 0 )
end
这只是一个例子,他们应该在黄色区域停留多少个刻度? 我假设有 15 个刻度,我要求海龟在他们的标签上打印它们的刻度编号,如果它跑得太快,你可能会错过它们的停留时间,所以调整模型的运行速度,看看它们何时停留和何时移动。你可以有不同的方法 继续 ,在这个中,他们只是向前移动 1 个补丁。
turtles-own [count-down]
to setup
clear-all
ask patches with [count neighbors != 8]
[set pcolor blue]
create-turtles 20
ask turtles
[setxy random-xcor random-ycor
pen-down
set count-down 15
]
ask n-of 20 patches
[ set pcolor yellow ]
reset-ticks
end
to go
move-turtles
tick
if ticks >= 720 [stop]
end
to move-turtles
ask turtles
[ ifelse pcolor != yellow
[continue]
[stay]
]
end
To continue
rt random 10
fd 1
end
to stay
set count-down count-down - 1 ;decrement-timer
set label count-down
if count-down = 0
[
Continue
set label ""
reset-count-down
]
end
to reset-count-down
set count-down 15
end
要回答有关"只有观察者才能询问所有海龟的集合"的部分,如果您这样做,就会出现该错误消息:
ask turtles [
ask turtles [
do-something
]
]
这在NetLogo中是不允许的,因为它几乎总是偶然的,而不是故意的。您可能只想让每只"做某事"一次;你可能不是想让每只为每对可能的两只"做点什么"。
不太明显的是,你让所有的海龟问所有的海龟,它是否被拆分为跨程序。因此,例如,如果您编写:
to go
ask turtles [ my-procedure ]
end
to my-procedure
ask turtles [ do-something ]
end
出于同样的原因,它仍然是错误的,但仅从瞥一眼就不容易看出这一点。
您的代码遵循后一种模式。 你有:
to move-turtles
ask turtles [
...
continue
...
]
end
to continue
ask turtles [
rt -90
...
]
end
我认为您不想在continue
程序中做ask turtles
。由于您在 ask turtles
中调用该过程,它已经是一个过程。 我建议把它写成:
to continue ;; turtle procedure
rt -90
...
end
该评论提醒您,它打算由海龟经营。 (我们在模型库中的所有模型中都遵循此样式。