Computational Modeling for
Social Sciences
Agent-based Modeling using NetLogo
Version 6.1.1
Ms. Nahed Taha
Department of Socio-Computing
Fall 2019
NetLogo Environment
How NetLogo Environment Looks Like?
2
NetLogo Environment
The window You see is called the NetLogo
Integrated Development Environment
(IDE).
IDE has the following parts:
Interface: Control, run, and visualize model
(Current tab).
Info: Description of model purpose, function,
and features (Middle tab).
Code: Model syntax (Last tab).
View: Monitor, i.e. the ‘World’, used for real-time
model visualization.
3
NetLogo Environment: The ‘Interface’ Tab
Interface Elements
Button Monitor
Slider Plot
Switch Output
Chooser Note
Input
4
Icon & Name Description
A button is either once or forever. When you click on a once button, it executes its
commands once. The forever button executes the commands over and over, until you
click on the button again to stop the action.
Enable the user to select a particular value from a range of numerical values. In the
code tab, it is accessed as global variables.
Enable the user to turn various elements of a model off or on. In the code tab, they are
also accessed as global variables, but they are Boolean variables.
Enable a model user to select a choice from a predefined drop-down menu that the
modeler has created. In the code tab, it is accessed as global variables, but they are
variables that have strings as their values.
are more free-form, allowing the user to input text that the model can use.
Display the value of a global variable or calculation updated several times a second.
They have no history but show the user the current state of the system.
Provide traditional 2D graphs enabling the user to observe the change of an output
variable over time
Enable the modeler to create free-form text-based output to send to the user.
Enable the modeler to place text information on the Interface tab (for example, to give a
model user directions on how to use the model) 5
NetLogo Environment: The ‘Interface’ Tab
Speed Slider: Allows the user to control the speed of a
model, that is, the speed at which turtles move, patches
change color, and so on.
To the left: the model slows down (i.e.
there are longer pauses between each
tick (time step)).
To the right: the model speeds up.
View Updates Checkbox: Controls whether view updates happen at all.
Update Mode Menu: Allows the user to switch between continuous and tick-
based updates.
6
NetLogo Environment: The ‘Interface’ Tab
Command Center
Command Center: Allows the users to
enter commands or directions to a model
and to interactively test out commands
and manipulate agents and the
environment.
Commands are instructions to NetLogo’s
agents: turtles, patches, links, and the
observer.
7
NetLogo Environment: The ‘Interface’ Tab
Command Center Quick Example
1
Into the Command Center input (at the
observer > prompt), type the following: Why do we get that answer?
1 count patches
2 ask n-of 20 patches [set pcolor green]
3 count patches with [pcolor = green]
3
2
8
NetLogo Environment: The ‘Interface’ Tab
Command Center Quick Example
Then, type the following:
4 create-turtles 10 [set size 3]
5 ask turtles [rt random 360 fd 10] 4
What are rt and fd?
5
9
NetLogo Environment: The ‘Interface’ Tab
Command Center Quick Example
ask turtles [rt random 360 fd 10]
This command asks each turtle picks a random This command asks the turtle move forward ten
whole number between 0 and 359 (random steps.
doesn’t include the number you give it as a
possible result). Each turtle turns right (or rt for
short) this number of degrees.
10
NetLogo Environment: The ‘Interface’ Tab
Inspect
Turtle, Patch and Link Monitors are easily
available through the View.
Just right-click on the turtle or patch you want to
inspect, and choose “inspect turtle” or “inspect
patch” from the popup menu.
You can watch, follow or ride a turtle by
selecting the appropriate item in the turtle
sub-menu.
Turtle, patch and link monitors can also be
opened from the Tools menu or by using the
inspect command).
11
NetLogo Environment: The ‘Info’ Tab
You will find useful ‘meta’
information about the model in Section Description
this tab. What is it? Model purpose and topic.
There may be additional sections, An overview of the functioning of the
such as: NetLogo Features, How it works model, core behaviors, and model
Related Models, Credits, and features.
How to Cite. Describes the buttons, sliders, and
How to use it
parameters.
What you see in plots and display when
Things to notice running model, as well as questions the
model can answer.
Things to try Experiments you can try.
Future enhancements or interesting
Extend the model
modifications.
12
NetLogo Environment: The ‘Code’ Tab
Extensions
Global Variables
Attributes of Agents:
Mobile Agents (Turtles)
Stationary Agents (Patches)
Button Procedures
Other Procedures
13
Diving In: A Quick Hands-On Tour
Let’s dive right in and get started
14
Aim of this Session
In this session, we’ll begin by creating our first
virtual agent-based world.
It is just a pod of green sea turtles swimming in a
simulated ocean.
15
Create A New Model
Select File ->New (or
Press Ctrl+N)
The landscape should be
empty, as we have no
agents and no landscape
to speak of at this time.
16
Our First Virtual Agent-based World
Let’s start by making an ocean
for our turtles.
Press the tab key twice to
bring up the patches> prompt
at the bottom of the screen,
then type in the following
command and press Enter:
set pcolor blue
Note: Don’t worry too much if the commands seem
cryptic: we’ll explain them in more detail later on.
17
Our First Virtual Agent-based World
This command set pcolor blue
ask all the patches in the
model, which you can think of
as grid cells on a map, to set
their patch color (abbreviated
as pcolor) to blue.
This will make the entire
landscape (OK, actually a
"seascape") a nice shade of
light blue.
18
Our First Virtual Agent-based World
Use a random number generator
to ask patches to adjust their
color slightly above and below
the standard blue shade.
if ((random-float 1.0) < 0.25) [ set pcolor blue - 3 ]
if ((random-float 1.0) < 0.10) [ set pcolor blue + 2 ]
Pay attention to the use of parentheses and brackets
– NetLogo language syntax is sensitive to such
things.
19
Our First Virtual Agent-based World
1
if ((random-float 1.0) < 0.25) [ set pcolor blue - 3 ]
if ((random-float 1.0) < 0.10) [ set pcolor blue + 2 ] 2
The first command asks NetLogo to ask all the patches in our simulation to
generate a random number between 0.0 and 1.0, and if the generated
number is less than 0.25, change the patch color to a darker shade of blue.
This results in about ¼ of our patches being shaded darker blue.
The second command asks for some of our patches to be colored a slightly
lighter shade of blue.
This results in about 1/10 of our patches being toned a little brighter.
20
Our First Virtual Agent-based World
We’ll smooth things out using the
built-in diffuse command, a function
designed to work like mathematical
sandpaper to even out a bumpy
surface.
However, diffuse does not work in the patches
context, so we’ll need to press tab twice to get
back to the observer context, then type in the
following command:
diffuse pcolor 0.50
21
Our First Virtual Agent-based World
The command diffuse pcolor 0.50
asks each patch to average out
50% of its pcolor value among its
eight nearest neighbors.
Repeat the previous command using
the mentioned steps.
2
1
3 Press Enter
22
Our First Virtual Agent-based World
Let’s add some turtles.
At the Observer prompt, enter
the following command to
create 30 turtles.
create-turtles 30
23
Our First Virtual Agent-based World
NetLogo has created a pod of 30
turtles.
They are all located on top of one another
in a blob in the middle of the ocean.
The default location for a new turtle when
it is created is at the origin, which is
currently defined to be right in the middle
of the landscape.
The turtles do not look much like
turtles, they look like little triangles.
The default turtle shape in NetLogo is a
triangle.
24
Our First Virtual Agent-based World
At the observer prompt, hit the
tab key to move over to the
turtles context.
Enter the following commands:
set shape “turtle”
set size 1.5
set color green
25
Our First Virtual Agent-based World
This command changes the shape for
our turtles from the default triangle to
set shape “turtle” a more turtlelike shape, conveniently
named "turtle".
set size 1.5
set color green
This command makes our turtles
one and a half times the normal This command makes our turtles turn
size so we can see them better. green; since they are supposed to be
green sea turtles, this seems
appropriate.
26
Turtles Shapes Editor
1
NetLogo has a wide variety of
built-in shapes for agents (such
as birds, wolves, rabbits,
people, cars, and airplanes,
just to name a few).
You can create your own 2
shapes using your artistic
talents and the handy Shape
Editor under the Tools menu.
27
Our First Virtual Agent-based World
To spread out the turtles, Enter the
following commands (still at the
turtles):
set heading (random 360)
forward 10
You should now have a ring of
turtles, all facing away from the
center of the seascape.
28
Our First Virtual Agent-based World
This command instructs the
turtles to move forward 10
units.
set heading (random 360)
forward 10
This command points each of the turtles in
some random direction between 0 and 360
degrees (which covers the entire span of
the compass, so it’s really quite random).
Quick note: NetLogo is quite agnostic about scale, so if you care about things like miles versus
kilometers or furlongs or fathoms, You’ll need to figure out an appropriate spatial resolution for your
model and be mindful of this as you develop agent behaviors.
29
Our First Virtual Agent-based World
Now let’s spread them around a
bit. Type in these commands:
set heading (random 360)
forward (random 10)
The turtles should now be
spread around the seascape
fairly randomly.
30
Our First Virtual Agent-based World
Now let’s spread them around a
bit. Type in these commands:
set heading (random 360)
forward (random 10)
The turtles should now be
spread around the seascape
fairly randomly.
31
Our First Virtual Agent-based World
Now let’s move them around
some more.
Type in the following command:
fd (random 10)
Note: if the command “fd” seems cryptic, it’s just the
abbreviated form for the NetLogo command "forward".
Many NetLogo commands have shortened forms, and
it’s not at all uncommon to find abbreviations in the
model code.)
32
Our First Virtual Agent-based World
To close out this demonstration,
let’s point all the turtles in a
common direction and have them
swim in formation.
set heading 45
fd 1
fd 1
fd 1
Repeat this until you or your turtles
get tired of swimming.
33
Thank You ☺
34