Friday, August 25, 2006

Slowing Repast simulations

Occasionally, one might want to slow a Repast simulation, say to observe change within your model at a slower pace. There is no built in functionality in Repast to do this. However slowing a simulation down can be achieved using the Tread.sleep comand in the model code. This comand slows (stalls) the program for a specific period of time

You could put this code in your model's stepping function (the function called every 'tick'), or some other place that would be called whenever an action occurs. The argument to sleep is the number of milliseconds you would like your program to sleep. See the Repast emailing list comment by Vos for futher information.

public void step() {
// do normal functionality in step

try {Thread.sleep(100); // sleep a tenth of a second
} catch (Exception ex) {
// ignore this exception

Alternativly one could alter the speed of the simulation durring the model run eg:

public void step() {
// Pause the simulation according the selected speed
if(simulationSpeed <100){
try {
Thread.sleep(10 * (100 - simulationSpeed));
} catch (InterruptedException e) {

To hava an adjustable simulation speed, you may want to create say a slider (as the picture shows). To do this include the following bit of code in this in the setup() method:

public void setup(){
// Adjust the simulation speed durring the model run

class SimulationSpeedListener extends SliderListener {
public void execute() {
simulationSpeed = value;
modelManipulator.addSlider("Simulation speed", 0, 100, 10, new SimulationSpeedListener());


Where simulationSpeed refers to a int set in the main program.

Tuesday, August 22, 2006

Regional Science

I just attended Regional Science Annual conference in Jersey, where I presented a paper entitled ‘Experimenting with cities, integrating Agent-Based Models and GIS: Applied to segregation’ The abstract of the talk was:

Cities are faced with many problems such as urban sprawl, congestion, crime, segregation, etc. They are also constantly changing. Computer modelling is becoming an increasingly important tool when examining how cities operate. Agent-Based Models (ABM) provide an environment to test different hypotheses and theories to urban change based on the individual behaviours of agents, thus leading to a greater understanding of how cities work.

This paper presents the development of generic agent-based simulation model for the examination of urban issues focusing on the integration of Geographical Information Systems (GIS) and ABM where space plays a central role, for past ABM have been criticised for not being spatial as they have been developed outside geography in other social sciences (Torrens 2003). The model allows for global patterns emerging from local interactions of individual agents. Using the generic framework, models can be developed rapidly to examine urban issues. One of the types of models will be presented: a segregation model. The model was created with only minor alterations to the basic model structure, highlighting how this approach can be applied to different styles of urban models.

Batty et al. (2004) write that it is hard to find clear examples of segregation process taking place, because it only becomes noticeable when it is clearly underway, and by then a detailed chronology becomes impossible to reconstruct. Schelling (1969) presented a model on the emergence of segregation where he showed that with mild preferences to locate amongst like demographic or economic activity groups, strict segregation would emerge unknowingly. This segregation is all too clear when one walks around the urban area, there are clusters of economic groups and residential groups based on ethnicity or social class.

To highlight this idea of segregation based on simple tastes and preferences, a simple segregation model has been created inheriting much of the features from the generic model. Individuals are given the same initial starting conditions but different preferences for their neighbours. What is clear is that with different percentages of similarity wanted, different patterns will emerge. However these patterns change as time passes, as the agents move to find areas in which they are happy to live in, thus changing the composition of the neighbourhood and the overall appearance of the system. Unlike the traditional segregation models, space is not restricted to discrete homogenous cells, more than one agent is allowed per area, and more intelligent movement, and searching mechanisms are introduced (e.g. agents don’t just move to random locations or the nearest empty cell). System dynamics are introduced with agents entering and leaving the system. To conclude the paper will present future avenues of research.

The PowerPoint of the talk can be downloaded from here:

Wednesday, August 09, 2006

How to create a .pgm file from an .ascii file

Creating a .pgm (portable greymap) file from an .ascii (ESRI Grid format) file, and vice versa, is a very simple task; if you know how to. I stumbled across this problem when I was trying to construct an Object2DGrid within the agent-based modelling toolkit Repast. There are three ways to construct an Object2DGrid: 1) specify the X and Y dimension; 2) construct from an input stream; or, 3) construct from a file. At present only a .pgm file can be read in as a stream or from a file location. This is fine if you wish to create a very basic agent space similar to those available in the Repast demo’s (e.g. Sugarscape) using a graphic’s package (Adobe Photoshop, Paintshop Pro, or GIMP (freeware) can all save as a .pgm file format). However, what happens if you have a raster file within a GIS (e.g. a digital elevation model, DEM), and you wish to use this within you repast model. For example, ArcGIS can only export a raster file (using ArcToolbox) as an .ascii file format. This is awkward because once you have converted a file to .ascii you can no longer view, or edit the file in ArcGIS, or any graphics package. Fortunately, there is a quick solution; just alter the header of the file and change the file extension and to .pgm and you will be able to open and edit the file in a graphic package..

This may seem far fetch, but this situation can arise. For example, Figure 1 is a .shapefile of a building floor plan. This was converted from ‘feature to raster’ using ESRI’s ArcToolbox so the ABM could use the underlying cell values to dictate the movement of the agents (Figure 2). Unfortunately, the raster file can only be exported as an .ascii file from ArcGIS. Figure 3 is a screen capture of the file viewed as text. However, the file can be converted by altering the header in Figure 3 to the header in Figure 4, and changing the file extension from .ascii to .pgm. The value ‘P2’ in the first line is always required, and ‘260’ and ‘260’ refer to the number of columns and rows, while the value in the third line refers to the highest value within the file (for scaling the values to be displayed). The third row should be set as either: 1, 3, 7, 15, 31, 63, 127, or 255. If this value is set considerably larger than the highest value within the file there will be less differential between the values (in terms of grey) to be displayed, and this may result in the image appearing as completely black.

Figure 1: ArcGIS .shapefile of a building floor plan.
Figure 2: Original .shapefile converted to a raster.

Figure 3: Raster file exported as .ascii file format.

Figure 4: Raster file altered to a .pgm file format

Thursday, August 03, 2006

Repast Models of the Week:The SLUDGE and SluGIS models

The SLUDGE model (Simulated Land Use Dependent on Edge Effect Externalities) was developed by Dawn Parker. The combines a simple cellular automaton and agent-based model which was designed to study the joint influence of distance-dependent spatial externalities and transportation costs on patterns of land use. Further information on her work can be found at discussed in the following papers:
A Repast model of SLUDGE has also been programmed by Robert Najlis. The source code for the original model can be downloaded from here. Alternatively if you viewing this post on a computer using windows, you can run the model directly by clicking here.
Robert Najlis has also developed a model called SluGIS which is a closely-related model of of SLUDGE. With the major distinction as it is a move away from the cellular space used in the previous model. SluGIS runs on a vector landscape. Robert Najlis has used Repast GIS functionally (and produced the same model both ESRIs and OpenMap interfaces of Repast).
A modified OpenMap version can be run by downloading the following and unzipping the following Shapefile package (download Shapefiles) and storing it to disk. Then by running the model by clicking here and choosing the shapefile you just saved to disk.
The original java files for the OpenMap and ESRI SluGIS models can be found here. A modified ESRI version which was created by Michael Leamy can be downloaded from here. All that needs to be done is to change the path names for the .shp file, the .gal file, and the refresh call. Look for this in
For more information on Repast GIS see the Repast website “How to use GIS data with Repast”.