agent-based modelling and simulations · prof. agostino poggi netlogo university of parma - 2...
TRANSCRIPT
University of Parma - 2NetLogoProf. Agostino Poggi
Introduction
♦ is a programmable modeling environment for simulating complex systems
♦Modelers can give instructions to hundreds or thousands of independent agents operating in parallel
♦Was created by Uri Wilensky in 1999
♦Current release is 5.2
University of Parma - 3NetLogoProf. Agostino Poggi
System Features
♦Cross-platform
Runs on different operating systems (e.g., Windows, Linux, MacOS, …)
♦Web-enabled
Runs within a web browser, but can be download and run locally
♦Models can be saved as applets to be embedded in web pages
University of Parma - 4NetLogoProf. Agostino Poggi
Language Features
♦ Is fully programmable
♦Has a simple language structure
♦ Is a Logo dialect extended to support agents and parallelism
♦Supports a unlimited numbers of agents and variables
♦Supports double precision arithmetic
♦Provides many built-in primitives
University of Parma - 5NetLogoProf. Agostino Poggi
Agents
♦The world is two dimensional and is divided up into a grid of patches
♦Patches are agents that represent a square piece of the world over which turtles can move
♦Turtles are agents that move around in the world
♦ Links are agents connecting pairs of turtles
♦The observer is an agent that can create turtles and have read/write access to all the agents and variables
University of Parma - 6NetLogoProf. Agostino Poggi
Patches (1/2)
♦Have a color defined by the pcolor variable
♦Have a location defined by the pxcor and pycorconstant variables
♦The patch in the center of the world has coordinates (0, 0)
♦The total number of patches is determined by the max-pxcor - min-pxcor and max-pycor - min-pycorpairs of variables
♦Usually every patch has the same number of neighbor patches (toroidal world)
University of Parma - 8NetLogoProf. Agostino Poggi
Turtles
♦Have an index defined by the who variable
♦Have a color defined by the color variable
♦Have a location defined by the xcor and ycorvariables Its value can be a decimal number: it can be positioned
at any point within a patch, default position is the center of the patch
♦Have a direction defined by the heading variable Its value is a number greater than or equal to 0 and less
than 360 (0 is north, 90 is east, …)
♦Can be divided in breeds
♦The total number of turtles is determined by the count turtles command
University of Parma - 9NetLogoProf. Agostino Poggi
Links
♦Represent “edges” on networks
♦Can “move”, “die”, “grow”, …
♦Have a start node and an end node represented by two turtles
♦Can be directed or undirected
♦Can be divided in breeds
♦Can have different colors, thickness, …
University of Parma - 10NetLogoProf. Agostino Poggi
Observer
♦Can create new agents and controls
♦Gives commands to patches, turtles and links
♦Can change the values of variables
♦Can produce information about the state of the simulations
♦ Is the interpreter between the user and the agents
University of Parma - 11NetLogoProf. Agostino Poggi
Breed
♦Allows to defines a subtype of turtles and links
breed [wolves wolf]
breed [sheep a-sheep]
directed-link-breed [unis uni]
undirected-link-breed [bis bi]
♦Each turtle has a breed variable
if breed = wolves [ ... ]
♦Each turtle can change breed
set breed sheep
♦Each breed can a specific default shape
set-default-shape wolves “wolf"
University of Parma - 12NetLogoProf. Agostino Poggi
Agent Set
♦ Is an homogeneous set of agents
♦Some agent sets are defined by NetLogo environment
turtles, patches and links
♦Subset of turtles, patches and links are defined by the use of some specific commands
♦ Agent sets are assigned through the commands
turtle-set, patch-set and link-set
University of Parma - 13NetLogoProf. Agostino Poggi
Data Types
♦Number values are used for defining calculations and are all floating point numbers
♦ Boolean values are used for defining conditions and correspond to the true and false constants that can be combined with the and, or, not and xor operators
♦ String values are used for defining names and represent immutable sequence of characters that are create with double quotes and concatenate with the word command
University of Parma - 14NetLogoProf. Agostino Poggi
List
♦Allows to store multiple pieces of information in a single value
♦Each piece of information can be values of anypossible type (i.e., a boolean, a number, a string, an agent, an agent set or even another list)
♦Pieces of information are enclosed between squarebrackets
♦Each piece of information is identified by an integer starting at 0
University of Parma - 15NetLogoProf. Agostino Poggi
Variables
♦Each turtle, patch and link has its set of variables
♦Some variables are defined by NetLogo environment
♦The turtles-own, patches-own and links-own keywords allow to define new variables
turtles-own [ energy speed ]
♦Variables value can be modified using the set command
set color red
set pcolor red
♦A turtle can also read and set variables of its patch
University of Parma - 16NetLogoProf. Agostino Poggi
Global & Local Variables
♦ If a variable is a global variable, there is only one value for the variable and all the agents can access it
♦A variable is made global by using the globalskeyword at the beginning of the program code
globals [ clock ]
♦Moreover, it is made global by adding at the interface level either a switch or a slider
♦Global variables can by read and set at any time by any agent
♦ Local variable are defined inside procedures
let limit 10
University of Parma - 17NetLogoProf. Agostino Poggi
Tick Counter
♦Usually simulations are discrete and time passes in discrete steps defined by a built-in tick counter
♦The tick command advances the tick counter by 1 and usually also update the view
♦The clear-all command clears the tick counter along with everything else
♦The reset-ticks command resets only the tick counter
♦The current value of the tick counter is provided by the ticks report and is shown in the NetLogo GUI above the view
University of Parma - 18NetLogoProf. Agostino Poggi
Turtles Creation
create-turtles 10
♦Asks the Observer to create 10 turtles
create-wolves 10
♦Asks the Observer to create 10 wolves
sprout 10
♦Asks the current patch to create 10 turtles
hatch 10
♦Asks the current turtle to create 10 turtles
University of Parma - 19NetLogoProf. Agostino Poggi
Links Creation
create-link-with turtle 2
♦ Creates an undirected link between the caller and turtle 2
create-link-to turtle 2
♦ Creates a directed link from the caller to turtle 2
create-link-from turtle 2
♦ Creates a directed link from turtle 2 to the caller
create-bi-with turtle 2
♦ Creates an undirected link (bis breed) between the caller and turtle 2
create-uni-to turtle 2
♦ Creates a directed link (unis breed) from the caller to turtle 2
University of Parma - 20NetLogoProf. Agostino Poggi
Agent Set Creation (1/2)
other turtles
♦All the turtles excluding the current
turtles-here
♦The turtles on the current patch
other turtles here
♦The turtles on the current patch excluding the current
patches at-points [[1 0] [0 1] [1 0] [0 1]] (neighbors4)
♦The patches around the current patch
turtles-on neighbors
♦The turtles standing on the eight surrounding patches
University of Parma - 21NetLogoProf. Agostino Poggi
Agent Set Creation (2/2)
turtles in-radius 3
♦The turtles less than 3 patches away
patches with [pcolor = red]
♦All the red patched
other turtles here with [color = red]
♦The other red turtles on the current patch
turtles with [(xcor>0) and (ycor>0)and(pcolor=green)]
♦The green turtles with coordinates greater than 0
[mylinks] of turtle 0
♦The links connected to turtle 0
University of Parma - 22NetLogoProf. Agostino Poggi
Agent Set Uses (1/3)
ask turtles [ die ]
♦Makes all the agents in the set do something
ask one-of links [ die ]
♦Picks a random agent from the set
ask min-one-of turtles [energy] [ die ]
♦Picks the agent in the set with min/max of some attribute
show count turtles
♦Finds out exactly how many agents there are in the set
University of Parma - 23NetLogoProf. Agostino Poggi
Agent Set Uses (2/3)
show mean [energy] of turtles with [color = red]
♦Computes (mean, median, min, max, sum, standard-deviation, variance) over some attribute of the agents in the set
if any? turtles [ show count turtles ]
♦Sees if the set of agents is empty
if member? self turtles [show "Yes, it's member !"]
♦Sees whether the current agent is in a set
if all? turtles [color = red] [show "everyone is red!"]
♦Sees if all the agents in a set satisfy a condition
University of Parma - 24NetLogoProf. Agostino Poggi
Agent Set Uses (3/3)
histogram [color] of turtles
♦Makes a distribution histogram of a variable in a set
ask (turtle-set self turtle 2 turtles with [color = red])
[ die ]
♦Creates new sets from other sets and individual agents
if (set1 = set2) [show "Identical sets !"]
♦Compares (=, !=, <, >) two sets
University of Parma - 25NetLogoProf. Agostino Poggi
Working with Lists (1/4)
set mylist [2 7 5 Bob [3 0 -2]]
♦Creates a constant list
set random-list list (random 10) (random 20)
♦Creates a list of two random integers
set mylist replace-item 2 mylist 10
♦Replaces the third item of mylist with 10
set mylist lput 42 mylist
♦Adds 42 as last item of mylist
University of Parma - 26NetLogoProf. Agostino Poggi
Working with Lists (2/4)
set mylist fput 12 mylist
♦Adds 12 as first item of mylist
set mylist but-last mylist
♦Removes the last item from mylist
set mylist but-first mylist
♦Removes the first item from mylist
foreach mylist show
♦Shows the items of mylist
University of Parma - 27NetLogoProf. Agostino Poggi
Working with Lists (3/4)
map round [1.2 2.2 2.7]
♦ Builds the list [1 2 3]
map [? * ?] [1 2 3]
♦ Builds the list [1 4 9] (? Means the current item)
member? 2 [1 2 3]
♦ Checks if 2 is member of the list [1 2 3]
position 2 [1 2 3]
♦Gets the position of 2 in the list [1 2 3]
University of Parma - 28NetLogoProf. Agostino Poggi
Working with Lists (4/4)
sort turtles
♦Builds a list of turtles sorted in ascending order by whovariable
sort-by [[size] of ?1 < [size] of ?2] turtles
♦Builds a list of turtles sorted in ascending order by sizevariable
list [1 2 3] [4 5 6]
♦Builds the list [[1 2 3] [4 5 6]]
sentence [1 2 3] [4 5 6]
♦Builds the list [1 2 3 4 5 6]
University of Parma - 29NetLogoProf. Agostino Poggi
Commands & Reporters (1/3)
♦Commands are actions for the agents to carry out
♦Reporters carry out some operation and report a result either to a command or another reporter
♦Commands and reporters defined by NetLogoenvironment are called primitives
♦User’s defined commands and reporters are called procedures
University of Parma - 30NetLogoProf. Agostino Poggi
Commands & Reporters (2/3)
♦Procedures can take inputs value
♦Each procedure is composed of
The keyword to or to-report
The name of the procedure
The optional arguments
A sequence of commands
The keyword end
♦After its definition, a procedure can be used elsewhere in the program
University of Parma - 34NetLogoProf. Agostino Poggi
File I/O (1/3)
file-open myfile.txt
♦Opens a file
file-close
♦Closes a file
file-close-all
♦Closes all the files
user-file
♦Opens a window for opening a file
user-new-file
♦Opens a window for creating a file
University of Parma - 36NetLogoProf. Agostino Poggi
File I/O (3/3)
file-read
♦Reads the next token in the file and interprets it in the appropriate type
file-read-line
♦Reads the next line in the file as a string
file-read-characters 4
♦Reads the next 4 characters (including newlines and spaces) in the file as a string
file-at-end?
♦Checks if there are no more characters left to read in the current file
University of Parma - 37NetLogoProf. Agostino Poggi
Graphical User Interface
toolbar
Allows to give “on the fly” commands to agents
University of Parma - 38NetLogoProf. Agostino Poggi
Toolbar Elements (1/2)
Button Allow the execution of a procedure
Slider Is a global variable which is accessible by all agents
Switch Is a visual representation for a true/false global variable
Chooser Allows to choose a value for a global variable from a list
of choices
Input Is a global variable that contains a string or a number
Monitor Displays display the current value of a variable
Plot Displays the history of the value of a variable
Output Is a scrolling area of text which can be used to create a
log of activity in the model
Note Allows the addition of text labels to the Interface tab
University of Parma - 39NetLogoProf. Agostino Poggi
Toolbar Elements (2/2)
button
switcher
slider
output
plot
University of Parma - 48NetLogoProf. Agostino Poggi
A Multi-Colors World (1/2)
patches-own [ soil-type ]
to setupclear-allask patches [
set soil-type random 4ifelse soil-type = 0 [ set pcolor YELLOW ] [ ifelse soil-type = 1 [ set pcolor RED ]
[ ifelse soil-type = 0 [ set pcolor GREEN ][ set pcolor BLUE ]
]]
]end
University of Parma - 50NetLogoProf. Agostino Poggi
Population Evolution (1/3)
breed [persons person]
persons-own [ age ]
to setupclear-allset-default-shape persons "person“reset-ticks
end
to go…
end
University of Parma - 51NetLogoProf. Agostino Poggi
Population Evolution (2/3)
to gocreate-persons 10 [
set xcor random-xcorset ycor random-ycorset color REDset age 0
]
ask persons [ set age (age + 1) ]
ask persons with [age > 10][ die ]
tickplot count persons
end
University of Parma - 53NetLogoProf. Agostino Poggi
Random Turtle Movement (1/2)
to setupclear-allcreate-turtles 100reset-ticks
endto go
ask turtles [fd 10 rt random 10 lt random 10 show absolute-value xcor ]
tickendto-report absolute-value [number]
ifelse number >= 0 [ report number ] [ report (- number) ]end
University of Parma - 55NetLogoProf. Agostino Poggi
Prey Predator Model (1/7)
breed [sheep a-sheep]breed [wolves wolf]
sheep-own [ age ]wolves-own [ energy age ]
to setup...
end
to go...
end
University of Parma - 56NetLogoProf. Agostino Poggi
Prey Predator Model (2/7)
to setupclear-allset-default-shape sheep "sheep"create-sheep initial-sheep [
set color whiteset size 1.5 setxy random-xcor random-ycorset age 0 ]
set-default-shape wolves "wolf"create-wolves initial-wolves [set color brownset size 1.5setxy random-xcor random-ycorset age 0set energy random (3 * energy-per-sheep-eaten)
]reset-ticks
end
University of Parma - 57NetLogoProf. Agostino Poggi
Prey Predator Model (3/7)
to goif not any? turtles [ stop ]ask turtles [ take-a-step ]ask sheep [ try-to-make-bunnies ]ask wolves [set energy (energy - 1)try-to-eattry-to-make-kittens ]
ask turtles [set age (age + 1) ]ask sheep [ if age > max-sheep-age [ die ] ]ask wolves [if age > max-wolves-age [ die ]if energy < 0 [ die ] ]
tickend
University of Parma - 58NetLogoProf. Agostino Poggi
Prey Predator Model (4/7)
to take-a-stepright random 50left random 50forward 1
end
to try-to-make-bunniesif random-float 200 < sheep-birth-rate [
hatch-sheep 1 [rt random-float 360fd 1set age 0 ]
]end
University of Parma - 59NetLogoProf. Agostino Poggi
Prey Predator Model (5/7)
to try-to-eatlet prey one-of sheep-hereif prey != nobody [ask prey [ die ]set energy energy + energy-per-sheep-eaten ]
end
to try-to-make-kittensif energy > wolves-energy-to-reproduce [hatch-wolves 1 [rt random-float 360fd 1set age 0set energy energy / 2 ]
set energy energy / 2]
end
University of Parma - 62NetLogoProf. Agostino Poggi
Walking on a Network (1/4)
breed [nodes node]breed [walkers walker]
walkers-own [location]
to setup…
end
to layoutlayout-spring nodes links 0.5 2 1
end
to go---
end
Resistance to change in their length for a change of 1 unit
Zero force length
Repulsion force that 2
nodes at a distance of 1 unit
will exert on each other
University of Parma - 63NetLogoProf. Agostino Poggi
Walking on a Network (2/4)
to setupclear-allset-default-shape nodes "circle"create-nodes 30 [ set color blue ]ask nodes [ create-link-with one-of other nodes ]repeat 500 [ layout ]ask nodes [ setxy 0.95 * xcor 0.95 * ycor ]create-walkers 5 [
set color redset location one-of nodesmove-to location
]reset-ticks
end
University of Parma - 64NetLogoProf. Agostino Poggi
Walking on a Network (3/4)
to goask links [ set thickness 0 ]
ask walkers [let new-location one-of [link-neighbors] of location
ask [link-with new-location] of location [set thickness 0.5 ]
face new-location move-to new-locationset location new-location
]
tickend