Determining if a population of sheep is in danger of extinction due to a wolf pack
One of the most famous agent-based simulations is the sheep-wolf predation example.
The model simulates two populations of animals: sheep and wolves coexisting together on a plane. Sheep move around and eat grass that grows on the plane; eating grass gives the sheep energy. Wolves predate sheep and that is how they get their energy. To move around the plane costs the animal some energy. If any animal's energy falls below 0, it dies.
In this recipe, we will build a 300-by-300 plane (grid) and populate it with 6,000 sheep and 200 wolves (initially). We will also introduce the concept of inheritance: we will create a generic Animal
class and then derive Sheep
and Wolf
classes from it. The idea behind it is simple: as the animals share some common characteristic (that is, both of them need to move around the plane), we do not have to implement the same code in two places of the script.
Getting ready
To execute this...