Modelling and Simulating Social Systems with MATLAB Lesson
Modelling and Simulating Social Systems with MATLAB Lesson 6 – Cellular Automata A. Johansson & W. Yu © ETH Zürich | 2009 -03 -16
Lesson 6 - Contents § Cellular Automata § Neighborhood definitions § Models Game of Life § Highway Simulation § Disease Spreading, revisited § § Exercises 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 2
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. § They are represented as grids with arbitrary dimension. § Cellular-automata simulations are discrete both in time and space. 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 3
Cellular Automaton § The grid can have an arbitrary number of dimensions: 1 -dimensional cellular automaton 2009 -03 -16 2 -dimensional cellular automaton A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 4
Moore Neighborhood § The cells are interacting with each neighbor cells, and the neighborhood can be defined in different ways, e. g. the Moore neighborhood: 1 st order Moore neighborhood 2009 -03 -16 2 nd order Moore neighborhood A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 5
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: 1 st order Von-Neumann neighborhood 2009 -03 -16 2 nd order Von-Neumann neighborhood A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 6
Game of Life § Ni = Number of 1 st order Moore neighbors to cell i that are activated. § For each cell i: Deactivate: If Ni <2 or Ni >3. 2. Activate: if cell i is deactivated and Ni =3 1. www. mathworld. wolfram. com 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 7
Game of Life § Ni = Number of 1 st order Moore neighbors to cell i that are activated. § For each cell i: Deactivate: If Ni <2 or Ni >3. 2. Activate: if cell i is deactivated and Ni =3 1. www. mathworld. wolfram. com 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 8
Highway simulation § As an example of a 1 -dimensional cellular automaton, we will present a highway simulation. For each car at cell i: Stay: If the cell directly to the right is occupied. 2. Move: Otherwise, move one step to the right, with probability p 1. Move to the next cell, with the probability p 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 9
Highway simulation § We have prepared some files for the highway simulations: § draw_car. m : Draws a car, with the function draw_car(x 0, y 0, w, h) § simulate_cars. m: Runs the simulation, with the function simulate_cars(move. Prob, in. Flow, with. Graphics) 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 10
Highway simulation § Running the simulation is done like this: simulate_cars(0. 9, 0. 2, true) 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 11
Kermack-Mc. Kendrick model § In lesson 4, we introduced the Kermack. Mc. Kendrick model, used for simulating disease spreading. § We will now implement the model again, but this time within the cellular-automata framework. 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 12
Kermack-Mc. Kendrick model § The Kermack-Mc. Kendrick model is specified as: S: Susceptible persons I: Infected persons R: Removed (immune) persons β: Infection rate γ: Immunity rate 2009 -03 -16 S β transmission R γ recovery A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch I 13
Kermack-Mc. Kendrick model § The Kermack-Mc. Kendrick model is specified as: S: Susceptible persons I: Infected persons R: Removed (immune) persons β: Infection rate γ: Immunity rate 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 14
Kermack-Mc. Kendrick model § The Kermack-Mc. Kendrick model is specified as: S: Susceptible persons I: Infected persons R: Removed (immune) persons β: Infection rate γ: Immunity rate 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 15
Kermack-Mc. Kendrick model § The Kermack-Mc. Kendrick model is specified as: S: Susceptible persons I: Infected persons R: Removed (immune) persons β: Infection rate γ: Immunity rate 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 16
Kermack-Mc. Kendrick model § The Kermack-Mc. Kendrick model is specified as: S: Susceptible persons I: Infected persons R: Removed (immune) persons β: Infection rate γ: Immunity rate 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 17
Kermack-Mc. Kendrick model § The Kermack-Mc. Kendrick 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 0 0 0 0 I I I S S 1 1 1 0 0 I I S S S 1 1 0 0 0 R I I I S S S 2 1 1 1 0 0 0 I I I S S 1 1 1 0 0 I S S S 1 0 0 0 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 18
Kermack-Mc. Kendrick model § The Kermack-Mc. Kendrick model is specified as: We now define a 2 -dimensional cellularautomaton, by defining a grid (matrix) x, where each of the cells is in one of the states: 0: Susceptible § 1: Infected § 2: Recovered § 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 19
Kermack-Mc. Kendrick model § The Kermack-Mc. Kendrick model is specified as: At each 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 γ. 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 20
Cellular-automaton implementation § Implementation of a 2 -dimensional cellularautomaton model in MATLAB is done like this: Iterate the time variable, t Iterate over all cells, i=1. . N, j=1. . N Iterate over all neighbors, k=1. . M § The iteration over the cells can be done either sequentially, or randomly. 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 21
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 22
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 23
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 24
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 25
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 26
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 27
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 28
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 29
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 30
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 31
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 32
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 33
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 34
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 35
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 36
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 37
Cellular-automaton implementation § Sequential update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 38
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 39
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 40
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 41
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 42
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 43
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 44
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 45
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 46
Cellular-automaton implementation § Random update: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 47
Boundary conditions § The boundary conditions can be any of the following: § Periodic: The grid is wrapped, so that what crosses a border is reappearing at the other side of the grid. § Fixed: Agents are not influenced by what happens at the other side of a border. 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 48
Boundary conditions § The boundary conditions can be any of the following: Fixed boundaries 2009 -03 -16 Periodic boundaries A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 49
MATLAB implementation of the The Kermack-Mc. Kendrick model 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 50
Set parameter values MATLAB implementation 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 51
Define grid, x MATLAB implementation 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 52
Define neighborhood MATLAB implementation 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 53
Main loop. Iterate the time variable, t MATLAB implementation 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 54
Iterate over all cells, i=1. . N, j=1. . N MATLAB implementation 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 55
For each cell i, j: Iterate over the neighbors MATLAB implementation 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 56
The model, i. e. updating rule goes here. MATLAB implementation 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 57
Breaking execution § When running large computations or animations, the execution can be stopped by pressing Ctrl+C in the main window: 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 58
Exercise 1 § Download the files draw_car. m and simulate_cars. m from the course web page, www. soms. ethz. ch/matlab 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. 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 59
Exercise 2 § Download the file disease_spreading. m which is an implementation of the Kermack. Mc. Kendrick 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 old implementation. 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 60
Exercise 2 b § Modify the model in the following ways: Change from the 1 st order Moore neighborhood to a 2 nd and 3 rd order Moore neighborhood. § Make it possible for Removed individuals to change state back to Susceptible. § 2009 -03 -16 A. Johansson & W. Yu / andersj@ethz. ch yuwen@ethz. ch 61
- Slides: 61