RePast Tutorial III

Preview:

DESCRIPTION

RePast Tutorial III. 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 Presentation

Citation preview

Introduction to Computational Modeling of Social Systems

Prof. Lars-Erik CedermanCenter for Comparative and International Studies (CIS)

Seilergraben 49, Room G.2, lcederman@ethz.chNils Weidmann, CIS Room E.3 weidmann@icr.gess.ethz.ch

http://www.icr.ethz.ch/teaching/compmodels

Lecture, December 21, 2004

RePast Tutorial III

2

Today’s agenda

• GUI versus batch mode• How to create charts• GraphIPD• How to use spaces• How to create displays• GridIPD

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>

4

GUI features

Graphical user interfaces offer:• Customized parameter panel• Dynamic graphs• Graphical displays• Probes

GraphIPD

GridIPD

5

Separating GUI and batch modes

Modelextends

SimpleModel

ModelGUIextendsModel

ModelBatchextendsModel

ExperIPDGraphIPD,GridIPD

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

Types of plots and charts

• Time seriesuser defined variable(s) over time

• Histogrambar chart showing a variable’s distribution

• Scatter plotsnapshot of two variables

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;

...

}

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

27

Advanced graphics

• Use network displays (see RePast How To document)

• Use custom Java drawing (see CustomGraphGUI of GraphIPD for an example)