Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
SchellingTutorial

SchellingTutorial

Ratings: (0)|Views: 181|Likes:
Published by Maksim Tsvetovat

More info:

Published by: Maksim Tsvetovat on Nov 02, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

07/19/2011

pdf

text

original

 
WCSS 2006 Introduction to MASON
MASON is a discrete-event simulator designed for very large "swarm-" style simulations. It is written inJava and is free open source, available at theMASON home page. MASON has grids, continuous regions,networks, hexagonal spaces, etc., all in 2D or 3D, plus the flexibility to create your own "space" datastructures. Any object can be inspected ("probed") in 2D or in 3D, and tracked with histograms, time seriescharts, etc. Among the simulators you may be familiar with, MASON is most similar to RePast.MASON was designed from first principles for large numbers of simulations on back-end clusters. To thisend it has a number of architecture features which make it somewhat different from other simulationpackages:
MASON's models are separated from its visualizers.
You can dynamically attach visualizers,detach them and run on the command line, or attach different ones. This allows us to
checkpoint
(serialize, or "freeze-dry") a running MASON simulation to a file, move it to a
different architecture
,and continue to run it. For example, we can run a MASON simulation with no visualization tools atall on a back-end Linux machine, checkpoint it, and examine/tweak the simulation mid-run on afront-end Mac under visualization. We can then put it back and continue to run from there. Amongcompiled-code multiagent simulators, MASON is unique in this respect.You'll see this architecture in the tutorial's separate
Schelling.java
(Model) and
SchellingWithUI.java
(Visualizer) files.
MASON is written in highly portable Java, 
backward-compatible with Java 1.3.1, so as to run onas many legacy cluster systems as possible.
MASON pays very careful attention to speed.
It has special data structures which are designed tobe hacked at a very low (and potentially unsafe) level if speed is called for. In this tutorial, suchstructures include Bag, IntBag, DoubleBag, and ObjectGrid2D. MASON also uses a highly efficientimplementation of the Mersenne Twister random number generator, and has both "fast" and "slowbut flexible" versions of much of its visualization toolkit.In the tutorial,
we will be using these data structures and visualizers but will not be takingadvantage of their faster procedures; rather we'll stick with slow-but-safe approaches.
As aresult, our simulation model will run at about 1/3 (or less) the speed it
could 
run, and our visualizerwill likewise run at about 1/5 the speed. But it will make the tutorial easier to understand.
MASON is compatible with ECJ
, our open-source evolutionary computation and stochastic searchlibrary, and presently among the very best available.
MASON is highly modular
. MASON was designed to be easily modified, extended, and otherwisebent any which way. We have tried hard to make the simulator core cleanly structured. A great manyelements of MASON can be separated and used by themselves independent of the reset of thesimulation package.
About the Tutorial
We will develop a simple version of the Schelling Segregation model. In this version of Schelling, Red andBlue agents live in a non-toroidal grid world. Each agent computes its happiness as a function of the sum of like-colored agents around it, weighted by distance from those agents. If an agent is not happy enough, itpicks a random empty location to move to. All agents have an opportunity to move once per time tick.As mentioned above, our example tutorial has emphasized tutorial brevity and simplicity over speed; thuswe use slow-but-safe mechanisms whenever. At the very end of the simulation, we do show how to use a"faster" drawing procedure, which you may be interested in.Here's the tutorial:1. Get MASON Running (not in the tutorial -- we'll do this by hand)2. A Minimum Simulation
 
 3. Add a Field (a representation of space)4. Define our Agents5. Set Up the Agents6. Build a Minimal GUI7. Add a Display8. Make the Agents Do Something9. Add Local Inspectability10. Add Global Inspectability11. Add a Histogram12. Add a Time Series Chart13. Speed Up Drawing14. Checkpoint the Simulation
Get MASON Running
We'll get this set up by hand. But in short, you'll need to have the following in your CLASSPATH:
The
 mason
