L – Modelling and Simulating Social Systems with MATLAB Lesson 5 – Introduction to agent-based simulations A. Johansson & W. Yu © ETH Zürich |
A. Johansson & W. Yu / Send us an abstract of your project. Don't re-invent the wheel but try to build upon existing models. Start simple and extend functionality. Look at project reports from previous semesters. Projects 2
A. Johansson & W. Yu / 3 Lesson 5 - Contents Swarm intelligence Human cooperation and coordination How to develop a multi-agent simulator
A. Johansson & W. Yu / Swarm Intelligence 4 Decentralized control Interaction and learning Self-organization
A. Johansson & W. Yu / Boids (Reynolds,1986) 5
A. Johansson & W. Yu / More information: 6 Separation: steer to avoid crowding local flockmates Alignment: steer towards the average heading of local flockmates Cohesion: steer to move toward the average position of local flockmates
A. Johansson & W. Yu / Human Cooperation Prisoner’s dilemma 7 15,1520,0 0,2019,19
A. Johansson & W. Yu / The Evolution of Cooperation (Axelrod,1984) Tit for tat Shadow of the future 8
A. Johansson & W. Yu / Coordination 1,10,0 1,1 9
A. Johansson & W. Yu / Learning Imitation Reinforcement Best reply 10
A. Johansson & W. Yu / From Factors to Actors (Macy,2002) Agents are autonomous Agents are interdependent Agents follow simple rules Agents are adaptive and backward-looking 11
A. Johansson & W. Yu / 12 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…
A. Johansson & W. Yu / 13 Programming a simulator Agent based simulations Time line Characterized by a state (or states) (location, dead/alive, color, level of excitation) Time t Time t + dt State update According to set of behavioral rules New state T1 T2 dt
A. Johansson & W. Yu / 14 Programming a simulator Agent based simulations Characterized by a state (location, dead/alive, level of excitation) Time t Time t + dt State update According to set of behavioral rules, including neighborhood New state Time line T1 T2
A. Johansson & W. Yu / 15 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 Save data Agents loop end Update state
A. Johansson & W. Yu / 16 Programming a simulator Step 1: Defining the initial state & parameters % Initial state t 0 =0;%begining of the time line dt=1 ;%time step T=100; %number of time steps %Initial state of the agents State1 = [ ]; State2 = [ ]; etc…
A. Johansson & W. Yu / 17 Programming a simulator Step 1: Defining the initial state & parameters % Initial state t 0 =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);
A. Johansson & W. Yu / 18 Programming a simulator Step 2: Covering each time step % time loop For t=t 0 :dt:T What happen at each time step? - Update environment - Update agents end
A. Johansson & W. Yu / 19 Programming a simulator Step 3: Covering each agent % agent loop For i=1:length(States) What happen for each agent? end
A. Johansson & W. Yu / 20 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!!
A. Johansson & W. Yu / 21 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!!
A. Johansson & W. Yu / 22 Programming a simulator Step 4: Updating agent i at time t % Initial state t 0 =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!!
A. Johansson & W. Yu / 23 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.
A. Johansson & W. Yu / >> p=simulation() p = 0.54 >> p=simulation() p = 0.72 Programming a simulator Running simulation
A. Johansson & W. Yu / 25 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.
A. Johansson & W. Yu / 26 Programming a simulator Alternatively : a framework for the simulator >> hist(p)