首页 > 解决方案 > 有没有解决方案只有观察者可以询问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

标签: netlogo

解决方案


这个错误是为了阻止你犯一个非常常见的错误。看看你的 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位于代码中,其中包含要求他们执行的命令。


推荐阅读