Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,

Similar presentations


Presentation on theme: "Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,"— Presentation transcript:

1 Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49, Room G.2, lcederman@ethz.chlcederman@ethz.ch Nils Weidmann, CIS Room E.3 weidmann@icr.gess.ethz.chweidmann@icr.gess.ethz.ch http://www.icr.ethz.ch/teaching/compmodels Lecture, December 21, 2004 RePast Tutorial III

2 2 Today’s agenda GUI versus batch mode How to create charts GraphIPD How to use spaces How to create displays GridIPD

3 3 Two modes of modeling Organizations of agents Animate agents Data Artificial world Observer Inanimate agents If then else If then else

4 4 GUI features Graphical user interfaces offer: Customized parameter panel Dynamic graphs Graphical displays Probes GraphIPD GridIPD

5 5 Separating GUI and batch modes Model extends SimpleModel ModelGUI extends Model ModelBatch extends Model ExperIPDGraphIPD,GridIPD

6 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() }

7 7 Types of plots and charts Time series user defined variable(s) over time Histogram bar chart showing a variable’s distribution Scatter plot snapshot of two variables

8 8 Showing time series Main class: OpenSequenceGraph uchicago.src.sim.analysis.OpenSequenceGraph Extension: NetSequenceGraph Specialized for network statistics (path length, cluster coefficient, density,...). Note: agentList should implement the Node interface! Declaration: public class ModelGUI extends Model { private OpenSequenceGraph graph;... }

9 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(); }

10 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

11 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.

12 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

13 13 Beyond RePast charts For tailor-made graphics, bypass RePast and develop your own custom classes Example can be found as part of the GraphIPD model ( CustomModelGUI ). It is also possible to integrate third-party libraries: –Java3D: http://java.sun.com/products/java-media/3D/ http://java.sun.com/products/java-media/3D/ –JFreeChart: http://www.jfree.org/jfreechart/ http://www.jfree.org/jfreechart/ –JMSL: http://www.vni.com/products/imsl/jmsl.htmlhttp://www.vni.com/products/imsl/jmsl.html

14 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;... }

15 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); } } }

16 16 Types of spaces Boundaries –Grid –Torus Cell’s shape –Rectangular –Hexagonal Cell’s content –One object –Collection of agents V1V1 0,0 0,1 0,2 0,3 0,4 1,0 1,11,3 1,4 1,2

17 17 Classes Object2DGrid A 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. OrderedMulti2DGrid A two-dimensional grid whose cell can contain more than one Object. The order of the Objects in each cell is first in, first out. Diffuse2DGrid A discrete approximation of two-dimensional diffusion. The space itself is a toroidal (donut-shaped) grid whose cells contain doubles.

18 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 agent space.putObjectAt(x, y, null); space.putObjectAt(newX, newY, agent); Random arrangement One occupant per cell

19 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)

20 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

21 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!

22 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(); }

23 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; } }

24 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

25 25 Drawable, space and display Space DisplayInterface Object2DGridObject2DDisplayDrawable Object2DTorusObject2DDisplayDrawable Diffuse2DValue2DDisplay N/A. Map to values to a ColorMap instead.ColorMap Multi2DGridMultiObject2DDisplayDrawable Multi2DTorusMultiObject2DDisplayDrawable OrderedMulti2DGridMultiObject2DDisplayDrawable OrderedMulti2DTorusMultiObject2DDisplayDrawable Object2DHexagonalGridObject2DHexaDisplayDrawable Object2DHexagonalTorusObject2DHexaDisplayDrawable DiffuseHexagonal2DValue2DHexaDisplay N/A. Map to values to a ColorMap instead.ColorMap RasterSpaceObject2DDisplayDrawable

26 26 Snapshots and movies Snapshots: dsurf.setSnapshotFileName("snapshot"); schedule.scheduleActionAtInterval(100, dsurf, "takeSnapshot"); Movies dsurf.setMovieName("movie.mov", DisplaySurface.QUICK_TIME); schedule.scheduleActionAtInterval(10, dsurf, "addMovieFrame"); schedule.scheduleActionAtEnd(dsurf, "closeMovie"); snapshot- 100.gif

27 27 Advanced graphics Use network displays (see RePast How To document) Use custom Java drawing (see CustomGraphGUI of GraphIPD for an example)


Download ppt "Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,"

Similar presentations


Ads by Google