Newer
Older
This is an example project to demonstrate Literate Programming to create [NetLogo](https://ccl.northwestern.edu/netlogo/) models from ODD+C protocols.
### Entities, state variables, and scales
```netlogo
;- State variables
patches-own [
state
```netlogo
;- Disease states
globals [
EMPTY
S
I
R
]
```
;- Setup disease states
set EMPTY 0
set S 1
set I 2
set R 3
### Process overview and scheduling
if ticks mod 12 = 0 [
assign-dispersal
]
disperse
clear-all
; ==> Setup disease states.
ask patches [
set state S
]
ask one-of patches [
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
```netlogo
;- Submodels
; ==> Dispersal.
; ==> Infection.
; ==> Disease course.
; ==> Update patches.
```
#### Dispersal
```netlogo
;- Dispersal
to assign-dispersal
ask patches with [ state != EMPTY ] [
set dispersal-tick (ticks + start-dispersal + random length-dispersal)
]
end
```
```netlogo
;- Dispersal
to disperse
ask patches with [ state != EMPTY and dispersal-tick = ticks ] [
let candidates other patches
in-radius dispersal-radius
with [ state = EMPTY ]
let num-candidates num-offspring
if count candidates < num-candidates [
set num-candidates count candidates
]
ask n-of num-candidates candidates [
set state S ; no dispersal of infected
; alternative:
; set state [state] of myself
; set ticks-to-death [ticks-to-death] of myself
]
]
end
```
#### Infection
```netlogo
;- Infection
to infect-patches
ask patches [
set infected-neighbours 0
]
ask patches with [ state = I ] [
ask neighbors [
set infected-neighbours infected-neighbours + 1
]
]
ask patches with [ state = S ] [
if random-float 1 < calc-infection-prob [
]
]
end
; ==> Infection probability.
```
```netlogo
;- Infection probability
to-report calc-infection-prob
report 1 - (1 - beta) ^ infected-neighbours
end
```
```netlogo
;- Infect patch
to infect-patch
set state I
set ticks-to-death ticks-infected
end
```
#### Disease course
```netlogo
;- Disease course
to age-infection
ask patches with [ state = I ] [
if ticks-to-death = 0 [
set state EMPTY
]
set ticks-to-death ticks-to-death - 1
]
end
```
#### Visualization
```netlogo
;- Update patches
to update-patches
ask patches [
ifelse state = EMPTY [ set pcolor white ]
[ ifelse state = S [ set pcolor blue ]
[ ifelse state = I [ set pcolor red ]
[ set pcolor green ] ] ]
]
end
```
### Code
```netlogo
;- file:Code.nls
; ==> Disease states.
; ==> State variables.
; ==> Setup.
; ==> Go.
```
### NetLogo file
In the main `nlogo` file, we only "include" an `nls` file to allow for the reverse mode.
The file `Model.nlogo` is simply copied from the `nlogo` directory via option `code_files` in the `[paths]` section of the `Yarner.toml`.
This separation of model code and user interface also allows to edit the model's UI elements in NetLogo's GUI builder tool, while using Literate Programming for the code.