Esempio n. 1
0
def test_directional_probability():
    """
    Tests that the probabilities of moving to the adjacent cells are computed
    correctly.
    """
    current_cell_pop = [{
        "species": "Herbivore",
        "age": 10,
        "weight": 15
    }, {
        "species": "Carnivore",
        "age": 5,
        "weight": 40
    }, {
        "species": "Carnivore",
        "age": 15,
        "weight": 25
    }]
    current_cell = bl.Jungle()
    current_cell.cell_population(current_cell_pop)
    jungle_right = bl.Jungle()
    jungle_left = bl.Jungle()
    desert = bl.Desert()
    jungle_right.animal_population[0].append(ba.Herbivore(weight=15))
    jungle_pop = [{
        "species": "Herbivore",
        "age": 10,
        "weight": 15
    }, {
        "species": "Herbivore",
        "age": 5,
        "weight": 15
    }]
    jungle_left.cell_population(jungle_pop)
    desert_pop = [{
        "species": "Herbivore",
        "age": 10,
        "weight": 20
    }, {
        "species": "Herbivore",
        "age": 5,
        "weight": 15
    }, {
        "species": "Herbivore",
        "age": 10,
        "weight": 35
    }]
    desert.cell_population(desert_pop)

    neighbour_cells = [desert, bl.Mountain(), jungle_right, jungle_left]

    herbivore_probabilities = current_cell.directional_probability(
        current_cell.animal_population[0][0], neighbour_cells)
    carnivore_probabilities = current_cell.directional_probability(
        current_cell.animal_population[1][0], neighbour_cells)

    assert herbivore_probabilities != carnivore_probabilities
    # The numbers below are calculated manually.
    assert carnivore_probabilities == pytest.approx([0.56, 0.0, 0.18, 0.25],
                                                    rel=1e-1)
Esempio n. 2
0
def test_update_cell_population(mocker):
    """
    Tests whether the cells animal population is updated after a migration
    cycle.
    """
    current_cell = bl.Desert()
    current_cell.animal_population[0].append(ba.Herbivore())
    desert = bl.Desert()
    jungle = bl.Jungle()
    savannah = bl.Savannah()

    neighbour_cells = [desert, bl.Mountain(), jungle, savannah]
    probability_list = [0.25, 0.0, 0.5, 0.25]
    mocker.patch("random.random", return_value=0.7)

    current_cell_old_population = current_cell.animal_population
    jungle_cell_old_population = jungle.animal_population

    current_cell.choose_migration_cell(current_cell.animal_population[0][0],
                                       neighbour_cells, probability_list)

    neighbour_cells.append(current_cell)
    for cell in neighbour_cells:
        cell.update_cell_population()

    current_cell_new_population = current_cell.animal_population
    jungle_cell_new_population = jungle.animal_population

    assert current_cell_new_population != current_cell_old_population
    assert jungle_cell_new_population != jungle_cell_old_population
Esempio n. 3
0
def test_propensity_carnivore():
    """
    Tests that the propensity function for carnivores works.
    """
    herbs = [{
        "species": "Herbivore",
        "age": 5,
        "weight": 50
    } for _ in range(4)]

    jungle = bl.Jungle()
    jungle.cell_population(herbs)
    assert jungle.propensity()[1] == pytest.approx(np.exp(4))

    savannah = bl.Savannah()
    savannah.cell_population(herbs)
    savannah.animal_population[0].append(ba.Herbivore(weight=25))
    assert savannah.propensity()[1] == pytest.approx(np.exp(4.5), rel=1e-1)

    desert = bl.Desert()
    desert.cell_population(herbs)
    desert.animal_population[0].append(ba.Herbivore(weight=68))
    assert desert.propensity()[1] == pytest.approx(np.exp(5.36), rel=1e-1)

    mountain = bl.Mountain()
    ocean = bl.Ocean()
    uninhabitable_landscapes = [mountain, ocean]
    for landscape in uninhabitable_landscapes:
        landscape.cell_population(herbs)
        assert landscape.propensity()[0] == 0
Esempio n. 4
0
def test_eat_request_herbivore():
    """
    Test if herbivores eats as expected.
    """
    jungle = bl.Jungle()
    pop = [{
        "species": "Herbivore",
        "age": 10,
        "weight": 15
    }, {
        "species": "Herbivore",
        "age": 5,
        "weight": 40
    }, {
        "species": "Herbivore",
        "age": 10,
        "weight": 30
    }, {
        "species": "Herbivore",
        "age": 5,
        "weight": 20
    }]
    jungle.cell_population(pop)
    start_weight = jungle.sum_of_herbivore_mass
    jungle.eat_request_herbivore()
    new_weight = jungle.sum_of_herbivore_mass
    assert new_weight > start_weight
Esempio n. 5
0
def test_regenerate():
    """
    Tests that fodder regenerates as expected in the landscape types jungle
    and savannah.
    """
    jungle = bl.Jungle()
    savannah = bl.Savannah()
    landscape_list = [jungle, savannah]
    for landscape in landscape_list:
        landscape.f = 0
        landscape.regenerate()
        assert landscape.f > 0
