package AntPheromones1; // A simple model of bugs in a 2D world. // See the Readme.txt file for details. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Formatter; import uchicago.src.sim.engine.Schedule; import uchicago.src.sim.engine.SimModelImpl; import uchicago.src.sim.engine.*; import uchicago.src.sim.util.*; import uchicago.src.sim.gui.*; import uchicago.src.sim.gui.ColorMap; import uchicago.src.sim.gui.DisplaySurface; import uchicago.src.sim.gui.Object2DDisplay; import uchicago.src.sim.gui.Value2DDisplay; import uchicago.src.sim.space.Object2DTorus; import uchicago.src.sim.engine.AbstractGUIController; public class GUIModel extends AntPheromones1 { private Object2DDisplay worldDisplay; // 2D Object lattice -> display (Repast) private Value2DDisplay pSpaceDisplay; // 2D Value lattice -> display (Repast) private DisplaySurface dsurf; // display surface (RePast) public static ColorMap pherColorMap; public static final int colorMapSize = 64; public static final double colorMapMax = colorMapSize - 1.0; ///////////////////////////////////////////////////////////////////// // setup // // this runs automatically when the model starts // and when you click the reload button, to "tear down" any // existing display objects, and get ready to initialize // them at the start of the next 'run'. // public void setup() { //System.out.printf( "==> GUIModel setup() called...\n" ); super.setup(); // the super class does conceptual-model setup // NOTE: you may want to set these next two to 'true' // if you are on a windows machine. that would tell repast // to by default send System.out and .err output to // a special repast output window. AbstractGUIController.CONSOLE_ERR = false; AbstractGUIController.CONSOLE_OUT = false; if ( dsurf != null ) dsurf.dispose(); // create the ColorMap for displayihg the amount of pheromone // as a degree of red-ness in the cells. pherColorMap = new ColorMap (); for (int i = 0; i < colorMapSize; i++) { // we specify the position i, and a fraction of each of RGB shade pherColorMap.mapColor ( i, i / colorMapMax, 0, 0 ); } dsurf = null; dsurf = new DisplaySurface( this, "Bug Display" ); registerDisplaySurface( "Main Display", dsurf ); if ( rDebug > 0 ) System.out.printf( "<== GUIModel setup() done.\n" ); } ///////////////////////////////////////////////////////////////////// // begin // // this runs when you click the "initialize" button // (the button with the single arrow that goes around in a circle) // public void begin() { DMSG(1, "==> enter GUIModel-begin()" ); buildModel(); // the base model does this buildDisplay(); buildSchedule(); dsurf.display(); DMSG(1, "<== leave GUIModel-begin() done." ); } ///////////////////////////////////////////////////////////////////// // buildDisplay // // builds the display and display related things // public void buildDisplay() { if ( rDebug > 0 ) System.out.printf( "==> GUIModel buildDisplay...\n" ); // create the link between the dsurf and the Object2DTorus, // and tell parts about each other as needed. worldDisplay = new Object2DDisplay( world ); worldDisplay.setObjectList( bugList ); // create the link between the pSpace and the dsurf. // we have to tell it how to scale values to fit the colorMap. // We want the largest possible state (pheromone) value // to map into the colorMapMax-th entry in the colorMap array. // The Value2DDisplay does this mapping with the parameters m,c: // int color index = (state / m) + c // so we want m = truncate( maxValue / maxColorIndex ) pSpaceDisplay = new Value2DDisplay( pSpace, pherColorMap ); int m = (int) (maxPher / colorMapMax); if ( rDebug > 1 ) System.out.printf( " -> pSpaceDisplay scaling m = %d.\n", m ); pSpaceDisplay.setDisplayMapping( m, 0 ); // add the pSpace first, so the bugs write over it. dsurf.addDisplayable( pSpaceDisplay, "Pheromone"); // note we will be able to probe the bugs with MB3 (right) dsurf.addDisplayableProbeable( worldDisplay, "Bugs"); addSimEventListener( dsurf ); // link to the other parts of the repast gui if ( rDebug > 0 ) System.out.printf( "<== GUIModel buildDisplay done.\n" ); } //////////////////////////////////////////////////////////////// // buildSchedule // // This builds the entire schedule, i.e., // - the base model step // - display steps. public void buildSchedule() { if ( rDebug > 0 ) System.out.printf( "==> GUIModel buildSchedule...\n" ); // schedule the current GUIModel's step() function // to execute every time step starting with time step 0 schedule.scheduleActionBeginning( 0, this, "step" ); // schedule the current GUIModel's processEndOfRun() // function to execute at the end of the run schedule.scheduleActionAtEnd( this, "processEndOfRun" ); } /////////////////////////////////////////////////////////////////////////////// // step // // executed each step of the model. // Ask the super-class (AntPheromones1) to do its step() method, // and then this does display related activities. // public void step() { super.step(); // the model does whatever it does // add things after this for all displays (graphs, etc) dsurf.updateDisplay(); } // processEndOfRun // called once, at end of run. public void processEndOfRun ( ) { if ( rDebug > 0 ) System.out.printf("\n\n===== GUIModel processEndOfRun =====\n\n" ); applyAnyStoredChanges(); endReportFile(); this.fireStopSim(); } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// // **** NO NEEd TO CHANGE THE REST OF THIS ***** //////////////////////////////////////////////////////////////////// // main entry point public static void main( String[] args ) { uchicago.src.sim.engine.SimInit init = new uchicago.src.sim.engine.SimInit(); GUIModel model = new GUIModel(); //System.out.printf("==> GUIMOdel main...\n" ); // set the type of model class, this is necessary // so the parameters object knows whether or not // to do GUI related updates of panels,etc when a // parameter is changed model.setModelType("GUIModel"); // Do this to set the Update Probes option to true in the // Repast Actions panel Controller.UPDATE_PROBES = true; model.setCommandLineArgs( args ); init.loadModel( model, null, false ); // does setup() // this new function calls ProbeUtilities.updateProbePanels() and // ProbeUtilities.updateModelProbePanel() model.updateAllProbePanels(); } }