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

时间:2016-08-11 14:05:59

标签: runtime-error netlogo

我试图让一堆turtels(Movers)穿过一扇门,避开白色的墙壁。不知何故,模型在几次运行后冻结。 Go按钮保持黑色和蓝色圆圈永远转动。没有错误MSG给出。它必须在“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 colour 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

1 个答案:

答案 0 :(得分:2)

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

您的意思是评论fd 1 set steps steps - 1中的dirchange吗?我的猜测是你有一堆乌龟面对同一个补丁(1,3或-1,3)而被卡住,因为没有一只乌龟可以移动,因为另一只乌龟在他们面前。而且因为你只是从他们的步骤中减去它们实际移动的步骤,其中一些从未达到0步。

由于这个原因,

While通常是一个错误的原语,特别是当你的移动代码中有这么多条件时,很难知道是什么导致你的while循环没有结束。是因为你的乌龟正面临着一堵墙,还是因为它们处于世界的边界,还是因为其他人正在阻挡他们的道路?您只是不知道,而且由于代码卡在一个循环中,您的模型视图不会更新,因此您无法查看正在发生的事情。

如果你坚持保留while,我至少会提出一个保护措施:写一个海龟记者,检查你的海龟是否能够移动并打破你的while,如果他们可以&#39 ;或者在移动时给他们有限数量的尝试,而不是要求他们实际移动。