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

Monday, December 3, 2007

Repast - Wave 2

This is a more complex situation for the wave.

Here we have the agents (green) and a lot of other feature layers.

The agents travel from their home locations (pink) to the safe points (red) ... not always through the safest path.


Saturday, November 3, 2007

Repast - Wave

Here goes a glimpse of the wave we're going to use with the simulation.

It uses geographical information to calculate it's boundaries, and a set of marks collected from the output of the scenario fluid simulation.

The progression of the wave follows a cellular automaton model to simulate the progression of a fluid between the marks.

Monday, August 13, 2007

Repast Space: Windows Dynamics

So we have a window that can float over the Object2DGrid and display the objects inside its area.

But this only makes sense if there's a possibility of moving the window over the grid. We'll do that using events - from the keyboard to start with.

Events comprehend a programming paradigm commonly used in interactive programs. Repast provides ways to use event handlers through the registration of adapters, in this case, keyboard adapters.

As an example, we show how to handle events from the directional keys in order to navigate through the space with a display window:

ModelBuilder.java


// The default move values.
int MOVE_Y = 10, MOVE_X = 10;

...

private void moveViewWindow() {
display.setViewWindow(viewX, viewY, width, height);
DisplaySurface surf = model.getDisplaySurface();
surf.updateDisplayDirect();
}

public void buildDisplays() {

DisplaySurface surf = model.getDisplaySurface();

surf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
if (viewY < space.getY() - MOVE_Y)
viewY += MOVE_Y;
break;

case KeyEvent.VK_UP:
if (viewY > MOVE_Y - 1)
viewY -= MOVE_Y;
break;

case KeyEvent.VK_LEFT:
if (viewX > MOVE_X - 1)
viewX -= MOVE_X;
break;

case KeyEvent.VK_RIGHT:
if (viewX < space.getX() - MOVE_X)
viewX += MOVE_X;
break;

default:

}
moveViewWindow();
}
});

display =
(ShapefileSpaceDisplay)space.getWindowDisplay();
width = display.getWidth();
height = display.getHeight();

surf.addDisplayableProbeable(display, "Window");
surf.display();
}

This will handle the keyboard events, triggered by the directional arrows, thus moving the display window over the space.

Every time the user moves the window, the area covered by it will change and so will the objects to be drawn.

Repast Spaces: Windows

Repast offers a set of space geometries to be used in the simulation. The concept of a space is now required for every simulation, i.e., some simulations may represent only space independent relationships between agents.

Our agents will require information about distances, spaces and relative positions. So we will require a space. Our space, until further notice, will be a grid of objects: Object2DGrid. This grid is actually a double layer grid, representing, at each position, one Object and one Integer value. The integer value can be useful to store information(altitude?) about a certain cell of the space.

There are some issues to be taken into account. The main problem is the size of that grid. It'll be huge! One approach could be the resize of the cells in way that they could fit the screen, but that would make the whole display unusable.

While the final goal is to have a display interface completely independent of the simulation architecture, the development task must have ways of inspecting the whole set of cells, with a useful resolution. So we need a floating window display over this grid.

This window will work as the display return by the space in the getDisplay method:

Space.java:
public Displayable getWindowDisplay() {
int x = INITIAL_X, y = INITIAL_Y;
return new WindowDisplay(Grid, x, y,
width, height, maxX, maxY);
}


x and y will be the origin of this window's initial position. Both width and height variables define the size of the window, maxX and maxY represent the maximum X value and maximum Y coordinate value, respectively.

This window must be able to draw every object placed between the rectangle defined by the upper left point (x, y) and the lower right point (x + width, y + height).


This means that this windows must scan this whole area. Other solution would asking every object if they are inside this particular area, but it's expect that the total amount of object largely exceed the number of cells inside this window. In order to do this scan, concurrency precautions must be taken into account, as explained before.

Next we'll explain how to move the window using (keyboard) events.

Tuesday, August 7, 2007

Repast and Concurrency

Repast uses a set of threads while executing the simulation model. Whether they are used in the simulation task or in the visualization processes one thing is certain, there are some objects (data structures) that may be accessed at the same time by different threads. This introduces some concurrency issues.

