ngineering
Building an Agent-Based Model in Netlogo
ABMS
www.drandrewjcollins.com
Outline
Here is what is covered
Netlogo environment and types of agents
Understanding contexts
Issuing agent commands
Getting information about your agents
Experimentation with Netlogo
www.drandrewjcollins.com
Different Kinds of Agents
Patches
Landscape (represents one square of a 2-D wrap-around grid)
Do not move
Have characteristics (e.g., pcolor)
Identified by their integer coordinates (pxcor, pycor)
www.drandrewjcollins.com
Check go add-ins now for later.
Different Kinds of Agents
Turtles
Move on top of patches
Have decimal coordinates (xcor, ycor), orientation (heading), and identification number (who)
No turtles when Netlogo sets up
Observer
Overlooks model
Creates world, populates it with turtles and patches, issues command.
www.drandrewjcollins.com
Netlogo environment
www.drandrewjcollins.com
Netlogo at startup
Patches are here but
not visible
www.drandrewjcollins.com
Default world is 33x33 grid
Default world shape is a torus
Patches are cells on grid (identified
by coordinates)
Can change location of “origin” (0,0)
patch.
www.drandrewjcollins.com
Set up the world
with max-pxcor and
max-pycor =1 and
patch size 100,
then execute the
command shown in
the Command Center.
Click on Edit to
get setup window.
www.drandrewjcollins.com
Issuing the
inspect
command
brings up
a data window
for the patch.
www.drandrewjcollins.com
Clicking here will
change who executes
the command.
The Netlogo manual
covers command
format and short cuts.
www.drandrewjcollins.com
ca is clear all
crt 1 is create one
turtle
www.drandrewjcollins.com
Contexts
All commands are context-specific
Observer (clear-all, create-turtles)
Turtles (jump, setxy)
Patches (neighbors, sprout)
We can change who is issuing the command via the command center
To issue a command from a turtle or patch context, use the “ask” command
www.drandrewjcollins.com
Features of Agent Commands
Built-in (primitives) vs. User-written (procedures)
See Netlogo “dictionary” for full list of primitives
Procedures typically written to apply to one type of agent (e.g., turtles)
Standard format
www.drandrewjcollins.com
Features of Agent Commands
Actions vs. output
Commands may be some action for agent to carry out (create, jump, hatch)
Commands may be some reporter to compute a value & return it (average-happiness, absolute-value, proportion-isolated)
Inputs (arguments) or not
Inputs are values that can be used to carry out actions
www.drandrewjcollins.com
Click on the “Procedures” tab and enter the following…
Now go back to the interface & type “setup” in the command window…
www.drandrewjcollins.com
We can also execute the “setup” command via a button
Use the add symbol to
create a button on the interface.
www.drandrewjcollins.com
Using the Ask Command
Issues commands for specified patches or turtles
All turtles or patches
Subset of turtles or patches with some characteristic (keyword: with)
Random sample of turtles or patches (e.g., n-of)
www.drandrewjcollins.com
Using the Ask Command
www.drandrewjcollins.com
Agentsets
Unordered set of agents
All agents must be of the same type (e.g., turtles)
Always presented in random order
Used to construct subsets of agents (e.g., all turtles with color=red)
Netlogo has built-in agentsets (turtles-here, turtles-at, turtles-on …)
www.drandrewjcollins.com
Once you have your agentset…
Here are a few things you can do:
Use ask? to make the agents in the agentset do something
Use any? to see if the agentset has at least one agent that fits criteria
Use all? to see if all agents in agentset fit some criteria
Use count to find out how many agents are in agentset
www.drandrewjcollins.com
www.drandrewjcollins.com
Breeds
Breed – a “natural” kind of agentset (other species than turtles)
example
A new breed comes with automatically derived primitives:
breed [wolves wolf]
create-<breed>
create-custom-<breed>
<breed>-here
<breed>-at
…
www.drandrewjcollins.com
Breeds
The breed is a turtle variable:
A turtle agent can change breed:
ask turtle 5 [ if breed = sheep … ]
Ask turtle 5 [ set breed sheep ]
www.drandrewjcollins.com
Lists
Ordered lists of things
All items do not need to be of same type
Lists cannot be modified, but they can be replaced
Example:
let a-list [ 0 “a string” true]; a list of a number,
a string, and a boolean
www.drandrewjcollins.com
Timing and Scheduling
Time
Measured in “ticks”
Usually 1 tick = 1 run of “go” command
Reset ticks using tick-reset command
Current tick retrieved via ticks reporter
Tick command advances the tick counter
Scheduling
Usually 1 “step” of model contained in “go” procedure
www.drandrewjcollins.com
Basic structure of a netlogo program
www.drandrewjcollins.com
Global variables
usually come first
Setup method
comes next
Then Main
method
Followed by
Subroutines
www.drandrewjcollins.com
Getting information about your agents
www.drandrewjcollins.com
Reporters and Views
Reporters
Report a result value (functions with return type)
Example with 1 input argument:
to-report abolute-value [number]
ifelse number >= 0
[report number]
[report 0 – number]
end
www.drandrewjcollins.com
Reporters and Views
Types of Views
Monitors
Plots
Output text areas
Monitors – display the current value of variables
www.drandrewjcollins.com
Reporters and Views
Plots – display the history of a variable’s value
Output text areas – log text info
www.drandrewjcollins.com
Experimentation with netlogo
www.drandrewjcollins.com
Experimentation
BehaviorSpace
Software tool integrated with Netlogo that facilitates experimentation
Sets up Netlogo to conduct multiple runs with different input variable values. Facilitates Monte Carlo simulation for data collection and analysis.
www.drandrewjcollins.com
Setup different runs
Define run parameters
www.drandrewjcollins.com
End Presentation
www.drandrewjcollins.com