1 / 39

Repast Tutorial

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

emily
Download Presentation

Repast Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Repast Tutorial Lars-Erik Cederman Department of Government, Harvard EITM Workshop, July 18, 2002

  2. Outline • A few words about Java • Repast: A Java-based toolkit for ABM • The IPD tutorial

  3. 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

  4. Some Java Buzzwords • Robust • Portable • Object-oriented

  5. The Java solution to platform- independence Mac 010111001 Interpreter myProgram.java ------ ----- -- -------- Interpreter 010111001 Windows myProgram.class ------ ----- -- -------- Compiler

  6. Simple data types int a = 12; double d = 3.14; boolean b = false; char c = 'X';

  7. 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: +, -, *, /, ++, --

  8. 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: +, -, *, /, ++, --

  9. 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 ==, !=, >, <, >=, <=)

  10. 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

  11. 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(); }

  12. Output: * ** *** **** *****

  13. Interface vs. implementation User only has to be familiar with the interface of an object, not its implementation Objects hide their functions and data

  14. Object Oriented Programming methods instance variables messages break() speed = 45.7; gear = 3; changeGears(g) a program an object

  15. 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

  16. 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

  17. 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()); } }

  18. 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); } }

  19. 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; } }

  20. 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

  21. 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

  22. 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

  23. Why Repast? • Alternatives: Swarm, Ascape, StarLogo... • Wish list: • user-friendliness • flexibility • performance • cumulation • support

  24. 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

  25. 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

  26. Prisoner’s Dilemma Game Column: C D C 3,3 0,5 Row: D 5,0 1,1

  27. 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

  28. The Four Strategies

  29. 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

  30. Payoffs for 4 x 4 Strategies

  31. 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)

  32. Experimental dimensions • 2 strategy spaces: B, C • 6 interaction processes: RWR, 2DK, FRN, FRNE, 2DS, Tag • 3 adaptive processes: Imit, BMGA, 1FGA tutorial

  33. “Soup-like” topology: RWR In each time period, a player interacts with four other random players. ATFT ALLC ALLD ALLD TFT TFT ALLC

  34. 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.

  35. 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.

  36. Adaptation through imitation Imitation ATFT ALLC ALLD ALLD TFT TFT? ALLC Neighbors at t

  37. 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

  38. 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

  39. Separating the GUI and batch modes Model extends SimpleModel GraphIPD,GridIPD ExperIPD ModelGUI extends Model ModelBatch extends Model

More Related