NetLogo:模型卡住,没有错误消息

NetLogo: Model gets stuck w no error message

我试着让一群乌龟(Movers)穿过大门并避开白色的墙壁。不知何故,模型在 运行 秒后冻结。 Go按钮永远保持黑色和蓝色圆圈转动。没有给出错误信息。它一定是卡在了“move-movers”函数中的一些计算中,但我无法确定原因。

我添加了我的代码的简化版本,但它仍然会导致崩溃。复制并粘贴到 运行。禁用世界环绕。包括“num-movers”变量的滑块。

breed [ movers mover ]
movers-own [ steps ] ; Steps will be used to determine if an agent has moved.

to setup
clear-all
reset-ticks
ask patches [ set pcolor green ]
basic-pattern
end

to basic-pattern ; sets up gate and wall
let wallXCor 16 ; sets a white line to determine the inside & outside of the gate
repeat 33 [
ask patch wallXCor 0 [ set pcolor white ]
set wallXCor wallXCor - 1 
]
ask patches with [ pycor > 0 ] [ set pcolor lime ] ; sets the outside of the gate to another color (lime)
; changes color of the center to lime to create a passable opening
ask patch 0 0 [ set pcolor lime ]
ask patch 1 0 [ set pcolor lime ]
ask patch -1 0 [ set pcolor lime ]
end

to distribute-agents ; Distributes the Movers outside the gate based on the patch color lime. The number needs to be set via slider "num-movers"
repeat num-movers [
ask one-of patches with [ pcolor = lime and pycor > 2 and any? turtles-here = false ] [
sprout-movers 1 [ set color red set shape "circle" facexy 0 -12 ] set num-movers num-movers- 1 ]
] end

to go
move-movers
tick
end

to move-movers ; reset the steps variable and facing
ask movers [ set steps steps + 1 ]
ask movers [ facexy 0 -3 ]
; following lines checks if next patch to be steped upon is "legal".
while [ any? movers with [ steps > 0 ] ] [     
ask movers with [ steps > 0 ] [
ifelse is-patch? patch-ahead 1
and not any? turtles-on patch-ahead 1
and [ not member? pcolor [ white brown ] ] of patch-ahead 1
[
fd 1
 set steps steps - 1
] [ dirchange ] 
]
]
end

to dirchange ;If not able to move to next patch change direction to allow a step backwards.
if ( pxcor <= 0 and ycor >= 0 ) [ facexy 1 3 ] ;fd 1 set steps steps - 1]
if ( pxcor >= 0 and ycor >= 0 ) [ facexy -1 3 ] ;fd 1 set steps steps - 1]
end

您没有收到错误消息,因为没有实际错误。代码只是卡在你的 while 循环中。

您是要注释掉 dirchange 中的 fd 1 set steps steps - 1 吗?我的猜测是你有一群乌龟面对相同的补丁(1,3 或 -1, 3)并卡住,因为 none 它们可以移动,因为另一只乌龟在它们前面。因为你只在他们实际移动时从他们的步数中减去,所以他们中的一些人永远不会达到 0 步。

由于这个原因,

While 通常是一个不好的原语,尤其是当你的移动代码中有这么多条件时,很难知道是什么导致你的 while 循环没有结束。是因为你的乌龟正对着墙,还是因为它们在世界的边界,还是因为其他人挡住了它们的去路?您只是不知道,而且由于代码陷入循环,您的模型视图不会更新,因此您看不到发生了什么。

如果你坚持要保留 while,我至少会提供一个保护措施:写一个海龟报告器来检查你的海龟是否能够移动并打破你的 while 如果它们可以't,或者给他们有限数量的 尝试 移动,而不是要求他们实际移动过。