예제 #1
0
def test_animal_death():
    """
    test that animal dies when fitness is 0 or with
    certain probability omega(1 - fitness)
    """
    animal = Herbivore(10, 50)
    animal.fitness = 0
    assert animal.will_die() is True
예제 #2
0
def test_animal_aging():
    """
    test every year animal age updates
    """
    animal = Herbivore(10, 50)
    before_age = animal.age
    animal.get_old()
    assert animal.age == before_age + 1
예제 #3
0
def test_fitness_update():
    """
    test fitness is updated when weight is changed
    """
    animal = Herbivore(10, 50)
    before_fitness = animal.fitness
    animal.lose_weight()
    assert animal.fitness != before_fitness
예제 #4
0
def test_animal_weight_loss():
    """
    Test if animal looses weight
    """
    animal = Herbivore(10, 50)
    before_weight = animal.weight
    animal.lose_weight()
    assert before_weight > animal.weight
예제 #5
0
 def gen_animal_data(self):
     seed(1)
     h1 = Herbivore()
     h2 = Herbivore()
     seed(1)
     c1 = Carnivore()
     c2 = Carnivore()
     return c1, c2, h1, h2
예제 #6
0
def test_animal_migration_chances():
    """
    test the probability of migration is zero if
    fitness is zero
    """
    animal = Herbivore(10, 50)
    animal.fitness = 0
    assert not animal.will_migrate()
예제 #7
0
 def animal_objects(self):
     np.random.seed(123)
     herb = Herbivore()
     herb.set_parameters(Herbivore.parameters)
     np.random.seed(123)
     carn = Carnivore()
     carn.set_parameters(Carnivore.parameters)
     return herb, carn
예제 #8
0
def test_check__phi_borders():
    """Test if method 'check__phi_borders()' verifies the _phy
    conditions '0 <= _phi <= 1' and returns an ValueError if  not
    satisfied"""
    with pytest.raises(ValueError):
        Herbivore(1, 50).check__phi_borders(-0.9999)
        Carnivore(1, 50).check__phi_borders(-0.9999)
        Herbivore(1, 50).check__phi_borders(1.0001)
        Carnivore(1, 50).check__phi_borders(1.0001)
 def animal_objects(self):
     np.random.seed(123)
     herb1 = Herbivore()
     herb2 = Herbivore()
     np.random.seed(123)
     carn1 = Carnivore()
     carn2 = Carnivore()
     print(herb1.weight, herb2.weight)
     return carn1, carn2, herb1, herb2
예제 #10
0
def test_update_weight_after_birth():
    """
    test that the weight of the mother is
    reduced by xi times the weight of the
    baby after reproduction
    """
    animal = Herbivore(20, 40)
    weight_before_birth = animal.weight
    baby_weight = 10
    animal.update_weight_after_birth(baby_weight)
    assert animal.weight < weight_before_birth
예제 #11
0
def test_calculate_fitness_and_formula():
    """Test if the method 'calculate_fitness()' correctly communicates to
    the method 'fit_formula()' and returns the correct fitness of the
    animal (pop_object)'"""

    herbivore = Herbivore(10, 20)
    carnivore = Carnivore(15, 30)
    assert pytest.approx(
        herbivore.calculate_fitness(herbivore.age, herbivore.weight,
                                    herbivore.parameters), 0.7292)
    assert pytest.approx(
        carnivore.calculate_fitness(carnivore.age, carnivore.weight,
                                    carnivore.parameters), 0.999969)
 def test_valueerror_for_negative_weight(self):
     """
     Tests if value error is being raised when a negative weight is set as input
     """
     with pytest.raises(ValueError) as err:
         Herbivore(5, -20)
         assert err.type is ValueError
예제 #13
0
def test_carnivore_kill(mocker):
    """
    Test that carnivore kills herbivore if
        1. carnivore fitness is greater than
        herbivore fitness
        2. the difference between carnivore
        fitness and herbivore fitness divided
        by DeltaPhiMax parameter is greater
        than random value.
    """
    mocker.patch('numpy.random.random', return_value=0.05)
    herbivore = Herbivore()
    carnivore = Carnivore()
    herbivore.fitness = 0.3
    carnivore.fitness = 0.9
    assert carnivore.will_kill(herbivore.fitness)
 def test_remove_animal(self, landscape_data):
     savannah = landscape_data['S']
     herb3 = Herbivore()
     savannah.add_animal(herb3)
     assert len(savannah.fauna_list['Herbivore']) == 3
     savannah.remove_animal(herb3)
     assert len(savannah.fauna_list['Herbivore']) == 2
