def test_migration(self): """ Test for migration Returns ------- """ i1 = Island(ISLE_MAP2) Herbivore.set_parameters({'mu': 1.0}) Carnivore.set_parameters({'mu': 1.0}) coordinate = (2, 3) i1.add_animal_island(coordinate, INI_HERB[0]['pop']) i1.add_animal_island(coordinate, INI_CARN[0]['pop']) for herbivore in i1.cells[2, 3].herbivores: herbivore.fitness = 1 for carnivore in i1.cells[2, 3].carnivores: carnivore.fitness = 1 i1.migration() # can only move left or down len_left_herb = len(i1.cells[2, 2].herbivores) len_down_herb = len(i1.cells[3, 3].herbivores) len_left_carn = len(i1.cells[2, 2].carnivores) len_down_carn = len(i1.cells[3, 3].carnivores) assert (len_down_herb + len_left_herb, len_left_carn + len_down_carn) == (20, 20)
def test_feed(self, herbivore_list): carnivore = Carnivore(5, 100) sorted_list = sorted(herbivore_list, key=lambda var: var.fitness) length_a = len(sorted_list) weight_a = carnivore.weight new_list = carnivore.feed(sorted_list) length_b = len(new_list) weight_b = carnivore.weight assert length_b < length_a assert weight_a < weight_b assert weight_b - weight_a <= carnivore.F * carnivore.beta # Should add tests that changes DeltaPhiMax and checks # that part of the code carnivore.set_parameters(DeltaPhiMax=0.5) carnivore = Carnivore(5, 100) assert carnivore.fitness > 0.5 sorted_list = sorted(herbivore_list, key=lambda var: var.fitness) for herbivore in sorted_list: herbivore.weight = 200 herbivore._compute_fitness = False herbivore._fitness = 0.0 times_to_eat = len(sorted_list) herbivores_eaten = 0 while len(sorted_list) > 0: carnivore.feed(sorted_list) herbivores_eaten += 1 assert herbivores_eaten == times_to_eat reset_parameters()
def set_default_animal(self): """ Method for resetting parameters for herbivores and carnivores. Returns ------- """ Herbivore.set_parameters(DEFAULT_HERBIVORE_PARAMETERS) Carnivore.set_parameters(DEFAULT_CARNIVORE_PARAMETERS)
def test_heatmap(self): """ Tests that heatmap makes array with correct number of herbivores. :return: """ Carnivore.set_parameters({"gamma": 0, "omega": 0, "mu": 0}) Herbivore.set_parameters({"gamma": 0, "omega": 0, "mu": 0}) initial_herbivores = 80 initial_carnivores = 20 herbivores, carnivores = self.sim.heatmap(self.sim.island.one_year()) nt.assert_equal(initial_carnivores, np.sum(carnivores)) nt.assert_equal(initial_herbivores, np.sum(herbivores)) nt.assert_equal(initial_herbivores, herbivores[2][2]) nt.assert_equal(initial_carnivores, carnivores[4][2])
def set_animal_parameters(self, species, params): """ Sets parameter for animals Parameters ---------- species: object params: dict Returns ------- """ if species == 'Herbivore': Herbivore.set_parameters(params) elif species == 'Carnviore': Carnivore.set_parameters(params)
def test_animals_must_migrate(self): """ Tests that no animals are left in cell after migration """ Herbivore.set_parameters({"mu": 1}) Carnivore.set_parameters({"mu": 1}) island = Island(self.geogr_simple) island.build_map() island.island[1][1].herbivores = [Herbivore() for _ in range(20)] island.island[1][1].carnivores = [Carnivore() for _ in range(20)] for herb in island.island[1][1].herbivores: herb.fitness = 1 for carn in island.island[1][1].carnivores: carn.fitness = 1 island.migration() nt.assert_equal(island.island[1][1].herbivores, []) nt.assert_equal(island.island[1][1].carnivores, [])
def test_procreation(self): """ Test that the number of animals increase :return: """ Herbivore.set_parameters({"gamma": 1}) Carnivore.set_parameters({"gamma": 1}) original = 60 new = 0 self.island.procreation() for row in self.island.island: for cell in row: new += \ cell.number_of_individuals()["herbivores"] + \ cell.number_of_individuals()["carnivores"] nt.assert_greater(new, original) nt.assert_less_equal(new, original * 2)
def test_feeding_carnivore_fit(self): """ Test for carnivore feeding method, with fit carnivore. Returns ------- """ c1 = Carnivore(1, 3000) # fitness ~= 1 c1.set_parameters({'DeltaPhiMax': 1.00001}) j1 = Jungle() j1.add_herbivore(5,10) j1.herbivores[0].fitness = 0 boolean = c1.feeding(j1.herbivores) c1.set_parameters({'DeltaPhiMax': 10.0}) # default-value assert c1.weight == 3007.5 assert boolean == [False]
def test_one_year(self): """ tests that attributes changes after a year """ island = Island(self.geogr_simple) island.build_map() Herbivore.set_parameters({"mu": 0}) Carnivore.set_parameters({"mu": 0}) island.island[1][1].herbivores = [Herbivore(40, 5)] island.island[1][1].carnivores = [Carnivore(20, 5)] for carnivore in island.island[1][1].carnivores: carnivore.fitness = 1 for herbivore in island.island[1][1].herbivores: herbivore.fitness = 1 island.one_year() nt.assert_equal(island.island[1][1].herbivores[0].age, 6) nt.assert_equal(island.island[1][1].carnivores[0].age, 6) nt.assert_not_equal(island.island[1][1].herbivores[0].weight, 40) nt.assert_not_equal(island.island[1][1].carnivores[0].weight, 20)
def test_feeding_carnivore_appetite(self): """ Test for a fit carnivore's feeding method, with low appetite. Returns ------- """ c1 = Carnivore(1, 20) c1.fitness = 1 c1.set_parameters({'DeltaPhiMax': 1.00001, 'F': 10.0}) j1 = Jungle() j1.add_herbivore(5,20) j1.herbivores[0].fitness = 0 boolean = c1.feeding(j1.herbivores) c1.set_parameters({'DeltaPhiMax': 10.0, 'F': 50.0}) # default-value assert c1.weight == 27.5 assert boolean == [False]
def test_standard_arguments(): """ Tests that simulation works without any given arguments. """ initial_herbivores = 150 initial_carnivores = 40 sim_ = BioSim() Carnivore.set_parameters({"gamma": 0, "omega": 0, "mu": 0}) Herbivore.set_parameters({"gamma": 0, "omega": 0, "mu": 1}) for row in sim_.island.island: for cell in row: for herb in cell.herbivores: herb.fitness = 1 sim_.island.migration() herbivores, carnivores = sim_.heatmap(sim_.island.one_year()) nt.assert_equal(initial_carnivores, np.sum(carnivores)) nt.assert_equal(initial_herbivores, np.sum(herbivores)) nt.assert_equal(initial_carnivores, carnivores[2][2])
def reset_parameters(): herbivore_parameters_right = { 'w_birth': 8.0, 'sigma_birth': 1.5, 'beta': 0.9, 'eta': 0.05, 'a_half': 40.0, 'phi_age': 0.2, 'w_half': 10.0, 'phi_weight': 0.1, 'mu': 0.25, 'lambda_': 1.0, 'gamma': 0.2, 'zeta': 3.5, 'xi': 1.2, 'omega': 0.4, 'F': 10.0 } carnivore_parameters_right = { 'w_birth': 6.0, 'sigma_birth': 1.0, 'beta': 0.75, 'eta': 0.125, 'a_half': 60.0, 'phi_age': 0.4, 'w_half': 4.0, 'phi_weight': 0.4, 'mu': 0.4, 'lambda_': 1.0, 'gamma': 0.8, 'zeta': 3.5, 'xi': 1.1, 'omega': 0.9, 'F': 50.0, 'DeltaPhiMax': 10.0 } Carnivore.set_parameters(**carnivore_parameters_right) Herbivore.set_parameters(**herbivore_parameters_right)
def test_cell_move_herbivore_and_carnivore(self): """ Test for moving herbivore and carnivore Returns ------- """ i1 = Island(ISLE_MAP2) Herbivore.set_parameters({'mu': 1.0}) Carnivore.set_parameters({'mu': 1.0}) coordinate = (2, 3) i1.add_animal_island(coordinate, INI_HERB[0]['pop']) i1.add_animal_island(coordinate, INI_CARN[0]['pop']) for herbivore in i1.cells[2, 3].herbivores: herbivore.fitness = 1 for carnivore in i1.cells[2, 3].carnivores: carnivore.fitness = 1 assert len(i1.cells[2, 3].herbivores) == 20 assert len(i1.cells[2, 3].carnivores) == 20 i1.cell_move_herbivores(coordinate) i1.cell_move_carnivores(coordinate) assert len(i1.cells[2, 3].herbivores) == 0 assert len(i1.cells[2, 3].carnivores) == 0 # can only move left or down len_left_herb = len(i1.cells[2, 2].herbivores_new) len_down_herb = len(i1.cells[3, 3].herbivores_new) len_left_carn = len(i1.cells[2, 2].carnivores_new) len_down_carn = len(i1.cells[3, 3].carnivores_new) assert (len_down_herb + len_left_herb, len_left_carn + len_down_carn) == (20, 20)
def test_death(self): """ Runs multiple times to minimise chance of failure due to all animals surviving. :return: """ original = 40 * 5 new = 0 test = Island(self.geogr_simple) test.build_map() Herbivore.set_parameters({"omega": 1}) Carnivore.set_parameters({"omega": 1}) test.island[1][1].herbivores = [Herbivore() for _ in range(10)] test.island[1][2].herbivores = [Herbivore() for _ in range(10)] test.island[1][1].carnivores = [Carnivore() for _ in range(10)] test.island[1][2].carnivores = [Carnivore() for _ in range(10)] test.death() for _ in range(5): for row in test.island: for cell in row: new += \ cell.number_of_individuals()["herbivores"] + \ cell.number_of_individuals()["carnivores"] nt.assert_less(new, original)
def test_set_parameters(self, carnivore_parameters_right, carnivore_parameters_wrong): carnivore = Carnivore(30, 10) with pytest.raises(TypeError): carnivore.set_parameters(**carnivore_parameters_wrong) carnivore.set_parameters(eta=1, phi_age=200) assert carnivore.eta == 1 assert carnivore.phi_age == 200 carnivore.set_parameters(**carnivore_parameters_right) assert carnivore.eta == 0.125 assert carnivore.phi_age == 0.4 reset_parameters()
} for _ in range(150)] }] ini_carns = [{ 'loc': (10, 10), 'pop': [{ 'species': 'Carnivore', 'age': 5, 'weight': 20 } for _ in range(40)] }] Herbivore.set_parameters({'zeta': 3.2, 'xi': 1.8}) Carnivore.set_parameters({ 'a_half': 70, 'phi_age': 0.5, 'omega': 0.3, 'F': 65, 'DeltaPhiMax': 9. }) landscape.Jungle.set_parameters({'f_max': 700}) sim = biosim.simulation.\ BioSim(island_map=geogr, ini_pop=ini_herbs, seed=123456, img_dir=biosim.simulation.DEFAULT_GRAPHICS_DIR) sim.simulate(num_steps=100, vis_steps=1, img_steps=1) sim.add_population(population=ini_carns) sim.simulate(num_steps=100, vis_steps=1, img_steps=1) sim.make_movie()