Only the observer can ask the set of all turtles in Netlogo有解决办法吗?

Is there a solution for Only the observer can ask the set of all turtles in 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 位于代码中,其中包含要求他们执行的命令。