def test_aging(self): """ Test that the animal age increases """ herb = Herbivore(weight=10, age=0) carn = Carnivore(weight=10) herb.aging() carn.aging() assert herb.age > 0, carn.age > 0
def test_kill_prey(self): """ Test kill prey. With a high fitness diff the carnivore will always kill the herbivore. """ carn = Carnivore(age=5, weight=30) herb_list = [Herbivore(age=100, weight=50) for _ in range(100)] mock_sorted_list = [(herb, herb.fitness) for herb in herb_list] kill_count = 0 for _ in mock_sorted_list: if carn.kill_prey(mock_sorted_list): kill_count += 1 assert kill_count == 100
def set_animal_parameters(species, params): """Set parameters for animal species. :param species: String, name of animal species :param params: Dict with valid parameter specification for species """ if species == "Herbivore": Herbivore.set_params(params) elif species == "Carnivore": Carnivore.set_params(params) else: raise ValueError("species needs to be either Herbivore or Carnivore!")
def test_sorted_herbivores_and_carnivores(self, highland_cell): """ :method: LandscapeCell.add_animals :method: LandscapeCell.sorted_herbivores :method: LandscapeCell.sorted_carnivores Check that sorting algorithms sort the lists by fitness """ highland_cell.add_animals([Herbivore(weight=50), Herbivore(weight=20)]) highland_cell.add_animals([Carnivore(weight=25), Carnivore(weight=40)]) sorted_herbivores = list( [herb[0] for herb in highland_cell.sorted_herbivores]) sorted_carnivores = highland_cell.sorted_carnivores assert sorted_herbivores == highland_cell.herbivores[::-1] assert sorted_carnivores == highland_cell.carnivores[::-1]
def procreation(self, cell): """Iterates through each animal in the cell and procreates. :param cell: Current cell object :type cell: object """ new_herbs = [] new_carns = [] n_herbs, n_carns = cell.herb_count, cell.carn_count for herb in cell.herbivores: # Herbivores give birth) give_birth, birth_weight = herb.give_birth(n_herbs) if give_birth: new_herbs.append(Herbivore(weight=birth_weight, age=0)) for carn in cell.carnivores: # Carnivores give birth give_birth, birth_weight = carn.give_birth(n_carns) if give_birth: new_carns.append(Carnivore(weight=birth_weight, age=0)) cell.add_animals(new_herbs + new_carns) # Add new animals to cell self._island.count_animals(num_herbs=len(new_herbs), num_carns=len(new_carns))
def add_population(self, population): """Add a population to specific `island` cells by providing a list of dictionaries. :param population: List of dictionaries specifying population :Example: .. code-block:: python example_pop = { 'loc': (4,4), 'pop': [ {'species': 'Herbivore', 'age': 2, 'weight': 60}, {'species': 'Herbivore', 'age': 9, 'weight': 30}, {'species': 'Herbivore', 'age': 16, 'weight': 14} ] } """ if type(population) == list: for loc_dict in population: # This loop will be replaced with a more elegant iteration new_animals = [ Herbivore.from_dict(animal_dict) if animal_dict["species"] == "Herbivore" else Carnivore.from_dict(animal_dict) for animal_dict in loc_dict["pop"] ] self._island.landscape[loc_dict["loc"]].add_animals(new_animals) self._island.count_animals(animal_list=new_animals) else: raise ValueError( f"Pop list needs to be a list of dicts! Was of type " f"{type(population)}." )
def test_constructor(self): """ Animals can be created """ herb = Herbivore(weight=10, age=0) carn = Carnivore() assert isinstance(herb, Herbivore), isinstance(carn, Carnivore)
def animals(self): """ create animals of different type, age and weight to use in test of fitness """ animals = [ Herbivore(age=0, weight=5), Herbivore(age=0, weight=1000), Herbivore(age=100, weight=5), Herbivore(age=100, weight=1000), Herbivore(age=0, weight=5), Carnivore(age=0, weight=5), Carnivore(age=0, weight=1000), Carnivore(age=100, weight=5), Carnivore(age=100, weight=1000) ] return animals
def test_weight_gain(self, reset_carnivore_params, reset_herbivore_params, params): """ Testing weight gain of carnivores. Assuming they have access to more fodder than they will eat. Making old heavy herbivores with low fitness. Carnivores should add beta * F weight Assuming fitness diff is larger than DeltaPhiMax such that the carnivore always kills the herbivore. """ carn = Carnivore(age=5, weight=40) # number of herbivores N = 1000 herb_list = [Herbivore(age=100, weight=200) for _ in range(N)] mock_sorted_list = [(herb, herb.fitness) for herb in herb_list] initial_weight = carn.weight _ = carn.kill_prey(mock_sorted_list) new_weight = carn.weight assert new_weight == initial_weight + carn.p["beta"] * carn.p["F"]
def test_animal_fitness(self, biosim): """ :property: Island.animal_fitness Test that animal fitness property is of correct shape and value """ fixed_herb_fitness = Herbivore(weight=20, age=5).fitness fixed_carn_fitness = Carnivore(weight=25, age=4).fitness assert biosim._island.animal_fitness == [[ fixed_herb_fitness, fixed_herb_fitness, fixed_herb_fitness ], [fixed_carn_fitness, fixed_carn_fitness]]
def test_add_remove_animals(self, highland_cell): """ :method: LandscapeCell.add_animals :method: LandscapeCell.remove_animals :property: LandscapeCell.animal_count Test that animals can be added and removed correctly """ animals = [Herbivore(), Carnivore(), Herbivore()] highland_cell.add_animals(animals) highland_cell.remove_animals([animals[0], animals[1]]) assert highland_cell.herb_count == 1
def test_reset_animals(self, highland_cell): """ :method: LandscapeCell.add_animal :method: LandscapeCell.reset_animals :property: LandscapeCell.carnivores :property: LandscapeCell.has_moved Test that has_moved property is correctly set and reset """ highland_cell.add_animals([Carnivore(), Herbivore()]) if highland_cell.carnivores[0].has_moved: highland_cell.reset_animals() assert not highland_cell.carnivores[0].has_moved
def test_lose_weight(self, reset_herbivore_params, reset_carnivore_params): """ Test that animals lose weight """ herb, carn = Herbivore(weight=20), Carnivore(weight=20) # Decreasing parameters herb.p['eta'] = 0.1 carn.p['eta'] = 0.2 herb_initial_weight, carn_initial_weight = herb.weight, carn.weight herb.lose_weight(), carn.lose_weight() # New weight of animal must be less than before assert herb.weight < herb_initial_weight assert carn.weight < carn_initial_weight
def carnivore(self): return Carnivore()
def test_constructor(self): """ Test carnivores can be constructed """ carn = Carnivore() assert isinstance(carn, Carnivore)
def test_parameters(self, reset_herbivore_params, reset_carnivore_params): """ Test parameters of herbs and carns """ herb, carn = Herbivore(), Carnivore() assert herb.p != carn.p
def reset_carnivore_params(): """ Set parameters of carnivores back to defaults """ yield Carnivore.set_params(Carnivore.p)