예제 #15
0
def test_animal_non_neagtive_weight():
    """
    test if animal age is  non-negative
    """
    h = Herbivore()
    c = Carnivore()
    assert h.weight >= 0
    assert c.weight >= 0
예제 #16
0
def test_fitness_range():
    """
    test fitness value is between 0 and 1
    """
    herb = Herbivore(np.random.randint(0, 100), np.random.randint(0, 100))
    carn = Carnivore(np.random.randint(0, 100), np.random.randint(0, 100))
    assert 1 >= herb.fitness >= 0
    assert 1 >= carn.fitness >= 0
예제 #17
0
    def gen_animal_data(self):
        """
        Two animals of the two different species (Herbivore, Carnivore) with
        predefined set of parameters

        Returns
        -------
        carn: Carnivore object
        herb: Herbivore object
        """
        c_params = {
            'eta': 0.30,
            'w_birth': 4.0,
            'sigma_birth': 1.7,
            'w_half': 10,
            'phi_age': 0.4,
            'phi_weight': 0.6,
            'a_half': 60,
            'beta': 0.2,
            'DeltaPhiMax': 11.0,
            'mu': 0.25,
            'gamma': 0.6,
            'zeta': 1,
            'omega': 0.6
        }
        h_params = {
            'eta': 0.30,
            'w_birth': 2.0,
            'sigma_birth': 1.5,
            'w_half': 10,
            'phi_age': 0.3,
            'phi_weight': 0.5,
            'a_half': 40,
            'beta': 0.3,
            'mu': 0.3,
            'gamma': 0.7,
            'zeta': 1,
            'omega': 0.3
        }
        np.random.seed(1)
        carn = Carnivore()
        carn.set_given_parameters(c_params)
        np.random.seed(1)
        herb = Herbivore()
        herb.set_given_parameters(h_params)
        return carn, herb
예제 #18
0
    def gen_animal_data(self):
        """
        Fixture to generate animals data to be used as input for landscape
        data

        Returns
        -------
        (carn_1, carn_2, herb_1, herb_2) : tuple
        """
        np.random.seed(1)
        herb_1 = Herbivore()
        herb_2 = Herbivore()
        np.random.seed(1)
        carn_1 = Carnivore()
        carn_2 = Carnivore()
        print(herb_1.weight, herb_2.weight)
        return carn_1, carn_2, herb_1, herb_2
예제 #19
0
 def test_add_animals(self, landscape_data):
     """
     Tests the add_animals function after adding an additional animal (herb3)
     """
     lowland = landscape_data["L"]
     assert len(lowland.fauna_dict['Herbivore']) == 2
     herb3 = Herbivore()
     lowland.add_animal(herb3)
     assert len(lowland.fauna_dict['Herbivore']) == 3
예제 #20
0
    def test_give_birth_lose_weight(self, gen_animal_data):
        carn, herb = gen_animal_data
        carn_baby = Carnivore(weight=2)
        herb_baby = Herbivore(weight=1)
        carn.lose_weight_give_birth(carn_baby)
        herb.lose_weight_give_birth(herb_baby)

        assert carn.weight == pytest.approx(4.5613871182275103)
        assert herb.weight == pytest.approx(3.2365180454948623)
예제 #21
0
def test_herbivore_params_keys():
    """ Tests that the given parameters are in the list of herbivor
    parameters"""
    keys_list = [
        'w_birth', 'sigma_birth', 'beta', 'eta', 'a_half', 'phi_age', 'w_half',
        'phi_weight', 'mu', 'lambda', 'gamma', 'zeta', 'xi', 'omega', 'F',
        'DeltaPhiMax'
    ]
    h = Herbivore()
    assert [key in h.parameters for key in keys_list]
예제 #22
0
    def test_kill_probability(self, animal_objects):
        """
        The given weights shows that the herb is more fit than the carn.
        so, the probability should be false.

        """
        np.random.seed(123)
        carn = Carnivore(weight=0)
        herb = Herbivore(weight=100)
        assert not carn.probability_of_kill(herb)
