270 likes | 467 Views
RePast Tutorial III. Week 7. Today’s agenda. GUI versus batch mode How to create charts GraphIPD How to use spaces How to create displays GridIPD. Two modes of modeling. If <cond> then <action1> else <action2>. Inanimate agents. Observer. Animate agents. Data.
E N D
RePast Tutorial III Week 7
Today’s agenda • GUI versus batch mode • How to create charts • GraphIPD • How to use spaces • How to create displays • GridIPD
Two modes of modeling If <cond> then <action1> else <action2> Inanimate agents Observer Animate agents Data Organizations of agents Artificial world
GUI features Graphical user interfaces offer: • Customized parameter panel • Dynamic graphs • Graphical displays • Probes GraphIPD GridIPD
Separating GUI and batch modes Model extends SimpleModel GraphIPD,GridIPD ExperIPD ModelGUI extends Model ModelBatch extends Model
Subclassing a GUI model class ModelGUI extends Model{ GUI variables (graph) setup() { super.setup(); params = ... delete old graphs } buildModel() { super.buildModel(); create graph } step() { super.step(); update graph } ... main() } class Model extends SimpleModel { model variables setup() buildModel() step() main() }
Types of plots and charts • Time seriesuser defined variable(s) over time • Histogrambar chart showing a variable’s distribution • Scatter plotsnapshot of two variables
Showing time series • Main class: OpenSequenceGraphuchicago.src.sim.analysis.OpenSequenceGraph • Extension: NetSequenceGraphSpecialized for network statistics (path length, cluster coefficient, density, ...). Note: agentList should implement the Node interface! • Declaration: publicclass ModelGUI extends Model { private OpenSequenceGraph graph; ... }
Showing time series (cont.) • Initialization: publicvoid setup() { super.setup(); if (graph != null) graph.dispose(); } • Instantiation: publicvoid buildModel() { super.buildModel(); graph = new OpenSequenceGraph(“Graph", this); graph.setXRange(...); graph.addAxisTitles(...); graph.display(); }
Showing time series (cont.) • Updating: publicvoid step() { super.step(); graph.step(); } • Adding variables: graph.createSequence(“X",this,“getXValue"); Name of the method returning the value to be displayed
OpenSequenceGraph API • addSequence(String name, Sequenceseq, Color color, int markStyle)Adds a sequence with a specific name to be drawn in a user-defined color and points in a pre-defined style • record() Records the data for this graph without updating the display • setYAutoExpand(boolean autoExpand) Sets whether the y-axis scale will expand to include new points or not • setYRange(double min, double max) Sets the initial range of the y-axis. • writeToFile() Writes this graph to a file.
Example: Scatter plot uchicago.src.sim.analysis.Plot Plot aPlot = new Plot("Test Plot");aPlot.addLegend(0, "Sin", Color.blue, Plot.FILLED_DIAMOND); aPlot.addLegend(1, "Manual", Color.red); aPlot.setConnected(true); aPlot.display(); for (double i = 0; i < 100; i++) { aPlot.plotPoint(i, Math.sin(i), 0); } aPlot.plotPoint(3.0, 4.0, 1); aPlot.plotPoint(5.0, 1.4, 1); aPlot.updateGraph(); aPlot.fillPlot();
Beyond RePast charts • For tailor-made graphics,bypass RePast and developyour own custom classes • Example can be found aspart of the GraphIPD model(CustomModelGUI). • It is also possible to integrate third-party libraries: • Java3D: http://java.sun.com/products/java-media/3D/ • JFreeChart: http://www.jfree.org/jfreechart/ • JMSL: http://www.vni.com/products/imsl/jmsl.html
GridIPD: How to use spaces • Two purposes: • Collection of agents • Spatial relationship of agents • Discrete • Package: uchicago.src.sim.space • Declaration:publicclass Model extends SimpleModel {protected Object2DGrid world;privateintworldSize; ... }
How to use spaces (cont.) • Initialization: publicvoid setup() { super.setup(); worldSize = 16; } • Instantiation: publicvoid buildModel() {super.buildModel();world = new Object2DGrid(worldSize, worldSize);for (int x = 0; x < worldSize; x++)for (int y = 0; y < worldSize; y++) { Player aPlayer = new Player(x,y,...); world.putObjectAt(x, y, aPlayer); agentList.add(aPlayer); } }}
0,1 0,3 0,2 0,0 0,4 1,1 1,3 1,2 1,0 1,4 V1 Types of spaces • Boundaries • Grid • Torus • Cell’s shape • Rectangular • Hexagonal • Cell’s content • One object • Collection of agents
Classes • Object2DGridA discrete two-dimensional grid whose cells may contain an object. • Object2DTorus A discrete two-dimensional torus whose cells may contain an object. • Multi2DGrid A two-dimensional grid whose cells can contain more than one Object. The order of the Objects in each cell is undefined. • OrderedMulti2DGridA two-dimensional grid whose cell can contain more than one Object. The order of the Objects in each cell is first in, first out. • Diffuse2DGridA discrete approximation of two-dimensional diffusion. The space itself is a toroidal (donut-shaped) grid whose cells contain doubles.
Usage • Random arrangement: Object2DGrid space = new Object2DGrid(spaceWidth, spaceHeight); for (int i = 0; i < numAgents; i++) { int x, y; do { x = Random.uniform.nextIntFromTo(0, space.getSizeX() - 1); y = Random.uniform.nextIntFromTo(0, space.getSizeY() - 1); } while (space.getObjectAt(x, y) != null); MyAgent agent = new MyAgent(x, y, space); space.putObjectAt(x, y, agent); agentList.add(agent); } • Moving agentspace.putObjectAt(x, y, null);space.putObjectAt(newX, newY, agent); Random arrangement One occupant per cell
Moore getMooreNeighbors(int x, int y, int xExtent, int yExtent, boolean nulls) Von Neumann getVonNeumannNeighbors(int x, int y, int xExtent, int yExtent, boolean nulls) Neighborhood
How to create displays • Displays: graphical presentations of agents and their environments • Package: uchicago.src.sim.gui • Declaration:publicclass ModelGUIextends Model {private DisplaySurface dsurf; ... } Extends JComponent
How to create displays (cont.) • Initialization: publicvoid setup() { super.setup(); if (dsurf != null) dsurf.dispose();DisplayConstants.CELL_WIDTH = 30;DisplayConstants.CELL_HEIGHT = 30;dsurf = new DisplaySurface(this, “2D Display"); registerDisplaySurface("Main", dsurf); } • Delegation to buildDisplaymethod: publicvoid buildModel() { super.buildModel(); buildDisplay(); } Size of a cell in pixels Good practice!
How to create displays (cont.) • Instantiation: publicvoid buildDisplay() { Object2DDisplay display = new Object2DDisplay(world); display.setObjectList(agentList); dsurf.addDisplayable(display, "Display"); addSimEventListener(dsurf); dsurf.display(); } • Updating: publicvoid step() { super.step(); dsurf.display(); }
How to create displays (cont.) • Cell drawing: publicclass Player implements Drawable {int x, y;public Player(int x, int y) {this.x = x;this.y = y; }publicvoid draw(SimGraphics g) { g.setDrawingParameters(DisplayConstants.CELL_WIDTH * 2/3, DisplayConstants.CELL_HEIGHT * 2/3, DisplayConstants.CELL_DEPTH * 2/3); g.drawFastRoundRect(COLOR[type]); } publicint getX() {return x; }publicint getY() {return y; } }
SimGraphics API • drawCircle(Color c)Draws a true circle with the specified color. • drawFastRoundRect(Color c)Draws a rounded rectangle of the specified color. • drawHollowOval(Color c)Draws a hollow oval in the specified color. • drawStringInRoundRect(Color rectColor, Color stringColor, String text)Draws the specified string inside a rounded rectangle. • ... • setDrawingParameters(int width, int height, int depth)Sets the parameters for the next drawing operation. Usually faster than drawRoundRect No fill
Snapshots and movies snapshot-100.gif • Snapshots:dsurf.setSnapshotFileName("snapshot");schedule.scheduleActionAtInterval(100, dsurf, "takeSnapshot"); • Moviesdsurf.setMovieName("movie.mov", DisplaySurface.QUICK_TIME);schedule.scheduleActionAtInterval(10, dsurf, "addMovieFrame");schedule.scheduleActionAtEnd(dsurf, "closeMovie");
Advanced graphics • Use network displays (see RePast How To document) • Use custom Java drawing (see CustomGraphGUI of GraphIPD for an example)