Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modelling and Simulating Social Systems with MATLAB

Similar presentations


Presentation on theme: "Modelling and Simulating Social Systems with MATLAB"— Presentation transcript:

1 Modelling and Simulating Social Systems with MATLAB
Modelling and Simulating Social Systems with MATLAB Lesson 6 – Continuous simulations A. Johansson & W. Yu © ETH Zürich |

2 Lesson 6 - Contents Some basis of motion dynamics
Lesson 6 - Contents Some basis of motion dynamics Pedestrian simulation Exercises

3 From discreet to continuous space
Cellular automaton (CA) Continuous simulation

4 Modelling agent’s motion in a continuous space
Agents are characterized by Their position X (m) Y (m) Their velocity Vx (m/s) Vy (m/s) Vy y x Vx

5 Modelling agent’s motion in a continuous space
Next position after 1 second is given by: x(t+1) = x(t) + Vx(t) y(t+1) = y(t) + Vy(t) Vy Time t+1 y Time t x Vx

6 Modelling agent’s motion in a continuous space
Time step dt is often different from 1: x(t+dt) = x(t) + dt*Vx(t) y(t+dt) = y(t) + dt*Vy(t) Time t+dt dt>1 Vy y Time t+dt dt<1 Time t x Vx

7 Modelling agent’s motion in a continuous space
The velocity is also updated in time. y x

8 Modelling agent’s motion in a continuous space
The velocity changes as a result of inter-individual interactions Repulsion Attraction Examples: atoms, planets, pedestrians, animals swarms…

9 Modelling agent’s motion in a continuous space
The changes of velocity vector is defined by an acceleration.

10 Modelling agent’s motion in a continuous space
The velocity changes as a result of interactions with the environment Repulsion Attraction Examples: bacteria, ants, pedestrian trails …

11 Modelling agent’s motion in a continuous space
The velocity changes as a result of a random process With or without bias Examples: exploration behaviour in many species of animals

12 Modelling agent’s motion in a continuous space
How to define a random move? Choose a random angle in a normal distribution Rotate the velocity by this angle Variance=1 In Matlab: newAngle = randn() Mean=0

13 Modelling agent’s motion in a continuous space
How to define a random move? Choose a random angle in a normal distribution Rotate the velocity by this angle In Matlab: meanVal=2; v=0.1; newAngle = meanVal + v*randn()

14 Modelling agent’s motion in a continuous space
How to define a BIASED random move? Unbalance the distribution toward positive or negative values In Matlab: meanVal=2; v=0.1; newAngle = meanVal + v*randn()

15 Programming the simulator
Initialization: Four elements are required to define the state of individuals X = [1 1 3 ]; Y = [2 1 1]; Vx = [ ]; Vy = [ ]; For t=1:dt:T For p=1:N [Vx(p) Vy(p)] = update( …) ; X(p) = X(p) + dt*Vx(p) ; Y(p) = Y(p) + dt*Vy(p) ; end The velocity is updated first, and then the position as a function of the velocity and the time step

16 Brownian Motion %Brownian Motion t = 100;n = 1000;dt=t/n;
x = zeros(1,n);y = zeros(1,n); dx = zeros(1,n);dy = zeros(1,n); x(1) = 0;y(1) = 0; hold on; k = 1; for j=2:n x(j) = x(j-1) + sqrt(dt)*randn; y(j) = y(j-1) + sqrt(dt)*randn; plot([x(j-1),x(j)],[y(j-1),y(j)]); M(k) = getframe; k = k + 1; end 16

17 Pedestrian Simulation
CA model Many-particle model

18 CA Model (A. Kirchner, A. Schadschneider, Phys. A, 2002)

19 Many-particle Model (Helbing, Nature,2000)
Desired velocity Repulsive force

20 Novelty of Your Project
Please make sure that your project has significant difference with any of the projects from last year! For comparison, please visit

21 Exercise 1 - Random move Download files from the folder \Ex1.
Exercise 1 - Random move Download files from the folder \Ex1. The main file is ‘simulator.m’ The function ‘update.m’ needs to be completed: Define two variables called meanVal and deviation that correspond to the mean value and the variance of the random distribution. Set meanVal to 0 and deviation to 0.1. Set alpha as a random value picked in a normal distribution which mean is meanVal and variance is deviation Launch a simulation with N=10 (stop it with ctrl+c)

22 Exercise 2 - Biased random move
Exercise 2 - Biased random move meanVal actually defines how biased is the motion. For instance, set meanVal to 0.3 and launch a simulation to see the strong bias for a given direction. We would like meanVal to be a parameter of the function update.m In the function update , add an additional input called ‘bias’. Set meanVal = bias instead of 0.3; In the main function simulator.m define a global variable bias=0.03 . In the update section, change the call to function update accordingly. Launch a simulation with N=1 to observe the slight bias in the movement of the agent

23 Exercise 3 - Interaction with the environment
Exercise 3 - Interaction with the environment Suppose now the agents can interact with the environment and fell attracted by stimulus they perceive nearby (like mosquitoes attracted by light, or ants attracted by pheromones). Each individual senses the stimulus on its right and left side and biases its motion toward the higher level of stimulus. Download files from the folder \Ex3. The function environment.m defines a hidden environment in which the agents are moving. The function getStimulus.m compares stimulus given by the environment on right and left side of a given individual. The function returns S=1 for a higher stimulus on the left side, S=0 if both side have the same level and S=-1 if higher on the right side. Include the function getStimulus in the update section of simulator.m so that the bias equals +bias if S=1, -bias if S= - 1 , and 0 if S=0. Launch the simulation with N=20. Where is approximately the highest level of stimulus?

24 Exercise 4 - Two kinds of individuals
Exercise 4 - Two kinds of individuals We now have two populations called 1 and 2. Population 1 is attracted by high stimulus and population 2 is repulsed by high levels of stimulus. Each individual is characterized by an additional state called type that can be either 1 or 2. Update the initialization section so that half of individuals have type=1 and the other half have type=2. Change the update section so that individuals of type 1 have a biased movement toward higher stimulus and individuals of type 2 have a biased movement toward lower levels of stimulus. Change the display section in order to plot individuals of type 1 in blue and individuals of type 2 in red. Launch the simulation with N=50. What can you say about the environment? Download file \Ex4\revealEnvironment.m and execute it to see how the environment was defined. 24 24

25 Exercise 5 (optional)- Tracking agents
Exercise 5 (optional)- Tracking agents We would like to reveal the environment by mean of a statistical study on the agent’s position, for both populations. To do so, we need to store their positions in time. We use 4 new vectors recordX1, recordY1, recordX2, recordY2 to store the X and Y values of agents of population 1 and 2. In the initialization section set recordX1, recordY1, recordX2 and recordY2 to an empty vector [ ]. After each update, store the X and Y value of the current agent in the right vector (use the function horzcat to add a value at the end of the vector) After the end of the simulation, plot red dots at coordinates (recordX1, recordY1) and blue dots at coordinates (recordX2, recordY2) . Launch the simulation with T=100 (initialization section) and N=20. Compare with the environment you revealed in the previous exercise. 25 25


Download ppt "Modelling and Simulating Social Systems with MATLAB"

Similar presentations


Ads by Google