# Introduction to Complex Systems

Prof. Dirk Brockmann, Winter Term 2021

# Practical Problem 3: Intro to Patches

## Second session: Intro to patches

Yesterday we coded our first programs with turtles, one of the agent types in netlogo. I also mentioned that we have another type of agent, the patch, in netlogo which is just one of the lattice squares that make up the world.

### Let's begin...

by opening a new netlogo file and like always we add two buttons, one setup button, one go button. Make sure you check the "forever" option for the go button and choose on ticks instead of continuous in the main header menu.

Now we write our template code

to setup
ca
reset-ticks

end

to go

tick
end

Patches cannot be created or killed and they cannot move. They are therefore simpler than turtles. Let's now turn all the patches into black or white color with 50% chance. We can do it like this:

### setup

to setup
ca
reset-ticks
ask patches [
ifelse random-float 1 < 0.5 [
set pcolor white
][
set pcolor black
]
]
end

to go

tick
end

The patch color is coded in the patch variable pcolor. To make it look better make a world that is 128x128 in size and chose a patchsize or 3 in the settings.

### go

Now we code a simulation that we also discussed in class. In each iteration a patch picks one of its neighbors and acquires the color of that neighbor. We can do this in a single beautiful linke

to go
ask patches [
set pcolor [pcolor] of one-of neighbors;
]
tick
end

The line can be understood best from back to front. the function neighbors are all eight neigbors of a patch. one-of neighbors returns a single neighbor [pcolor] of ... returns that neighbors pcolor. This is then used to set the pcolor of the refering patch.

### try it

when you run the code you should see a dynamic ragged pattern.

## More than one color...

Now let's do this with more than one color. All we need to do is to initialize the system with more than one color for example with the 13 base colors of netlogo:

to setup
ca
reset-ticks
ask patches [
set pcolor one-of base-colors
]
end