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 5 – Agent-based simulations A. Johansson & M. Moussaid © ETH Zürich |

2 Lesson 5 - Contents How to develop a multi-agent simulator Exercises
Lesson 5 - Contents How to develop a multi-agent simulator Exercises

3 Programming a simulator
Programming a simulator Agent based simulations The models simulate the simultaneous operations of multiple agents, in an attempt to re-create and predict the actions of complex phenomena. Agents can be pedestrians, animals, customers, internet users, etc… 3 3

4 Programming a simulator
Programming a simulator Agent based simulations State update According to set of behavioral rules Time t Time t + dt Characterized by a state (or states) (location, dead/alive, color, level of excitation) New state Time line T1 T2 dt 4 4

5 Programming a simulator
Programming a simulator Agent based simulations State update According to set of behavioral rules, including neighborhood Time t Time t + dt Characterized by a state (location, dead/alive, level of excitation) New state Time line T1 T2 5 5

6 Programming a simulator
Programming a simulator Programming an agent based simulator often goes in five steps Initialization Initial state; parameters; environment Time loop Processing each time steps Agents loop Processing each agents Update Updating agent i at time t Save data For further analysis Initialization Time loop end Agents loop end Update state Save data 6 6

7 Programming a simulator
Programming a simulator Step 1: Defining the initial state & parameters % Initial state t0=0; %begining of the time line dt=1 ; %time step T=100; %number of time steps %Initial state of the agents State1 = [ ]; State2 = [ ]; etc… 7 7

8 Programming a simulator
Programming a simulator Step 1: Defining the initial state & parameters % Initial state t0=0; %begining of the time line dt=1 ; %time step T=100; %number of time steps %Initial state of the agents State1 = zeros(1,50); State2 = rand(1,50); 8 8

9 Programming a simulator
Programming a simulator Step 2: Covering each time step % time loop For t=t0:dt:T What happen at each time step? - Update environment - Update agents end 9 9

10 Programming a simulator
Programming a simulator Step 3: Covering each agent % agent loop For i=1:length(States) What happen for each agent? end 10 10

11 Programming a simulator
Programming a simulator Step 4: Updating agent i at time t % update % Each agent has 60% chance to switch to state 1 randomValue=rand(); if (randomValue<0.6) State(i)=1; else State(i)=0; end Use sub-functions for better organization!! 11 11

12 Programming a simulator
Programming a simulator Step 4: Updating agent i at time t % update % Each agent has ‘proba’ chance to switch to state 1 randomValue=rand(); if (randomValue<proba) State(i)=1; else State(i)=0; end Define parameters in the first part!! 12 12

13 Programming a simulator
Programming a simulator Step 4: Updating agent i at time t % Initial state t0=0; %begining of the time line dt=1 ; %time step T=100; %number of time steps proba=0.6; %probability to switch state Define parameters in the first part!! 13 13

14 Programming a simulator
Programming a simulator Step 5: Final processing % Outputs and final processing propAlive = sum(states)/length(states); And return propAlive as the output of the simulation. 14 14

15 Programming a simulator
Running simulation >> p=simulation() p = 0.54 0.72

16 Programming a simulator
Programming a simulator Alternatively : a framework for the simulator % Running N simulations N=1000 %number of simulation for n=1:N p(n)=simulation(); end Use a framework to run N successive simulation and store the result each time. 16 16

17 Programming a simulator
Programming a simulator Alternatively : a framework for the simulator >> hist(p) 17 17

18 A simple dice game The rules:
A simple dice game The rules: Each player throws 2 dice and gain a number of points equal to the sum of the two dice. If the dice produce the same value, the score is double. 2 ; 5 : 7 Normal score 4 ; 4 : 8 x 2 = 16 Bonus score The players roll the dice N times, in turn. Each one cumulate his points. The player that has the best score after N rolls win.

19 A simple dice game The cheater:
A simple dice game The cheater: There is a cheater among the players. This cheater own some special dice that have a higher probability to produce the same value. In a multi-agents simulation, each player would be an agent characterized by his score, and his behaviour (cheater or fair player). We now develop this simulation… 19 19

20 Exercise 1 The behaviour of a fair player:
Exercise 1 The behaviour of a fair player: Write a function called fairRoll.m that returns the score of a fair player: Inputs : nothing Outputs: the score S Write the function in 3 steps: (1) simulate the rolling of dice n°1 and dice n°2, (2) calculate the sum, (3) check if the two values are the same and, in that event, double the sum to get the final score. Hints : The command rand()*6 returns a random value between 0 and 6 The function ceil(n) returns the nearest integer greater than or equal to n Combine them to get a random integer value between 1 and 6. 20 20

21 Exercise 2 The dice game with fair players
Exercise 2 The dice game with fair players Use the template file simulation.m to build the simulator Initialization: set variables N=100 agents, T=50 time steps, and a vector called score of length N, used to store the score of each player (initial values set to zero). In the update section, use the function fairRoll.m to update the score of player n Return the final vector score as an output. Run the simulation. What is the mean score of all players at the end of the game? Use the function max(score) to determine who is the winner. 21 21

22 Exercise 3 The behaviour of a cheater:
Exercise 3 The behaviour of a cheater: Write a function called cheatRoll.m that return the score of a cheater: Inputs : a variable called probaCheat representing the probability to have a successful cheat. Outputs: the final score S Hints Write your code in four steps : Determine the value of the first dice Pick a random number between 0 and 1, the cheat is successful if this number is lower then probaCheat Determine the value of the second dice according to step 2. Calculate and return the score S 22 22

23 Exercise 4 The dice game with one cheater:
Exercise 4 The dice game with one cheater: Go back to your simulator. In the initialization section, add a new state called behaviour with length(behaviour)=N , behaviour(i)=1 if player i is a cheater and behaviour(i)=0 if i is a fair player. Set player n°1 as a cheater and all the others as fair players. Modify the update section in order to call either fairRoll or cheatRoll, with respect to the behaviour of the player n . Set the value of probaCheat to 0.6 (ideally, set it as a parameter in the initialization section). Run the simulation. Who is the winner? What is the score of the cheater and the average score of the fair players? 23 23

24 Exercise 5 A framework for the simulator: Create a new script file.
Exercise 5 A framework for the simulator: Create a new script file. Use a for loop to repeat S times the simulation. At the end of each simulation, store the id of the winner (the cheater has its id=1). What is the winning rate of the cheater after S=100 games? Try to change the probability of successful cheating (probaCheat) to reduce the winning rate to 75%. 24 24

25 Exercise 6 (optional) Interactions and behavioural changes
Exercise 6 (optional) Interactions and behavioural changes In most of the case, the cheater does not cheat systematically, in order not to be too suspicious. Change the update section again, so that the cheater only use his special dice when another player has a better score than him. Call the functions fairRoll or cheatRoll according to the current score of the cheater. The behaviour vector should eventually be updated as well, in case of a change of strategy. What is the cheater’s new winning rate ? 25 25


Download ppt "Modelling and Simulating Social Systems with MATLAB"

Similar presentations


Ads by Google