GEOG3150 - GIS, Geocomputation and Geoplanning - Semster 2


Table of Contents

1. Advanced Variables
2. Grass Grows...
3. Giving Birth.
4. Creating a graph.

Practical 3, part 1 - Advanced Variables


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.

Contexts and variables

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.

  1. You should have made a copy of the simple model you created for practical 2. Open this now; we will add new functionality to this model.
  2. 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.

  3. To create these new variables, add the following code right at the top of the model (before the to setup line). turtles-own [
     energy
    ]

    patches-own [
     time-since-eaten
    ]
  4. 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.

    Code for using the 'energy' variable

    We will make use of the energy variable right away.

  5. Add the following line to the 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).
  6. Also add the following lines to the 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 ).
  7. 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.

    Code to eat grass
  8. In the '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).
  9. Image showing all turtles have died

    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.

  10. Finally, check that everything works by clicking on the 'interface' tab and running the model. If it runs too quickly for you to see what is happening use the control at the top to slow it down. You should see the sheep die. This is because we have given them the ability to eat grass, but we haven't given the grass the ability to grow back! We'll do this next.

Have a go at the questions below and make sure you're comfortable with the answers, then move on to part 2.

Questions

  1. List some of the built-in variables you have come across that belong to turtles.  
  2. Also list some for patches.  
  3. What is the URL for the NetLogo documentation that shows you all the built-in variables that belong to patches and turtles.  
  4. Write the NetLogo code that you would use to increase the value of a variable called happiness by ten units.  
  5. Write the NetLogo code that you would use to test if the value of a variable called deprivation is greater or equal to twenty and, if it is greater, set it to zero:  
     
     

[School of Geography homepage] [Leeds University homepage]