A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)

The Space Object

At this point we need to step outside the 'model object and begin to consider the other two objects: CarryDropSpace and CarryDropAgent.

First we will consider the Space object. The space object will be a single object that will contain a pre-packaged RePast container that acts like a space; for this model we will use RePast's Object2DGrid object.

First we just define the variables for the grid space object. The Object2DGrid is an object provided by RePast that will be contained in our 'Space' object; some of the functions RePast provides we will use, but we will layer others on top of them:

CarryDropSpace

// CarryDropSpace
package demo;

import uchicago.src.sim.space.Object2DGrid;


public class CarryDropSpace {
private Object2DGrid moneySpace;

}

In this case, it is not enough to just create the space object. The first thing we need to do is fill it with objects. The objects that are stored are Integer objects (note that an Integer is not an integer- an integer (with a small i) is a data type, while an Integer (with a capital I) is a Java object. The value of the Integer object will be the amount of money found 'on the ground' at that point in the landscape.

We will initialize the space object by filling each cell with an Integer object, with the initial value of zero:

// 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));
      }
    }
  }
}

I know that the constructor for an Object2DGrid object can take two integers as arguments, corresponding to x- and y- dimensions. I know this from the Repast API- look in the api folder in the expanded folder that was created when RePast was installed, or online at the RePast home page.

We will need to initialize this by adding some money to it when the space object is created. Our strategy will be to create a constructor that takes as its arguments the information needed to initialize the space object, and do the initialization in the constructor.

To do this we will need another parameter, the amount of money. It's easy to see that we would want this to be a model parameter, so let's go ahead and add it to the code in the right way:

CarryDropModel

// 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;

  public String getName(){
    return "Carry And Drop";
  }

  public void setup(){
    System.out.println("Running setup");
  }

  public void begin(){
    buildModel();
    buildSchedule();
    buildDisplay();
  }

  public void buildModel(){
    System.out.println("Running BuildModel");
  }

  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

// CarryDropAgent
package demo;

public class CarryDropAgent {

}

CarryDropSpace

// 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));
      }
    }
  }
}

Notice that it has also been added to the init parameters, and get and set methods are now provided.

Our next step will be to integrate the new space object into the model.

Previous: Alerts in Subroutines

Next: The Space Object in the Sim Model

Go to Table of Contents


A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)