Table of Contents
1. Advanced Variables |
2. Grass Grows... |
3. Giving Birth. |
4. Creating a graph. |
Recall from practical 1 that variables can
be used to hold information about different objects in NetLogo. For example, turtles might
have a variable called age
that saves the age of each turtle. Similarly,
patches might have a variable called grain
that stores (for example) the
amount of grain growing on each patch.
We have already made use of some turtle and patch variables, such as color
,
xcor
and ycor
(for turtles), or pcolor
for
patches. These are build in to NetLogo; you can see a full list of built in variables here:
http://ccl.northwestern.edu/netlogo/docs/dictionary.html#builtinvariables
However, to build more interesting models we need to be able to create new variables to store different pieces of information that might be required for the system under investigation. This part of the practical will look at how to do this.
We want to create two new variables. One, called energy
, will store the
amount of life energy each turtle has. If this reaches 0 then they die. The second
variable, time-since-eaten
, will store the amount of time that has passed
since a patch of grass has been eaten. Grass takes some time to regrow, so this variable
can tell us when a patch has completely regrown.
to setup
line).
turtles-own [
energy
]
patches-own [
time-since-eaten
]
In the code above, turtles-own
tells NetLogo that we are going to
define some new variables that will belong to the turtles. Variables can then be
created in between the square brackets (one per line). We create a single variable
called energy
. Similarly, the patches-own
code means that
we are about to define variables for the patches.
We will make use of the energy
variable right away.
setup-turtles
procedure, just after the
ask turtles [
line:
set energy 5
This will give each turtle 5 units of energy at
the beginning of the model. (See the right image if you're not sure exactly where the
new code should go).go
procedure, just after the
ask turtles [
line:
set energy ( energy - 1 )
if energy <= 0[
die
]
The first line reduces the value of the energy
variable by one. So, each
time the model iterates every turtle will lose one unit of energy. The next line
(if energy <= 0[
) checks to see if the turtle has lost all its energy. If
it has, then the die
command removes the turtle from the model. (If you're
confused about the 'if
' statement, refer to
the lecture ).You'll notice that in the image of the code above, some of the text is grey. In
NetLogo, anything that appears after a ';
' symbol is called a
comment. These aren't actually read by NetLogo - they can be used by the
developer to explain what it is that their code is doing. Always include
comments in your code! These aren't just so that other people can understand
your code, they are also so that you can remember what your code does in case you
forget.
Anyway, now the turtles have some energy when they are created and each time the model iterates they lose some. To conclude this part of the practical, we will add some code to make the agents eat grass and gain energy.
go
' procedure, find the code that makes the turtles eat the
grass:
if pcolor = green [
set pcolor brown
]
All that code does is change the colour of the patch that the turtle is standing on.
Add two new lines so that the code now reads:
if pcolor = green [
set pcolor brown
set energy (energy + 5 )
set time-since-eaten 0
]
The new code still changes the colour of the patch from green to brown to indicate that
it has been eaten. But now it also gives the turtle five units of energy and sets
the time-since-eaten
variable to 0 to indicate that no time has
elapsed since the patch was eaten. (This will be important later when we want grass
to grow back slowly over time).
Note: the more keen-eyed of you will have noticed that time-since-eaten
is a variable that belongs to the patches, so we shouldn't be able to change it from
within the turtle context (i.e. in an ask turtles [ ... ]
block). Remember
we can get around this because NetLogo lets you access patch variables from the turtle
context for the patch that a turtle is currently standing on. This is quite
important; if you're confused either ask a demonstrator or refer back to the
variables diagram
from the lecture.
Have a go at the questions below and make sure you're comfortable with the answers, then move on to part 2.
happiness
by ten units.
deprivation
is greater or equal to twenty and, if it is greater,
set it to zero: