Table of Contents
1. Advanced Variables |
2. Grass Grows... |
3. Giving Birth. |
4. Creating a graph. |
This part is very short - all you need to do is add a few lines so that the grass in the model re-grows after a certain amount of time. Importantly, we are going to create a slider so that the person running the model can experiment with different regrowth times.
grass-regrow-time
(if you
can't remember how to create sliders, have a look back at
practical 2). Give the slider a range of
0 to 100 with a default value of your choice (I chose 80).Recall that, in NetLogo, sliders represent variables. So, if you have created
a slider called grass-regrow-time
, NetLogo also makes a variable called
grass-regrow-time
that you can refer to in your model code. The variable
will be part of the observer context, so all turtles and patches can see what
value it holds. We are going to use this variable to determine how long it takes for
grass to regrow once it has been eaten.
go
procedure, once the
ask turtles
block has finished (after the closing ']
'):
ask patches[
if pcolor = brown [
set time-since-eaten time-since-eaten + 1
if time-since-eaten > grass-regrow-time [
set pcolor green
]
]
]
(see the right image if you're not sure where the code should go).It might look complicated at first, but the code above doesn't do anything that you haven't seen before. Its overall job is to see if enough time has passed for brown patches to become green again. The following table explains what each line does:
Code | Explanation |
---|---|
ask patches [ |
This tells NetLogo to run through the patches, one by one, so we can see if they are ready to re-grow. |
if pcolor = brown [ |
This checks the colour of an individual patch. If the patch is brown then it might be time to regrow. If it is green, then we just ignore it. |
set time-since-eaten time-since-eaten + 1 |
This increases the value of the time-since-eaten variable by
one unit. This is how we keep track of the amount of time that has passed since
the patch became brown (i.e. the grass was eaten). |
if time-since-eaten > grass-regrow-time [ |
Here, we make use of the grass-regrow-time slider. If enough
time has passed since the patch become brown, the we can do something to it. |
set pcolor green |
This simply changes the colour of the patch from brown to green. Importantly, the line above makes sure that this will only happen if enough time has passed. If enough time hasn't passed, then the patch will stay brown (for now). |
] |
This finishes the if time-since-eaten > grass-regrow-time [
block. |
] |
This finishes the if pcolor = brown [ block. |
] |
This finishes the ask patches [ block. |
Understanding the logical flow of the program as it compares variables and decides
what to do is a common task in computer programming and a very useful skill in general.
If you're not sure about how the if
commands work, have a look back at
the lecture .
Now you can move on to the next part where we will allow the turtles to give birth.