Skip to content
Snippets Groups Projects
README.md 3.44 KiB
Newer Older
Martin Lange's avatar
Martin Lange committed
# Literate NetLogo

This is an example project to demonstrate Literate Programming to create [NetLogo](https://ccl.northwestern.edu/netlogo/) models from ODD+C protocols.
Martin Lange's avatar
Martin Lange committed

Martin Lange's avatar
Martin Lange committed
## Rabies model - ODD protocol

### Purpose

### Entities, state variables, and scales
Martin Lange's avatar
Martin Lange committed

Each patch has a disease state.
Martin Lange's avatar
Martin Lange committed

```netlogo
;- State variables
patches-own [
  state
  infected-neighbours
  dispersal-tick
Martin Lange's avatar
Martin Lange committed

```netlogo
;- Disease states
globals [
  EMPTY
  S
  I
  R
]
```
Martin Lange's avatar
Martin Lange committed

```netlogo
;- Setup disease states
set EMPTY 0
set S 1
set I 2
set R 3
### Process overview and scheduling


```netlogo
;- Go
to go
  if ticks mod 12 = 0 [
    assign-dispersal
  ]
  disperse

  infect-patches
  update-patches
; ==> Submodels.
Martin Lange's avatar
Martin Lange committed
### Design concepts
Martin Lange's avatar
Martin Lange committed
### Initialization
Martin Lange's avatar
Martin Lange committed
;- Setup
to setup
  clear-all
  ; ==> Setup disease states.

  ask patches [
    set state S
  ]
  ask one-of patches [
  update-patches
Martin Lange's avatar
Martin Lange committed
  reset-ticks
end
Martin Lange's avatar
Martin Lange committed
### Input data

### Submodels

```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.
; ==> Infect patch.
```

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

Martin Lange's avatar
Martin Lange committed
## Appendix

### 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.