0

I am trying to write a procedure in Netlogo that first identifies two random neighbors of a red patch, and then changes the two patches to blue. The patches on the grid are either red, blue or white.

I put this code in the "to go" section and it invalidated the "tick". It also doesn't seem to run anywhere.

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

Here is the complete code:

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

1 Answers1

1

What about this? It works fine until all neighbors of red patch become blue. You just need to add button "setup" and "go" to your interface.

MODIFIED:

in this example, the red patch firstly check the color of its neighbors and only IF there are more or equal 2 with white color, then it turns random 2 of them into blue. ELSE (from "ifelse"not enough white neighbors), nothing happens.

In this approach you will not have this warning: 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 

3rd modification:

if you are a patch and your color is red, then ask i) 2 of your neighbors (whatever neighbor, thus can be blue, white, red) to ii) turn blue if you are white. So, if these 2 neighbors from i)

  • are both blue/red -> nothing happens.
  • are both white -> both turn
  • blue one white one different color -> only the white turn blue

The code:

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

final state:

enter image description here

maycca
  • 3,848
  • 5
  • 36
  • 67
  • Now it is throwing this error message: Requested 2 random agents from a set of only 1 agents. error while patch 12 -2 running N-OF called by procedure RUN-ALL-VOTERS-TALKED-2-PEOPLE called by procedure GO called by Button 'go' – sugarquebert Oct 14 '15 at 18:14
  • I added the full code to my questions. Can anyone enlighten me here? – sugarquebert Oct 14 '15 at 18:46
  • However, are you sure that it is not enough to convert at least one white patch to blue? do you need two of them at any time step? – maycca Oct 14 '15 at 18:51
  • I don't need 2 to *convert* 2, but at least ask two patches around it, if it's white, then to turn it bluet, but if it's red or blue then leave it alone. – sugarquebert Oct 14 '15 at 20:42
  • the idea is that one patch (voter) communicates with 2 of its neighbors. – sugarquebert Oct 14 '15 at 20:42