Oop Enonce
Oop Enonce
1 Introduction
The aim of this project is to emulate the evolution of a small ocean, with fish and sharks living in it.
Classes include a class Animal, with two subclasses, Fish and Shark, and an Ocean class.
The Fish and Shark classes includes a num class variable that you can use as an easy identifier : if animal is a fish
or a shark object, animal.num will returns either 1 or 2.
A support file (OOPAssignment_support) is provided, and includes a method called show in the Ocean class to
display the animation showing the evolution of the forest. You cannot use it before the last function (tick) is
written.
A __str__ method is provided for the Ocean class for small tests, but it migth not be very useful to debug your
functions. Learn to read the error messages.
In all of the assignment, a position refers to a couple of integers. (Either a list or a tuple, with two values, the
abscissa and ordinate.)
2 First steps
The ocean is represented by a matrix, where each cell can have 3 values : either None, or an instance of the Fish
or Shark classes. For ease of visualisation, the provided __str__ method displays 0, 1, and 2’s, but it important
to remember that cells do not hold integers. You cannot deviate from this without breaking the animation.
The Ocean class also features an attribute holding a list of all the animals in it, that will be useful for the tick
method at the end.
1. Write the initialize method. It takes no argument other than self and spawns fish and sharks in the ocean.
For each cell in the grid, there is a 10% chance for a shark to spawn, and a 30% chance for a fish to spawn. If
not animal spawns, the cell stays empty. Two animals can’t exist at the same time in a cell. For each animal
spawned you need to create a new instance, add it to the grid, add it to the list of animals of the ocean.
You can use rd.randint(a,b) to get random integer between a and b included.
1
CPES2 OOP : Home assignment
Example : If we take a look at the small ocean below (let’s call it sea) :
| 0 0 1 |
| 2 2 1 |
| 0 1 1 |
3 The Animals
Now let’s take a look at our animals. We have the Animal class which holds all the methods, and the Fish and
Shark subclasses which will serve to define class variables.
For our animals we want to memorize their position, how long before they reproduce, and wether or not they are
alive. (At the end of each tick, all the dead animals will be removed).
3. Write the __init__ method for the Animal class. It takes the abscissa and ordinate of the animal as argument
and creates 3 attributes : the position, the reproduction value initialized at 0, and wether or not it’s alive as a
boolean.
4. Write the reproduce method. It takes as argument the ocean, and a position, and spawns an a new animal of
the same subclass at the indicated position. Don’t forget to add the new animal to the list of animals of the
ocean, and to reset the reproduction value of the parent.
There are two main ways to get the class of the animal in order to know which animal to spawn : you can use
the type function which return the class of the object, or you can use the num class variable of the animal.
5. Write the move method. It takes the ocean, a dictionary representing the surroundings of the animal, and a cell
type (0, 1, or 2), and moves the animal to a random adjacent cell of the specified type. (For now we assume
such a cell exists, its existence will be checked in another function).
Be careful, there a lot of things to check in this function :
— make sure to empty the cell were the animal was before moving
— if there is an animal in the cell where the animal is moving (for example if a shark eats a fish) mark that
animal as dead
— update the cell where the animal has moved, and change the position of the animal in its attributes
— if the reproduction value of the animal exceed the reproduction threshold of its specie, it reproduces in
its former cell
6. Add two class variables to the Shark class : a starting energy, and a maximum energy (with values 3 and 6
respectively, you will be able to change them once you’re done).
7. Write the __init__ method for the Shark class to add an energy attribute to sharks, with a starting value
equal to the starting energy class variable.
(The Fish class does not have a __init__ method, because it has no additional attributes compared to the
Animal class. Therefore it uses the constructor from its parent class.)
With the initial values for proportions, reproduction rate, energy, neither species should disappear. I have run
simulation of up to 1000 ticks (with the base 50 × 50 grid), but if you want to increase the length of the simulation
from the starting 100 ticks, go up in steps, as Python needs to memorize the grid for each tick before creating the
animation, meaning going for too many ticks risks crashing Python or your computer.
You can also use the function LV (for Lotka-Volterra, a prey-predator model) to plot the number of fish and sharks
through the simulation. And you can also plot the number of sharks as a ”function” of the number of fish. This is
what i got on one of my simulations with 500 ticks.