2017-11-09 LAB05

posted Nov 9, 2017, 6:09 AM by Konstantinovich Samuel   [ updated Nov 10, 2017, 5:57 AM ]
Local Variables:

We use local variables because:

-we need to remember a number but only need it inside the current function

-we want to avoid repeating a complex calculation multiple times

-it is cleaner in our code to put a complex expression in a simple variable name:

-You want to remember a single random value, to use in multiple places:

This code is VERY hard to read, it also recounts the same agentset 3 times: 

if count turtles with [ abs pxcor + abs pycor > 20 ] < 10
   [ function1]
if count turtles with [ abs pxcor + abs pycor > 20 ] >= 10 and count turtles with [ abs pxcor + abs pycor > 20 ] < 100
  [ function2]

There is a repeating complicated calculation being made, that makes the rest of the code more difficult to read. We can replace this with a local variable to make it much more clear:
let badTurtles count turtles with [ abs pxcor + abs pycor > 20 ]
if badTurtles < 10
   [ function1]
if badTurtles >= 10 and badTurtles < 100
  [ function2]

You can also store random values, to use them in multiple places:

;3 code blocks have an equal chance of being run:
let n random 3
if n = 0 [ CODEBLOCK1 ]
if n = 1 [ CODEBLOCK2 ]
if n = 2 [ CODEBLOCK3 ]

;as long as the codeblocks do not change n, this will only trigger

;one of the 3 codeblocks!

This does not require ifelse, and it lets you run ANY kind of code, including report.

If you ONLY want pick VALUES randomly, you do not need to do this:

one-of [ 1 2 3 4] 

one-of [ red green blue]


both of these pick a random value from the brackets. 

BUT you could not run code this way

Goal: Patches can vote! 

Do Now: Log in, open NetLogo + lecture notes page.

Patches have two useful functions:



Check them out in the dictionary.

LabN05 Voting 

(submit in hw server by Monday 8am) 

You only get 1 lab period today however

Phase I - 2 color voting


All patches choose a color randomly to start. 

1. They then look at their 8 neighbors, and see how many red/blue neighbors they have. 

After everyone completes step 1:

2 Every patch decides if their neighbors are a majority of one color and if they are they change to that color.

The problem you will have: You will not finish 1 before starting 2. This means that some people are counting the new color instead of the original. This affects results unpredictably!

TWO WAYS of asking agents to do TWO THINGS:


ask patches[

Each patch runs BOTH A and B, before the next one starts. 

Halfway through half of the patches will have run A and B, the other half would have run NEITHER.


ask patches[  A ]

ask patches [  B ]

ALL patches run A. Then all patches run B.

Halfway through, all patches would have run A, and NONE would have run B.


Add monitors for each color so you can see approximately what percent wins in the end!


Assign each patch a pcolor of red or blue randomly.

Before you write more:

Patches must own some variables for this lab. They need to keep track of how many of their neighbors are a specific pcolor.

Go:  (make a forever button)

1. Make every patch to look at how many red/blue neighbors they have. They need to remember this number for later.

2. After everyone counts, make every patch to change their color if there is a *majority. They should stay the same if there is a tie. ( simple majority, 5 or more of one color.)

You must make sure step 1 completes before you start step 2. 

Separate these actions, because you don't want to count the UPDATED colors of neighbors, you want to count the ORIGINAL colors before the changes happen!

Phase II - 3 color voting

-Make a new monitor for green patches.

-Make a new button setup-3way : 

    Make the patches red, blue, green randomly.

-Make a button go-3way :  (make a forever button)

   If there is a majority of any one color ( 5 or more of any color ) then change to that color.