Presentation is loading. Please wait.

Presentation is loading. Please wait.

The CarryDrop Model (Steps 4-9) : A RePast Tutorial by John Murphy by Junjie Sun 8/2/2004 Department of Economics Iowa State University.

Similar presentations


Presentation on theme: "The CarryDrop Model (Steps 4-9) : A RePast Tutorial by John Murphy by Junjie Sun 8/2/2004 Department of Economics Iowa State University."— Presentation transcript:

1 The CarryDrop Model (Steps 4-9) : A RePast Tutorial by John Murphy by Junjie Sun 8/2/2004 Department of Economics Iowa State University

2 Review of the Story Agents move around a grid spaceAgents move around a grid space Move in one of the eight directionsMove in one of the eight directions Each cell may contain money; agents pick it up and carry it with themEach cell may contain money; agents pick it up and carry it with them Collision compensation; then the agent chooses a new directionCollision compensation; then the agent chooses a new direction Agent has lifespan; when agent dies, its wealth is spread randomly onto the grid, and the dead agent is replaced with a newly born in a diff. location with a diff. lifespanAgent has lifespan; when agent dies, its wealth is spread randomly onto the grid, and the dead agent is replaced with a newly born in a diff. location with a diff. lifespan

3 Review of Model Structure CarryDropModel - a class that instantiates the SimModel objectCarryDropModel - a class that instantiates the SimModel object CarryDropAgent - a class specifying the agentsCarryDropAgent - a class specifying the agents CarryDropSpace - a class describing the spaceCarryDropSpace - a class describing the space

4 What ’ s been done in Step 1-3 CarryDropModelCarryDropModel –Variables: schedule, numAgents –Methods: setup(), begin(), buildModel(), buildSchedule(), buildDisplay(), getName(), getSchedule(), getInitParam(), getNumAgents(), setNumAgents(), main() CarryDropAgentCarryDropAgent CarryDropSpaceCarryDropSpace

5 Add User-Settable Parameters (Step 4-1) Number of Agents (NumAgents)Number of Agents (NumAgents) Size of World X (WorldXSize)Size of World X (WorldXSize) Size of World Y (WorldYSize)Size of World Y (WorldYSize)

6 Add User-Settable Parameters (Step 4-2) Add worldXSize & worldYSize as class variablesAdd worldXSize & worldYSize as class variables private int worldXSize; private int worldYSize; Add WorldXSize & WorldYSize to the getInitParam functionAdd WorldXSize & WorldYSize to the getInitParam function String[ ] initParams = { "NumAgents", "WorldXSize", "WorldYSize"}; Add the get and set methods for these variables, for example,Add the get and set methods for these variables, for example, public int getWorldXSize(){ return worldXSize; }

7 Compiling and Running the Basic Model (Step 5-1) Create a new project in IDE and add in three.java files, namely, CarryDropModel.java CarryDropAgent.java CarryDropSpace.javaCreate a new project in IDE and add in three.java files, namely, CarryDropModel.java CarryDropAgent.java CarryDropSpace.java The model won ’ t do anything, but it should compile and display part of the RePast GUIThe model won ’ t do anything, but it should compile and display part of the RePast GUI

8 Codes to be Added in main Method (Step 5-2) Creates a new obj. of type SimInit:Creates a new obj. of type SimInit: SimInit init = new SimInit(); Creates a new object of type CarryDropModel:Creates a new object of type CarryDropModel: CarryDropModel model = new CarryDropModel(); Loads the model using the loadModel method of the Init obj.:Loads the model using the loadModel method of the Init obj.: init.loadModel(model, "", false);

9 Default Values for User- Settable Parameters (Step 6) private static final int NUMAGENTS = 100;private static final int NUMAGENTS = 100; private static final int WORLDXSIZE = 40;private static final int WORLDXSIZE = 40; private static final int WORLDYSIZE = 40;private static final int WORLDYSIZE = 40; private int numAgents = NUMAGENTS;private int numAgents = NUMAGENTS; private int worldXSize = WORLDXSIZE;private int worldXSize = WORLDXSIZE; private int worldYSize = WORLDYSIZE;private int worldYSize = WORLDYSIZE;

