2016-02-26 63 views
1

在我的模型中,龜的數量是基於用戶使用滑塊定義的值的動態值。滑塊可以取值在2到10之間。每個烏龜都有自己的一組座標和特徵,因此我使用下面的代碼來創建它們。Netlogo中的動態圖表

create-parties 1 
[set color red set label-color red set label who + 1 set size 3 setxy party1-left-right party1-lib-con ] 
create-parties 1 
[set color green set label-color red set label who + 1 set size 3 setxy party2-left-right party2-lib-con ] 
if Num-of-parties >= 3 
    [ create-parties 1 
[set color blue set label-color red set label who + 1 set size 3 setxy party3-left-right party3-lib-con ] ] 

我已經重複上述,直到Num-of-parties = 10。

在其中一個模塊中,我創建了一個條件,如果某個值達到0的龜就會死亡。

在我用一套電流積創建使用下面的代碼圖表模型的後面部分:

set-current-plot "Voter Support" 
    set-current-plot-pen "Party1" 
    plot 100 * [my-size] of turtle 0/sum[votes-with-benefit] of patches 
    set-current-plot-pen "Party2" 
    plot 100 * [my-size] of turtle 1/sum[votes-with-benefit] of patches 
    if Num-of-parties >= 3 [ set-current-plot-pen "Party3" 
    plot 100 * [my-size] of turtle 2/sum[votes-with-benefit] of patches ] 

等等等等所有十個可能的海龜。

問題是,如果用戶在刻度10處定義了5只烏龜和3只烏龜,那麼由於沒有烏龜3,但是由於用戶定義的烏龜數量有限,因此代碼的一部分會拋出錯誤值爲5.

請告知如何解決此問題。謝謝,感謝幫助。

問候

回答

4

在編寫模型代碼,你應該嘗試應用DRY原則:不要重複自己。分別創建每個龜,然後試圖通過分別將它們分別編址爲turtle 0turtle 1等來解決它們會導致各種問題。你在繪圖時遇到的只是冰山一角。

幸運的是,NetLogo爲您提供了處理「動態」數量海龜所需的所有設施。 ask是最常用於此的原語,但還有很多其他處理整個代理商的原語。你可以閱讀更多關於agentsets in the programming guide

在繪圖的情況下,您可以ask各方創建一個「臨時繪圖筆」。我們將使用who號碼給這些筆中的每一筆賦予一個唯一的名稱。 (這是在的NetLogo的who數量的極少數合法的用途之一)

將這個代碼中的「情節設置命令」的情節領域:

ask parties [ 
    create-temporary-plot-pen (word "Party" (who + 1)) 
    set-plot-pen-color color ; set the pen to the color of the party 
] 

(請注意,你不會需要先前定義的繪圖筆:您可以將它們刪除,每次設置繪圖時都會動態創建新繪圖筆。)

要做實際的繪圖,我們可以使用非常類似的代碼。把這段代碼放在你的圖的「Plot update commands」字段中:

ask parties [ 
    set-current-plot-pen (word "Party" (who + 1)) 
    plot 100 * my-size/sum [ votes-with-benefit ] of patches 
] 
+0

非常感謝你的幫助。 – Yuvaraj