Initiation Netlogo (v2)
Rémi Lemoy
Romain Reulier
Sébastien Rey Coyrehourcq
Rouen
16-17/02/2023
https://page.hn/qr9czo
Programme
...indicatif...
9h30 - Accueil
10h00 - Intro SMA & modélisation
11h00 - Netlogo (1ère phase)
12h30 - Pause déjeuner
Jour 1
Jour 2
13h30 - Présentation modèle
17h00 - Fin
9h30 accueil
9h45 - Exploration de modèles
12h30 - Pause déjeuner
17h00 - Fin
11h00 - Prise en main Netlogo
15h00 - Pause café
13h30 - Netlogo (2ème phase)
15h00 - Pause café
... too long; didn't read ... (src: Arnaud Banos)
Moi : créé un modèle netlogo de diffusion d'épidémies
ChatGpt : Pour créer un modèle NetLogo de diffusion d'épidémies, vous pouvez suivre les étapes suivantes:
-
Ouvrez NetLogo et créez un nouveau modèle.
-
Définissez les agents dans le modèle. Dans ce cas, nous allons utiliser des tortues pour représenter les personnes.
-
Définissez les propriétés des tortues, telles que la santé (malade ou en bonne santé) et la vitesse de déplacement.
-
Écrivez le code pour déterminer comment les tortues se déplacent dans l'environnement. Vous pouvez utiliser la fonction "forward" pour déplacer les tortues dans une direction aléatoire.
-
Écrivez le code pour déterminer comment la maladie se propage. Vous pouvez utiliser la fonction "ask" pour faire en sorte que les tortues malades aient une probabilité de transmettre la maladie à d'autres tortues avec lesquelles elles entrent en contact.
-
Ajoutez des boutons et des curseurs pour contrôler les paramètres du modèle, tels que la vitesse de déplacement des tortues et la probabilité de transmission de la maladie.
-
Exécutez le modèle et observez comment la maladie se propage à travers la population.
Voici un exemple de code de base pour ce modèle:
turtles-own [health] to setup clear-all create-turtles num-turtles ask turtles [ set health "healthy" setxy random-xcor random-ycor ] end to go ask turtles [ move spread-disease ] end to move forward 1 end to spread-disease if health = "sick" [ ask other turtles-on patch-ahead 1 [ if health = "healthy" [ set health "sick" ] ] ] end
NetLogo
Workspace
NetLogo
Model library
NetLogo
Dictionary
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 random-xcor
set ycor random-ycor
]
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 : LOOP
NetLogo
How do you ask our turtle to do something ?
ask [
... do something ...
]
1
3
8
2
Agenset
...
1
3
8
2
...
List
foreach [self] of [ t ->
ask t [
... do something ...
]
]
list contain anything you want
agentset contain only agents
RANDOMIZED
SAME ORDER
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 = "foo"
SI [EXPRESSION] == TRUE ALORS
INSTRUCTIONS
SINON SI [EXPRESSION] == TRUE
INSTRUCTIONS
SINON
INSTRUCTIONS
FIN SI
ex : (a != "bar")
jump
jump
ex : a == "Foo"
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 neighbors4 [
set pcolor green
]
]
0
0
NetLogo
my dear neighbors
5
5
ask [
ask neighbors with [any? turtles-here] [
set pcolor red
]
]
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 2023
By sebastien rey coyrehourcq
Netlogo - Formation Rouen 2023
- 199