我試圖讓一堆turtels(搬運工)去槽入口,避免牆壁是白色的。不知何故模型在幾次運行後會凍結。去按鈕保持黑色和藍色的圓圈永遠。沒有錯誤給予的味精。它必須在「move-movers」函數中進行一些計算,但我不能確定原因。NetLogo:模型卡住w沒有錯誤消息
我添加了我的代碼的簡單版本,但仍然會導致崩潰。複製&粘貼即可運行。禁用世界換行。包含「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
在這種情況下,你會建議使用什麼來代替while循環? –
我建議編寫一個可以遞歸調用的命令過程,只要條件不滿足。它通常會使調試更容易。 –