Tutorial On Agent-Based Models in Netlogo: June 2012
Tutorial On Agent-Based Models in Netlogo: June 2012
net/publication/267229756
CITATION READS
1 3,305
2 authors:
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Catherine A A Beauchemin on 27 November 2014.
Abstract
This tutorial will introduce the participant to designing and implementing an agent-
based model using NetLogo through one of two different projects: modelling T cell
movement within a lymph node or modelling the progress of a viral infection through
an in vitro cell culture. Each project is broken into a series of incremental steps of
increasing complexity. Each step is described in detail and the code to type in is initially
provided. However, each project has room to grow in complexity and biological realism
so participants who finish more rapidly will be assisted in bringing their project beyond
the scope of the tutorial or in developing a project of their own.
Suggested reading
In preparation for the class and tutorial, participants are encouraged to read:
• N Moreira. In pixels and in health: Computer modeling pushes the threshold of medical
research. Science News, 169(3):40–44, 21 Jan, 2006
to understand how agent-based models of infection and/or immunity are typically made of.
1
1 NetLogo: A basic introduction
Interface: Where the simulation gets displayed and the end-user can interact with it using
the available sliders, buttons, etc. which were made available by the author of the
model.
Information: Where you can find general information about the model: what it is, how to
use it, etc. It is essentially the help page for the model.
Procedure: Where the NetLogo code driving the model is located. You can modify it and
see what happens or you can write brand new code.
2
initially, where should they be, what colour should they be, what should be their initial state
(e.g., infected, uninfected, dead).
The “go” button does exactly what you’d expect: it makes the simulation go. But be
sure to press “setup” before you press go.
The remaining buttons allow you to trigger certain actions during the course of the
simulations. The various sliders and switches allow you to adjust various parameters or
conditions of the simulations: You can play around with them even as the simulation is
running. Finally, the monitors and plots are used to display variables of the simulations
(e.g., how many cells there are).
Note that if you want to restart the simulation, you can press “go” to stop the currently
running simulation and then press “setup” to reinitialize it.
Now that you know how to run a model, let’s learn how to make one.
Patch: A small rectangle (in 2D) or block (in 3D) of the simulation world. A patch is
identified by its coordinate, (x, y) or (x, y, z). Thus it CANNOT move but it can hold
variables (e.g., its colour, how long ago it was visited, what type of a site it is). You
cannot have different breeds of patches: there is only one set of patches making up
your world.
Turtle: A mobile agent which can go from one patch to another based on whatever rules
are defined for its motion. Because it can move, a turtle agent is identified not by its
coordinate but instead by a “who” number. Turtles can also hold variables, and you
can have different breeds of turtles (different types of turtle agents, e.g., rabbits and
hares).
Link: A connection which can be created between two turtle agents. It appears as a line
corresponding to the shortest path between the two turtles. A link is identified by the
who number of the two turtles it links. Links can hold variables, and you can have
different breeds of links.
Note that by default the NetLogo world uses toroidal boundary conditions meaning that
anything which disappears off one edge of the world reappears on the opposite side. You
can change this (in NetLogo 2D only) under “Settings...” by unchecking the “World wraps”
checkboxes. For our two projects, we’ll use the default periodic boundary conditions.
3
1.4 Want to go further?
The projects below show you what to do step-by-step. But if you want to extend these
models or develop your own, the following resources will provide you with the information
you need:
Models Library You will find those within NetLogo under ‘File’ → ‘Models Library’. In
particular, check out the ‘Code Examples’ models which are very helpful. Most of these
codes are also available online at http://ccl.northwestern.edu/netlogo/models,
but not the ‘Code Examples’, unfortunately. Read the Models’ description to see if it
is likely to contain the type of code/behaviour you are looking to code-up in your own
simulation.
4
2 Project: T cell movement within lymph nodes
5
breed [mice mouse]. Note that ; is used to indicate a comment: everything after the ; is
ignored by NetLogo.
We’ll create the T cells and DCs as circles which map to spheres in NetLogo 3D. We’ll
make the DCs twice as large as the T cells. We’ll make 100 green T cells and 2 red DCs. So
let’s write the ‘setup’ function to do this:
to setup
clear-all
set-default-shape turtles "circle" ; all turtles will be circles (spheres)
create-tcells 100 [
set color green ; make T cell green
setxyz random-xcor random-ycor random-zcor ; put T cell at random location
]
create-DCs 2 [
set size 2 ; make DC of size 2 (twice that of T cells)
set color red ; make DC red
setxyz random-xcor random-ycor random-zcor
]
end
Let’s link the ‘setup’ function to a ‘setup’ button. Go to your ‘Interface’ tab. Make
sure ‘Button’ is selected, press ‘Add’, and click anywhere in the Interface window. Under
‘Commands’ type ‘setup’, i.e. the name of the function you just created. Every time you
click the ‘setup’ button, your ‘setup’ function is run. Click it a few times to see how your
initial setup changes.
to go
ask tcells [
right random-normal 0 90 ; Pick random turn angle: avg = 0 std dev = 90 deg
roll-right random-normal 0 90 ; Pick random roll angle
forward 1
]
end
The ‘right angle’ command turns your turtle to the right by angle degrees. Here, instead
of specifying the exact angle, we use ‘random-normal 0 90’ which will pick a random angle
from a normal distribution of mean zero and standard deviation 90◦ .
6
Now, create a button as before, but this time enter ‘go’ in the ‘Command’ and check the
‘Forever’ checkbox. Click the button: wow, look at these T cells go! You can adjust the
speed with the slider.
But perhaps you’d like to create a button to see the T cells move by one step only.
Sure, create a button as before, enter ‘go’ in the ‘Command’, but this time, do not check
the ‘Forever’ checkbox and you might want to change the display name to ‘one step’ or
something similar to distinguish it from the other ‘go’ button.
7
in ‘to setup’ add
to go
ask tcells [
right random-normal 0 90
roll-right random-normal 0 90
forward tcell-step
]
tick-advance dt
end
Now your tick counter in the 3D View is in minutes. Let’s adjust its label accordingly.
Click on ‘Settings’ and under ‘Tick counter label’ enter ‘Time (min)’, and hit ‘Ok’. I also
recommend you set the update to ‘on ticks’ rather than ‘continuous’.
For this purpose it might make sense to use links. We will create a link between a T cell and
a DC when the distance requirement is met, and we could give the link a property tDCfree
which would keep track of the time left before the T cell is free from the DC.
At the top of the file, before the functions, add
links-own [ tDCfree ]
This states that all links will now have a variable called tDCfree. It is like globals
variables, but it belongs only to a specific type of agent, in this case the link agents. Next,
we’ll break the to go function into several functions to make the code clearer. In your code,
replace the to go function with
8
to go
move-tcells ; move T cell movement commands into a function
identify-DCbound-tcells ; new function to identify DC-bound T cells
update-link-status ; new function to update DC-T cell links
tick-advance dt
end
Here, we’ve left the definition of the two new functions to later. It’s good to make your
changes incrementally. Now check that your code still works as before by pressing the ‘setup’
and ‘go’ buttons. If all is still working, you’re ready to start creating the identify-DCbound-tcells
function. What we want is to:
• Create a directed link from the DC and all T cells within a 2 patch diameter.
9
For each DC, create-links-to creates a link from that DC to all T cells whose colour is not
blue which are located within a radius of 2 patches. The with [color != blue] ensures
that only T cells that are not blue (i.e. that are not already linked to a DC) are considered.
The ask end2 [...] means ask the turtle at the end of the link (in this case it is a T cell
since the directed link is from the DC to the T cell) to become blue.
That’s great, but now the T cells remain blue forever and their link to the DCs are never
removed. Time to write our update-link-status function:
Wow, this is awesome! But I sure wish we could track of how many encounters took place. . .
Now, in your ‘Interface’ tab, add a ‘Monitor’. Under ‘Reporter’ enter nDCmeet and under
‘Display name’ something like Number of DC-T cell encounters for clarity. Now run the
simulation and see how the Monitor gets updated and keeps growing as more and more
encounters occur.
10
This will plot the number of DC-T cell contacts made thus far (nDCmeet) as a function of
time (ticks).
In your ‘Interface’ tab, add a ‘Plot’, call it something meaningful like Number of DC-T
cell encounters and set appropriate names for the x and y axis labels (e.g., time (min),
and Number). Now run the simulation and see what happens... cool!
11
3 Project: Viral infection spread in vitro
12
In your ‘Procedure’ tab, you should enter
to setup
clear-all
; setup whole grid
ask patches [
set pcolor white ; uninfected
]
; setup initially infected cells
ask n-of 5 patches [
set pcolor green ; latently infected
]
end
Note that ‘;’ is used to indicate a comment: everything after the ‘;’ is ignored by NetLogo
so it allows you to leave yourself notes in the code to remember what the commands do. The
n-of 5 command tells NetLogo to pick 5 patches at random.
Now, let’s link the setup function to a ‘setup’ button. Go to your Interface tab. Make
sure ‘Button’ is selected, press ‘Add’, and click anywhere in the Interface window. Under
‘Commands’ enter setup, i.e. the name of the function you just created. Every time you
click the ‘setup’ button, your setup procedure is run. Click it a few times to see how your
initial setup changes.
13
3.2.3 Deciding who lives or die
Ok, so we have infected cells: now what? Well, these newly infected or eclipse cells, after
some time, will begin sythesizing viral proteins and soon they should be able to release virus,
becoming infectious. And after some time producing virus, these infectious cells will cease
viral production and undergo apoptosis either because they have exhausted the available
nutrients making all these virions, or due to toxicity resulting from viral production.
To reproduce this behaviour, we’ll have each cell keep track of when it should become
infectious and when it should die based on when it was infected. In your ‘Procedure’ tab,
above to setup, we will define the following patch properties:
Now we have to make sure that, when we initially infect our pfu cells, all of them
decide right then when they should become infectious and die (i.e., set the value of their
tinfectious and tdead. So inside to setup, replace your ask n-of pfu section with this
one:
so now our pfu infected cells have decided that they will become infectious after 4 ticks and
will die after 11 ticks (4 + 7). NetLogo uses ‘ticks’ to keep track of time.
Now we need to make time advance and keep track of whether these cells are ready to
change state. Below the end command marking the end of the setup procedure, add a new
go procedure.
to go
; check if latently infected cells become infectious
ask patches with [pcolor = green] [
if tinfectious <= ticks [
set pcolor red
]
]
; check if infectious cells die
ask patches with [pcolor = red] [
if tdead <= ticks [
set pcolor black
]
]
tick-advance 1 ; advance time by one time step
end
14
To run this procedure, create a button as before, but this time enter ‘go’ in the ‘Com-
mand’. Once the ‘go’ button has been created, click the ‘setup’ button and then the new
‘go’ button: the ‘ticks:’ number at the top of your simulation grid go up every time you click
on ‘go’ and when you get to ‘ticks: 5’, all your cells become red (infectious). If you keep
clicking, they’ll turn black (dead).
But all this clicking is not good for your wrist. It would be nice if it could just keep
going. Right-click on the ‘go’ button, select ‘Edit...’ and check the ‘Forever’ checkbox. Now,
if you click the ‘go’ button, it will go so fast that you won’t see a thing. You can adjust the
speed at which things happen by moving the slider at the top of the ‘Interface’ tab which
defaults to ‘normal speed’ but can be adjusted as you wish.
If you would like to keep the option of being able to make the simulation advance by just
one step, create another button with the command go, but do not check ‘Forever’ and set
the ‘Display name’ to something like ‘one step’ to distinguish it from the ‘go’ button. Now
you can ‘go’ continuously or one step at a time.
15
globals [
dt ; time step duration (h)
eclipsedur ; duration of eclipse phase (h)
infectiousdur ; duration of infectious phase (h)
]
It is always good to leave yourself comments about what your variables do and what units
they are in. We now need to set the value of these 3 variables. Inside to setup, right after
clear-all add
set tinfectious 4
set tdead tinfectious + 7
with
To impose the duration of a time step, dt, in to go, replace the command tick-advance 1
with tick-advance dt. And to make these time-units obvious when looking at the simu-
lation, go to your ‘Interface’ tab, click on ‘Settings...’ and under ‘Tick counter label’ enter
something like ‘time post-infection (h)’, and click ‘Ok’.
to go
spread-the-infection
16
to spread-the-infection
ask patches with [pcolor = red] [
let nnei count neighbors with [pcolor = white]
if nnei > 0 [ ; if more than 0 neighbours are uninfected
ask one-of neighbors with [pcolor = white] [ ; ask one of them
set pcolor green
set tinfectious ticks + random-normal eclipsedur (0.1 * eclipsedur)
set tdead tinfectious + random-normal infectiousdur (0.1 * infectiousdur)
]
]
]
end
The command let nnei count neighbors with [pcolor = white] will set variable nnei
equal to the number of uninfected neighbour the infectious cell has. And if it has more than
zero, it procedes to infect one of them.
The command random-normal eclipsedur (0.1 * eclipsedur) means that the dura-
tion of the eclipse phase for each newly infected cell will be chosen at random from a normal
distribution of mean eclipsedur and a standard deviation of 10% of eclipsedur. Having
these durations be random is more realistic because not all cells will become infected at the
same time. The same is true for tdead.
Time to test out your infection spread. Go back to ‘Interface’ and type ‘go’. Cool!
17
set divtime 7.0
set maxdiv 7
set-default-shape turtles "face happy"
create-tcells 2 [
set size 2
set color magenta
setxy random-xcor random-ycor
]
which sets the time between T cells divisions to 7 h, and the number of divisions the CTL
will undergo before contraction to 7 division. Additionally, it specifies that the default shape
for T cells will be a happy face symbol. It then adds 2 magenta (naive) T cells at random
locations on the grid.
As for letting these cells move about, mature, divide and die, you will need to add the line
update-tcells just above tick-advance dt in to go, and at the bottom of the ‘Procedure’
file, add the following new procedure:
to update-tcells
ask tcells [
let pclr [pcolor] of patch-here
if (pclr = green) or (pclr = red) [
ifelse color = magenta [
set color cyan
set shape "face sad"
set tdiv ticks + random-normal divtime 1.0
set ndiv 0
][
ask patch-here [ set pcolor black ]
if tdiv <= ticks [
ask patch-here [
sprout-tcells 1 [
set shape "face sad"
set color cyan
set size 2
set tdiv [tdiv] of one-of other tcells-here
set ndiv [ndiv] of one-of other tcells-here
]
]
set tdiv ticks + random-normal divtime 1.0
set ndiv ndiv + 1
if ndiv > maxdiv [
die
]
]
18
]
]
right random 360
forward 1
]
end
References
[1] J. B. Beltman, A. F. Marée, J. N. Lynch, M. J. Miller, and R. J. de Boer. Lymph node
topology dictates T cell migration behavior. J. Exp. Med., 204(4):771–780, 2007.
19
[3] T. R. Mempel, S. E. Henrickson, and U. H. von Adrian. T-cell priming by dendritic cells
in lymph nodes occurs in three distinct phases. Nature, 427(6970):154–159, 8 January
2004.
20