directory.
This holds the MASON code and documentation.
The
jcommon-1.0.0.jar
file.
This holds utility code used by JFreeChart.
The
jfreechart-1.0.1.jar
file.
This is the primary code for JFreeChart, which MASON employsto draw charts and graphs.
The
itext-1.2.jar
file
. This holds the iText PDF document generation library, which MASONuses to output charts and graphs in publication-quality fashion.
The
jmf.jar
file
. This holds Sun's Java Media Framework, which MASON uses to produce moves.You can also download an operating system-specific version of the framework with extra movieexport options if you like.
The
quaqua-colorchooser-only.jar
file.
(OS X Users only). Java's color picker is poor for OSX: this library provides a nice replacemen. I have no idea if it works well for Windows or not -- try itand see!Additionally, you'll need to have
Java3D
installed -- though not used for this tutorial, it'll make compilingMASON much simpler.None of the additional libraries are actually required to
run
MASON: without them it will simply refuse tomake a given operation available (such as generating charts and graphs). However to compile a MASONsimulation, these libraries are required unless you go in and manually remove the MASON code whichrelies on them. This can be reasonably easily done, but it's inconvenient.
A Minimum Simulation
We begin with a minimum simulation that does nothing at all. Create a file called
Schelling.java
, storedin a directory called
 wcss
located in the
sim/app
directory of MASON. The file looks like this:
package sim.app.wcss;import sim.engine.*;import ec.util.*;import sim.util.*;public class Schelling extends SimState{public Schelling(long seed){super(new MersenneTwisterFast(seed), new Schedule());} /** Resets and starts a simulation */public void start(){super.start(); // clear out the schedule} public static void main(String[] args){
 
doLoop(Schelling.class, args);System.exit(0);}}
An entire MASON simulation model is hung somewhere off of a single instance of a subclass of 
sim.engine.SimState
. Our subclass is going to be called
Schelling
. A SimState has two basic instancevariables: an
ec.util.MersenneTwisterFast
random number generator called
random
, and a
sim.engine.Schedule
called
schedule
. We create these and give them to SimState through super().
MersenneTwisterFast
is the fastest existing Java implementation of the Mersenne Twister random numbergenerator. I wrote it :-) and it's used in lots of production code elsewhere, including NetLogo. The generatoris essentially identical to java.util.Random in its API, except that it is
not threadsafe
.
Schedule
is MASON's event schedule. You can schedule "agents" (implementations of 
sim.engine.Steppable
) on the schedule to be fired and removed at a later date. When the Schedule isstepped, it advances to the minimum-scheduled agent and fires it (calling its
step(SimState)
method).Agents may be scheduled for the same timestep. Within a timestep, agents' firing order may be specifiedrelative to one another. Agents with the same timestep and ordering are fired in random order. Agents mayalso be scheduled to be fired at regular intervals instead of being one-shot. Our agents will be all fired atregular intervals and will not use any particular firing order (they'll be random relative to other agentsscheduled at that timestep).
start()
is called by MASON whenever a new simulation run is begun. You can do whatever you like in thismethod, but be sure to call
super.start()
first. There's also a
finish()
.MASON models typically can be run both from the command line and also visualized under a GUI.Command-line model runs are usually done by calling
main(String[] args)
on your SimState subclass.Usually all that needs to be done here is to call the convenience method
doLoop
and then call
System.exit(0)
.
doLoop(...)
is a convenience method which runs a MASON simulation loop. A basic loop -- which youcould do yourself in main() if you wanted -- starts a MASON simulation (calling start()), steps its Scheduleuntil the Schedule is out of events to fire, and then cleans up (calling finish()) and quits. doLoop does a littlebit more: it also optionally prints out a bit of statistics information, checkpoints to file and/or recovers fromcheckpoint, handles multiple jobs, and stops the simulation in various situations, among others. It does mostof what you'd want.
Compile and run the simulation (as java sim.app.wcss.Schelling)
(Files in folder 1 if you're just following along)You'll get back something like this:
MASON Version 12. For further options, try adding ' -help' at end.Job: 0 Seed: 1155246793094Starting sim.app.wcss.SchellingExhausted
Very exciting.
Add a Field
So far our model only has a representation of time (Schedule). Let's add a representation of space. MASONhas many representations of space built-in: they're called
fields
. In our Schelling model, we'll use a simpletwo-dimensional grid of Objects. MASON can do a lot more representations than this, but it'll serve for ourpurposes in the tutorial. In the Schelling.java file, add:
public int neighborhood = 1;public double threshold = 3;public int gridHeight = 100;public int gridWidth = 100; public ObjectGrid2D grid = new ObjectGrid2D(gridWidth, gridHeight); // a dummy one to startpublic Bag emptySpaces = new Bag();

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->