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:
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 ]
What's Next?
This model can be extended and improved in many other ways. Try
figuring out how to do some of the following things on your own.
6. HAVE YOUR TURTLES PICK THE CLOSEST FOOD SOURCE:
That's not a typo. If the vision-range setting is set to a high
number your turtles may in fact travel towards a far away food source even
if a closer food source is available. How can you fix that?
7. HAVE YOUR TURTLES CATCH DISEASES:
Whenever you get too many animals crowded into one space the
liklihood of disease increases. How might you model that?
8. HAVE YOUR TURTLES NEED (AND SEEK OUT) WATER.
Most animals need drinking water to survive. Have some of your
patches become watering-holes, that your turtles must routinely visit
in order to survive (hint: create an agentset called water, or a
variable that is owned by the patches, see "traffic basic" and "traffic
grid").
9. HAVE YOUR THE WATERING-HOLES DRY UP OVER TIME:
10. CREATE MALE AND FEMALE TURTLES:
Right now your turtles simple make more of themselves whenever they
have enough energy... again, not realistic. (hint: To create male and
female turtles you will want to create 'breeds', see "Wolf Sheep
Predation")