netlogo - 有没有解决方案只有观察者可以询问Netlogo中所有海龟的集合?
问题描述
下面链接的代码不起作用,因为当您运行代码时它会告诉您:
Only the observer can ASK the set of all turtles.
error while turtle 35 running ASK
called by procedure MOVE-TURTLES
called by procedure GO
called by Button 'go'
我检查了代码,但找不到解决方案。
globals [marked-patches]
turtles-own [Angle lastAngle]
to setup
clear-all
create-turtles number [ setxy random-xcor random-ycor]
ask turtles [
set color red
set size 1
set Angle random-float 0.05]
reset-ticks
end
to go
ask patches [set pcolor yellow]
ask turtles[
move-turtles
set lastAngle Angle
set Angle random 360
right Angle
do-return-plot]
do-count-patches
if (count patches = marked-patches) [stop]
tick
end
to plot-patch-count
set-current-plot "Area Covered"
set-current-plot-pen "Number of Patches"
set marked-patches count patches with[pcolor = yellow]
plot marked-patches
end
to do-return-plot
set-current-plot "Return Plot"
plotxy lastAngle Angle
end
to do-count-patches
set marked-patches count patches with[pcolor = yellow]
show marked-patches
end
to move-turtles
ask turtles [
rt random 360
fd 1
set pcolor yellow
pen-down]
ifelse show-travel-line? [pen-down][pen-up]
end
解决方案
这个错误是为了阻止你犯一个非常常见的错误。看看你的 go 程序 - 它已经有了ask turtles [ move-turtles ...]
,然后你在 move-turtles 程序中做的第一件事就是ask turtles
再次。这就是消息告诉你的内容,你有一只乌龟要求所有的乌龟做某事。如果你有 10 只海龟,那么每只海龟会移动 10 次,因为每只海龟都会告诉所有海龟移动。
在您的情况下,您需要考虑订单。你想让一只乌龟移动,然后计算角度等等,在下一只乌龟开始之前做所有事情。如果是这样,则将其留ask turtles
在 go 过程中并将其从 move-turtles 过程中删除。这将修复消息。
但是,您在 go 过程中也有一些绘图,而在 move-turtles 过程中也有一些绘图。您可能还想考虑如何将命令分解为过程,每个过程只做一件事和一件事。我发现对于 NetLogo 的新手来说,让 go 程序更容易地运行一个要调用的程序列表,然后这些程序执行移动、绘图等操作。然后这些程序以ask turtles
. 这种方法的好处是它ask turtles
位于代码中,其中包含要求他们执行的命令。
推荐阅读
- c# - 将音频缓冲区从网页流式传输到 C# Google Cloud Speech to Text SDK 并收到 400 错误
- c# - 获取 System.Drawing.Color 值作为 ARGB 十六进制代码
- java - Weblogic - ClassNotFoundException - 部署期间
- docker - 为什么我不能总是用 Ctrl-C 杀死一个 docker 进程?
- ios - iOS 模拟器 - 从 Finder 中拖动图像未添加到 Xcode 10 中的照片
- bash - Makefile - 编译所有子目录并在单独的目录中输出 .o 文件
- clips - CLIPS:当值不在多槽中时匹配
- android - 支持库必须使用 compileSdkVersion 28 的确切版本规范错误
- django - 如何在我的 django 模板中修复此错误?
- .htaccess - htaccess 重写用于修改 url 结构和重定向的问题