A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)
Conceptually it makes sense to continue talking about the model- we have now created a landscape, but no agents and no actions. However, lots of points will be made clearer if we begin to create the graphic display. This is one of the areas where RePast is extremely convenient, but there are some subtle points.
To begin, we should define our objective. We want to see our 2-dimensional grid. We want the background to be white. We want the money we have placed on the landscape to be visible; we want the cell that indicates that money is present to also indicate, by color, how much money is present.
To do this, we make use of three pre-built RePast objects: DisplaySurface, Value2DDisplay, and ColorMap.
DisplaySurface objects are basically windows. A ColorMap object is a linking of particular values to particular colors- we will use it to say that a '0' is black, a '1' is dark red, a '2' is lighter red, a '3' is even lighter, and so forth to indicate how much money is in a given cell. Value2DDisplay is an object that links a map to someplace in your model that provides values to display, and that when added to a DisplaySurface links the window, the function, and the color map together.
To use these objects we must:
It's easier than it sounds, but it's still a lot of little steps- we'll take it slowly.
The variable for the DisplaySurface object is declared in the variable declarations section; tearing down the display surface, re-instantiating it, and registering it are done in the 'setup' method, as below (note the new import statement!). Notice how the argument for creating the display surface object is 'this'- that is, the model object that is running the setup method passes itself as an argument to the display surface:
// CarryDropModel
package demo;
import uchicago.src.sim.engine.Schedule;
import uchicago.src.sim.engine.SimInit;
import uchicago.src.sim.engine.SimModelImpl;
import uchicago.src.sim.gui.DisplaySurface;
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;
private DisplaySurface displaySurf;
public String getName(){
return "Carry And Drop";
}
public void setup(){
System.out.println("Running setup");
cdSpace = null;
if (displaySurf != null){
displaySurf.dispose();
}
displaySurf = null;
displaySurf = new DisplaySurface(this, "Carry Drop Model Window 1");
registerDisplaySurface("Carry Drop Model Window 1", displaySurf);
}
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: Getting Money- A Brief Aside
Go to Table of Contents
A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)