2015-10-13 25 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