Lab 9: Predator/Prey Simulation

The goal of this lab is to experiment with a biological simulator, and especially how polymorphism is useful in its design.

1. Setup and introduction

As in previous assignments, move into your cs235 directory. Then clone the repository for this lab and move into the new lab9 directory.

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

If you do ls -F, you'll see an item simulation, which is a directory containing the source code for the simulation package, which contains the grid and windows. You will not need modify any of this, but you may look at it if you're curious.

You will be working with various implementations of the Agent interface. In addition to what is in that interface, you need to know a few public methods on the SimulationGrid class:

Conveniently, the first three of these will do the right thing when you pass them a position that is outside the grid: both isOccupied() and isOpen() return false, and getAgentAt() returns null. You must, however, be sure that your xPos, yPos is in-bounds when you call setAgentAt().

2. Initial experiments

After the demonnstration of the program, the first thing you should do is glance over the code to re-familiarize yourself with it. Compile PredPrey.java and the species files. (Note: You will get a warning message about "unchecked or unsafe operations" when you compile any of your species. 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 &

Try running the simulation with various initial populations. If you observe that either foxes or rabbits have a particular advantage (say, foxes always eat all the rabbits before the rabbits eat all the clover, or if clover always becomes extinct), then you can adjust the growth rate, speed, and visual acuity of one or more species to try to put them on more equal footing. 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.

3. Experimental 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:

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:

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?

4. 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.

5. 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.


Thomas VanDrunen, Cary Gray
Last modified: Tue Nov 2 15:15:12 CDT 2010