Modelling and Simulating Social Systems with MATLAB

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Bison Management Suppose you take over the management of a certain Bison population. The population dynamics are similar to those of the population we.
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
VK Dice By: Kenny Gutierrez, Vyvy Pham Mentors: Sarah Eichhorn, Robert Campbell.
Controlled Assessment
Matlab tutorial course Exercises 4:. Exercises – for loops Download the scripts loops_example.m and if_else_example.m, run the code Download the function,
PROBABILITY  A fair six-sided die is rolled. What is the probability that the result is even?
Horse race © Horse race: rules 1.Each player chooses a horse and puts it into a stall. Write your name next to the.
Minority Games A Complex Systems Project. Going to a concert… But which night to pick? Friday or Saturday? You want to go on the night with the least.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Probability And Expected Value ————————————
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
Bell Work A card is drawn at random from the cards shown and not replaced. Then, a second card is drawn at random. Find each probability. 1. P(two even.
Check it out! : Simple Random Sampling. Players of a dice game roll five dice and earn points according to the combinations of numbers they roll.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
Chapter 9: Simulation Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Scratch Internet- Open Chrome hit “Create” 1.
MOM! Phineas and Ferb are … Aims:
L – Modelling and Simulating Social Systems with MATLAB Lesson 5 – Introduction to agent-based simulations A. Johansson & W. Yu ©
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Statistics 11 Understanding Randomness. Example If you had a coin from someone, that they said ended up heads more often than tails, how would you test.
World of Wokcraft The very best in Single pan cooking themed fantasy gaming!
Objective of the lesson Use Blockly to make a dice for Snakes and Ladders All of you will: – Make an image which displays when you press a button Most.
Modelling and Simulating Social Systems with MATLAB
L – Modeling and Simulating Social Systems with MATLAB
Modelling and Simulating Social Systems with MATLAB
Determining the theoretical probability of an event
Number guessing game Pick a random number between 1 and 10
L – Modeling and Simulating Social Systems with MATLAB
Student Activity 1: Fair trials with two dice
Object of the game Yahtzee Large straight Small straight Full house
Chapter 5 - Functions Outline 5.1 Introduction
CS005 Introduction to Programming
Learning to Program in Python
© Akhilesh Bajaj, All rights reserved.
Probability And Expected Value ————————————
Lesson Objectives Aims You should be able to:
Truth tables: Ways to organize results of Boolean expressions.
Introduction to TouchDevelop
Truth tables: Ways to organize results of Boolean expressions.
Chapter 3.1 Probability Students will learn several ways to model situations involving probability, such as tree diagrams and area models. They will.
Writing Functions( ) (Part 4)
Matthew Renner, Trish Beeksma, Patch Kenny
Object of the game Yahtzee Large straight Small straight Full house
Probability And Expected Value ————————————
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Console.WriteLine(“Good luck!”);
Truth tables: Ways to organize results of Boolean expressions.
‘If’ statements, relational operators, and logical operators
Probability in Baseball
Pseudo-Code Conditional Branches
Drift and Sounder* Analytics
Object of the game Yahtzee Large straight Small straight Full house
Matlab tutorial course
Object of the game Yahtzee Large straight Small straight Full house
Programming Fundamental
Presentation transcript:

Modelling and Simulating Social Systems with MATLAB 11.06.2018 Modelling and Simulating Social Systems with MATLAB Lesson 5 – Agent-based simulations A. Johansson & M. Moussaid © ETH Zürich |

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

Programming a simulator 11.06.2018 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

Programming a simulator 11.06.2018 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

Programming a simulator 11.06.2018 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

Programming a simulator 11.06.2018 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

Programming a simulator 11.06.2018 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 = [0 0 0 0]; State2 = [1 1 1 1]; etc… 7 7

Programming a simulator 11.06.2018 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

Programming a simulator 11.06.2018 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

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

Programming a simulator 11.06.2018 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

Programming a simulator 11.06.2018 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

Programming a simulator 11.06.2018 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

Programming a simulator 11.06.2018 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

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

Programming a simulator 11.06.2018 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

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

A simple dice game The rules: 11.06.2018 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.

A simple dice game The cheater: 11.06.2018 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

Exercise 1 The behaviour of a fair player: 11.06.2018 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

Exercise 2 The dice game with fair players 11.06.2018 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

Exercise 3 The behaviour of a cheater: 11.06.2018 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

Exercise 4 The dice game with one cheater: 11.06.2018 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

Exercise 5 A framework for the simulator: Create a new script file. 11.06.2018 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

Exercise 6 (optional) Interactions and behavioural changes 11.06.2018 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