390 likes | 572 Views
Repast Tutorial. Lars-Erik Cederman Department of Government, Harvard EITM Workshop, July 18, 2002. Outline. A few words about Java Repast: A Java-based toolkit for ABM The IPD tutorial. Java. Conceived by Sun in the early 1990s
E N D
Repast Tutorial Lars-Erik Cederman Department of Government, Harvard EITM Workshop, July 18, 2002
Outline • A few words about Java • Repast: A Java-based toolkit for ABM • The IPD tutorial
Java • Conceived by Sun in the early 1990s • Became the new standard for the web thanks to platform-independence Java object model syntax C++ C syntax
Some Java Buzzwords • Robust • Portable • Object-oriented
The Java solution to platform- independence Mac 010111001 Interpreter myProgram.java ------ ----- -- -------- Interpreter 010111001 Windows myProgram.class ------ ----- -- -------- Compiler
Simple data types int a = 12; double d = 3.14; boolean b = false; char c = 'X';
Simple data types: integers int a = 12; int b = -3; a = a/4 + (b*2 - 1)*3; a++; // same as a = a + 1; b--; // same as b = b - 1; Operators: +, -, *, /, ++, --
Simple data types: doubles double a = 3.14; double b = 12.7; a = a / 2.0 + (b * 2.2 - 1.9)*3.2; a++; // same as a = a + 1.0; b--; // same as b = b - 1.0; Operators: +, -, *, /, ++, --
Simple data types: booleans boolean a = false; boolean b = true; a = (a && b) || true; a = !b; b = (3 > 2); a = (2.0 == 2.0); Operators: !, &&, ||, (comparison ==, !=, >, <, >=, <=)
if (a > 2) { a = 0; } if (!x) { a = 0; } else { a = -1; b = 2; } if (!x && (a == b)) { a = 0; } else if (c > d) { a = -1; b = 2; } If-statements condition statement
For-loops iteration initialization condition body of loop int i; for (i = 0; i < 5; i++) { System.out.println(i); } for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); } System.out.println(); }
Output: * ** *** **** *****
Interface vs. implementation User only has to be familiar with the interface of an object, not its implementation Objects hide their functions and data
Object Oriented Programming methods instance variables messages break() speed = 45.7; gear = 3; changeGears(g) a program an object
Objects: Classes and Instances Classes are "object factories". Objects are created as instances by allocating memory for them. Handles are used to keep track of the objects. Class Instances Car new Car() car1 car2 Handles car3
ElevatorProject • Two tenants live in an apartment complex with 1 occupying the first floor and 2 the second • The tenants move in and out of the building using the elevator • Calculate the expected waiting time for both tenants 2 1 0 Classes Objects Tenant Elevator
Elevator Project: Code public class Model { public static void main(String[] args) { int n = 1000; Tenant tenant1 = new Tenant(1); Tenant tenant2 = new Tenant(2); Elevator elevator = new Elevator(); for (int i=0; i<n; i++) { if (Math.random()<0.5) tenant1.move(elevator); else tenant2.move(elevator); } System.out.print("Waiting time = " + elevator.averageWaitingTime()); } }
Tenant class public class Tenant { int home,floor; public Tenant(int f) { home = f; if (Math.random()< 0.5) floor = 0; else floor = f; } public boolean atHome() { return floor == home; } public void move(Elevator elevator) { elevator.callToFloor(floor); if (atHome()) elevator.takeToFloor(this,0); else elevator.takeToFloor(this,home); } }
Elevator class public class Elevator { int floor, waitingTime, numTrips; public Elevator() { if (Math.random()< 0.5) floor = 0; else floor = (int)(2.0*Math.random())+1; waitingTime = 0; numTrips = 0; } public void callToFloor(int f) { waitingTime = waitingTime + Math.abs(f-floor); numTrips++; floor = f; } public void takeToFloor(Tenant tenant,int f) { floor = f; tenant.floor = f; } public double averageWaitingTime() { return (double)waitingTime/(double)numTrips; } }
Elevator class public class ModifiedElevator extends Elevator { public ModifiedElevator() { floor = 0; waitingTime = 0; numTrips = 0; } public void takeToFloor(Tenant tenant,int f) { super.takeToFloor(tenant,f); floor = 0; } } inheritance
Readings on Java Schildt, Herbert. 2001. Java2: A Beginner’s Guide. Osborne McGraw Hill. MIT Bookstore and Quantum Books (on Broadway by Kendall Square) Eckel, Bruce. 1998. Thinking in Java. Upper Saddle River, NJ: Prentice Hall. See also: http://www.mindview.net See also Sun’s Java Tutorial: http://java.sun.com/docs/books/tutorial/index.html
Modeling in Repast • “REcursive Porous Agent Simulation Toolkit” • Repast is an open-source software framework for creating agent-based simulations using the Java programming language • Developed by the Social Science Research Computing at the University of Chicago since January 2000: http://repast.sourceforge.net • Modeled on Swarm but easier to use and better documented
Why Repast? • Alternatives: Swarm, Ascape, StarLogo... • Wish list: • user-friendliness • flexibility • performance • cumulation • support
What does Repast offer? • Like Swarm it provides basic simulation tools: • Engine • Utilities: interaction spaces, random numbers • Graphical user interface including control panels, graphs, 2D-displays, probes and more • Routines for replication, data collection and some analysis • Sample models
Iterated Prisoner’s Dilemma • Cohen, Riolo, and Axelrod. 1999. “The Emergence of Social Organization in the Prisoner's Dilemma” (SFI Working Paper 99-01-002) http://www.santafe.edu/sfi/publications/99wplist.html • In The Evolution of Cooperation, Robert Axelrod (1984) created a computer tournament of IPD • cooperation sometimes emerges • Tit For Tat a particularly effective strategy
Prisoner’s Dilemma Game Column: C D C 3,3 0,5 Row: D 5,0 1,1
One-Step Memory Strategies Strategy = (i, p, q) i = prob. of cooperating at t = 0 p = prob. of cooperating if opponent cooperated q = prob. of cooperating if opponent defected C p Memory: C D q C D D t t-1
D D D D D D TFT meets ALLD Cumulated Payoff p=1; q=0 0 + 1 + 1 + 1 = 3 i=1 Row (TFT) C D C Column (ALLD) D i=0 1 5 + 1 + 1 + = 8 p=0; q=0 0 1 2 3 4 t
Three crucial questions: 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 • 2 strategy spaces: B, C • 6 interaction processes: RWR, 2DK, FRN, FRNE, 2DS, Tag • 3 adaptive processes: Imit, BMGA, 1FGA tutorial
“Soup-like” topology: RWR In each time period, a player interacts with four other random players. ATFT ALLC ALLD ALLD TFT TFT ALLC
ALLD ALLD ALLD TFT TFT ALLC TFT ALLC ATFT 2D-Grid Topology: 2DK The players are arranged on a fixed torus and interact with four neighbors in the von-Neumann neighborhood.
ATFT TFT ALLC ATFT ALLD ALLD TFT ALLC TFT Fixed Random Network: FRN The players have four random neighbors in a fixed random network. The relations do not have to be symmetric.
Adaptation through imitation Imitation ATFT ALLC ALLD ALLD TFT TFT? ALLC Neighbors at t
Tutorial Sequence Part 1. SimpleIPD: strategy space Part 2. EvolIPD: RWR Part 3.GraphIPD: charts and GUI Part 4.GridIPD: 2DK Part 5.ExperIPD: batch runs and parameter sweeps
Working in batch mode Batch Model Parameter File Data File Replication Runs Statistics Package • Batch mode allows the user to rerun the model without using the GUI
Separating the GUI and batch modes Model extends SimpleModel GraphIPD,GridIPD ExperIPD ModelGUI extends Model ModelBatch extends Model