Programming Assignment:
Interacting Classes
Predator - Prey Model
Purpose
We learn how to modify actions in the model that involve collaboration
among different classes
Motivation
Biologists want to study the classic predator-prey model of interaction.
The first instance of this model being studied mathematically, using differential
equations, was in a 1920's study of the interaction of fish and sharks
in the Mediterranean Sea by Italian mathematicians Lotka and Volterra.
The biologists want to modify the model to have two species, a predator
species and a prey species. The actions of both species will include
breeding, but the predator species will not be able to breed unless it
has eaten recently. The predator species will also eat the prey species.
If the predator species does not eat enough, it will die.
Field studies of predator-prey systems, including the study of sharks
and fish in the Mediterranean Sea and lynx and hare in parts of northern
North America, have shown a cycle behavior where the interdependent populations
increase, then decrease in size. Of course, if the prey species dies
out, then the predator, having no food, will also die out. If the
predator species dies out, then the prey will increase in population as
much as possible, filling our bounded world. Biologists want to determine
whether the fish simulation model can be modified so that the interacting
predator-prey species will exhibit this cyclic behavior.
Download the following files and place them in a folder called PredatorPrey
Create new LJHS project called Predator and store the project in the PredatorPrey folder. Move fish.dat
into the PredatorPrey folder. Add all *.cpp files to your project then run the project.
If you have warnings with RandGen, remove randGen.cpp from your project.
You should get a two-dimensional array display.
Instead of displaying spaces between the fishes in the output, display a dot ('.') for the space.
Determine which class needs to be editied and then edit the file so that dots '.''s are output
instead of blank spaces.
Specifications of the Model
The basic model works as before with the actions of the fish changed.
There will be two species of fish, one of which is a predator (sharks).
Both species have an age, the number of steps since they were created.
The actions of a predator are as follows:
-
Increment age
-
If the time since the last meal is greater than a given time of starvation,
this fish dies
-
If it did not die, then the fish tries to eat.
-
the fish eats a random choice from among any neighboring prey
-
If it did not eat, then the fish tries to move
-
movement is as in the original model, into a random empty neighbor
-
If the predator is five or older, then it breeds. This is different from the last
assignment where the fish breed only during the ages of 10-20 years old. You
will have to make modifications to implement the new breeding age.
-
it creates a new fish of the same species in each empty neighboring position
with its probability of breeding, independent of the other neighbors.
-
The time since its last meal must be correctly maintained.
The actions of a prey are as follows:
-
If the prey is five or older, then the fish breeds. This is different from the last
assignment where the fish breed only during the ages of 10-20 years old. You
will have to make modifications to implement the new breeding age.
-
it creates a new fish of the same species in each empty neighboring position
with its probability of breeding, independent of the other neighbors.
-
The fish tries to move
-
movement is as in the original model, into a random empty neighbor
We note that we will need the following changes to the program:
-
Add a class Species that encapsulates the characteristics of the different
species of fish.
-
probability of breeding
-
predator or not
-
for predator, time to starve
-
for predator, time since last meal the breeding can take place
-
Modify the Environment class
-
read in species attributes for prey and sharks, read in fish, distinguishing
prey and sharks
-
modify the AddFish member function to assign age and species to each fish
-
provide a new member function to remove fish that are eaten or starve
-
provide a new member function that returns the fish at a position, so that
predators can "see" prey
-
Modify the Fish class
-
provide a new member function Act that encapsulates all the actions of
a fish and is called by the Simulation Step function
-
Act will have two main parts, depending on the species o the fish
-
change the Move function to be private, called by other Fish member functions
as needed
-
provide a new private member function Breed, that handles breeding for
all fish
-
provide a new private member function EatOrMove that handles a shark eating
-
if the shark does not eat, it moves as in the original mode
-
provide new private member functions to create the neighborhood of positions
containing prey
-
PreyNeighbors, analogous to EmptyNeighbors
-
AddIfPrey, analogous to AddIfEmpty
-
modify the ShowMe member function to distinguish sharks, '^', and prey,
'o'
-
make the random number generator a data member of the class, since it is
used in more than one function
-
Modify the Simulation Step function to use Act instead of Move
-
Modify the Display class to display the two species so that they can be
distinguished.
-
The format of the fish.dat file will be changed
-
The first line will contain two integers, the number of rows and the number
of columns in the grid, as before
-
The second line will contain the parameters for the prey species, the probability
of breeding, and two 0's for starvation's and breeding times
-
The third line will contain the parameters for the shark species
-
probability of breeding
-
time to starve
-
time from last meal that a shark can breed
-
The remaining lines will each specify one fish
-
two integers for the row and column of the position
-
one integer for the age
-
a zero for a prey, a one for a shark
Write a version of the program to implement these changes. Note:
you will need to be careful that fish that are eaten do not later try to
move if they happen to appear later in the list of fish that was passed
to the Simulation Step function. Include appropriate calls to DebugPrint
in your new version of the program to help you in debugging the program.
You will have to create your own fish.dat data file. You should have numerous
data files to test different situations. Turn in your data file, what you were trying
to test for, and the actual results.
What to turn in
- Report explaining how you implemented the additions and changes.
- Code for any header or implementation file altered, with comments indicating the
code changes. Use a highlight marker and highlight all your changes/additions.
- Output should show the original location of the fish loaded in by the fish.dat data
file, followed by the desired number of steps to prove your expected results. Note what
you were testing for with each sample data.