Lab 9: 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.

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

The first thing you should do is glance over the code to familiarize yourself with it. This version of the simulator is similar to what you saw previously for the Life game. In the Life version, every cell in the grid was occupied by an agent; every agent stayed in the cell in which it was created; and each step of the simulation first invoked plan() for all of the agents, then step(), so that all of the agents acted simultaneously. This version changes all of those features: some cells are unoccupied (so that getAgentAt() returns null), some of the species move from cell to cell, and each step of the simulation calls a single act() once for each agent. The agents therefore move one after another, with each getting one chance to act in each round.

The Clover and Rabbit classes are the same as what you received in class yesterday.

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. 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 reproductive threshold, speed, or 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. 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

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:

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 Mar 13 07:51:38 CDT 2012