Print

Print


Hi,

I'm new to MASON and tried creating a very simple simulation following the
manual's student example but changing it to a sparse grid (2D).  It
compiles fine, but when I try to run it I can't see any agents.  My student
also created her own simulation and has the same problem.  The main
similarity between us is that we are running java 1.6 on Mac OSX with
Eclipse. Her code is not the same as mine which is what is leading me to
believe we either both missed something obvious or one of these 3 factors
is why the visualization is failing...

Any ideas on what could be causing this issue?  I'm copying my classes
below in case that helps.

Thanks!
Megan

package sim.app.simplepredprey;

import sim.portrayal.grid.*;
import sim.engine.*;
import sim.display.*;
import sim.portrayal.simple.*;
import javax.swing.*;
import java.awt.Color;

/**
 * Creates all of the GUI elements, including console and visualization
 * Grid is 2D, and elements to display are circles
 * @author megan olsen
 *
 */
public class SimulationWithUI extends GUIState{

public Display2D display;
public JFrame displayFrame;
SparseGridPortrayal2D gridPortrayal = new SparseGridPortrayal2D();
private final short worldwidth = 100; //update when update in Simulation
private final short worldheight = 100; //update when update in Simulation
/**
 * directly from tutorial in manual.pdf
 */
public static void main(String[] args)
{
SimulationWithUI vid = new SimulationWithUI();
Console c = new Console(vid);
c.setVisible(true);
}
/**
 * directly from tutorial in manual.pdf
 */
public SimulationWithUI() { super(new
Simulation(System.currentTimeMillis())); }
public SimulationWithUI(SimState state) { super(state); }
public static String getName() { return "Predator Prey Simulation"; }
 /**
 * directly from tutorial in manual.pdf
 */
public void start()
{
super.start();
setupPortrayals();
}
/**
 * directly from tutorial in manual.pdf
 */
public void load(SimState state)
{
super.load(state);
setupPortrayals();
}
/**
 * directly from tutorial in manual.pdf with minor adjustments
 */
public void setupPortrayals()
{
Simulation sims = (Simulation) state;
 //tell the portrayals what to portray and how to portray them
gridPortrayal.setField(sims.grid);
OvalPortrayal2D oval = new OvalPortrayal2D(Color.black);
gridPortrayal.setPortrayalForAll(oval);
 //reschedule the displayer
display.reset();
 //redraw the display
display.repaint();
}
 /**
 * directly from tutorial in manual.pdf with minor adjustments
 */
public void init(Controller c)
{
super.init(c);
display = new Display2D(worldwidth,worldheight,this);
display.setClipping(false);
 displayFrame = display.createFrame();
displayFrame.setTitle("Predator Prey Display");
c.registerFrame(displayFrame); //so the frame appears in the display list
displayFrame.setVisible(true);
display.setBackdrop(Color.green);
display.attach(gridPortrayal, "Grid");
}
 /**
 * directly from tutorial in manual.pdf
 */
public void quit()
{
super.quit();
if(displayFrame!=null)
displayFrame.dispose();
displayFrame = null;
display = null;
}
}

package sim.app.simplepredprey;

import sim.engine.*;
import sim.util.*;
import sim.field.grid.*;

/**
 * Main Model class. Holds the world grid and all agents.  Controls time
steps.
 * @author megan olsen
 *
 */
public class Simulation extends SimState{

/**
 * The world - type allows multiple objects at each integer location
 */
public SparseGrid2D grid;

/**
 * width of the world
 */
private final int gridWidth;
/**
 * height of the world
 */
private final int gridHeight;
/**
 * the number of predator agents in the start of the simulation
 */
private short numPred;
//private short numPrey;
 public Simulation(long seed)
{
super(seed);
gridWidth = 100;
gridHeight = 100;
numPred = 2;
// numPrey = 10;
 }
 /**
 * For setup of the model. Clears the grid and creates the initial agents,
 * setting the agents to be scheduled at each time step.
 * @author megan olsen
 * @param none
 * @return none
 */
public void start()
{
super.start();
grid = new SparseGrid2D(gridWidth, gridHeight);
//grid.clear();
 /* for(int i=0; i<numPred; i++)
{
Predator p = new Predator();
grid.setObjectLocation(p,  new
Int2D(grid.getWidth()/2+random.nextInt(grid.getWidth()/2),
grid.getHeight()/2 - random.nextInt(grid.getHeight()/2)));
schedule.scheduleRepeating(p);
}*/
 Predator p = new Predator();
grid.setObjectLocation(p, new Int2D(50,50));
schedule.scheduleRepeating(p);
/* for(int j=0; j<numPrey; j++)
{
 }*/
}
 /**
 * Runs the simulation using the built in "doLoop" that steps through
scheduled agents.
 * @param args
 */
public static void main(String[] args)
{
doLoop(Simulation.class, args);
System.exit(0);
}
}

package sim.app.simplepredprey;

import sim.engine.*;
import sim.field.grid.*;
import sim.util.*;

/**
 * Simple agent class for predators
 * @author megan olsen
 *
 */
public class Predator implements Steppable{

//location is built in -- ObjectLocation
/**
 * The method controlling what each agent does at each time step.
 * @author megan olsen
 * @param state the SimState object, used to interact with the world and
other agents
 */
public void step(SimState state)
{
//get access to the world through the parameter
Simulation mysim = (Simulation) state;
SparseGrid2D grid = mysim.grid;
 Int2D location = grid.getObjectLocation(this); //this agent's location
 //Int2D can't be changed, so we must create a mutable int to set a new
location
/* MutableInt2D newloc = new MutableInt2D();
newloc.x = grid.tx(location.x + 1);
newloc.y = grid.ty(location.y + 1);
 System.out.println(newloc.x + " " + newloc.y);
grid.setObjectLocation(this, new Int2D(newloc));*/
}
}