160 likes | 175 Views
This tutorial provides a step-by-step guide on how to implement the CarryDrop Model using the Repast simulation toolkit. It covers topics such as agent movement, collision compensation, lifespan, and user-settable parameters. The tutorial also includes code snippets and explanations to help users understand and implement the model.
E N D
The CarryDrop Model (Steps 4-9) : A RePast Tutorial by John Murphy by Junjie Sun 8/2/2004 Department of Economics Iowa State University
Review of the Story • Agents move around a grid space • Move in one of the eight directions • Each cell may contain money; agents pick it up and carry it with them • Collision 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. lifespan
Review of Model Structure • CarryDropModel - a class that instantiates the SimModel object • CarryDropAgent - a class specifying the agents • CarryDropSpace - a class describing the space
What’s been done in Step 1-3 • CarryDropModel • Variables: schedule,numAgents • Methods: setup(), begin(), buildModel(), buildSchedule(), buildDisplay(), getName(), getSchedule(), getInitParam(), getNumAgents(), setNumAgents(), main() • CarryDropAgent • CarryDropSpace
Add User-Settable Parameters (Step 4-1) • Number of Agents (NumAgents) • Size of World X (WorldXSize) • Size of World Y (WorldYSize)
Add User-Settable Parameters (Step 4-2) • Add worldXSize & worldYSize as class variables private int worldXSize; private int worldYSize; • Add WorldXSize & WorldYSize to the getInitParam function String[ ] initParams = { "NumAgents" , "WorldXSize", "WorldYSize"}; • Add the get and set methods for these variables, for example, public int getWorldXSize(){ return worldXSize; }
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.java • The model won’t do anything, but it should compile and display part of the RePast GUI
Codes to be Added in main Method (Step 5-2) • Creates a new obj. of type SimInit: SimInit init = new SimInit(); • Creates a new object of type CarryDropModel: CarryDropModel model = new CarryDropModel(); • Loads the model using the loadModel method of the Init obj.: init.loadModel(model, "", false);
Default Values for User-Settable Parameters (Step 6) • private static final int NUMAGENTS = 100; • private static final int WORLDXSIZE = 40; • private static final int WORLDYSIZE = 40; • private int numAgents = NUMAGENTS; • private int worldXSize = WORLDXSIZE; • private int worldYSize = WORLDYSIZE;
Alerts in Subroutines (Step 7) • System.out.println("Running setup"); • System.out.println("Running BuildModel"); • System.out.println("Running BuildSchedule"); • System.out.println("Running BuildDisplay");
The Space Object (Step 8-1) • Define the variable for space object using RePast’s Object2DGrid: private Object2DGrid moneySpace; • Fill moneySpace with Integer objects: public CarryDropSpace(int xSize, int ySize){ moneySpace = new Object2DGrid(xSize, ySize); for(int i = 0; i < xSize; i++){ for(int j = 0; j < ySize; j++){ moneySpace.putObjectAt(i,j,new Integer(0)); } } } }
Q: How can you know the args in the methods / constructors? A: See RePast/Java API (Step 8-2) • Object2DGrid(int xSize, int ySize) Constructs a grid with the specified size. • Integer(int value) Constructs a newly allocated Integer object that represents the primitive int argument. • putObjectAt(int x, int y, Object object) Puts the specified object at (x,y)
Need another parameter, the amount of money (Step 8-3) • In CarryDropModel.java, add: private static final int TOTALMONEY = 1000; private int money = TOTALMONEY; String[ ] initParams = { "NumAgents" , "WorldXSize", "WorldYSize", "Money" }; public int getMoney() { return money; } public void setMoney(int i) { money = i; }
Integrate the Space Object into the Model (Step 9-1) • Allocate a variable for the space object: private CarryDropSpace cdSpace; • Create the space object in buildModel(): cdSpace = new CarryDropSpace (worldXSize, worldYSize); • Destroy the space object in setup(): cdSpace = null;
Some Clarifications (Step 9-2) • The 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 built
Overview : What’s been done in Step 1-9 • CarryDropModel 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() • CarryDropAgent • CarryDropSpace Variables: moneySpace Constructors: CarryDropSpace()