2016-10-17ETH Zürich
Modeling and Simulating Social Systems with MATLAB
Lecture 4 – Cellular Automata
Chair of Computational Social Science
Olivia Woolley, Lloyd Sanders, Dirk Helbing
2016-10-17 Modeling and Simulating Social Systems with MATLAB 2
2016-10-17 Modeling and Simulating Social Systems with MATLAB 33
Projects – Suggested Topics1 Traffic Dynamics 9 Evacuation Bottleneck 17 Self-organized Criticality 25 Facebook
2 Civil Violence 10 Friendship Network Formation
18 Social Networks Evolution
26 Sequential Investment Game
3 Collective Behavior 11 Innovation Diffusion 19 Task Allocation & Division of Labor
27 Modeling the Peer Review System
4 Disaster Spreading 12 Interstate Conflict 20 Artificial Financial Markets
28 Modeling Science
5 Emergence of Conventions
13 Language Formation 21 Desert Ant Behavior 29 Simulation of Networks in Science
6 Emergence of Cooperation
14 Learning 22 Trail Formation 30 Opinion Formation in Science
7 Emergence of Culture 15 Opinion Formation 23 Wikipedia 31 Organizational Learning
8 Emergence of Values 16 Pedestrian Dynamics 24 Social Contagion 32 Network/systemic resilience
2016-10-17 Modeling and Simulating Social Systems with MATLAB 44
Seminar thesis
Studying a scientific paper and/or a data set
Reproducing and extending results / running a simulation in MATLAB
Writing a report and giving a talk
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5
Strategies for Research Projects
“Data project”: Choose a dataset for which you have an interesting
question Come up with a research question that can be tested
with that data Run simulations that either use the dataset as input or
that reproduce certain aspects of the data Look for scientific publications on the topic to place your
project in the context of existing work
2016-10-17 Modeling and Simulating Social Systems with MATLAB 6
Strategies for Research Projects
“Paper project”: Choose a scientific paper (or several papers) on a
modeling and simulation approach that looks interesting Come up with a research question that builds upon the
paper but is different from their research questions Extend, simplify, modify, and/or combine existing models
for testing your research question Run simulations for your model and compare the results
to the ones for the original model
2016-10-17 Modeling and Simulating Social Systems with MATLAB 7
Research Plan Structure
1-2 (not more!) pages stating:
Brief, general introduction to the problem
How you represent the problem with a model
Research questions you want to try to answer
Existing literature and previous projects you will base your model on and possible extensions. https://github.com/msssm/interesting_projects/wiki
Research methods you are planning to use
2016-10-17 Modeling and Simulating Social Systems with MATLAB 8
Research Plan
Upon submission, the Research Plan can: be accepted be accepted under revision be modified later on only if change is justified (create
new version)
Talk to us if you are not sure
Deadline for submitting a proposal:
23 October 2016 (midnight)
2016-10-17 Modeling and Simulating Social Systems with MATLAB 9
Recap: Dynamical Systems
Dynamical systems are described by a set of differential equations; numerical solutions can be calculated iteratively using Euler's method (examples: Lotka-Volterra and Kermack-McKendrick)
The values and ranges of parameters critically matter for the system dynamics (Example 2, epidemiological threshold)
Time resolution in Euler's method must be sufficiently high to capture “fast” system dynamics (Example 3)
MATLAB provides the commands ode23 and ode45 for solving differential equations (Example 3)
9
2016-10-17 Modeling and Simulating Social Systems with MATLAB 1010
Recap: Simulation Models
What do we mean when we speak of
(simulation) models?
Formalized (computational) representation of social (or other kinds of) dynamics
Reduction of complexity, i.e. highly simplifying assumptions
The goal is not to reproduce reality in general (only very specific aspects of it)
Formal framework to test and evaluate causal hypotheses against empirical data (or stylized facts)
2016-10-17 Modeling and Simulating Social Systems with MATLAB 1111
Recap: Simulation Models
Strengths of (simulation) models?
Computational laboratory: test how micro dynamics lead to macro patters
There are usually no experiments in social sciences but computer models can be a “testing ground”
Particularly suitable where analytical models fail (or where dynamics are too complex for analytical modeling)
2016-10-17 Modeling and Simulating Social Systems with MATLAB 1212
Recap: Simulation Models
Weakness of (simulation) models? The choice of model parameters and
implementation details can have a strong influence on the simulation outcome
We can only model aspects of a system, i.e. the models are necessarily incomplete & reductionist
More complex models are generally not better (only when simplification is not possible)
It can be hard to relate simulation results to a realistic and relevant empirical question
2016-10-17 Modeling and Simulating Social Systems with MATLAB 1313
Recap: Simulation Models
How to we deal with known limitations?
Use empirical input and formal optimization to rule out arbitrariness of the model
Test for implementation- and specification- dependence of simulations
Validate the model mechanism both with observations and causal theory
Use empirical data to evaluate the predictive power of the simulation model
2016-10-17 Modeling and Simulating Social Systems with MATLAB 1414
Cellular Automaton (plural: Automata)
2016-10-17 Modeling and Simulating Social Systems with MATLAB 1515
Cellular Automaton (plural: Automata)
A cellular automaton is a rule, defining how the
state of a cell in a grid is updated, depending on
the states of its neighbor cells
Cellular-automata simulations are discrete in
time and space
2016-10-17 Modeling and Simulating Social Systems with MATLAB 16
Cellular Automaton
The grid can have an arbitrary number of
dimensions:
1-dimensional cellular automaton
2-dimensional cellular automaton
2016-10-17 Modeling and Simulating Social Systems with MATLAB 17
Moore Neighborhood
The cells are interacting with each neighbor
cells, and the neighborhood can be defined in
different ways, e.g. the Moore neighborhood:
1st order Moore neighborhood 2nd order Moore neighborhood
2016-10-17 Modeling and Simulating Social Systems with MATLAB 18
Von-Neumann Neighborhood
The cells are interacting with each neighbor cells,
and the neighborhood can be defined in different
ways, e.g. the Von-Neumann neighborhood:
1st order Von-Neumann neighborhood
2nd order Von-Neumann neighborhood
2016-10-17 Modeling and Simulating Social Systems with MATLAB 19
Game of Life
Ni = Number of 1st order Moore neighbors to cell
i that are activated.
For each cell i:1. Deactivate active cell iff Ni <2 or Ni >3.2. Activate inactive cell iff Ni =3
i
2016-10-17 Modeling and Simulating Social Systems with MATLAB 2020
Game of Life
Ni = Number of 1st order Moore neighbors to cell
i that are activated.
For each cell i:1. Deactivate active cell if Ni <2 or Ni >3.2. Activate inactive cell if Ni =3
2016-10-17 Modeling and Simulating Social Systems with MATLAB 2121
Game of Life Want to see the Game of Life in action? Type:
life
2016-10-17 Modeling and Simulating Social Systems with MATLAB 22
Complex Patterns
22
“Gosper Glider Gun”:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 23
Game of Life: Some Bits of History1970: Conway presents the Game of Life; discovery of the glider (speed c/4) and the standard spaceships (speed c/2); discovery of the Gosper glider gun (first pattern found with indefinite growth)
1971: First garden of eden (pattern that has no parent pattern and can only occur at generation 0) was discovered; first pattern with quadratic growth (breeder)
1989: Discovery of the first spaceships with velocities c/3 and c/4
1996: Game of Life simulated in Game of Life
2000: A Turing machine was built in Game of Life
2004: Construction of the largest interesting pattern so far: caterpillar spaceship with speed 17c/45 consisting of 11,880,063 alive cells
2013: The first complete replicator (a pattern that produces an exact copy of itself) was built
2014: Game of Life wiki contains more than 3000 interesting patterns
2016-10-17 Modeling and Simulating Social Systems with MATLAB 24
Life in Life
24
The Game of Life can be used to simulate the
Game of Life:
http://www.youtube.com/watch?v=xP5-iIeKXE8
2016-10-17 Modeling and Simulating Social Systems with MATLAB 25
Life in Life
25
The Game of Life can be used to calculate
everything (if a Turing machine can):
http://www.youtube.com/watch?v=My8AsV7bA94
2016-10-17 Modeling and Simulating Social Systems with MATLAB 2626
1-dimensional Cellular Automata
2016-10-17 Modeling and Simulating Social Systems with MATLAB 2727
Cellular Automaton: Principles
1. Self Organization: Patterns appear from
random start patterns
2. Emergence: High-level phenomena can
appear such as gliders, glider guns, etc.
3. Complexity: Simple rules produce complex
phenomena
2016-10-17 Modeling and Simulating Social Systems with MATLAB 2828
Highway Simulation
Simple example of a 1-dimensional cellular
automaton
Rules for each car at cell i:1. Stay: If the cell directly to the right is occupied.
2. Move: Otherwise, move one step to the right, with probability p
Move to the next cell, with the probability p
2016-10-17 Modeling and Simulating Social Systems with MATLAB 2929
Highway Simulation
We have prepared some files for the highway
simulations: draw_car.m : Draws a car, with the functiondraw_car(x0, y0, w, h)
simulate_cars.m: Runs the simulation, with the functionsimulate_cars(moveProb, inFlow, withGraphics)
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3030
Highway Simulation
Running the simulation is done like this:
simulate_cars(0.9, 0.2, true)
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3131
Kermack-McKendrick Model
In lecture 3, we introduced the Kermack-
McKendrick model, used for simulating disease
spreading
We will now implement the model again, but this
time instead of using differential equations we
use the approach of cellular automata
2016-10-17 Modeling and Simulating Social Systems with MATLAB 32
Kermack-McKendrick model
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3333
Kermack-McKendrick Model
The Kermack-McKendrick model is specified as:
S: Susceptible persons
I: Infected persons
R: Removed (immune)
persons
β: Infection rate
γ: Recovery rate
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3434
Kermack-McKendrick Model
The Kermack-McKendrick model is specified as:
S: Susceptible persons
I: Infected persons
R: Removed (immune)
persons
β: Infection rate
γ: Recovery rate
dSdt
=−β I ( t )S ( t )
dIdt
= β I (t )S ( t )−γ I ( t )
dRdt
=γ I (t )
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3535
Kermack-McKendrick Model
The Kermack-McKendrick model is specified as:
S: Susceptible persons
I: Infected persons
R: Removed (immune)
persons
β: Infection rate
γ: Immunity rate
dSdt
=−β I ( t )S ( t )
dIdt
= β I (t )S ( t )−γ I ( t )
dRdt
=γ I (t )
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3636
Kermack-McKendrick Model
The Kermack-McKendrick model is specified as:
S: Susceptible persons
I: Infected persons
R: Removed (immune)
persons
β: Infection rate
γ: Recovery rate
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3737
Kermack-McKendrick Model
The Kermack-McKendrick model is specified as:
For the MATLAB implementation, we need to
decode the states {S, I, R}={0, 1, 2} in a matrix x.
S S S S S S S
I I I S S S S
I I I I S S S
R I I I S S S
R I I I S S S
I I I S S S S
I S S S S S S
0 0 0 0 0 0 0
1 1 1 0 0 0 0
1 1 1 1 0 0 0
2 1 1 1 0 0 0
2 1 1 1 0 0 0
1 1 1 0 0 0 0
1 0 0 0 0 0 0
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3838
Kermack-McKendrick Model
Define microscopic rules for Kermack-
McKendrick model:
In every time step, the cells can change states
according to:
A Susceptible individual can be infected by an Infected neighbor with probability β, i.e. State 0 → 1, with probability β.
An individual can recover from an infection with probability γ, i.e. State 1 → 2, with probability γ.
2016-10-17 Modeling and Simulating Social Systems with MATLAB 3939
Kermack-McKendrick Model
The Kermack-McKendrick model is specified as:
We now define a 2-dimensional cellular-
automaton, by defining a grid (matrix) x, where
each of the cells is in one of the states: 0: Susceptible 1: Infected 2: Recovered
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4040
Cellular-Automaton Implementation
Implementation of a 2-dimensional cellular
automaton model in MATLAB is done like this:
The iteration over the cells can be done either
sequentially, or randomly.
Iterate the time variable, tIterate over all cells, i=1..N, j=1..N
Iterate over all neighbors, k=1..M
End k-iterationEnd i-iteration
End t-iteration
…
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4141
Cellular-Automaton Implementation
Sequential update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4242
Cellular-automaton implementation
Sequential update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4343
Cellular-automaton implementation
Sequential update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4444
Cellular-automaton implementation
Sequential update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4545
Cellular-automaton implementation
Sequential update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4646
Cellular-automaton implementation
Sequential update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4747
Cellular-automaton implementation
Random update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4848
Cellular-automaton implementation
Random update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 4949
Cellular-automaton implementation
Random update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5050
Cellular-automaton implementation
Random update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5151
Cellular-automaton implementation
Random update:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5252
Cellular-automaton implementation
Attention:
Simulation results can be very sensitive to the type of update used
Random update is usually preferable but also not always the best solution
Just be aware of this potential complication and
check your results for dependency on the
updating scheme!
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5353
Boundary Conditions
The boundary conditions can be any of the
following: Periodic: The grid is wrapped, so that what exits on
one side reappears at the other side of the grid.
Fixed: Agents are not influenced by what happens at the other side of a border.
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5454
Boundary Conditions
The boundary conditions can be any of the
following:
Fixed boundaries Periodic boundaries
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5555
MATLAB Implementation of the Kermack-McKendrick Model
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5656
MATLAB implementation
Set parameter values
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5757
MATLAB implementation
Define grid, x
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5858
MATLAB implementation
Define neighborhood
2016-10-17 Modeling and Simulating Social Systems with MATLAB 5959
MATLAB implementation
Main loop. Iterate the time variable, t
2016-10-17 Modeling and Simulating Social Systems with MATLAB 6060
MATLAB implementation
Iterate over all cells, i=1..N, j=1..N
2016-10-17 Modeling and Simulating Social Systems with MATLAB 6161
MATLAB implementation
For each cell i, j: Iterate over the neighbors
2016-10-17 Modeling and Simulating Social Systems with MATLAB 6262
MATLAB implementation
The model, i.e. updating rule goes here.
2016-10-17 Modeling and Simulating Social Systems with MATLAB 6363
Aborting Execution
When running large computations or animations, the
execution can be aborted by pressing Ctrl+C in the main
window:
2016-10-17 Modeling and Simulating Social Systems with MATLAB 64
References
Wolfram, Stephen, A New Kind of Science. Wolfram
Media, Inc., May 14, 2002.
http://www.conwaylife.com/wiki/
Martin Gardner. The fantastic combinations of John
Conway's new solitaire game "life". Scientific American
223 (October 1970): 120-123.
Schelling, Thomas C. (1971). Dynamic Models of
Segregation. Journal of Mathematical Sociology 1:143-
186.
2016-10-17 Modeling and Simulating Social Systems with MATLAB 65
Exercise 1
Get draw_car.m and simulate_cars.m from
http://www.coss.ethz.ch/education/matlab.html or
from https://github.com/msssm/lecture_files
Investigate how the flow (moving vehicles per
time step) depends on the density (occupancy
0%..100%) in the simulator. This relation is
called the fundamental diagram in transportation
engineering.
2016-10-17 Modeling and Simulating Social Systems with MATLAB 66
Exercise 1b
Generate a video of an interesting case in your
traffic simulation.
We have uploaded an example file:
simulate_cars_video.m
2016-10-17 Modeling and Simulating Social Systems with MATLAB 67
Exercise 2
Download the file disease.m which is an
implementation of the Kermack-McKendrick
model as a cellular automaton
Plot the relative fractions
of the states S, I, R, as
a function of time, and
see if the curves look
the same as for the
implementation last class
2016-10-17 Modeling and Simulating Social Systems with MATLAB 68
Exercise 3
Modify the model Kermack-McKendrick model in
the following ways: Change from the 1st order Moore neighborhood to a
2nd and 3rd order Moore neighborhood. Make it possible for
Removed individualsto change back to Susceptible
What changes?