GEOG3150 - GIS, Geocomputation and Geoplanning - Semster 2


Table of Contents

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

Practical 1, Part 3 - Using ask


In this part, we will start to use an extremely useful command: ask. There is more information about the command in the official NetLogo documentation (http://ccl.northwestern.edu/netlogo/docs/programming.html#ask). But first, we'll look at the different contexts in a bit more detail.

    Contexts: Observer, Turtle, Patch (and Link)

    Contexts and variables

    Recall from the lecture that there are three different contexts (actually there are four, but we won't look at the 'link' context in this course). Also, remember that turtles, patches and the observer each have different variables that store pieces of information about them. The figure below gives examples of some of the different variables that patches, turtles and the observer have access to (this example is from the Wolf Sheep Predation model, but the ideas are the same for all models). Note that patches and turtles both have variables that store their (x,y) coordinates and colour (xcor, ycor, color). To stop us from getting confused, the equivalent patch variables have a 'p' in front of them (pxcor, pycor, pcolor).

    Contexts and variables

    There is also more information in the NetLogo User Manual.

    So far, all the commands that have been issued have been sent to the observer context. This is the 'God' context that oversees the model. In order to give commands to turtles or patches, and to change the values of their variables, commands need to be sent to the turtle and patch contexts respectively. One way to do this, is to tell the command centre specifically to send commands to the turtles or patches, rather than the observer. We will do this now.

  1. Re-open the 'Virus' model from the Models Library (under 'Biology').
  2. Initialise the model (either by using the 'setup' button or by issuing the setup command in the Command Centre).
  3. Try entering the following command into the Command Centre to change the value of the patches' pcolor variable. This should cause the patches to change colour: set pcolor blueWhat happens?
  4. Using the command centre to change context

    If everything worked 'correctly' (or at least as it is supposed to) you should have seen the following error in the Command Centre: ERROR: You can't use PCOLOR in an observer context, because PCOLOR is turtle/patch-only. The message is saying that you cannot change the value of the pcolor variable, because that variable belongs to turtles and patches only. The observer has no variable called pcolor.

    To get round this problem, we can tell the Command Centre to send the command to the patches instead of the observer.

  5. Click on 'observer' text in the bottom-left corner of the NetLogo screen (see the right image for example) and select 'patches'. This tells the Command Centre to send commands to the patches context, rather than the observer.
  6. Now try the command again: set pcolor blueWhat happens this time?
  7. Now change the context again, this time to 'turtles'. Issue the following command (note that we're setting a variable called color, not pcolor). set color brownWhat happens this time?
  8. You should see all the turtles turn brown. This is because we have just changed the value of their color variable which, like pcolor for patches, controls their colour.

  9. Finally, issue the following command to the turtles (note that this time we do use pcolor): set pcolor blackWhat happens this time?
  10. Did some of the patches turn black? Which ones?

    What's happening here is really, really useful in models. Turtles are given direct access to the patch that they are currently standing on. Don't worry if you don't understand this straight away, as you start to use NetLogo in earnest you will see how useful this is.

    Using ask

    Although changing contexts using the command centre is OK, it would be better if there was a way to send commands to turtles or patches directly, without re-configuring the commands centre. We can do this using the ask command.

  11. Click on the bottom-left corner of the NetLogo Command Centre and tell it to send commands to the 'observer' again (it should be sending commands to the turtles at the moment). This is the normal behaviour.
  12. Re-initialise the model, either by using the 'setup' button or by issuing setup in the Command Centre.
  13. As you did before, try changing the colours of the patches with this command: set pcolor blue Again, you should get an error telling you that the 'observer' context has no variable called pcolor. This is to be expected, because the command is being sent to the observer, not the patches.
  14. Now, issue the same command, but use ask to send it to the patches, rather than the observer: ask patches [ set pcolor blue ]
  15. Virus model with blue patches

    Did the patches turn blue? There are a few things happening here:

  16. Now try this command: ask turtles [ set color brown ] What happens?
  17. Now try two commands to set the x and y coordinates of the turtles: ask turtles [ set xcor 1 set ycor 5] Where have all the turtles moved to? Try this as well ask turtles [ set xcor -10 set ycor -5] The people will move again. If you're not sure why this is happening, ask someone!
  18. Using ask and with

    The previous examples of ask have been applied to all the turtles or patches in the model. However, most of the time it is more useful to execute a command on a smaller group. To do this, we can use the with command.

  19. First reset the model, either by clicking on 'setup' or by issuing the setup command.
  20. Execute the following command, that will have an affect only on the people that have been infected by a virus ask turtles with [ sick? = true ] [ set color brown ]
  21. Here, instead of giving all the turtles to the ask command, we give it the group of turtles who have a value of true stored in their variable called sick (this is a special variable created specifically for the 'virus' model).

    Visually, these two commands are constructed like so:

    Visual illustration of the 'ask' command
  22. Now try it again, but change the colour of those who aren't ill ask turtles with [ sick? = false ] [ set color blue ]
  23. Each person also has a variable that remembers their age (you wouldn't know this without looking at the code - we'll get onto this next). So we can use ask to run commands on people of different ages: ask turtles with [ age > 20 ] [ set color yellow ]
  24. Finally, we will use a combination of ask and some other commands to do something more interesting than changing colours.

    Virus model with 10 agents
  25. Move the 'people' slider from 150 down to 10. This will reduce the number of people in the model and make it easier to see the impact that the following commands will have.
  26. Setup the model. You should now see only a few people in the world.
  27. Issue the following command: ask turtles [ forward 1 ] What is happening? What happens if a negative number is sent to the forward command? ask turtles [ forward -1 ]
  28. Now try these commands and see what happens (they have to be entered into the Command Centre one by one): ask turtles [ facexy 0 0 ]
    ask turtles [ forward 1 ]
    ask turtles [ forward 1 ]
    ask turtles [ forward 1 ]
  29. You can probably see that the first command (ask turtles [ facexy 0 0 ]) tells each turtle to spin round and face the coordinate (0,0) (which happens to be in the middle of the world in this model). The commands that follow (ask turtles [ forward 1 ]) tell the agents to move forward one step in the direction that they are facing. This might seem trivial, but you now have covered the main commands that you need to create an agent-based model!

    Remember, information about all the different commands that are available can be found in the NetLogo documentation. In particular, the NetLogo Dictionary lists every command that is available.

Activities (Practical 1, Part 3)

Setting commands to go to the observer context

For this activity, continue to use the 'Virus' model. Make sure that the Command Centre is going to send commands to the observer context (you should see 'observer' just next to the commands box, as in the right image). Also, press 'setup' to initialise the model.

Note the commands that you would use to do the following things. Try running them in the Command Centre to check that you are correct.
  1. Write a command to change the colour of all of the patches to brown. Command:
  2. Write a command that will ask all agents to move forward one step. Command:
  3. Write a command that will ask all blue turtles to move forward five steps. (Hint, use a combination of ask and with). Command:
  4. Write a command that will ask the turtles to change the colour of the patch that they are standing on to blue. Command:
  5. Write a command that will ask all of the red turtles to change the colour of the patch that they are standing on to orange. Hint: remember that turtle colour is stored in a variable called color and patch colour is stored in a variable called pcolor. Command:

That is quite a lot to take in, so don't worry if it seems a bit overwhelming. As with anything new, as you practice it will become familiar. That's all for this practical, if you're really keen you can move straight on to Practical 2, or go and have a well deserved cup of tea.


[School of Geography homepage] [Leeds University homepage]