2015-10-13 22 views
0

我正在嘗試在Netlogo中編寫一個程序,該程序首先識別紅色補丁的兩個隨機鄰居,然後將這兩個補丁更改爲藍色。網格上的補丁可以是紅色,藍色或白色。Netlogo Procedure n-of 2 neighbors

我把這段代碼放在「去」部分,它使「tick」無效。它也似乎不在任何地方運行。

to run-2-neighbors-people 
    if all-voters-talked-2-people 
    [ ask n-of 2 neighbors with [ pcolor = red ] 
    [ set pcolor blue] 
    ] 
    end 

下面是完整的代碼:

patches-own 
[ 
    vote ;; my vote (0 or >= 1) 
    total ;; sum of votes around me 
] 

to setup 

    clear-all 
    ask n-of (percentage-of-voters/100 * count patches) patches [ 
    set vote 1 ] 
    ask patches [set-color] 
    ask patches [set initial-voters count patches with [ pcolor = red ] ] 
    ask patches [set non-voters count patches with [pcolor = white] ] 

    reset-ticks 

end 


to go 
    ask patches 
    [ set total (sum [vote] of neighbors) ] 
    ask patches [if pcolor = white 
    [if total >= 4 [set pcolor blue ] ] ] 
    ask patches [if pcolor = white 
    [if total < 4 [ set vote 0 ] ] ] 
    run-all-voters-talked-2-people 
    run-share-on-social-media 
    tick 
end 

to run-share-on-social-media 
    if share-on-social-media 
    [ask patches with [pcolor = red] [ 
     sprout 1 [ 
     set shape "arrow"] 
     ]] 
end 

to run-all-voters-talked-2-people 
    ask patches with [pcolor = red] [ 
    ask n-of 2 neighbors with [ pcolor = red ] 
     [set pcolor blue] ] 
end 


to set-color ;; patch color procedure 
    ifelse vote >= 1 
    [ set pcolor red ] 
    [ set pcolor white ] 
end 
+0

這是什麼「它使'剔'失效」是什麼意思? –

+0

@SethTisue它可能意味着當他添加了這段代碼時,程序沒有打勾,這可能是因爲它沒有終止。 –

+0

我認爲我們需要看到更多的代碼,例如「所有選民說話的2人」程序是做什麼的? –

回答

1

這個是什麼?它工作正常,直到所有紅色補丁的鄰居都變成藍色。你只需要添加按鈕「設置」和「去」你的界面。

修改:

在這個例子中,紅色斑塊,首先檢查其鄰國的顏色且僅當有更多或等於2的白色,然後把它們的隨機2成藍色。 ELSE(來自「ifelse」不夠白色的鄰居),沒有任何反應。

在這種方法中,你不會有這樣的警告:Requested 2 random agents from a set of only 1 agents.

to setup 
    clear-all 
    reset-ticks ; add reset ticks in "setup" - without this you can't use "tick" in "go" procedure 
    setup-patches 
end 

to setup-patches 
    ; create random world with 3 basic colors 
    ask patches [ 
    set pcolor white 
    ] 
    ask n-of floor (count patches/3) patches [ 
    set pcolor blue 
    ] 
    ask n-of 5 patches with [pcolor = white] [ 
    set pcolor red 
    ] 
end 

to go 
    ask patches with [pcolor = red] [ 
    ; turn white patches to blue only if the red patch has at least 2 neighbors with pcolor white. 
    ; if there is only 1 neighbor with pcolor white, nothing happens 
    ifelse count neighbors with [pcolor = white] >= 2 
     [ ask n-of 2 neighbors with [pcolor = white] [ 
      set pcolor blue 
     ] 
     ] 
     [ stop ] 
     ] 
    tick ; add tick at the end of "go" procedure 
end 

第三修改:

,如果你是一個補丁,你的顏色是紅色,然後問我)你的鄰居的2 (任何鄰居,因此可以是藍色,白色,紅色)ii)如果你是白色的話會變成藍色。所以,如果這兩個鄰居來自i)

  • 都是藍色/紅色 - >沒有任何反應。
  • 均爲白色 - >二者轉
  • 藍色一個白色一個不同的顏色 - >只有白轉藍色

的代碼:

to go 
    ask patches with [pcolor = red] [ 
    ask n-of 2 neighbors [ 
     if pcolor = white [ 
     set pcolor blue 
     ] 
    ] 
    ] 
    tick 
end 

最終狀態:

enter image description here

+0

現在,它拋出此錯誤消息:請求從一組代理中的2個隨機代理。 錯誤,而修補程序12 -2運行N-OF 程序調用RUN-ALL-VOTERS-TALKED-2-PEOPLE 程序調用GO 按鈕'去' – sugarquebert

+0

我添加了完整的代碼到我的問題。任何人都可以在這裏啓發我嗎? – sugarquebert

+0

但是,您確定將至少一個白色補丁轉換爲藍色是不夠的嗎?你隨時需要兩個嗎? – maycca