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.

No comments: