CS 231

Assignment 3: Greenfields II

Due 7 November 2007

Overview

For this project, we will be returning to the sugarscape concept to simulate a predator-prey relationship. The scenario will involve hawks, squirrels, and trees. The hawks eat the squirrels, the squirrels depend on the trees for food and shelter, and the hawks also require the trees for shelter and nesting.

The primary data structures we'll need for the greenfield are a 2D array and linked lists. Unlike project 1, each location in the greenfield will be able to hold multiple agents (e.g. trees, squirrels, and hawks). We will use a list at each greenfield location as a container for the agents located there. We will also implement some form of search method on the lists, as we will be querying the lists to see if there are any agents of a particular type at that location.

Setup

A partner is required for this project.

Create a new project in BlueJ, or whatever your preferred IDE is. Both XCode (on macs) and Eclipse (open source IDE that runs on many platforms) are more full-featured alternatives. Call the project something informative.

Procedure

The project is broken down in a number of small steps. If any step takes you longer than 30-60min, stop by and ask some questions.

Please test as you go. For each class, write a main method for each class that tests all of the methods you've written to ensure that they do the right thing. Print statements are extremely useful, because they let you see what your program is doing, so put in lots of them.

Maxwell's style guide provides some general guidelines for writing code.

Note that you may choose your own names for classes and variable fields, for the most part the names given below are only suggestions.

  1. As noted in class, this project will have a parent Entity class as well as individual child classes for each type of Entity: Hawk, Squirrel, and Tree. Use the Entity class provided here.

  2. Create a Hawk class that extends the Entity class. The Hawk class also needs to implement the Predator interface and the Critter interface. The interfaces contain all of the methods required by the simulator. In addition, the Hawk class will need a public static final int Type field, and constants for the birthrate, deathrate, and hunt success rate.
  3. Create a Squirrel class that extends the Entity class. The Squirrel class also needs to implement the Prey interface and the Critter interface. The interfaces contain all of the methods required by the simulator. In addition, the Squirrel class will need a public static final int Type field, and constants for the birthrate, deathrate, and dodge rate.
  4. Create a linked list data structure, EntityList, that holds objects of type Entity. The methods you will need to implement include:
    • public EntityList() - constructor that specifies the maximum number of elements that can be in the list.
    • public int size() - returns the number of elements in the array.
    • public boolean empty() - returns true if the list is empty.
    • public void clear() - clears the list of all entries.
    • public Entity peek() - returns the Entity at the front of the list, or null.
    • public void insert(Entity A) - inserts the agent into the list
    • public void remove(Entity A) - removes the agent referenced by A from the list, if it is on it.
    • public Entity get(int type) - returns an Entity of the specified type, if one exists on the list, otherwise null.
  5. You will want to make the list implement the Iterable<Entity> interface by implementing an Iterator<Entity> for it. The Iterator does not need to have a working remove function. Implementing a remove function qualifies as an extension.

  6. Create a new Greenfield class that contains a 2D array of type EntityList. The Greenfield class needs the following methods.
    • public Greenfield(int rows, int cols) - makes a Greenfield of the given size and creates a new AgentList at each location.
    • public EntityList get(int r, int c) - returns the EntityList at location (r, c).
    • public void put(Entity e) - inserts the Entity into the list at the Entity's location.
    • public void remove(Entity e) - removes the Entity from the list at its current location.
    • public int count(int r, int c) - returns the number of entities at that location.
  7. Create a Simulator class that brings it all together. The Simulator needs to have a Greenfield, an EntityList of Hawks and an EntityList of Squirrels. The Simulator class needs four methods.
    • public Simulator(int r, int c, int h, int s) - constructor for a Simulator that creates a simulation with the specific number of rows, columns, Hawks, and Squirrels.
    • public void initialize(int r, int c, int h, int s) - initializes the Simulator with the specified number of rows, columns, hawks, and squirrels. For the above constructor, you probably want to just call this function.
    • public void iterate() - executes one iteration of the simulation. You can design your own algorithm, if you wish, but it should do at least the following.
      1. For each squirrel on the squirrels list:
        1. Calculate if the squirrel has offspring
        2. Calculate if the squirrel dies of old age
        3. Move the squirrel
        1. Calculate if the hawk has offspring
        2. Calculate if the hawk dies of old age
        3. If there aren't any squirrels at the current location, move the hawk
        4. If there are squirrels at the hawk's current location have the hawk engage in a hunt
        5. If successful (Hunt and !Dodge), remove the caught squirrel and increment the Hawk's health
        6. Call the TurnDepletion function
        7. See if the hawk died of starvation
      2. Remove any squirrels that died
      3. Remove any hawks that died
      4. Add the baby squirrels
      5. Add the baby hawks
    • public void write() - generate a text-based visualization of your greenfield. If you want to include it in a web page, save it to a file and put <pre> before it and </pre> after it.

  8. Run your simulation on a 20x40 grid using 300 Squirrels and 40 Hawks as the initial condition. Find some birthrate, death rate, hunt success, and dodge success values that result in a fairly stable population. Some parameters to try are:
    • Squirrel birth rate of .25 and a death rate of 0.20
    • Hawk birth rate of .10 and a death rate of 0.05
    • Hawk success rate that averages to 0.75
    • Hawk depletion rate of 0.20
    • Squirrel depletion rate of 0.0 (they can always find food)
    • The Hawk gets 0.8 health on a successful hunt
  9. Try running the simulation with the opposite starting condition (40 Squirrels and 300 Hawks) and see what happens.
  10. For both of the above tests, produce a graph showing the numbers of Hawks and Squirrels at each time step.

Extensions

Writeup

Follow the writeup instructions to create a web page for your assignment. Send the instructor an email with the code in a zip or tar file as well as a pointer to the URL for the writeup.