repast tutorial iii
DESCRIPTION
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 then else . Inanimate agents. Observer. Animate agents. Data. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/1.jpg)
Introduction to Computational Modeling of Social Systems
Prof. Lars-Erik CedermanETH - Center for Comparative and International Studies (CIS)
Seilergraben 49, Room G.2, [email protected] Deiwiks, CIS Room E.3, [email protected]
http://www.icr.ethz.ch/teaching/compmodels
RePast Tutorial III
Week 7
![Page 2: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/2.jpg)
2
Today’s agenda
• GUI versus batch mode• How to create charts• GraphIPD• How to use spaces• How to create displays• GridIPD
![Page 3: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/3.jpg)
3
Two modes of modeling
Organizations of agents
Animate agents
Data
Artificial world
Observer
Inanimate agents
If <cond>
then <action1>
else <action2>
If <cond>
then <action1>
else <action2>
![Page 4: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/4.jpg)
4
GUI features
Graphical user interfaces offer:• Customized parameter panel• Dynamic graphs• Graphical displays• Probes
GraphIPD
GridIPD
![Page 5: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/5.jpg)
5
Separating GUI and batch modes
Modelextends
SimpleModel
ModelGUIextendsModel
ModelBatchextendsModel
ExperIPDGraphIPD,GridIPD
![Page 6: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/6.jpg)
6
Subclassing a GUI model
class Model extends SimpleModel { model variables
setup()
buildModel()
step()
main()}
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()}
![Page 7: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/7.jpg)
7
Types of plots and charts
• Time seriesuser defined variable(s) over time
• Histogrambar chart showing a variable’s distribution
• Scatter plotsnapshot of two variables
![Page 8: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/8.jpg)
8
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:
public class ModelGUI extends Model {
private OpenSequenceGraph graph;
...
}
![Page 9: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/9.jpg)
9
Showing time series (cont.)
• Initialization: public void setup() { super.setup(); if (graph != null) graph.dispose(); }
• Instantiation: public void buildModel() { super.buildModel();
graph = new OpenSequenceGraph(“Graph", this); graph.setXRange(...); graph.addAxisTitles(...); graph.display(); }
![Page 10: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/10.jpg)
10
Showing time series (cont.)
•Updating: public void step() { super.step();
graph.step(); }
•Adding variables: graph.createSequence(“X", this,“getXValue");
Name of the method
returning the value to be displayed
![Page 11: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/11.jpg)
11
OpenSequenceGraph API
• addSequence(String name, Sequence seq, 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.
![Page 12: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/12.jpg)
12
Example: Scatter 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();
uchicago.src.sim.analysis.Plot
![Page 13: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/13.jpg)
13
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
![Page 14: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/14.jpg)
14
GridIPD: How to use spaces
• Two purposes:– Collection of agents– Spatial relationship of agents
• Discrete• Package: uchicago.src.sim.space• Declaration:
public class Model extends SimpleModel { protected Object2DGrid world; private int worldSize;
... }
![Page 15: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/15.jpg)
15
How to use spaces (cont.)
• Initialization: public void setup() { super.setup(); worldSize = 16; }
• Instantiation: public void 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); } }}
![Page 16: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/16.jpg)
16
Types of spaces
• Boundaries– Grid– Torus
• Cell’s shape– Rectangular– Hexagonal
• Cell’s content– One object– Collection of agents
V1
0,0
0,1
0,20,3
0,4
1,0
1,1 1,3
1,41,2
![Page 17: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/17.jpg)
17
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.
![Page 18: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/18.jpg)
18
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 arrangeme
nt
One occupant per
cell
![Page 19: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/19.jpg)
19
Neighborhood
• 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)
![Page 20: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/20.jpg)
20
How to create displays
• Displays: graphical presentations of agents and their environments
• Package: uchicago.src.sim.gui• Declaration:
public class ModelGUI extends Model { private DisplaySurface dsurf;
...
}Extends
JComponent
![Page 21: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/21.jpg)
21
How to create displays (cont.)
• Initialization: public void 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 buildDisplay method: public void buildModel() { super.buildModel();
buildDisplay(); }
Size of a cell in pixels
Good practice!
![Page 22: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/22.jpg)
22
How to create displays (cont.)
• Instantiation: public void buildDisplay() {
Object2DDisplay display = new Object2DDisplay(world); display.setObjectList(agentList); dsurf.addDisplayable(display, "Display"); addSimEventListener(dsurf); dsurf.display();
}
• Updating: public void step() { super.step();
dsurf.display(); }
![Page 23: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/23.jpg)
23
How to create displays (cont.)
• Cell drawing: public class Player implements Drawable {
int x, y; public Player(int x, int y) { this.x = x; this.y = y; } public void draw(SimGraphics g) { g.setDrawingParameters(DisplayConstants.CELL_WIDTH * 2/3,
DisplayConstants.CELL_HEIGHT * 2/3, DisplayConstants.CELL_DEPTH * 2/3);
g.drawFastRoundRect(COLOR[type]); } public int getX() { return x; } public int getY() { return y; }
}
![Page 24: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/24.jpg)
24
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
![Page 25: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/25.jpg)
25
Drawable, space and display
Space Display Interface Object2DGrid Object2DDisplay Drawable
Object2DTorus Object2DDisplay Drawable
Diffuse2D Value2DDisplay N/A. Map to values to a ColorMap instead.
Multi2DGrid MultiObject2DDisplay Drawable
Multi2DTorus MultiObject2DDisplay Drawable
OrderedMulti2DGrid MultiObject2DDisplay Drawable
OrderedMulti2DTorus MultiObject2DDisplay Drawable
Object2DHexagonalGrid Object2DHexaDisplay Drawable
Object2DHexagonalTorus Object2DHexaDisplay Drawable
DiffuseHexagonal2D Value2DHexaDisplay N/A. Map to values to a ColorMap instead.
RasterSpace Object2DDisplay Drawable
![Page 26: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/26.jpg)
26
Snapshots and movies
• 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");
snapshot-100.gif
![Page 27: RePast Tutorial III](https://reader036.vdocuments.us/reader036/viewer/2022062309/56813720550346895d9eade0/html5/thumbnails/27.jpg)
27
Advanced graphics
• Use network displays (see RePast How To document)
• Use custom Java drawing (see CustomGraphGUI of GraphIPD for an example)