GEOG3150 - GIS, Geocomputation and Geoplanning - Semster 2


Table of Contents

1. Running an example model
2. The Basics
3. Using ask

Practical 1, Part 2 - NetLogo basics and commands


In this part of the practical you will see how the basic elements of the NetLogo program fit together.

The NetLogo Program

Firstly, it's important to understand how the program itself works. The main display consists of the following components:

Annotated image of initial NetLogo screen
Interface This tab selects the main interface. This is where you can add elements (buttons, sliders, switches, etc.) that control the model and allow you to view the results.
Info This tab presents information about the model you currently have loaded.
Code This tab is where the you can write the computer code that controls the model.
Model window This window shows the locations of the agents and condition of the patches (basically what is happening as the model runs).
Command Centre The command centre displays output messages. It can also be used to interact with the model via commands (we will come onto commands shortly).
Buttons, sliders, graphs, etc. These controls are used to interact with the model and understand what it is doing. You add them yourself, depending on which ones are needed for the particular application (not all models will need a graph of results for example).

    The Command Centre and Basic Commands

    Before starting, recall from the lecture that a model in NetLogo consists of 'patches', 'turtles' and the 'observer' (look at the lecture slide to refresh yourself if you have forgotten).

    The command centre can be used to send commands to the model. There are two parts: the top part shows outputs from the model and the bottom part allows you to enter commands that are sent to the model. NetLogo has a fantastic user manual, so if it isn't clear what a command does, or how it should be used, look it up. The NetLogo Dictionary is particularly useful. Each time a new command is introduced, I have included a link to the official documentation.

  1. Load the 'Wolf Sheep Predation' model from the models library (under 'Biology').
  2. Press the setup button to initialise the model. You should see some sheep, wolves and green grass
  3. Switch the 'grass' slider to 'On'. This tells the model to simulate the sheep eating grass - we'll need this feature later.
  4. The show command

  5. The first command that we will experiment with is show. Type the following into the command centre:show "Hello World" You should see NetLogo repeat "Hello World" back to you, like this: observer> show "hello world"
    observer: "hello world"
    Try this again with some other text (don't forget to put quotes around the text).
  6. This might not seem particularly useful, but the show command comes into its own when you combine it with other commands.

    The count command

  7. Now combine two commands: show and count as follows: show count turtles What does this show you? Try this as well: show count patches What does that tell you?
  8. Two things are actually happening when you run those commands. The count command is counting something (in this case the number of turtles or patches). The show command is behaving the same way as it was before, except now it is showing us the number provided by the count command, rather than the "Hello World" text.

    The with command

    Documentation for the 'with' command
  9. We can use the with command to influence a group of all the turtles or patches. Try the following: show count patches with [pcolor = green] show count patches with [pcolor = brown]
  10. What is happening with those two commands?

  11. Now run the model for a few seconds (press 'Go' to start it, then press it again to stop). Repeat the two commands above. What result do you get now? (Note, if none of the grass turned brown you need to flick the 'grass?' slider to 'On').
  12. The with command actually needs two pieces of information. It needs a group of agents or patches on the left, and a test on the right (in this case we test the colour of the patches). You can find more information about all the commands available in the NetLogo documentation (click on 'NetLogo Dictionary' and then look for the 'with' command.)

    Variables

    In NetLogo, it is possible to save values in things called 'variables' (for info, see the documentation on variables). Different parts of the model can check the values of the variables to decide what they should do. For example, in the Wolf Sheep Predation model, each of the sliders changes the value of a variable. So, if you move the wolf-gain-from-food slider from 20 to 40, that will change the value of a variable called wolf-gain-from-food. Each time a wolf eats a sheep it checks the value of that variable to decide how much new 'energy' it will gain. In this case, it will now get 40 units rather than only 20.

    Individual turtles and patches can also have their own variables; this is one of the ways that models can account for heterogeneity (we'll cover this in more detail in later lectures). For example, in the Wolf Sheep Predation model, the wolves and sheep have a variable called energy. This records how much energy each individual wolf or sheep will have at any given time. If this reaches zero, then they will die. We'll look at variables in more detail in the next section.

    The set command

    The last command we'll look at is set. This command can be used to manually change the values of variables without having to use a slider. You tell the command which variable to change, and what to change it to.

  13. See what happens when you use the following set commands. Before running each command, setup the model. Then execute the command and then let the model run for 10-20 seconds. set sheep-gain-from-food 12
    set sheep-gain-from-food 50
    set initial-number-wolves 100
    set wolf-reproduce 10

Activities (Practical 1, Part 2)

You've been introduced to a large number of commands so far. In this short activity, see if you can work out what the commands are doing and then build your own. For these questions, we will use the 'Wealth Distribution' model (under 'Social Science' category). Some of these are tricky, if you get stuck ask someone else sitting nearby or a demonstrator.

Begin by setting up the model. You should see a large number of "poor" red people, fewer "middle class" green people and even fewer "rich" blue people. The colour of a patch shows you how wealthy it is (brighter yellow -> more wealth). For more information you can look on the 'Info' tab.

Note the commands that you would use, as well as the answer, to answer the following questions:
  1. What is the total number of people in the model? Command: Number:
  2. How many rich (blue) people are there in the model? Command: Number:
  3. How many poor (red) people are there in the model now? Command: Number:
  4. How many patches have more than 10 units of wealth. (Hint: grain-here tells us how much wealth (or grain) is present at each patch in the same way that pcolor told us the colour of each patch). Command: Number:

Once you have finished this, move onto the next part to learn about some more commands.


[School of Geography homepage] [Leeds University homepage]