def test_move(self): """ Tests that animal moves to the right cell Test that the animals are added to the new cell and removed from the old cell """ map1 = """\ OOOOOO OODOJO OOJJOO OOOOOO""" rd.seed(5) m = Ma.Map(map1) m.populate_map((1, 2), [Fa.Carnivore( age=10, weight=50) for _ in range(100)]) m.populate_map((1, 2), [Fa.Herbivore( age=15, weight=30) for _ in range(10)]) new_cell = m.migrate_to((1, 2)) m.move() assert m.island[1, 2].total_pop + m.island[ new_cell].total_pop == 110 assert m.island[1, 2].total_pop == 62 assert m.island[new_cell].total_pop == 48
def animal_mating(self): """ All the animals in the cell try to mate and newborns are added to population if successful :return: """ herb_born = [] for animal in self.pop_herbivores: if animal.check_birth(self.herbivore_pop): potential_herb = Fa.Herbivore() if animal.p['xi'] * potential_herb.weight > animal.weight: continue else: herb_born.append(potential_herb) animal.weight -= animal.p['xi'] * potential_herb.weight carn_born = [] for animal in self.pop_carnivores: if animal.check_birth(self.carnivore_pop): potential_carn = Fa.Carnivore() if animal.p['xi'] * potential_carn.weight > animal.weight: continue else: carn_born.append(potential_carn) animal.weight -= animal.p['xi'] * potential_carn.weight self.pop_herbivores.extend(herb_born) self.pop_carnivores.extend(carn_born)
def test_update_fitness(self): """ Tests if the fitness returns a integer Tests that fitness is zero when weight is 0 """ herb = Fa.Herbivore() herb2 = Fa.Herbivore(weight=0) assert isinstance(herb.fitness, (int, float)) assert herb2.fitness == 0
def test_aging(self): """ Test that the age of a animal is the correct one. Test that the age is equal to zero when a animal is born """ herb = Fa.Herbivore() herb.aging() assert herb.age == 1 assert Fa.Herbivore().age == 0 assert isinstance(herb.age, int)
def propensity_herb(self): """ Find the propensity of the cell for herbivores :return: int: propensity """ if isinstance(self, Ocean) or isinstance(self, Mountain): return 0 else: e_k = self.fodder / ( (self.herbivore_pop + 1) * Fa.Herbivore().p['F']) return math.exp(Fa.Herbivore().p['landa'] * e_k)
def propensity_carn(self): """ Find the propensity of the cell for carnivores :return: int: propensity """ if isinstance(self, Ocean) or isinstance(self, Mountain): return 0 else: e_k = self.get_herb_weight() / ( (self.carnivore_pop + 1) * Fa.Carnivore().p['F']) return math.exp(Fa.Carnivore().p['landa'] * e_k)
def test_check_death(self): """ Tests if an animal with weight=0 returns True(animal dies) and that the function returns a boolean expression Tests that an animal dies with a possibility and survives with another possibility """ h_die = Fa.Herbivore(weight=0) rd.seed(1) h_unlucky = Fa.Herbivore(weight=20, age=90) h_survivor = Fa.Herbivore(age=20, weight=40) assert h_unlucky.check_death() is True assert h_survivor.check_death() is False assert h_die.check_death() is True assert isinstance(h_die.check_death(), bool)
def test_annual_cycle(self): """ Tests that annual cycle works as it should """ map12 = """\ OOOO OJJO OOOO""" m = Ma.Map(map12) m.populate_map((1, 2), [Fa.Carnivore( age=10, weight=50) for _ in range(10)]) m.populate_map((1, 2), [Fa.Herbivore( age=15, weight=30) for _ in range(5)]) for _ in range(10): m.annual_cycle()
def test_populate_map(self): """ Tests that population can be added to specific cells in map """ map1 = """\ OOOOOO OJDJJO OSJJOO OOOOOO""" m = Ma.Map(map1) pos = (1, 1) pop = [Fa.Carnivore(), Fa.Herbivore(), Fa.Carnivore()] m.populate_map(pos, pop) assert m.island[1, 1].total_pop == 3 assert m.island[1, 1].carnivore_pop == 2
def test_get_weight(self): """ Tests if function returns correct weight and that its an integer """ herb = Fa.Herbivore(weight=10) assert herb.get_weight() == 10 assert isinstance(herb.get_weight(), (int, float))
def test_check_birth(self): """ When there is only one animal, test that no birth occurs. Test that animals cannot give birth if their weight is too low, too low would be less than 33.25 with default parameters. test that method works and returns True or False """ herb = Fa.Herbivore(weight=60, age=20) herb2 = Fa.Herbivore(weight=33.24, age=2) carn = Fa.Carnivore(weight=60, age=20) print(min(1, herb.p['gamma'] * herb.fitness*(4 - 1))) # 0.58 assert herb.check_birth(1) is False rd.seed(11) # rd.random() = 0.45 assert herb.check_birth(4) is True assert isinstance(herb.check_birth(40), bool) assert herb2.check_birth(100) is False assert carn.check_birth(6) is True
def test_weight_decrease(self): """ Tests if weight_decrease function decreases weight according to formula """ herb = Fa.Herbivore(weight=10) herb.weight_decrease() print(herb.weight) assert herb.weight == 9.5
def test_carnivore_eat(self): """ Tests that the weight increases according to formula when a carnivore eats a herbivore. Tests that carnivore stops eating when it has tried to kill everyone Tests that Carnivore stops eating when carnivore is full Tests that Herbivore list decreases when carnivore eats """ c = Fa.Carnivore(weight=40) herbs = [Fa.Herbivore(weight=20, age=80) for _ in range(10)] herbs2 = [Fa.Herbivore(weight=12, age=80) for _ in range(100)] rd.seed(1) c.eat(herbs) assert len(herbs) == 8 assert c.weight == 70 c.eat(herbs2) assert len(herbs2) == 95 assert c.weight == 113.5
def test_constructor_default(self): """ Tests instantiation of constructor default and its parameters """ herb = Fa.Herbivore() assert isinstance(herb, Fa.Herbivore) assert herb.age == 0 assert herb.weight >= 0 assert herb.fitness >= 0
def test_herbivore_eat(self): """ Test that weight increases with 9 when appetite*beta = 9 Test that fitness increases when an animal eats """ herb = Fa.Herbivore(weight=1) a = herb.fitness herb.eat(10) assert herb.weight == 10 assert a < herb.fitness
def test_carnivore_prob_eating(self): """ Tests that method returns a bool Tests that method returns 0 when fitness of carnivore is less or equal to herbivores fitness Tests that when rd.random() is less than probability of eating returns True """ c = Fa.Carnivore(age=10, weight=60) herb = Fa.Herbivore(age=80, weight=3) herb2 = Fa.Herbivore(age=5, weight=70) c2 = Fa.Carnivore(age=60, weight=50) rd.seed(224) # 0.06, 0.19 assert c.prob_eating(herb) is True assert c.prob_eating(herb) is False assert c2.prob_eating(herb2) is False assert isinstance(c.prob_eating(herb), bool) Fa.Carnivore.p['DeltaPhiMax'] = 0.5 # Carn fitness - herb fitness bigger than DeltaPhiMax assert c.prob_eating(herb)
def add_population(self, population): """ Add a population to specific locations on the island :param: population: dict """ for dicti in population: location = dicti['loc'] self.map.check_input_in_sim(location) population_list = [] for popu in dicti['pop']: if popu['age'] < 0 or popu['weight'] <= 0: raise ValueError('''Age must be a non negative number and weight must be a positive number''') if popu['species'] == 'Herbivore': population_list.append( Fa.Herbivore(age=popu['age'], weight=popu['weight'])) elif popu['species'] == 'Carnivore': population_list.append( Fa.Carnivore(age=popu['age'], weight=popu['weight'])) else: raise ValueError('That is not a species ') self.map.populate_map(location, population_list)
def test_set_parameter(self): """ Tests that new parameters can be set with method """ new_p = { 'w_birth': 4, 'sigma_birth': 2, 'F': 15} Fa.Herbivore.set_parameter(new_p) h = Fa.Herbivore() assert h.p['w_birth'] == 4 assert h.p['F'] == 15 reset = {'w_birth': 8, 'sigma_birth': 1.5, 'F': 10} Fa.Herbivore.set_parameter(reset)