예제 #23
0
    def generate_animal_data(self):
        """
        Generates population of animals and an array of their initial weight to
        be used in test_birth_weight_dist

        Returns
        -------
        birth_weight_list: list
            list of animals' initial weight
        """
        birth_weight_list = []
        for _ in range(20):
            carn = Carnivore()
            carn.set_default_weight()
            birth_weight_list.append(carn.weight)
        for _ in range(20):
            herb = Herbivore()
            herb.set_default_weight()
            birth_weight_list.append(herb.weight)
        return birth_weight_list
예제 #24
0
    def test_kill_probability(self, gen_animal_data):
        """
        The given weights shows that the herb is more fit than the carn.
        so, the probability should be false.
        ----------
        gen_animal_data: Carnivore and Herbivore objects

        """
        np.random.seed(1)
        carn = Carnivore(weight=10)
        herb = Herbivore(weight=100)
        assert not carn.kill_prob(herb)
예제 #25
0
    def test_die(self, mocker):
        """
        Tests the die_animal method and death_prob in two conditions:
        1. animal with fitness zero (herb_1).
        2. a fixed return value for random number with the use of mock
        (carn_1).

        Explanation of code:
        First, generating animals population for one of the landscapes types
        (i.e. jungle).
        Generating herb_1 with weight 0 to get the fitness zero and gain
        death_prob = True.
        Also, generates carn_1 with some default value just to have fixed
        values for its fitness.
        Then, use mock for numpy random number and set the return_value to
        it in the way taht it always fulfill death_prob = True for carn_1
        Finally, run the die_animal method for all of the animals of jungle
        instance and assert that the 2 animals are died and only the other 2
        are in the animal list.

        """

        herb_1 = Herbivore(6, 0)
        herb_2 = Herbivore()
        carn_1 = Carnivore(1, 5)
        carn_2 = Carnivore()
        params = {'f_max': 10.0}
        j = Jungle(params)
        j.add_animal(carn_1)
        j.add_animal(carn_2)
        j.add_animal(herb_1)
        j.add_animal(herb_2)
        herb_1.calculate_fitness()
        assert herb_1.death_prob
        mocker.patch('numpy.random.random', return_value=0.0)
        j.die_animals()
        h_count = len(j.in_cell_fauna['Herbivore'])
        c_count = len(j.in_cell_fauna['Carnivore'])
        fauna_count = h_count + c_count
        assert fauna_count == 2
    def animal_objects(self):
        self.herb_small = Herbivore(5, 20)
        self.herb_large = Herbivore(5, 50)
        self.herb_young = Herbivore(10, 20)
        self.herb_old = Herbivore(50, 20)
        self.herb_no_weight = Herbivore(20, 0)
        self.herb = Herbivore()

        self.carn_small = Carnivore(5, 20)
        self.carn_large = Carnivore(5, 50)
        self.carn_young = Carnivore(10, 20)
        self.carn_old = Carnivore(50, 20)
        self.carn = Carnivore()
예제 #27
0
def test__animal_birth_probability():
    """
    1. Test no birth if single herbivore or carnivore
    2. Test no birth if weight of herbivore or
    carnivore is less than output of following
    condition:
        xi(w_birth + sigma_birth)
    """
    animal = Herbivore(20, 50)
    assert not animal.birth(1)

    animal = Carnivore(10, 50)
    animal.weight = 5
    assert not animal.birth(200)
예제 #28
0
    def test_add_and_remove_fauna(self, gen_landscape_data):
        """
        check the length of current animals in cells after addition
        or removing

        Parameters
        ----------
        gen_landscape_data: tuple

        """
        sav, des = (gen_landscape_data[i] for i in ('s', 'd'))
        assert len(sav.in_cell_fauna['Carnivore'] +
                   sav.in_cell_fauna['Herbivore']) == 4
        assert len(des.in_cell_fauna['Herbivore']) == 2
        assert len(des.in_cell_fauna['Carnivore']) == 2
        herb_3 = Herbivore()
        sav.add_animal(herb_3)
        assert len(sav.in_cell_fauna['Carnivore'] +
                   sav.in_cell_fauna['Herbivore']) == 5
        sav.remove_animal(herb_3)
        assert len(sav.in_cell_fauna['Carnivore'] +
                   sav.in_cell_fauna['Herbivore']) == 4
예제 #29
0
def test_check_known_parameters():
    """Test method 'check_unknown_parameters()' if it identifies the
    given parameter and does not return ValueError"""
    Herbivore.check_unknown_parameters(params={'zeta': 100})
    Carnivore.check_unknown_parameters(params={'mu': 100})
예제 #30
0
def test_create_animal():
    """"""
    h = Herbivore()
    assert h.age == 0