Table of Contents
1. Running an example model |
2. The Basics |
3. Using ask |
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.
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
).
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.
setup
command in the Command Centre).pcolor
variable. This should cause the patches to change colour:
set pcolor blue
What happens?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.
set pcolor blue
What happens this time?color
, not
pcolor
).
set color brown
What happens this time?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.
pcolor
):
set pcolor black
What happens this time?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.
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.
setup
in the Command Centre.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.ask
to send it to the patches, rather
than the observer:
ask patches [ set pcolor blue ]
Did the patches turn blue? There are a few things happening here:
ask
command expects two inputs, both on the
right hand side of the command.patches
.set pcolor blue
tells every patch to set their pcolor
variable to the value blue
.
ask turtles [ set color brown ]
What happens?
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!
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.
setup
command.ask turtles with [ sick? = true ] [ set color brown ]
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:
ask turtles with [ sick? = false ] [ set color blue ]
ask
to run commands on people of different ages:
ask turtles with [ age > 20 ] [ set color yellow ]
Finally, we will use a combination of ask
and some other commands to
do something more interesting than changing colours.
ask turtles [ forward 1 ]
What is happening? What happens if a negative number is sent to the forward
command?
ask turtles [ forward -1 ]
ask turtles [ facexy 0 0 ]
ask turtles [ forward 1 ]
ask turtles [ forward 1 ]
ask turtles [ forward 1 ]
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.
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.
ask
and
with
).
Command:
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.