As an example, imagine a Object2DGrid. This grid is used to place the Agents. Lets suppose that the drawing strategy consists in scanning this grid, looking for Objects to draw. If the grid is scanned with no thread safety precautions, many errors will pop! These errors are due to the simultaneous access of that grid.

To face this problem we can use some thread safety techniques. As we're using Java, we can use some constructions offered by the language.

Example:

...
Object2DGrid grid;
...
Drawable d = null;
synchronized (grid) {
for (int y = 0; y < grid.getSizeY(); y++) {
for (int x = 0; x < grid.getSizeX(); x++) {
d = (Drawable)grid.getObjectAt(x,y);
if (d != null)
d.draw(g);
}
}
}

In this example we have the exact situation described above. The whole statement must be executed after the lock on the grid is captured.

Tuesday, July 10, 2007

Repast: Organization 2

Repast suggests an architecture to be used while designing a simulation. Basically every simulation is divided, in it's simpler form into a Model Object and an Agent Object.

While this shows how Repast is organized in it's basic structure, does little in aiding the design of a robust architecture.

The way we're organizing our simulations is as follows:
Model Object
ModelBuilder Object
Space Object
Agent Object

The Model must define the parameters of the simulation and works a central point in all the process. So this connects the Agents with the Space and both with the parameters defined by the user. It also specifies the schedules for the different actions of the Agents.

The ModelBuilder encapsulates a set of operations within the Model. The point of this separation is that the set and get of parameters is a quite extensive block of code. We reserve the Model Object to this operations.

The get and set operations works as the Java standard get and set operation. If we have a integer variable named fooBar, it should have the corresponding get and set operations:
public int getFooBar()
public void setFooBar(int i)

Note the capitalized F in the name of the function and uncapitalized f in the name of the variable.

So the model has, besides the get and set operations:
- a setup() function, defining what should be performed (independently of the user set parameters) in order to allow the construction (build) of the Model.
- a begin() function, defining what should be built (dependably of the user set parameters).

These two functions are required by the platform and are attached to the buttons on the interface. This is why one must setup and then initialize every time the simulation is started.

fig.1 - Repast bar

The 8th button is the setup button. It's attached to the setup() function.

The 5th button is the initialize button. It's attached to the begin() function.

Actually, the first time a simulation is played it only needs to be initialized. This gives another perspective of the setup() function. It's supposed to tear down the model. This means it should destroy every instance that is going to be set in the begin() function later on.

We actually make every instance null, after an eventual dispose() operation and call the System.gc(). This forces the garbage collector to clean up and free memory. After this we create the required new instances. Note that the space is not usually destroyed/created here. Here we manage the Agents data structure, the Schedule and the eventual displays. The space has it's own set of operations.

So the begin() function is pretty complex. It involves building all the main components of the model. These are the Space, the Agents, the Schedule and the Displays. This requires four different operations, namely:
buildSpace()
buildAgents()
buildSchedule()
buildDisplay()

The ModelBuilder was thought specifically because of these operations. The begin function looks as something like this:

public void begin() {
builder.buildSpace();
builder.buildAgents();
builder.buildSchedule();
builder.buildDisplays();
}

The everything is set on the builder. This is only for organization purposes. Good practice anyway.

More to follow!

Monday, July 9, 2007

Repast: Organization

Repast is a Agent Based Simulation Platform (Recursive Porous Agent Simulation Toolkit). It was first developed by the University of Chicago. More history and related links can reached starting from it's wikipedia article.

Repast is the Simulation platform we're using to simulate the emergency plan. As far as we're concerned we need something that's well documented and, obviously, significantly tested. Repast fits the description.

It's basic structure is a set of two objects, the Model and the Agent.

The Model holds the simulation itself. It's required by the platform to actually perform any operation.

The Agent represents the simulation agent. As a Agent based environment, this one must be present.

After some weeks studying the platform we came to to conclusion that it is counterproductive to see the simulation in such simple terms.

In the next post we'll present a more robust and useful way to think while working with this (and probably other) platform.