10 Alerts in Subroutines (Step 7) System.out.println("Running setup");System.out.println("Running setup"); System.out.println("Running BuildModel");System.out.println("Running BuildModel"); System.out.println("Running BuildSchedule");System.out.println("Running BuildSchedule"); System.out.println("Running BuildDisplay");System.out.println("Running BuildDisplay");

11 The Space Object (Step 8-1) Define the variable for space object using RePast ’ s Object2DGrid:Define the variable for space object using RePast ’ s Object2DGrid: private Object2DGrid moneySpace; private Object2DGrid moneySpace; Fill moneySpace with Integer objects:Fill moneySpace with Integer objects: public CarryDropSpace(int xSize, int ySize){ public CarryDropSpace(int xSize, int ySize){ moneySpace = new Object2DGrid(xSize, ySize); moneySpace = new Object2DGrid(xSize, ySize); for(int i = 0; i < xSize; i++){ for(int i = 0; i < xSize; i++){ for(int j = 0; j < ySize; j++){ for(int j = 0; j < ySize; j++){ moneySpace.putObjectAt(i,j,new Integer(0)); } } } } moneySpace.putObjectAt(i,j,new Integer(0)); } } } }

12 Q: How can you know the args in the methods / constructors? A: See RePast/Java API (Step 8-2) Object2DGrid(int xSize, int ySize)Object2DGrid(int xSize, int ySize) Constructs a grid with the specified size. Integer(int value)Integer(int value) Constructs a newly allocated Integer object that represents the primitive int argument. putObjectAt(int x, int y, Object object)putObjectAt(int x, int y, Object object) Puts the specified object at (x,y)

13 Need another parameter, the amount of money (Step 8-3) In CarryDropModel.java, add:In CarryDropModel.java, add: private static final int TOTALMONEY = 1000; private static final int TOTALMONEY = 1000; private int money = TOTALMONEY; private int money = TOTALMONEY; String[ ] initParams = { "NumAgents", "WorldXSize", "WorldYSize", "Money" }; String[ ] initParams = { "NumAgents", "WorldXSize", "WorldYSize", "Money" }; public int getMoney() { return money; } public int getMoney() { return money; } public void setMoney(int i) { money = i; } public void setMoney(int i) { money = i; }

14 I ntegrate the Space Object into the Model (Step 9-1) Allocate a variable for the space object:Allocate a variable for the space object: private CarryDropSpace cdSpace; Create the space object in buildModel():Create the space object in buildModel(): cdSpace = new CarryDropSpace (worldXSize, worldYSize); Destroy the space object in setup():Destroy the space object in setup(): cdSpace = null;

15 Some Clarifications (Step 9-2) The space object is actually destroyed (set to null) in setup before it is created in the buildModelThe space object is actually destroyed (set to null) in setup before it is created in the buildModel Reason is that in each simulation run, the object needs to be reset to nothing before it gets ready to be builtReason is that in each simulation run, the object needs to be reset to nothing before it gets ready to be built

16 Overview : What ’ s been done in Step 1-9 CarryDropModelCarryDropModel Variables: schedule, cdSpace, numAgents, worldXSize, worldYSize, money Methods: setup(), begin(), buildModel(), buildSchedule(), buildDisplay(), getName(), getSchedule(), getInitParam(), getNumAgents(), setNumAgents(), getWorldXSize(), setWorldXSize(), getWorldYSize(), setWorldYSize(), getMoney(), setMoney(), main() CarryDropAgentCarryDropAgent CarryDropSpaceCarryDropSpace Variables: moneySpace Variables: moneySpace Constructors: CarryDropSpace() Constructors: CarryDropSpace()


Download ppt "The CarryDrop Model (Steps 4-9) : A RePast Tutorial by John Murphy by Junjie Sun 8/2/2004 Department of Economics Iowa State University."

Similar presentations


Ads by Google