Showing posts with label Repast Code Example. Show all posts
Showing posts with label Repast Code Example. Show all posts

Monday, January 19, 2009

Generations Model

The Schelling (1971, 1978) model of segregation is probably one of the best known agent-based models and unknowingly, one of the pioneers in the field of Aagent-based modelling (Schelling, 2006). He emphasised the value of starting with rules of behaviour for individuals and using simulation to discover the implications for large scale aggregate outcomes through the interactions of these individuals. This key feature of the model arises because the decisions of any one individual can impact in unexpected and unanticipated ways upon the decisions of others.

While the model is excellent in the sense that it uses of simple logic to illustrate how segregation could emerge, through the mild tastes and preferences to locate amongst like social or economic groups. The model has no population turnover (Fossett and Waren, 2005), households are ‘immortal’ and thus a satisfied household can reside in the same location for ever.

While I been exploring Schellings model (see Crooks, 2008), recently I cam across the "Generations Model" by Hugh Stimson which extends Schellings model so that agents have a chance of of dying and giving birth. But what is interesting about the model is that depending on how many agents of opposite type live nearby, agents may give birth to a new 'mixed' type of agent, who do not share their parent’s biases.

The model is programmed in RepastJ and can be downloaded from here (30kb). More information about the model can be found on Hugh Stimson's blog.


References:


Crooks, A. T. (2008), Constructing and Implementing an Agent-Based Model of Residential Segregation through Vector GIS, Centre for Advanced Spatial Analysis (University College London): Working Paper 133, London, England. (pdf)

Fossett, M. and Waren, W. (2005), Overlooked Implications of Ethnic Preferences for Residential Segregation in Agent-Based Models, Urban Studies, 42(11): 1893-1917.

Schelling, T.C. (1971), Dynamic Models of Segregation, Journal of Mathematical Sociology 1: 143-186.

Schelling, T.C. (1978), Micromotives and Macrobehavior, WW Norton and Company, New York, NY.

Schelling, T.C. (2006), Some Fun, Thirty-Five Years Ago, in Tesfatsion, L. and Judd, K.L. (eds.), Handbook of Computational Economics: Agent-Based Computational Economics, North-Holland Publishing, Amsterdam, Netherlands, pp. 1639-1644.

Tuesday, November 06, 2007

The Basic Immune Simulator

Just a quick note on a Repast model we recently found called the Basic Immune Simulator developed by Charles Orosz, Virginia Folcik and Christina Sasswas utilizing RepastJ. The model was developed to explore the behaviour of the immune system as a complex system.

The site provide the source code of the model and instructions to run the model. Which addresses some of the key issues with regard to sharing information with the social sciences which Axelrod (2007) recommends.


Further information and model code can be seen at the Basic Immune Simulator website and an article

Folcik, V.A., An, G.C. and Orosz, C.G.(2007) The Basic Immune Simulator: An agent-based model to study the interactions between innate and adaptive immunity. Theoretical Biology and Medical Modelling, 4:39.

Axelrod (2007) ‘Simulation in the social sciences’ inRennard, J.-P. (ed), Handbook of Research on Nature-Inspired Computing for Economics and Management, Idea Group Reference, Hershey, PA.


Wednesday, April 18, 2007

Creating Slider bars in Repast

Slider bars (as in the image above) are a easy way to change model parameters of a simulation. Someone asked how to create a Slider Bar within a Repast model, so I thought I would share it. To create one is relatively straight forward. All you need to do is place this piece of code in the setup method:

//creates a slider which has to be an int.
RangePropertyDescriptor pdMovement = new RangePropertyDescriptor("Movement", 0, 1000, 200);

descriptors.put("Movement", pdMovement);

Where “Movement” relates to the “Movement” parameter in the getInitParam method. For example:

String [] initparams = {"PerAgents", "Movement",
};
return initparams;
}

Further information on PropertyDescriptors can be found on the Repast Website under "How to Create PropertyDescriptors"


Wednesday, April 11, 2007

SIMSEG

The SIMSEG model is an extension of the Thomas Schellings segregation model. It allows users to explore ethnic diversity in terms of number of groups and the relative sizes of each group, specify ethnic preferences, specify the level of socio-economic inequality within and between ethnic populations and to specify agents preferences for neighbourhood status and housing quality within a cellular space. Neighbourhoods are bounded by larger areas to evaluate neighbourhood ethnic mix and agents can only move to areas where they can afford.

A free trial version of the SimSeg Learning Edition can be downloaded from the website: http://simseg.com/download/index.htm and the site also provides a useful review article of theoretical perspectives on residential segregation in American urban areas along with other resources.

If you interested in segregation, there are a whole host of other models available on the internet. For example Schelling Segregation Model created by Chris Cook, written is C# (the source code is available), A java applet created by Raj Singh, A NetLogo model or a Repast model created byGirardin et al., (2006) .

Further Reading:

Fossett, M. and Senft, R. (2004), 'SIMSEG and Generative Models: A Typology of Model-Generated Segregation Patterns', Proceedings of the Agent 2004 Conference on Social Dynamics: Interaction, Reflexivity and Emergence, Chicago, USA, pp. 39-78, Available at http://www.agent2005.anl.gov/Agent2004.pdf


Thursday, March 29, 2007

PropertyWindow class

There was this message on the Repast mailing list the other day:

Dear list,
I want to probe some variables of my agents of the OpenMap Layer. I did this by making the following method:
public String[] gisPropertyList() {

String []gisPropertyList = {"Lat/Lon","getLatLonPointString","nativeCountry","getNativeCountry",

"residence","getResidence","age","getAge","gender","getGender" };


return gisPropertyList;

It works, however when I probe one of my agents I get the screen below. It seems like something is on top of the last fields. How can I fix this? Thanks!


I had exactly the same problem as this is to do with the PropertyWindow class which only permits 4 items to be displayed in the anl.repast.gis.display package. Click here to see my modification that works around this problem. I hope this helps.


Tuesday, February 13, 2007

Pedestrian Fire Evacuation Model

I found this model a few months ago, its quite good as it shows how pedestrians exit a building when a fire is spreading through it. The model is written in Repast and makes use of .xml and txt files. While it is extremely basic, the model highlights how ABM can be utilized to examine emergency egress from a building. On a side note, CASA has a long history of pedestrian modelling using ABM (Click here to see more along with checking out the CASA working papers)


Unfortunately I don’t remember where the original source code came from but if you wrote it please let me know so I can give you full credit.

The model can be run by clicking here (Sorry it will only work in Windows). For instructions on how to run a Repast model see the Repast website. Alternatively the original source code can be downloaded from here.

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.

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