2016-08-11 33 views
0

我試圖讓一堆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 

回答

2

您沒有收到錯誤消息,因爲沒有實際的錯誤。代碼只是卡在你的while循環中。

您是不是想要在您的dirchange中註釋掉fd 1 set steps steps - 1?我的猜測是,你有一羣面對相同補丁(無論是1,3還是-1,3)的海龜,並且因爲沒有一隻龜能夠移動,因爲另一隻龜在它們前面。而且因爲如果他們實際移動的話,他們只能從他們的步驟中減去,其中一些從未達到0步。

While通常是一個不好的原因,因爲這個原因,特別是當你在移動代碼中有這麼多條件時,很難知道是什麼導致你的while循環沒有結束。是因爲你的龜面臨着一堵牆,還是因爲它們處於世界的邊界,還是因爲別人阻擋了他們的路?你只是不知道,而且因爲代碼被困在一個循環中,你的模型視圖不會更新,所以你不能看到發生了什麼。

如果你堅持保留while,我會至少提供一個保護措施:寫一個烏龜記者,檢查你的龜是否能夠移動,如果他們不能,或給他們一個有限的數字的嘗試在移動,而是要求他們實際移動。

+0

在這種情況下,你會建議使用什麼來代替while循環? –

+0

我建議編寫一個可以遞歸調用的命令過程,只要條件不滿足。它通常會使調試更容易。 –