Esempio n. 6
0
def test_available_fodder_herbivore():
    """
    Tests that the available fodder for herbivores in a cell is computed
    corresponding to the given formula.
    """
    jungle = bl.Jungle()
    herbs = [{
        "species": "Herbivore",
        "age": 5,
        "weight": 40
    } for _ in range(4)]
    jungle.cell_population(herbs)

    assert jungle.available_fodder_herbivore == 16
    jungle.eat_request_herbivore()
    assert jungle.available_fodder_herbivore == pytest.approx(15.2)
Esempio n. 7
0
def test_choose_migration_cell(mocker):
    """
    Tests whether animals are migrating to the correct cell, given the animals
    probability to move to a specific cell.
    """
    current_cell = bl.Desert()
    current_cell.animal_population[0].append(ba.Herbivore())
    desert = bl.Desert()
    jungle = bl.Jungle()
    savannah = bl.Savannah()
    neighbour_cells = [desert, bl.Mountain(), jungle, savannah]
    probability_list = [0.25, 0.0, 0.5, 0.25]
    mocker.patch("random.random", return_value=0.7)
    current_cell.choose_migration_cell(current_cell.animal_population[0][0],
                                       neighbour_cells, probability_list)
    assert len(current_cell.new_population[0]) == 0
    assert len(jungle.new_population[0]) == 1
Esempio n. 8
0
def test_propensity_herbivore():
    """
    Tests that the propensity function for herbivores works.
    """
    jungle = bl.Jungle()
    assert jungle.propensity()[0] == pytest.approx(np.exp(80))

    savannah = bl.Savannah()
    assert savannah.propensity()[0] == pytest.approx(np.exp(30))

    desert = bl.Desert()
    assert desert.propensity()[0] == 1

    mountain = bl.Mountain()
    ocean = bl.Ocean()
    uninhabitable_landscapes = [mountain, ocean]
    for landscape in uninhabitable_landscapes:
        assert landscape.propensity()[0] == 0
 def landscape_position_in_map(self):
     """
     Creates a numpy array map from the string map.
     """
     numpy_map = np.empty((len(self.string_map), len(self.string_map[0])),
                          dtype=object)
     for x, line in enumerate(self.string_map):
         for y, cell in enumerate(line):
             if cell == "J":
                 numpy_map[x, y] = bl.Jungle()
             elif cell == "S":
                 numpy_map[x, y] = bl.Savannah()
             elif cell == "D":
                 numpy_map[x, y] = bl.Desert()
             elif cell == "M":
                 numpy_map[x, y] = bl.Mountain()
             elif cell == "O":
                 numpy_map[x, y] = bl.Ocean()
     return numpy_map
Esempio n. 10
0
def test_available_fodder_carnivore():
    """
    Tests that the available fodder for carnivores in a cell is computed
    corresponding to the given formula.
    """
    jungle = bl.Jungle()
    herbs = [{
        "species": "Herbivore",
        "age": 5,
        "weight": 50
    } for _ in range(4)]
    ba.Carnivore.set_animal_parameters({"DeltaPhiMax": 0.0001})
    carn = [{
        "species": "Carnivore",
        "age": 5,
        "weight": 500
    } for _ in range(2)]
    pop = herbs + carn
    jungle.cell_population(pop)
    assert jungle.available_fodder_carnivore == pytest.approx(1.3333333)
    jungle.eat_request_carnivore()
    assert jungle.available_fodder_carnivore == pytest.approx(0.666666, 2)
Esempio n. 11
0
def test_migrate():
    """
    Tests that migration of animals work as expected.
    """
    current_cell_pop = [{
        "species": "Herbivore",
        "age": 10,
        "weight": 15
    }, {
        "species": "Herbivore",
        "age": 20,
        "weight": 35
    }, {
        "species": "Carnivore",
        "age": 5,
        "weight": 40
    }, {
        "species": "Carnivore",
        "age": 15,
        "weight": 25
    }]
    current_cell = bl.Savannah()
    current_cell.cell_population(current_cell_pop)
    jungle_right = bl.Jungle()
    jungle_left = bl.Jungle()
    desert = bl.Desert()
    jungle_right.animal_population[0].append(ba.Herbivore(weight=15))
    jungle_pop = [{
        "species": "Herbivore",
        "age": 10,
        "weight": 15
    }, {
        "species": "Herbivore",
        "age": 5,
        "weight": 15
    }]
    jungle_left.cell_population(jungle_pop)
    desert_pop = [{
        "species": "Herbivore",
        "age": 10,
        "weight": 20
    }, {
        "species": "Herbivore",
        "age": 5,
        "weight": 15
    }, {
        "species": "Herbivore",
        "age": 10,
        "weight": 35
    }]
    desert.cell_population(desert_pop)

    neighbour_cells = [desert, bl.Mountain(), jungle_right, jungle_left]

    random.seed(124)
    current_cell.migrate(neighbour_cells)

    neighbour_cells.append(current_cell)
    for cell in neighbour_cells:
        if not isinstance(cell, bl.Mountain):
            assert cell.new_population != cell.animal_population