A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)
Let's look at the Agent Objects.
For now let's just endow our agents with a few attributes:
We have said that our model involves agents that carry money around, so the 'money' variable should not be a surprise.
The x and y position variables may be a bit more confusing; before all is said and done the agent's location will be stored in two places: it will have its own variables that indicate its position, and it will be kept in another Object2DGrid in the space object. This will add an overhead in that we will have to be certain that whenever the agent moves, both places get the updated information. However, we will need each place for different steps along the procedure.
The number of time steps until the agent dies is another aspect of the basic algorithm of the routine; agents are supposed to live for a while and then die. However, as with many things, this seemingly simple aspect of the program will invite complications. Specifically, how will we set the ranges for minimum and maximum life spans? Ideally we would like to reset these as model parameters. This means that they get 'built into' the model object as every other model parameter (default values, get and set methods, added to the array of strings returned by getInitParams- review the earlier pages here and here for details). The way we will tell the agents what their life spans should be is to pass these values as arguments in the constructor of the agent object.
Hence:
// CarryDropModel
package demo;
import java.awt.Color;
import uchicago.src.sim.engine.Schedule;
import uchicago.src.sim.engine.SimInit;
import uchicago.src.sim.engine.SimModelImpl;
import uchicago.src.sim.gui.DisplaySurface;
import uchicago.src.sim.gui.ColorMap;
import uchicago.src.sim.gui.Value2DDisplay;
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 static final int AGENT_MIN_LIFESPAN = 30;
private static final int AGENT_MAX_LIFESPAN = 50;
private int numAgents = NUMAGENTS;
private int worldXSize = WORLDXSIZE;
private int worldYSize = WORLDYSIZE;
private int money = TOTALMONEY;
private int agentMinLifespan = AGENT_MIN_LIFESPAN;
private int agentMaxLifespan = AGENT_MAX_LIFESPAN;
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();
displaySurf.display();
}
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");
ColorMap map = new ColorMap();
for(int i = 1; i<16; i++){
map.mapColor(i, new Color((int)(i * 8 + 127), 0, 0));
}
map.mapColor(0, Color.white);
Value2DDisplay displayMoney =
new Value2DDisplay(cdSpace.getCurrentMoneySpace(), map);
displaySurf.addDisplayable(displayMoney, "Money");
}
public Schedule getSchedule(){
return schedule;
}
public String[] getInitParam(){
String[] initParams = { "NumAgents", "WorldXSize", "WorldYSize", "Money", "AgentMinLifespan", "AgentMaxLifespan"};
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 int getAgentMaxLifespan() {
return agentMaxLifespan;
}
public int getAgentMinLifespan() {
return agentMinLifespan;
}
public void setAgentMaxLifespan(int i) {
agentMaxLifespan = i;
}
public void setAgentMinLifespan(int i) {
agentMinLifespan = 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 {
private int x;
private int y;
private int money;
private int stepsToLive;
public CarryDropAgent(int minLifespan, int maxLifespan){
x = -1;
y = -1;
money = 0;
stepsToLive =
(int)((Math.random() * (maxLifespan - minLifespan)) + minLifespan);
}
}
// 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;
}
public Object2DGrid getCurrentMoneySpace(){
return moneySpace;
}
}
Previous: Generating a Display
Go to Table of Contents
A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)