2014-02-14 93 views
2

我試圖選擇owner龜的status小於myself的鄰居補丁。如果存在這種補丁,則myself將其領土擴展到這些補丁併成爲owner。但我無法要求NetLogo確定相鄰修補程序ownerstatus。我可以用owner選擇鄰居,然後打印這些所有者的status,但就是這樣。任何幫助將非常感激。代碼如下。NetLogo從補丁變量查詢龜變量

breed [animals animal] 

animals-own [ orig territory food status] 
patches-own [ owner hsi] 

to setup 
    clear-all 
    ask patches 
    [ 
    set owner nobody 
    set hsi random 5 
    set pcolor scale-color (black) hsi 1 4 
    ] 
    let $colors [red pink yellow blue orange brown gray violet sky lime] 
    ask n-of 10 patches 
    [ 
    sprout-animals 1 
    [ 
     set orig patch-here 
     set territory patch-set orig 
     set status random 4 
     set color item who $colors 
     set pcolor color 
     set owner self 
     pen-down 
    ] 
    ] 
    reset-ticks 
end 

to go 
    if all? animals [food >= 150] [ stop ] 
    if ticks = 50 [ stop ] 
    ask animals [ expand ] 
    tick 
end 

to expand 
    let neighborline no-patches 
    let pot-comp no-patches 
    if food < 150 
    [ 
    ask territory 
    [ 
     if any? neighbors with [owner = nobody] 
     [ 
     set neighborline (patch-set neighborline neighbors with [owner = nobody]) ;this works fine. 
     ] 
     if any? neighbors with [owner != nobody] 
     [ 
     set pot-comp (patch-set pot-comp neighbors with [[status] of owner < [status] of myself]) ; this does not work. What am I doing wrong? 
     ] 
    ] 
     let target max-n-of 3 neighborline [hsi] 
     set territory (patch-set territory target) ; grow territory to 3 patches with no owners and highest HSI 
     ask territory 
     [ 
     set owner myself 
     set pcolor [color] of myself 
     ] 
    set food sum [hsi] of territory 
    ] 
end 

回答

2

在這個代碼塊:

if any? neighbors with [owner != nobody] 
[ 
    set pot-comp (patch-set pot-comp neighbors with 
    [[status] of owner < [status] of myself]) 
] 

這給你一個預期的輸入是一個龜agentset或龜,但得到任何人,而不是錯誤。原因是您首先要檢查是否與業主有鄰居關係,但是您正在檢查status所有鄰居(通過neighbors with)。

所以,也許你可以將owner != nobody添加到你的with子句中?

set pot-comp (patch-set pot-comp neighbors with 
    [owner != nobody and [status] of owner < [status] of myself]) 

但你得到沒有指定哪個龜補丁無法訪問龜變量。這是因爲with在提問者層次結構中會帶你更深一層,所以myself現在指的是補丁,而不是像你可能希望的那樣引用動物。您可以通過使用臨時變量(例如expand過程頂部的let current-animal self)來解決這類問題。在你的情況下,我會直接將狀態存儲在一個變量中。略微調整,當前的ask territory塊可能會成爲:

let my-status status ; store this while `self` is the animal 
ask territory 
[ 
    set neighborline (patch-set neighborline neighbors with [owner = nobody]) 
    set pot-comp (patch-set pot-comp neighbors with 
    [owner != nobody and [status] of owner < my-status]) 
] 

請注意,我擺脫了if any? neighbors with [...]檢查。您並不需要它們:如果沒有適合您條件的修補程序,則只需將空補丁集添加到現有修補程序集中,這沒有任何意義。

+0

謝謝Nicolas!我一直陷在這太久。現在我明白「我自己」是指錯誤的水平。 – user2359494