CMPT 126 - Harbour Centre - Fall 2006
Home  
Assigned Readings  
Examples  
Labs  
Assignments  
References  
Gradebook  
 
 

In this assignment, will implement an individual-based model of animal behaviour. This is an extension of the work you did in Lab 7. You can use your solution to Lab 7 as a starting point, or you can use this solution.

The Problem

The general problem statement is the same as for Lab 7:

Biologists often use computer models to simulate behaviour of animals. Computer models are specific descriptions of behaviour that can be implemented in a computer program. The programs can be run to test assumptions against real data and used to generate new hypotheses that can be tested experimentally.

In Individual-Based Models, many individual animals are simulated separately. Our model will simulate (let's say) Zebras grazing.

The zebra's (rectangular) range will be divided into a grid of small habitats. Each zebra will be in one of the habitats. In each time unit, a zebra can move to an adjacent square in one of the four compass directions (north, south, east, west), but can't move off the edge of the grid.

In each of the habitats, there is a certain amount of food (grass). Of course, as zebras eat the grass, it disappears. The grass does slowly regrow as time passes.

When zebras eat, they gain energy. If they don't eat for long enough, they burn all of their energy reserves and starve to death.

Zebras decide which direction to move (or to stay where they are) based on the amount of food available, and the number of other zebras in that it will have to share the food in that location with. The zebra always makes the best decision based on what it can see in the adjacent habitats.

As with any model, there will be many parameters that can be adjusted to tune the way the system works. For example, the amount of grass that a single zebra eats. It may be necessary to change these values frequently, so the design should allow for this.

Further refinement to the system:
  • We will assume that the time unit of the model is one day.
  • For each individual, we will have to keep track of its current energy reserve level. This is roughly equivalent to its size (or fat stores, or whatever). There will be a maximum value for the reserve level, indicating the fattest a zebra can get.
  • The amount of grass in each habitat will be controlled by parameters to the model. Both the initial amount of food and the rate of regrowth will be indicated.
  • The units for the amount of grass in a habitat and the energy (size) of the zebra will be the same. So, if the zebra east one unit of grass, it gains one energy unit.
  • At the start of the simulation, each zebra will have an energy level of half of its maximum.
  • At the start of the simulation, the zebras will be placed at random positions.

Moving

Each day, a zebra can make one of five decisions: stay where it is, or move one habitat north, south, east, or west. Of course, if the zebra is at the edge of the range, some of these decisions won't be possible because it would lead it off the edge of the world.

The choice will be made based on the amount of food it can eat in each of the habitats available from the five choices. The amount of food available to a zebra in a habitat is: the total amount of food in a square divided by the number of zebras there (including itself).

A zebra can only eat so much food in a day. Any amount of food beyond that is irrelevant. So, any squares with more food available than can be eaten in a day have equal quality as far as our zebras are concerned.

Running around from habitat to habitat burns energy, so there will be an energy cost to moving to another habitat. So, the quality of the four moving options will be decreased by the cost of making the move. If the animal does decide to move to another habitat, their energy reserves will decrease by this amount.

Eating

Each day the zebra will try to eat. The amount of food it actually gets will be determined by the amount of grass in the habitat. The zebra will eat as much grass as possible, up to the maximum amount it can eat or the amount available in the habitat.

The amount of grass that is eaten is removed from the total amount in the habitat, and added to the energy reserves of the zebra.

Dying

Sadly, not all of our virtual zebras will survive. If a zebra's energy level falls to zero, it dies. Dead zebras should be removed from the simulation. Dead zebras have no chance of being resurrected.

Thoughts

Some thoughts about how realistic this model is...

Our zebras don't reproduce. That means that the model will settle in a state where there is a lot more grass available than zebras to eat it. Adding some kind of reproduction to the model would be more interesting, but would add significant complication. [We'd have to keep track of boy and girl zebras for one.]

The way we have had the zebra determine the quality of a habitat assigns a penalty to a square with other zebras present. This will tend to keep the individuals spread out. This isn't terribly realistic: real zebras tend to hang out in herds for safety. A better habitat quality function could encourage the zebras to herd, but still move as a herd to good food sources.

The Assignment

Design

Create a file design.txt that extends the initial design from Lab 7 (using either your design or the one from the solutions). In this file, indicate the classes that you will create, and the public methods they will have. For each, briefly indicate what they will do.

This must be completed before you begin the implementation. It does not have to exactly match your final implementation.

Implementation

Create a Java classes that implement your design and the model as described above.

At each time step, print some output that will let the user keep track of their model. This could include a representation of the grid of habitats. For example, for each habitat, you could do something like this:

nums = "";
for( int i=0; i<zebrasAt(x,y); i++ ) {
    nums += "*";
}
s = s + String.format("%5.1f%-4s", amountGrass(x,y), nums );

That will create output like this, indicating the amount of grass and number of zebras in each location:

 13.8      8.3*     4.0
 10.1      4.8      1.5*
 92.1     18.9****  5.8

If possible, we will provide a class that you can use to easily create a GUI window representing this information. But, you should continue assuming this won't happen.

Hints and Tips

The problem is big, but dividing it intelligently into classes should make it much easier to attack. Don't forget to test individual classes and methods as you go.

Create a separate class for the model parameters. That way, they will be centralized (and can be easily changed), and will be accessible from all of your other classes. You can use a sample Param class to get some values for the parameters that actually do something interesting.

Dealing with positions and moving around can be simplified a lot by creating some classes to deal with them. You can use the provided Direction enumerated type and the Position class. You can then (for instance) determine the quality of all of the possible adjacent squares like this:

Position p;
for( Direction d : Direction.values()) {
   p = currentPosition.move(d);
   if ( p != null ) {
    qual = quality(p);
    }
}

This uses two features that are new in Java 5.0 (and are thus only covered in the new edition of the text): enumerated types (section 3.7) and the new style for-loop that uses iterators (section 5.8).

Submitting

All of your assignments in this class will have some marks allocated for style. This will include easy to understand and modify code. In particular, you should use comments where appropriate, use good variable names, split your code into methods/functions logically, and indent consistently.

For this assignment, there will also be marks for the design of the system.

When you're done, create a ZIP file containing all of the files you created for this assignment (no .class files) and any of the supplied .java files that you used. Submit it with the submission server.