Herbivore haven

Let's open a new Jupyter Notebook. Before running a full-blown simulation, let's test whether our classes behave as we expect them to, starting with the Animal class. First of all, let's import both classes and initiate one instance:

import random
from animals import Herbivore, Island

A = Herbivore(10)

Now, let's run a few tests on it, checking how our ecosystem ages and breeds:

>>> A.age
0
>>> A._age()
>>> A.age
1

So far so good—the initial value of the animal's age should be 0, and incremented every time we run the _age method.

Now, let's check breeding. We used the random.seed method, which pins the following outcomes of the random functions in the package in the following code. This allows us to reproduce the exact results without compromising the randomness:

>>> random.seed(123)
>>> A2 = A.breed()

>>> A2.survival_skill
9

As you can see, the new animal has a survival skill of 9— one "skill" point away from it parent's; it seems to be correct. 

Now let's create an instance of Island and test it. Take a look at the following snippet; here, we initiate an island with a population of 10 and a population cap of 100:

I = Island(initial_pop=10, max_pop = 100)

Next, we check the initial year of the island and the number of animals on it:

>>> I.year
0

>>> len(I.animals)
10

Seems to be fine! We could add a few more tests, but let's cut straight to it and run the simulation. All we need to do is just run compute_epoches for a given number of years—we'll take 15:

stats = I.compute_epoches(15)

Feel free to investigate the results. However, as we've built a probabilistic model based on random chance, we'd better simulate multiple systems at once and estimate the distribution of results, rather than one outcome, which may or may not be representative of the system as a whole. It is simple—all we need is to simulate multiple islands at once, with the same initial conditions and store all the resulting statistics together. In the following snippet we declare the parameters of the systems, and then simply create a list of islands (archipelago?) and store the statistics from each of them:

params = {'initial_pop': 10, 'max_pop':100}
years, N_islands = 15, 1000

islands = [Island(**params) for _ in range(N_islands)]
stats = [ island.compute_epoches(years) for island in islands]

The resulting statistics, en masse, should be more reliable for analysis. And, if anything, adding more islands to the simulation is very easy.

Later in this chapter, we will visualize and discuss those metrics. Before that, let's add a little more complexity to the system. In the default settings, survival_skill is declared, but never used—all the animals survive until they die of old age. In the following section, we'll add another type of island—harsh ones, where the weather conditions will constantly
kill the animals whose survival skill is too low.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.69.172