Lets take a few first steps with our modeling frameworks. We
will be fairly brief , limiting ourselves to simple "kicking the tires"
and writing a hello world in each ABM system (here is the NetLogo First Steps).
Our goal is to gain a feel for what each system is capable of so that
we can make intelligent decisions on which one to use for a given
task. Note: most images below can be clicked on for a full size image.
|
To download RePast, go to their web site,
downloading the package for your system. As of this writing, the
version is 2.0.1, and requires Java 1.4. The site has a mail list
sign-up, which I recommend. The package includes considerable browser-based documentation in the docs/ directory for fast viewing, and the readme.html includes an overall description. The docs/how_to/how_to.html gets into running and programming RePast, while docs/api/ contains the JavaDocs. |
|
|
Start RePast by double clicking the lib/repast.jar file. When
it starts, you are presented with the control strip which is part of
every model. You can browse the Model Library,
by clicking on the folder icon. Start by selecting the SugarScape model
from the list presented, then clicking on "load".
You'll see the setup to the right. To run the model, click either the "Single Step" or "Forward" buttons. To pause the model, click the "Pause" button. The parameters can be set using the parameter window controls. The simulation is a version of the SugarScape model made famous by Axtell and Epstein in their 1996 book Growing Artificial Societies. |
|
|
RePast is a Java Language based system. For those new to Java, the Java Tutorial is an excellent place to start. Download for easy use.
Take some time to browse through the RePast HowTo . Its Running a Simulation and GUI sections are good starting places for how to use the demos. The SimpleModel HowTo describes initial programming using the SimpleModel class we'll use below. The FAQ is a good place if all else fails! The code for the demos is in the downloaded src/ directory under uchicago/src/sim. Its a good idea to bookmark your local copy of the API documentation, for both Java and RePast. They will be your constant companion as you write models! |
|
We can model this RePast by having the buttons be agents, with both
color and a group ID, placed randomly on the graphic window. Each
tick we will chose a pair of agents, connecting them together and
changing all of their group of agents to be the same color and have the
same group ID. At the end of each turn we will find the largest
group cluster, posting its size. Before we start, you can browse the final two Java source files.
We'll build the model in phases, in order to simplify the process.
For our first phase, we'll build a minimal subclass of SimpleModel (See: API Docs, HowTo Docs).
It will not need an agent class yet, because we'll simply do all the
initialization necessary to pop-up a DisplaySurface .. the window in
which the agents operate (API Docs, HowTo Docs). You can view the Java source for our first phase. To compile and run the model you use:
javac -classpath lib/repast.jar:. TutModel.java
Note that I've changed directory to where the java file is, and
made a link to the RePast lib/ directory .. both to simplify the
compilation and execution of the model. The results are to the right:
|
|
|
The Class name is "TutModel", extending SimpleModel. It has
two variables: the Display Surface and its size (buttons along an
edge).
The class only has three methods: "setup" and "buildModel", along with a static (Class) "main" method who's only job is to start the model.
Setup is called when a model is initialized, or is restarted, both from the Control Panel. BuildModel is called when the model is first run, either by the Step or the Run buttons in the Control Panel.
Both are discussed in the HowTo.
Note that only 16 lines of code got us running!
If you run this model, clicking on Step or Run simply increase the
tick count with no other effect because we have not added a "step"
method. So this is a "no-op" model at this point, but has the initial structure we need to get started. |
public class TutModel extends SimpleModel {
|
|
In NetLogo, Turtles are provided as part of the framework. In
RePast, the Agents are implemented by the programmer, almost from
scratch. This is facilitated by the use of Java Interfaces, in
particular the Drawable interface. By implementing the methods: "draw", "getX" and "getY", any object can be placed in the RePast Space (API, HowTo) created in Phase 1.
In our second phase, we create our Button "agent", TutAgent.java, along with the changes needed in TutModel.java
needed to incorporate the agents and initialize them randomly on the
DisplaySurface. TutAgent is quite minimal, just enough to be
Turtle-like: have a color, an ID (who) and be a Drawable. The changes to TutModel are the minimum
to add the agents: introduce a variable "buttons" to hold the number of
buttons and add a loop creating the buttons. |
|
To compile and run the new pair of java files do:
javac -classpath lib/repast.jar:. TutAgent.java TutModel.java
java -classpath lib/repast.jar:. TutModel &
|
Here is the entire new TutAgent class. It implements Drawable
by providing "draw", "getX", "getY", thus letting it be put into
RePast Spaces. We added the "who" and "color" attributes to make
the TutAgent be turtle-like, both for our button experiment and for
future NetLogo-like models in RePast. We added "setXY" method, and the
"space" instance variable so that TutAgents could add themselves to
spaces automatically for the TutModel. Thus "setXY" both updates
the x, y instance variables and places the agent correctly in the
space.
Note "setXY" also removes us from out prior position in the space if need be.
|
public class TutAgent implements Drawable {
|
|
Here are the changes in TutModel from phase 1. We add the
instance variable "buttons" for the number of buttons to use in the
model. Nothing is added to "setup". In
"buildModel" we simply add the code to create the buttons (TutAgent's)
and initialize them randomly, both random colors and random positions.
The call to btn.setXY, in addition to
setting the x and y instance variables, adds the TutAgent to the RePast
"space" .. the grid the agents live on. See above.
We also add the TutAgent to a list, agentList, which is part of the SimpleModel class as a convenience.
|
public class TutModel extends SimpleModel {
|
|
The third phase is to add behavior to the TutModel. This is
done by providing a "step" method, which is called by SimpleModel each
tick of the scheduler. Now when we run the demo model, we see the
buttons begin to change colors and after running for a bit, the colors
merge to being primarily the same color.
Here is the new source code for TutModel.java and TutAgent.java.
|
|
|
The change to TutModel is to add the "step" method.
Like in the NetLogo model, we chose two random buttons, and then
merge their groups by making all of the group1 agents become group2
agents. Note the final call to
dsurf.updateDisplay(). This is how the changes occur to the
display. It is more efficient having the Model explicitly decide
when to redraw the display rather than having it always do so.
|
public class TutModel extends SimpleModel {
|
|
The change to TutAgent is to add the group instance variable which starts out set to "who".
We provide getters/setters for "group" so that we can merge the groups during the "step" method above.
This completes TutAgent, there will be no more changes to it.
|
public class TutAgent implements Drawable {
|
|
We complete our RePast model by adding parameters and a graph.
Only TutModel changes. It adds two features, user setable
parameters (for number of buttons and size of the space), and a graph
showing the growth of the largest cluster of connected buttons. Here is the final source code for TutModel.java and TutAgent.java. |
|
|
Only TutModel changes. The first change is to add parameters for the "buttons" and "spaceSize" variables.
To tell RePast which elements to include in the parameter panel, we
create a constructor for the TutModel which sets the SimpleModel
variable "params" to a string array of the parameters to display. (See getInitParam in SimpleModel)
RePast requires all user parameters to be available via getter/setter methods, which we also add to TutModel
|
public class TutModel extends SimpleModel {
|
|
Our second change is to add the cluster size graph to the model.
First, we add two instance variables: "clusterSz", which holds the
largest cluster of buttons so far, and "plot", the RePast
OpenSequenceGraph object. In "setup", we initialize both ..
clusterSz to 1, and plot to have the desired name and axis labels, and
to plot the clusterSz variable via a new Sequence instance.
Graphs can have many of these, one per plot line. We only need
one. Note the use of a dynamically created class with a single
method reporting clusterSz.
In "buildModel", we use the current value of "buttons" to set the axes to reasonable values, and pop-up the plot window.
In "step" we add code to manage "clusterSz", updating it if the
current size is greater. We then plot the current value by
calling "plot.step". Finally we pause the model when we get to
the point that all the buttons are connected. |
public class TutModel extends SimpleModel {
|