Initiation Netlogo
Marion Le Texier
Rémi Lemoy
Sébastien Rey Coyrehourcq
Rouen
14-15-16/06/2021
Programme
...indicatif...
9h00 accueil
9h30 - Intro SMA & modélisation
10h30 - Prise en main Netlogo
12h30 - Pause déjeuner
Jour 1
Jour 2
14h00 - Présentation modèle
17h30 - Fin
9h00 accueil
9h30 - Exploration de modèles
12h30 - Pause déjeuner
14h00 - Fonctions Anonymes
17h30 - Fin
NetLogo
Workspace
NetLogo
birth of turtles
xcor
ycor
color
size
shape
turtles-own [
energy
]
create-turtles 2 [
set shape "turtle"
set color orange
set size 1
set xcor random-xcor
set ycor random-ycor
set energy random 25
]
1
2
energy
heading
size 1
xcor 3
ycor 5
energy 5
heading 160
xcor 2
ycor 6
heading 340
energy 15
size 1
NetLogo
World of wonders
xcor
ycor
color
size
shape
anything you want
heading
pxcor
pycor
pcolor
anything you want
Turtles
Patches
Time
NetLogo
World of wonders
xcor
ycor
color
size
shape
anything you want
heading
pxcor
pycor
pcolor
anything you want
Observer
Turtles
create-turtles
clear-all
reset-ticks
Patches
Time
reset-ticks
(ticks)
tick
tick
tick
tick
NetLogo
birth of turtles
xcor
ycor
color
size
shape
anything you want
turtles-own [
;declare variable you want for turtles
]
create-turtles <number of turtles> [
; init turtles variables
]
create-turtles 2 [
set shape "turtle"
set color orange
set size random 3
set xcor randomxcor
set ycor randomycor
]
1
2
heading
xcor 1
ycor 2
heading 280
size 1
xcor 4
ycor 1
heading 20
size 2
NetLogo
Action !
ask [
set color blue
]
ask with
[ color = orange ]
set color blue
]
NetLogo
Action !
ask [
set color orange
]
2
3
4
1
5
4
4
1
5
3
2
ask (turtle-set ) [
set heading 180
]
4
1
5
3
2
4
2
3
4
1
5
2
NetLogo
move your body
0
-5
0
5
(patch 3 -1)
-5
5
5
-5
NetLogo
move your body
0
0
5
ask [
set heading towards (patch 3 -1) ]
ask [ face (patch 3 -1) ]
-5
-5
5
5
-5
NetLogo
move your body
0
0
5
ask [
set heading towards ]
-5
-5
5
5
-5
NetLogo
move your body
0
-5
0
5
ask [
set heading towardsxy 0.5 -1.5 ]
- 0.5
0.5
-5
5
5
-5
Need distance ?
show [distance patch 3 -1 ]
of
ask [
show distance patch 3 -1 ]
ask [
show distance ]
NetLogo
move your body
NetLogo
move your body
forward 1
centroid to centroid
NetLogo
move your body
NetLogo
move your body 2
forward 1
centroid to centroid
NetLogo
move your body
NetLogo
move your body
forward 1
centroid to not centroid
NetLogo
move your body
patch-left-and-ahead 45 1
patch-right-and-ahead 45 1
NetLogo
move your body
patch-right-and-ahead 45 1
NetLogo
move your body
patch-right-and-ahead 45 1
move-to
NetLogo
move your body
knowing future patch ? => see model library
NetLogo
Leaving command center
Command Center is great to DEBUG your program, at any time, by asking turtles, patches, trying commands ...
BUT
this is not the way we write our programs
[credits: stan openshaw]
NetLogo
Leaving command center
to setup
clear-all
create-turtles 2
reset-ticks
end
to go
ask turtles [
fd 1 ;; forward 1 step
rt random 10 ;; turn right
lt random 10 ;; turn left
]
tick
end
setup
go
interface tab
code tab
NetLogo
Leaving command center
to setup
clear-all
create-turtles nb-turtles
reset-ticks
end
to go
ask turtles [
fd 1 ;; forward 1 step
rt random 10 ;; turn right
lt random 10 ;; turn left
]
tick
end
setup
go
interface tab
code tab
nb-turtles
4
NetLogo
Leaving command center
to whatyouwant
ask turtles [
set color first shuffle [orange blue]
]
end
setup
go
interface tab
code tab
whatyouwant
...
NetLogo
Exercice
to move-by-color
; move turtle of color = my-color to one specified patch
end
setup
go
interface tab
code tab
...
move-by-color
Algorithms in one word : IF
NetLogo
How do you manage differents behaviors for our turtle ?
energy > 15
?
?
sinon
sinon
energy > 15
...
SI [EXPRESSION] == TRUE ALORS
INSTRUCTION
FIN SI
...
ex : (2 + 2 <= 4)
jump
Algorithms in one word : IF
NetLogo
a = 3
SI [EXPRESSION] == TRUE ALORS
INSTRUCTIONS
FIN SI
...
ex : (2*a <= 4)
jump
jump
Algorithms in one word : IF
NetLogo
a = "A"
SI [EXPRESSION] == TRUE ALORS
INSTRUCTIONS
SINON SI [EXPRESSION] == TRUE
INSTRUCTIONS
SINON
INSTRUCTIONS
FIN SI
ex : (a != "A")
jump
jump
ex : a == "B"
Algorithms in one word : IF
NetLogo
Algorithms in one word : IF
NetLogo
IF <condition > [
... code ...
IFELSE < condition > [
... code ...
]
][
]
ask turtles with [color = white] [
ifelse energy > 15 [
set color green
]
[
set color orange
]
]
ask turtles [
ifelse color = white and energy > 15[
set color green
]
[
if color = white [
set color orange
]
]
]
ask turtles [
if color = white and energy > 15 [
set color green
]
]
... code ...
Algorithms in one word : IF
NetLogo
( IFELSE
< condition > [
]
]
ask turtles [
(ifelse
energy > 15 and color = white [
set color green
]
color = white
[
set color orange
])
]
<condition 2> [
<condition n > [
...
... code ...
... code ...
][
... code ...
])
... code ...
NetLogo
it's alive !
turtle to breeds
xcor
ycor
color
size
shape
heading
inherit
speed
energy
target
breed [diplodocus a-diplodocus]
diplodocus-own [
sleeping-time ; sleep
childrens ; nb of childrens
]
builtin variables
sleeping-time
childrens
aggressivity
breed [velociraptors a-velociraptor]
velociraptors-own [
aggressivity ; level of aggressivity
target ; target to kill
group ; my group of hunters
]
group
NetLogo
Exercice
breed [houses house]
breed [diplodocus a-diplodocus]
diplodocus-own [ target ]
to setup
; a) create nb houses
; b) create nb diplodocus
; with a random house as target
end
to go
; a) if distance to target = 0
; -> change target of diplodocus
; b) if distance to target < 1
; -> move to target
; -> else forward 1
; tick
end
t0
t1
t0
t1
NetLogo
list vs agentset / patchset
1
15
3
8
2
4
6
ask
with
set
let
...
one-of
min-one-of
max-one-of
any?
member?
of
...
RANDOMIZED
at each call
count
NetLogo
list vs agentset / patchset
Agentset are returned by primitive
or created manually
turtle-set ( list
1
3
ask turtles with [color = orange]
ask turtle 4 [ show other turtles-here ]
ask patch -9 -5 [ show turtles-on neighbors ]
ask turtles with [color = green] [ show turtles-on neighbors ]
)
(turtle-set
1
3
)
NetLogo
list vs agentset / patchset
List are returned / used by primitive
or created manually with many possibilities
list (random 10) (random 10)
list
1
3
[ 3 4 5 9]
foreach
sort / sort-by / sort-on
<- anonymous function
map
filter
similar to agentset but
MAINTAIN ORDER
NetLogo
Understanding Context
ask
[
]
set
let
color
mycolor
mycolor
blue
set
size
random 3
NetLogo
Understanding Context
ask
[
]
set
let
color
mycolor
mycolor
blue
set
size
random 3
ask
patch-here [
set
pcolor
mycolor2
let
mycolor2
mycolor - 2
]
patch-here
NetLogo
my dear neighbors
5
5
ask [
ask in-radius x [
...
]
]
1
2
0
0
NetLogo
my dear neighbors
5
5
ask [
ask in-radius 1 [
set color green
]
]
0
0
NetLogo
my dear neighbors
5
5
ask [
ask in-radius 1 [
set pcolor green
]
]
0
0
.
.
.
.
.
.
.
.
.
NetLogo
my dear neighbors
5
5
ask [
ask in-radius 1 [
set pcolor green
]
]
0
0
.
.
.
.
.
.
.
.
.
NetLogo
of ? or ask ?
[ energy] of
Get a value for a turtle, or values for turtles
or use it with ask to do complex things in few lines ...
ask [ in-radius 3] of
[ set color red ]
[ energy] of
[ energy] of
sum, max, min
NetLogo
of ? or ask ?
Different taste of of
max-n-of 2 [energy]
max-n-of 2 with [color = blue] [energy]
1
5
1
8
1
8
3
5
[ 15, 8, 12, 6 ]
max-n-of 2 with [color = blue] [energy]
max-one-of [energy]
1
NetLogo
of ? or ask ?
Also works with patches or agenset
1
8
5
[ 15, 8, 12, 6 ]
1
1
3
3
8
ask max-one-of [ sum [energy] of turtles-here ] [ show sum [energy] of turtles-here]
ask with-max [count turtles-here with [energy > 10]] [ set pcolor red]
NetLogo
Communicate T - P
... in-radius <number> ...
( T to T )
( T to P )
... patch-here ...
( P to T )
... other turtles-here ...
... neighbors ... ou ... neighbors4 ...
... turtles-on ...
... turtles-on ...
... patch-right/left-and-ahead ...
... turtles-at ...
... in-cone <distance> <angle> ...
NetLogo
Organize your code
With procedure and function
NetLogo
Organize your code
With procedure and function
NetLogo
Organize your code
With "finite state machine" pattern
DinoLogo
DinoLogo
step 1 : diplodocus
DinoLogo
step 2 : velociraptor
DinoLogo
step 3 : humans
DinoLogo
step 4 : Dying
DinoLogo
step 4 : state
state = next-state
apply current state
compute next-state
state
next-state
state
to-report compute-new-diplodocus-state
(ifelse
state = "exploring" [
; ... switch to eating
; ... or continue to explore
]
state = "eating" [
; ... switch to exploring
; ... or continue to eat
])
report ???
end
to apply-current-diplodocus-state
(ifelse
state = "exploring" [
; ... move action
]
state = "eating" [
; ... switch to exploring
; ... eat action
])
end
to go-diplo
ask diplodocus [
set state next-state
apply-current-diplodocus-state
set next-state compute-new-diplodocus-state
]
end
next-state
"eating"
"exploring"
DinoLogo
step 5 : obstacle
- use nw:turtles-in-radius 3
- compute all nodes & links accessible
- use nw:turtles-on-path-to
get path to next-target
get accessible nodes only
... max-one-of ...
select target (grass-qty) in these accessible patches
go to target !
sprout + create-link + neighbors
Netlogo - Formation Rouen 2021
By sebastien rey coyrehourcq
Netlogo - Formation Rouen 2021
- 496