Lecture: 12-11-2012

posted Dec 11, 2012, 5:56 AM by Samuel Konstantinovich   [ updated Dec 11, 2012, 8:49 AM ]
Voting Model:
Today you will extend your voting system to work with an initial state of 3 colors. A majority of 5 or more of the same color will still be required to change a patch.

1. Your setup should always clear-all (and therefore be observer context). After that it should pick random colors for each patch. (lets use Red/Blue/Green) 

easy way : use the one-of [ ]  command, like  one-of [2 3 9] chooses from those three numbers with equal probability. You can use it on a list of colors as well. 

How do you randomly select your starting colors if you wanted different probabilities?

BAD IDEA : Since some of you do this I will ask you to think about why it doesn't work:

ask agentset 
    if random 100 < 20 
    [ A ]    
    if random 100 < 60 and random 100 >= 20
    [ B ]
    if random 100 >=60 
    [ C ]
;Why does this sometimes not pick one of the 3 options A B and C? 

;When random 100 is run, it is different every time... so EACH of those booleans could be false. *sad face*

GOOD IDEA: Use random one time, and store that number in a local variable. 
(NEW COMMAND:  let newVariable value)

ask agentset
    let rand random 100  ;
    ;This puts the random value into a local variable.
    if rand < 20 
    [A ]
    if rand <60 and rand >=20 
    [B ]
    if rand >=60 

2. How do you structure your go command? 
You probably want to make all of your forever buttons that control the entire program use OBSERVER CONTEXT functions.

;Your go really should just look like this because it describes the algorithm in terms of the big ideas:
to go
  ask patches [ countNeighbors]
  ask patches [ changeColor]

;There are several things to notice:
;1. The 2 parts are using separate ask commands. 
;2. The 2 parts use other functions, instead of writing a bunch of code in the if statements.
;    It is usually better to make functions with readable names to make understanding the code much easier.
;3. Since the function is observer context, you can easily add turtles to the model later if you wanted. 

You need to write the two other functions. When you are asking patches to run a command they should be patch context.

* countNeighbors should count the number of different color neighbors and store the results in a patches variable (patches-own). For three way voting, you require three variables to store the counts.  
    -counting the number of patches isn't enough you have to SET the variable to the count. 
    -you want to set the variable to the count of the neighbors with a certain color.

* changeColor should then make the patches pick a new color based on the counts.
    -you DON'T want to use else here. Look at this bad code:
         ifelse x > 4 [ A ] [ B ]  ;would default to B even when x has no majority.