Table of Contents
Defining Different Types of Agent |
Agent-specific variables |
Creating the visitors |
Making the visitors move |
Through the previous practical classes you now have all the basic knowledge you'll need to create quite advanced models in NetLogo. For the remaining three practical sessions we will work on a model of a festival. This is also the model that you can use for your projects. Here's the scenario:
You have been approached by the organisers of a music festival who are concerned with the amount of crime that takes place there. They have asked you to create a model of the festival and suggest ways in which they could intervene to prevent people from being mugged. The festival site consists of two stages (a main stage and secondary stage) as well as a mixture of burger stands, beer tents and vegetarian food stalls.
You will create a model that is able to do the following:
To save time, you have been provided with a skeleton model to work with. The festival environment has been created, but otherwise the model doesn't do anything.
Important: to force the browser to download the file you might need to right-click on the link and choose Save link as ...
There is a command at the start that you might not recognise:
breed [visitors visitor]
Up to now, we have always just referred to turtles
when we want to
work with the agents in a model. This is fine when you only have one type of agent,
but later we will have two types ('visitors' and 'muggers') so we
need a way to differentiate between them.
NetLogo includes the breed
command to make this possible. In the code above,
we tell NetLogo that we will create a specific type of agent referred to as
'visitors' (plural) and 'visitor' (when there is only one of them).
After writing the breed
command it is possible to access just
the visitor agents with:
ask visitors [ .. ]
Having created visitor
agents, we can now create variables that are
unique to visitors. We can use agent-specific variables to store pieces of information
that are unique to a particular agent type. In this model, our visitors will walk
around the festival area, so they need a variable that will keep track of where they
are going.
breed
line:
visitors-own[
destination
]
This creates a new variable called destination
that our visitors can use
to keep track of their current destination (a burger stand, the main stage, a veggie
food stall, etc.).
Next, we need to make some minor improvements to the model to create the visitor agents.
number-of-visitors
'. You can also add as many visitors
as you like; I set the initial value to be 100 with a maximum of 500.
(If you have forgotten how to make a slider, refer back to the
sliders section of Practical 2).setup-people
' that will create the
visitors. We have created code to do a similar job already, for example see the
setup-turtles
procedure in
practical 2. You can use the code below to help you create the procedure -
remember that anything after a ';
' is a comment and ignored by
NetLogo. You will have to write the real code in place of the comments.
to setup-people
create-visitors number-of-visitors [
;set the shape of the new visitor to "person"
;set the colour of the visitor to gray
;give the person a random (x,y) coordinate
;This last bit sets the visitor's 'destination' variable
set destination one-of patches with [
pcolor = yellow or pcolor = green or pcolor = blue or pcolor = orange or pcolor = red
]
]
end
The last thing that the code above does is set each visitor's
destination
variable. This is the patch that the person is going to walk
to as they move about the festival. We choose any patch that is either
a beer stand (yellow), veggie food stand
(green), burger van (blue),
small stage (orange) or big stage (red).
setup-people
to the 'setup
' procedure.
(Remember that NetLogo will run the setup
procedure when someone presses
the 'Setup Model' button).The last thing to do to finish the basic model is to add a few lines of code so that the agents actually move around from stall to stall. The diagram on the right illustrates the rules that control the behaviour of the visitors (i.e. their 'thought process').
At each iteration, the visitors check to see if they have reached their destination
by comparing the patch that they are currently standing on to the one they are trying
to get to (if ( patch-here = destination )
). If they have reached their
destination patch, then they choose a new (random) destination to travel to. Then
they move forward towards their destination.
move
' procedure (which is empty at the
moment).
if ( patch-here = destination ) [
set destination one-of patches with [
pcolor = yellow or pcolor = green or pcolor = blue or pcolor = orange or pcolor = red
]
]
face destination
forward 1
That's all for the this practical, you now have a simple model with some virtual people wandering around a music festival. This probably wont have taken very long, so complete the activities below. Then, if you have the energy, have a look at Practical 5.
turtles-on
command.