Introduction to Computational Modeling of Social Systems RePast Tutorial II Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49, Room G.2, lcederman@ethz.ch Nils Weidmann, CIS Room E.3, weidmann@icr.gess.ethz.ch http://www.icr.ethz.ch/teaching/compmodels Lecture, December 14, 2004
Today’s agenda 2 • IPD: Experimental dimensions • EvolIPD model • Random numbers • How to build a model (2) • Scheduling • Homework C
Three crucial questions: 3 1. Variation : What are the actors’ characteristics? 2. Interaction : Who interacts with whom, when and where? 3. Selection : Which agents or strategies are retained, and which are destroyed? (see Axelrod and Cohen. 1999. Harnessing Complexity )
Experimental dimensions 4 • 2 strategy spaces: B, C • 6 interaction processes: RWR, 2DK, FRN, FRNE, 2DS, Tag • 3 adaptive processes: Imit, BMGA, 1FGA
“Soup-like” topology: RWR 5 In each time period, ATFT ALLC a player interacts ALLD ALLD with four other random players. TFT TFT ALLC
2D-Grid Topology: 2DK 6 ALLD ALLD ALLD The players are arranged on a fixed torus and interact TFT TFT ALLC with four neighbors in the von-Neumann neighborhood. TFT ALLC ATFT
Fixed Random Network: FRN 7 The players have four random neighbors ATFT TFT ALLC in a fixed random ATFT network. The relations do not have to be symmetric. ALLD ALLD TFT ALLC TFT
Adaptation through imitation 8 Imitation ATFT ALLC ALLD ALLD TFT TFT? ALLC Neighbors at t
Adaptation with BMGA Comparison error (prob. 0.1) 9 Genetic adaptation 6.0 Fixed spatial neighborhood 2.8 2.2 9.0 0.8
BMGA continued Copy error (prob. 0.04 per “bit”) 10 Genetic adaptation 6.0 Fixed spatial p =0; q =0 => p =1 ; q =0 neighborhood 2.8 6.0 9.0 0.8
Tutorial Sequence 11 December 7 SimpleIPD: strategy space Today EvolIPD: RWR December 21 GraphIPD: charts and GUI GridIPD: 2DK January 11 ExperIPD : batch runs and parameter sweeps
EvolIPD: flowchart 12 setup() buildModel() resetPlayers() interactions() adaptation() play() play() reportResults() remember() remember() step() addPayoff() addPayoff()
Markovian vs. asynchronous adaptation 13 Markovian t-1 t asynchronous
Going sequential 14 private void stepMarkovian() { // We carry out four sub-activities: // Reset the agents' statistics // Loop through the entire agent list for ( int i = 0; i < numPlayers; i++) { // Pick the agent final Player aPlayer = (Player) agentList.get(i); private void stepAsynchronous() { resetPlayer(aPlayer); // We carry out four sub-activities: } for ( int i = 0; i < numPlayers; i++) { // Pick an agent at random // Let them interact with their neighbors final Player aPlayer = (Player) agentList.get( for ( int i = 0; i < numPlayers; i++) { this .getNextIntFromTo(0, numPlayers - 1)); final Player aPlayer = (Player) agentList.get(i); interactions(aPlayer); // Reset the agent's statistics } resetPlayer(aPlayer); // FIRST STAGE OF DOUBLE BUFFERING! // Let it interact with its neighbors // Let all agents calculate their adapted type first interactions(aPlayer); for ( int i = 0; i < numPlayers; i++) { // Let it adapt final Player aPlayer = (Player) agentList.get(i); adaptation(aPlayer); adaptation(aPlayer); } // Let it update its new type updating(aPlayer); // SECOND STAGE OF DOUBLE BUFFERING! } // Second, once they know their new strategy, // let them update to the new type reportResults(); // Report some statistics for ( int i = 0; i < numPlayers; i++) { } final Player aPlayer = (Player) agentList.get(i); updating(aPlayer); } reportResults(); // Report some statistics }
How to work with random numbers 15 • RePast full-fledged random number generator: uchicago.src.sim.util.Random • Encapsulates the Colt library random number distributions: http://hoschek.home.cern.ch/hoschek/colt/ • Each distribution uses the same random number stream, to ease the repeatability of a simulation • Every distribution uses the MersenneTwister pseudo-random number generator
Pseudo-random numbers 16 • Computers normally cannot generate real random numbers • “Random number generators should not be chosen at random” - Knuth (1986) • A simple example (Cliff RNG): x 1 = 0.25850929940455103 x 2 = 0.28236111950289455 X 0 = 0.1 x 3 = 0.4568461655760814 x 4 = 0.3408562751932891 X n+1 = |100 ln(X n ) mod 1| x 5 = 0.6294370918024157 x 6 = 0.29293640856857195 140 x 7 = 0.7799729122847907 120 x 8 = 0.849608774153694 100 Frequency 80 x 9 = 0.29793011540822434 60 x 10 = 0.08963320319223556 40 20 x 11 = 0.2029456303939412 0 ... 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
“True” random numbers 17 • New service offered by the University of Geneva and the company id Quantique http://www.randomnumber.info/ • No (yet) integrated into RePast
Simple random numbers distribution 18 • Initialization: Automatically standard executed by Random.setSeed(seed); deviation SimpleModel Random.createUniform(); Random.createNormal(0.0, 1.0); • Usage: mean standard deviation int i = Random.uniform.nextIntFromTo(0, 10); double v1 = Random.normal.nextDouble(); double v2 = Random.normal.nextDouble(0.5, 0.3); mean standard deviation
Available distributions 19 • Beta • Normal (or Gaussian) • Binomial • Pareto • Chi-square • Poisson • Uniform • Empirical (user- defined probability • … Normal distribution function) • Gamma • Hyperbolic Beta • Logarithmic
Custom random number generation 20 • May be required if two independent random number streams are desirable • Bypass RePast’s Random and use the Colt library directly: import cern.jet.random.*; import cern.jet.random.engine.MersenneTwister; public class TwoStreamsModel extends SimModel { Normal normal; Uniform uniform; seeds public void buildModel() { super .buildModel(); MersenneTwister generator1 = new MersenneTwister(123); MersenneTwister generator2 = new MersenneTwister(321); uniform = new Uniform(generator1); normal = new Normal(0.0, 1.0, generator2); } public void step() { int i = uniform.nextIntFromTo(0, 10); double value = normal.nextDouble(); } }
How to build a model (2) 21 • If more flexibility is desired, one can extend SimModelImpl instead of SimpleModel • Differences to SimpleModel – No buildModel() , step() , ... methods – No agentList , schedule , params , ... fields – Most importantly: no default scheduling • Required methods: public void setup() public String[] getInitParam() public void begin() public Schedule getSchedule() public String getName()
SimModelImpl 22 import uchicago.src.sim.engine.Schedule; import uchicago.src.sim.engine.SimInit; import uchicago.src.sim.engine.SimModelImpl; public class MyModelImpl extends SimModelImpl { public static final int TFT = 1; public static final int ALLD = 3; private int a1Strategy = TFT; private int a2Strategy = ALLD; private Schedule schedule; private ArrayList agentList; public void setup() { a1Strategy = TFT; a2Strategy = ALLD; schedule = new Schedule(); agentList = new ArrayList(); } public String[] getInitParam() { return new String[]{"A1Strategy"}; }
SimModelImpl (cont.) 23 public String getName() { return "Example Model"; } public void begin() { Agent a1 = new Agent(a1Strategy); introspection Agent a2 = new Agent(a2Strategy); agentList.add(a1); agentList.add(a2); schedule.scheduleActionBeginning(1, this , "step"); } public void step() { for (Iterator iterator = agentList.iterator(); iterator.hasNext();) { Agent agent = (Agent) iterator.next(); agent.play(); } }
SimModelImpl (cont.) 24 public String[] getInitParam() { return new String[]{"A1Strategy"}; } public int getA1Strategy() { return a1Strategy; } public void setA1Strategy( int strategy) { this .a1Strategy = strategy; } public static void main(String[] args) { SimInit init = new SimInit(); SimModelImpl model = new MyModelImpl(); init.loadModel(model, null , false ); }
How to use a schedule 25 • Schedule object is responsible for all the state changes within a Repast simulation schedule.scheduleActionBeginning(1, new DoIt()); schedule.scheduleActionBeginning(1, new DoSomething()); schedule.scheduleActionAtInterval(3, new ReDo()); tick 1: DoIt, DoSomething tick 2: DoSomething, DoIt tick 3: ReDo, DoSomething, DoIt tick 4: DoSomething, DoIt tick 5: DoIt, DoSomething tick 6: DoSomething, ReDo, DoIt
Different types of actions 26 • Inner class class MyAction extends BasicAction { public void execute() { doSomething(); } } schedule.scheduleActionAt(100, new MyAction()); • Anonymous inner class schedule.scheduleActionAt(100, new BasicAction(){ public void execute() { doSomething(); } ); • Introspection schedule.scheduleActionAt(100, this , "doSomething");
Recommend
More recommend