Skip to content
Snippets Groups Projects
README.md 8.43 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 using [Yarner](https://github.com/mlange-42/yarner).
Martin Lange's avatar
Martin Lange committed

Martin Lange's avatar
Martin Lange committed
The document you are currently reading contains the [ODD protocol](#rabies-model-odd-protocol) ([Grimm et al. 2006, 2010](#references)) of a model for Rabies in red foxes. The ODD description is interleaved with code blocks showing the NetLogo code used for the described aspect (ODD+C). Using the Literate Programming tool [Yarner](https://github.com/mlange-42/yarner), these code blocks are extracted from the document and arranged into a working NetLogo model.
Martin Lange's avatar
Martin Lange committed

* [How to use it](#how-to-use-it)
* [How it works](#how-it-works)
* [Rabies model - ODD protocol](#rabies-model-odd-protocol)
* [References](#references)
* [Appendix](#appendix)

## How to use it

**Read the ODD**

The best way to understand how to use Literate Programming with Yarner to create a NetLogo model from an ODD protocol is to read the [ODD+C of the Rabies model](#rabies-model-odd-protocol), below. It contains the complete code of the model, except the UI elements.

See section [How it works](#how-it-works) for how the model is extracted from this document.

**Run from downloads**

The [Downloads](https://git.ufz.de/oesa/literate-netlogo/-/jobs/artifacts/main/download?job=build) contain a folder `model` with files `Model.nlogo` and `Code.nls`. Open the `.nlogo` file with NetLogo.

**Build from sources**

1. [Install Yarner](https://mlange-42.github.io/yarner/installation.html)
2. Clone the sources of this example:  
   `git clone https://git.ufz.de/oesa/literate-netlogo.git`
3. `cd` into folder `literate-netlogo`
4. Run command `yarner` to extract the model code
5. Open file `model/Model.nlogo` with NetLogo

Martin Lange's avatar
Martin Lange committed
## How it works

Yarner scans the document for code blocks and puts them together using macros. Each code block has a name, given in its first line and prefixed with `;-`:

```netlogo
;- A code block
let x 0
```

When a code block is referenced by another code block using a macro with `; ==>`, it is placed inside the referencing code block:

```netlogo
;- Outer code block
; ==> A code block.
let y 1
```

The resulting extracted code would look like this:

```netlogo
let x 0
let y 1
```

See the [Yarner documentation](https://mlange-42.github.io/yarner/) for a comprehensive guide and a description of all features.
Martin Lange's avatar
Martin Lange committed

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

* [Purpose](#purpose)
* [Entities, state variables, and scales](#entities-state-variables-and-scales)
* [Process overview and scheduling](#process-overview-and-scheduling)
* [Design concepts](#)
* [Initialization](#)
* [Input data](#)
* [Submodels](#)

Martin Lange's avatar
Martin Lange committed
### Purpose

The purpose of this model is to demonstrate how NetLogo models can be created from ODD protocols, using Literate Programming with Yarner.

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

The model landscape is represented by a grid of 1km x 1km cells, each representing a potential home range of a fox family.

The only entity in the model are fox families. Each fox family is represented by the NetLogo patches (i.e. grid cells).

Each patch (i.e. each potential fox family) has four state variables:
Martin Lange's avatar
Martin Lange committed

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

The disease state (`state`) represents the state of the patch. Possible states are `EMPTY`, `S`usceptible, `I`nfected and `R`ecovered:

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

Disease states are internally represented by whole numbers 0-3:

```netlogo
;- Setup disease states
set EMPTY 0
set S 1
set I 2
set R 3
State variable `infected-neighbours` is a counter for the number of infected neighbouring families. `ticks-to-death` is a count-down timer for infected families until death. `dispersal-tick` indicates the month of the year (index [0..11]) of the family's next dispersal event.

### Process overview and scheduling

Processes simulated by the model are [Dispersal](#dispersal), [Infection](#infection) and [Disease course](#disease-course). Processes are executed on every monthly time step in the above order.

After to model processes, the patch colour is updated for [Visualization](#visualization).

```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
Fox families as the only model entities live on a rectangular grid of habitat patches. Patches can be empty or occupied.

Reproduction and juvenile dispersal are subsumed under a single process [Dispersal](#dispersal). Once a year, a certain number of female offspring disperses from each fox family. Time and target location of dispersal are stochastic. Dispersal is restricted by a maximum dispersal radius.

Fox families can become infected with Rabies. Rabies can be transmitted to the 8 immediate neighbouring families. Infection is stochastic and infection probability depends on the number of infected neighbours.

Martin Lange's avatar
Martin Lange committed
### Initialization
The model is initialized by populating each habitat patch with a `S`usceptible fox family. One randomly selected family is infected.

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

The model uses no input data.

Martin Lange's avatar
Martin Lange committed
### 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
## References

Grimm V, Berger U, Bastiansen F, Eliassen S, Ginot V, Giske J, Goss-Custard J, Grand T, Heinz S, Huse G, Huth A, Jepsen JU, Jørgensen C, Mooij WM, Müller B, Pe’er G, Piou C, Railsback SF, Robbins AM, Robbins MM, Rossmanith E, Rüger N, Strand E, Souissi S, Stillman RA, Vabø R, Visser U, DeAngelis DL. 2006. **A standard protocol for describing individual-based and agent-based models**. *Ecological Modelling* 198:115-126.

Grimm V, Berger U, DeAngelis DL, Polhill G, Giske J, Railsback SF. 2010. **The ODD protocol: a review and first update**. *Ecological Modelling* 221: 2760-2768.

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.