Tutorial 4: Extending a NetLogo Model


Introduction

This tutorial assumes that you have completed the standard tutorials 1-3 on the NetLogo site.
Tutorial #1: Models
Tutorial #2: Commands
Tutorial #3: Procedures
Hopefully you also took some time to play with a couple of the models in the "model library" that come with NetLogo.

Extending Tutorial #3

In tutorial #3 you created a very basic model. In that model your turtles can be considered to be sheep or cows or some other animal that eats grass. However the model is very limited and unrealistic. Let's improve that model.

1. LOAD TUTORIAL #3: Open NetLogo, goto the "Models Library" and open "Tutorial 3" (you will find it under "Code Examples").

2. RUN THE MODEL, AND LOOK AT THE CODE.

3. SELECT OR CREATE A NEW SHAPE FOR YOUR TURTLES:
Right now your turtles are multi-color triangles. Find (or design a new shape for your turtles by going to 'Tools' and then "Turtle Shapes Editor'. Make sure and write down the name of the shape that you decide to use. In order to use the shape that you picked you will need to go to the "setup-turtles" procedure and add the following code:
set-default-shape turtles "cow" ;; Change cow to the shape name that you wrote down.
If you want to change the color of your new shapes you can do that by adding this line of code:
ask turtles [ set color white ]


4. CREATE THE FOLLOWING 3 SLIDERS IN THE INTERFACE:
Sliders

5. HAVE YOUR TURTLES AGE:
Right now provided they find enough grass to eat your turtles (or cows, or sheep or whatever) will live forever. That's certainly not very realistic. You will need to change your program in a couple of places so that your turtles can grow old and die.

- Add the variable "age" for turtles.
turtles-own [energy age]

- Change the setup-turtles procedure so that turtles start with a random age.
ask turtles [ set age random max-lifespan ]

- Change the move-turtles procedure so that turtles age by one day every time that they move.
ask turtles [ set age age + 1 ]

- Change the check-death procedure so that turtles die if their age exceed the max-lifespan limit.
ask turtles [ if age >= max-lifespan [die] ]

- Change the reproduce procedure so that new turtles start off with an age of 0.
hatch 1 [ set energy birth-energy set age 0 ]


5. HAVE YOUR TURTLES LOOK FOR FOOD:
At the moment your turtles wander around randomly and if they happen to stumble upon food they eat it. A turtle could easily starve to death, even when there is plenty of grass around. That's certainly not very realistic. Real animals look for food and travel towards it. We will need to change your program in a couple of places so that your turtles can do that.

- Find the following code in the move-turtles procedure.
right random 360
forward 1

- Change those two lines, to the following 5 lines.
let candidates patches in-radius vision-range with [ pcolor = green ]
ifelse any? candidates
[ face one-of candidates ]
[ rt random 360 ]
forward 1


5. HAVE THE GRASS GROW WHEN IT RAINS.
At the moment the grass grows radomly after it has been eaten. In many parts of the world the grass depends on seasonal rains, and only grow a few times a year. Change your program so that the "rain-frequency" slider that you created controls how often the grass grows.

- Find the following code in the regrow grass procedure.
if random 100 < 3 [ set pcolor green ]

- Replace that line with this one:
if ticks mod rain-frequency = 0 [ set pcolor green ]


Completed Tutorial 4.

(right click and choose save-link-as to download and run).