A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)
Actually, some of the lines of code that we have placed in the 'spreadMoney' method of the CarryDropSpace object will be useful for several reasons; thus we will pull them out into their own method. (Note also some variable name changes- this is just for style and limited improvements in clarity. As an added technical note, we check for a 'null' condition, but one should never occur because we initialize the whole grid to include values):
// CarryDropModel
package demo;
import uchicago.src.sim.engine.Schedule;
import uchicago.src.sim.engine.SimInit;
import uchicago.src.sim.engine.SimModelImpl;
public class CarryDropModel extends SimModelImpl {
// Default Values
private static final int NUMAGENTS = 100;
private static final int WORLDXSIZE = 40;
private static final int WORLDYSIZE = 40;
private static final int TOTALMONEY = 1000;
private int numAgents = NUMAGENTS;
private int worldXSize = WORLDXSIZE;
private int worldYSize = WORLDYSIZE;
private int money = TOTALMONEY;
private Schedule schedule;
private CarryDropSpace cdSpace;
public String getName(){
return "Carry And Drop";
}
public void setup(){
System.out.println("Running setup");
cdSpace = null;
}
public void begin(){
buildModel();
buildSchedule();
buildDisplay();
}
public void buildModel(){
System.out.println("Running BuildModel");
cdSpace = new CarryDropSpace(worldXSize, worldYSize);
cdSpace.spreadMoney(money);
}
public void buildSchedule(){
System.out.println("Running BuildSchedule");
}
public void buildDisplay(){
System.out.println("Running BuildDisplay");
}
public Schedule getSchedule(){
return schedule;
}
public String[] getInitParam(){
String[] initParams = { "NumAgents" , "WorldXSize", "WorldYSize", "Money" };
return initParams;
}
public int getNumAgents(){
return numAgents;
}
public void setNumAgents(int na){
numAgents = na;
}
public int getWorldXSize(){
return worldXSize;
}
public void setWorldXSize(int wxs){
worldXSize = wxs;
}
public int getWorldYSize(){
return worldYSize;
}
public void setWorldYSize(int wys){
worldYSize = wys;
}
public int getMoney() {
return money;
}
public void setMoney(int i) {
money = i;
}
public static void main(String[] args) {
SimInit init = new SimInit();
CarryDropModel model = new CarryDropModel();
init.loadModel(model, "", false);
}
}
// CarryDropAgent
package demo;
public class CarryDropAgent {
}
// CarryDropSpace
package demo;
import uchicago.src.sim.space.Object2DGrid;
public class CarryDropSpace {
private Object2DGrid moneySpace;
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));
}
}
}
public void spreadMoney(int money){
// Randomly place money in moneySpace
for(int i = 0; i < money; i++){
// Choose coordinates
int x = (int)(Math.random()*(moneySpace.getSizeX()));
int y = (int)(Math.random()*(moneySpace.getSizeY()));
// Get the value of the object at those coordinates
int currentValue = getMoneyAt(x, y);
// Replace the Integer object with another one with the new value
moneySpace.putObjectAt(x,y,new Integer(currentValue + 1));
}
}
public int getMoneyAt(int x, int y){
int i;
if(moneySpace.getObjectAt(x,y)!= null){
i = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
}
else{
i = 0;
}
return i;
}
}
Previous: Initializing the Space Object
Go to Table of Contents
A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)