• Non ci sono risultati.

Modelling and Simulations Agent-Based

N/A
N/A
Protected

Academic year: 2022

Condividi "Modelling and Simulations Agent-Based"

Copied!
65
0
0

Testo completo

(1)

Agent-Based

Modelling and Simulations

NetLogo

Agostino Poggi

(2)

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

(3)

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

(4)

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

(5)

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

(6)

Patches (1/2)

♦ Have a color defined by the pcolor variable

♦ Have a location defined by the pxcor and pycor constant 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-pycor pairs of variables

♦ Usually every patch has the same number of neighbor patches (toroidal world)

(7)

Patches (2/2)

(8)

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 ycor variables

 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

(9)

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, …

(10)

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

(11)

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"

(12)

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

(13)

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

(14)

List

♦ Allows to store multiple pieces of information in a single value

♦ Each piece of information can be values of any

possible type (i.e., a boolean, a number, a string, an agent, an agent set or even another list)

♦ Pieces of information are enclosed between square brackets

♦ Each piece of information is identified by an integer starting at 0

(15)

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

(16)

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 globals keyword 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

(17)

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

(18)

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

(19)

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

(20)

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

(21)

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

(22)

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

(23)

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

(24)

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

(25)

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

(26)

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

(27)

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]

(28)

Working with Lists (4/4) sort turtles

♦ Builds a list of turtles sorted in ascending order by who variable

sort-by [[size] of ?1 < [size] of ?2] turtles

♦ Builds a list of turtles sorted in ascending order by size variable

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]

(29)

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 NetLogo environment are called primitives

♦ User’s defined commands and reporters are called procedures

(30)

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

(31)

Commands & Reporters (3/3)

(32)

Conditionals

(33)

Loops

(34)

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

(35)

File I/O (2/3)

(36)

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

(37)

Graphical User Interface

toolbar

(38)

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

(39)

Toolbar Elements (2/2)

button

switcher

slider output

plot

(40)

Code View

(41)

Interface View (1/3)

(42)

Interface View (2/3)

(43)

Interface View (3/3)

(44)

Model Setting

(45)

Info View

(46)

Model Library

(47)

Turtle Shape Editor

(48)

A Multi-Colors World (1/2) patches-own [ soil-type ]

to setup clear-all

ask patches [

set soil-type random 4

ifelse soil-type = 0 [ set pcolor YELLOW ] [ ifelse soil-type = 1 [ set pcolor RED ]

[ ifelse soil-type = 0 [ set pcolor GREEN ] [ set pcolor BLUE ]

] ] end]

(49)

A Multi-Colors World (2/2)

(50)

Population Evolution (1/3) breed [persons person]

persons-own [ age ] to setup

clear-all

set-default-shape persons "person“

reset-ticks end

to go

… end

(51)

Population Evolution (2/3) to go

create-persons 10 [ set xcor random-xcor set ycor random-ycor set color RED

set age 0 ]

ask persons [ set age (age + 1) ] ask persons with [age > 10]

[ die ]

tickplot count persons end

(52)

Population Evolution (3/3)

(53)

Random Turtle Movement (1/2) to setup

clear-all

create-turtles 100 reset-ticks

end to go

ask turtles [

fd 10 rt random 10 lt random 10 show absolute-value xcor ]

endtick

to-report absolute-value [number]

ifelse number >= 0 [ report number ] [ report (- number) ] end

(54)

Random Turtle Movement (2/2)

(55)

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

(56)

Prey Predator Model (2/7)

to setup clear-all

set-default-shape sheep "sheep"

create-sheep initial-sheep [ set color white

set size 1.5

setxy random-xcor random-ycor set age 0 ]

set-default-shape wolves "wolf"

create-wolves initial-wolves [ set color brown

set size 1.5

setxy random-xcor random-ycor set age 0

set energy random (3 * energy-per-sheep-eaten) ]

(57)

Prey Predator Model (3/7) to go

if not any? turtles [ stop ] ask turtles [ take-a-step ]

ask sheep [ try-to-make-bunnies ] ask wolves [

set energy (energy - 1) try-to-eat

try-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 ] ]

endtick

(58)

Prey Predator Model (4/7) to take-a-step

right random 50 left random 50 forward 1

end

to try-to-make-bunnies

if random-float 200 < sheep-birth-rate [ hatch-sheep 1 [

rt random-float 360 fd 1

set age 0 ] ]

end

(59)

Prey Predator Model (5/7) to try-to-eat

let prey one-of sheep-here if prey != nobody [

ask prey [ die ]

set energy energy + energy-per-sheep-eaten ] end

to try-to-make-kittens

if energy > wolves-energy-to-reproduce [ hatch-wolves 1 [

rt random-float 360 fd 1set age 0

set energy energy / 2 ] set energy energy / 2 end]

(60)

Prey Predator Model (6/7)

(61)

Prey Predator Model (7/7)

(62)

Walking on a Network (1/4) breed [nodes node]

breed [walkers walker]

walkers-own [location]

to setup end …

to layout

layout-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

(63)

Walking on a Network (2/4) to setup

clear-all

set-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 red

set location one-of nodes move-to location

]reset-ticks end

(64)

Walking on a Network (3/4) to go

ask 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-location set location new-location

] tick end

(65)

Walking on a Network (4/4)

Riferimenti

Documenti correlati

orizzonte temporale di medio e lungo periodo e si riferiscono alla finalità ultima dell’Azienda sa- nitaria, ovvero il miglioramento del benessere so- ciale e dello stato di

In Corollary 6.2 it is shown that most imprimitive groups are relation groups, in the following sense: If H is an imprimitive group of degree n with a block of imprimitivity ∆ so

lividus to polluted sediments from the Bagnoli-Coroglio SNI, testing the interactive effects of sediment-dependent stress with physical disturbance caused by the

A critical step in the differentiation process of all epithelial cells is cell detachment from the basement membrane, mostly dependent to modification

Auch wenn sich bei Wolff eine eindeutige Bewertung der Hauptzüge der aufkläre- rischen Strömung findet (z. des Primats des Praktischen, der Unabhängigkeit der Philosophie von

Se la FDA approva la richiesta (che deve aver ricevuto anche un ethical clearance da parte di un Institutional Review Board), il medico curante sarà tenuto a monitorare i

Since the Voivod is obligated to make an appropriate referral to the Police, Internal Security Agency or other relevant authorities for information concerning the problem if

● NetLogo uses the ask command to give commands to turtles, patches, and links. Any code you put