Lab 9: Predator-Prey Simulation

In lab this week you and your partner (or partners) will be experimenting with a biological simulator that was introduced yesterday in class. This is an exercise in using subtype polymorphism.

1. Background

In class on Wednesday, your received a printed copy on the Agent interface and three classes that implement it, along with the needed declarations from class SimulationGrid. The subclasses of Agent represent the various species occupying the simulated world, which is represented by the SimulationGrid. As the name suggests, the world is modelled as a rectangular grid of cells, where each cell can be occupied by at most one Agent.

On each tick of the clock, the simulation calls the act() method for each agent in the grid. That agent can examine the world around it (via methods on the grid), update its state, and, if appropriate, move to another cell.

This is similar to the ball-catching project. In that program, you are writing the Player class, which corresponds to an agent in this simulation. The constructor for a Player was passed its coordinates in the grid and the GridInfo for the grid it was in. The relationship between various agents and the grid here is similar: an agent can examine locations in the by calling three methods on SimulationGrid:

Conveniently, these methods all do the right thing when passed a position that is outside the grid: both isOccupied() and isOpen() return false, and getAgentAt() returns null.

In the ball-catching game, the Player never updates the grid directly; the only way that it could interact was to return a new position to move to, and the simulation took care of everything else. In this program, an agent must update the grid using the method

Movement requires calling this method twice: once to clear the cell where the agent previously resided (by setting the agent there to null) and then a second to occupy its new location. Because the grid allows only one agent to occupy a cell, setting the agent in a cell results in the demise of any agent that previously occupied that location.

2. Getting started

To get started, move into your csci235 directory Then clone the repository for this lab and move into the new lab9 directory.

hg clone /cslab/class/csci235/labs/lab9
cd lab9

If you list the files here (use ls -F), you'll see an item simulation, which is a directory containing the source code for the simulation package. That provides all of the code to manage the grid and the windows that display it. You will not need modify any of this, but you may look at it if you're curious.

Your simulation starts out with three agent classes, implementing behaviors of Clover, Rabbit, and Fox. Even before you look at the code, you can imagine the strategies of each:

The things that you and your partner(s) will be doing today include:

  1. Read the code for the various agents to understand how each implements its strategy (and to understand the fine points of that implementation).
  2. Run a series of experiments with various initial populations to see how they turn out. (Do any species go extinct? Are there any peculiar behaviors that need to be explained?)
  3. Make some changes to the simulation. For example, you might add one or more new species to the simulation. You also might do something like impose a maximum lifetime on a particular species, or you could model effort (movement) as reducing weight, so that a particular instance could die of starvation. This is a chance for some imagination.

You will probably want to make use of the printed copy of the classes that you received in class. As you work, write down what you do and what you observe, because you'll need to produce a written report.

3. Initial experiments

There are four methods that an agent can call on the SimulationGrid in which it lives. Recall that the methods for examining the grid can be safely called with positions that are out of bounds: off-grid positions show up as neither open nor occupied, and getAgentAt() returns null. You must make sure that the position you supply in a call to setAgentAt() method must be valid.

Compile PredPrey.java and the species files. (Note: You will get a warning message about "unchecked or unsafe operations" when you compile PredPrey. You may ignore that.) Now run the simulation. You can select between a small view or big view, by using the -small and -big flags, as in

java PredPrey -big &

The big view gets you a world that is 180x120 cells; the small view's world is 60x30.

Try running the simulation with various initial populations. Try to find an initial setup where you cannot predict the outcome—where the outcome partially depends on the random placement and movement on the agents; for example, you might find that with 100 clover, 20 rabbits, and 5 foxes, sometimes the rabbits will become extinct, and other times the clover becomes extinct.

Take notes regarding your observations about the patterns of behavior. If you notice any peculiarities, can you explain why they happen? Can you think of a way to eliminate any oddities? (If nothing looks strange to you, you might look here.)

4. Initial changes

If you observe that some species has a particular bias—for example, that the foxes always eat all the rabbits before the rabbits eat all the clover, or if clover always becomes extinct—then you can adjust the reproductive threshold, speed, or visual acuity of one or more species to try to put them on more equal footing. Make notes about how these kinds of tweaks affect the overall behavior.

5. Major changes

Now you and your partner should think about what major changes you would like to make to the system to make it more interesting. (And don't forget that you can use hg commit to save the state and hg revert to back out of unhappy experiments.) Some obvious directions include the following:

There are two things that may not be immediately obvious: how an agent dies and what is required when a new agent is created. For ideas about death, look at what happens when prey is eaten (as when a Rabbit eats a Clover): can you figure out what makes the Clover disappear? (Hint) For what needs to happen when an agent is created, look at the code for reproduction in the provided species. (Hint)

The Java documentation includes a list of pre-defined color names that are available after importing java.awt.color.

Some ideas that students have come up with in the past:

Most changes can be made by working within a single class, adding/changing instance variables and fixing the constructors and the act() method. Where some global action is needed (as would be the case with natural disasters), you might want to do something in the step() method of class PredPrey, either at the beginning or right before the call to view.display() at the end.

When you have an idea, talk to me first so I can help you refine it and point you in the right direction on how to implement it.

As you try out your modified simulation, look for patterns of behavior. What parts seem to be realistic in modeling the real world? Can you make the system stable (no species becoming extinct, at least for a long time), or can you at least find a starting configuration where the results are unpredictable?

6. Report

Along with the code you wrote or modified, for this lab you should turn in a short report (a few paragraphs long). Talk about what you initially observed in the simulation before you modified it; what you wanted to change to make the simulation more realistic or interesting; what changes you made (including an explanation of the implementation and references to what part of the code you modified); and what you observed when running your new version of the simulation. You may write the report in a text file, but be sure to include all of your names.

7. Turn-in

Turn in a hardcopy of your report.

In addition, turn in the .java source files for your species with a command like

/cslab/class/csci235/bin/handin lab9 Clover.java Rabbit.java Fox.java

Be sure to include any new species that you have added and any other .java files that you have modified.


Thomas VanDrunen, Cary Gray
Last modified: Thu Oct 23 07:23:23 CDT 2014