Lab 8: Predator-Prey Simulation

The goal of this lab is to experiment with a biological simulator, and especially to see how polymorphism is useful in its design. This write-up assumes that you have already done the pre-lab reading.

1. Setup and introduction

In class on Monday, your received a printed copy on the Agent interface and the three classes that implement it, along with the needed declarations from class SimulationGrid. You can look at javadoc with information you need about the Agent and SimulationGrid classes here.

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

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

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.

2. Initial experiments

You should already be familiar with the code. Recall that each of the agent classes implements the Agent interface, with almost all of the interesting behavior in the method act().

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 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 &

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

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

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.

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 lab10 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: Tue Mar 18 10:09:18 CDT 2014