Understanding the Existing Program
My next step was to meet with Jamie, an experienced programmer who developed the initial version of the program (and the two demos). Jamie helped me understand how the biologists were using the program, as well as how the program worked inside.
The Big Picture
For modeling purposes, the biologists think of the environment as a rectangular grid, with fish moving from cell to cell in the grid. Each cell contains zero or one fish.
A simulation is often represented as a series of repeated timesteps. In this case, in each timestep every fish has an opportunity to move to one of its immediately adjoining neighbors, but only if the neighboring cell is empty. Confirming my earlier conclusions, Jamie told me that fish in this simulation only move forward and to the side; they never move backward. Thus, the fish in row 3 and column 2 (also known as location (3, 2)) in the illustration above has two neighboring locations to which it could move, the one in front of it and the one to its right (locations (3, 3) and (4, 2)). The fish in location (4, 8) has one location to which it could move, the one to its left.
One of the purposes of the simulation is to help the biologists understand how fish movement is affected by the size and density of the fish population. To do this, they run the simulation many times with different initial fish configurations. The program reads a file that specifies the size of the environment, the number of fish, and their starting locations. By changing the file read in by the program, the biologists can run the simulation with different initial conditions and see what happens.
The Design
As I had discovered when looking at SimpleMBSDemo1
and
SimpleMBSDemo2
, to model fish swimming in a
bounded environment, the Marine Biology Simulation program has
Fish
objects and an environment object modeled as a
BoundedGrid
. The purpose of the program is to simulate fish
moving in the environment, so SimpleMBSDemo2
also has a
Simulation
object.
The program also has classes to implement its graphical user interface, but I was not going to be modifying that, so we didn't spend much time talking about those.
In each timestep in the simulation, each fish has the opportunity to move to an adjoining location in the grid. This leads to a number of design questions. Who is responsible for keeping track of the fish in the environment? Who is responsible for knowing where a fish may move? Who is responsible for actually moving a fish? Who is responsible for knowing what behavior (moving, aging, breeding, dying, etc.) is required as part of the simulation? Jamie had considered several options and decided on the following design.
- The
Fish
class encapsulates basic information about a fish (color, location, and so on) and basic fish behavior. For now, this behavior is just moving to an adjacent cell. - Originally, Jamie had planned to create an
Environment
class to model the environment in which the fish move, but then realized that there is nothing about the behavior of the environment (which is just a container for the fish objects) that depends on the objects in it being fish. They could be band members marching on a field, physics particles moving in a small space, or any other object that you might want to model in a bounded, grid-like environment. As a result, Jamie decided to use theBoundedGrid
class from the Grid Package (an existing collection of classes) to model the environment. The Grid Package also includes classes that could be used as the basis for the graphical user interface. -
The
Simulation
class represents the running simulation, specifying the behavior that happens in every timestep of the simulation, fish movement in this case. Of course, the fish have to know how to do whatever it is that the simulation wants them to do, so theSimulation
and theFish
classes share the responsibility for knowing what behavior is required in the simulation. - To run the program, something needs to construct the initial
objects and get them going. This is often called a driver.
I had already seen the code for the
SimpleMBSDemo2
driver, and had run theMBSApp
driver, although without looking at the code.
As I learned, the Marine Biology Simulation has more than four classes,
but these four — the driver, Simulation
,
BoundedGrid
, and Fish
— constitute the
conceptual core of the design.