示例#1
0
    def test_baseanimal_init_function_inherits_correctly_to_subclass(
            self, herb, carn):
        """Tests that the attributes inherits correctly to the animal species.

        This test function also tests if the class raises errors if the animals
        are initiated with incorrect value ranges.
        """
        assert herb.a == 0
        assert carn.a == 0
        assert carn.weight > 0
        assert herb.weight > 0
        assert 0 <= herb.fitness <= 1, "Fitness needs to be in interval [0,1]"
        assert 0 <= carn.fitness <= 1, "Fitness needs to be in interval [0,1]"
        with pytest.raises(ValueError):
            Herb(age=-1)
        with pytest.raises(ValueError):
            Carn(age=-300)
        with pytest.raises(ValueError):
            Herb(weight=-1)
        with pytest.raises(ValueError):
            Carn(weight=-1)
        with pytest.raises(ValueError):
            Herb(age="hello")
        with pytest.raises(ValueError):
            Carn(weight="hi")
        with pytest.raises(ValueError):
            Herb(weight=[1, 2, 3])
示例#2
0
 def test_migrate_all_animals_all_move(self, jungle, herb_list_big,
                                       carn_list_big, tear_down_params):
     """Test to see that all animals migrate if they deside to migrate.
      """
     j = jungle
     j.herb_list = herb_list_big
     j.carn_list = carn_list_big
     neighbors = (Jungle(), Jungle(), Jungle(), Jungle())
     Herb.set_parameters({"mu": 100, "F": 0})
     Carn.set_parameters({"mu": 100, "F": 0})
     j.migrate_all_animals(neighbors)
     assert len(j.herb_move_from_list) == 1000
     assert len(j.carn_move_from_list) == 1000
示例#3
0
    def test_will_birth(self, mocker):
        """Tests that the will_birth method works correctly.

        The weights of herbivores and carnivores are set as needed to have the
        correct return object from the will_birth method.
        The mocker is used to give spesific values from random functions used
        in the module.
        """
        h = Herb()
        c = Carn()
        h.weight = 0
        c.weight = 0
        return_object_herb = h.will_birth(10)
        return_object_carn = c.will_birth(10)
        assert return_object_herb is None
        assert return_object_carn is None
        mocker.patch("random.uniform", return_value=0)
        h = Herb()
        c = Carn()
        h.weight = 100
        c.weight = 100
        return_object_herb = h.will_birth(10000)
        return_object_carn = c.will_birth(10000)
        assert isinstance(return_object_herb, Herb)
        assert isinstance(return_object_carn, Carn)
示例#4
0
    def set_animal_parameters(self, 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":
            h = Herb()
            h.set_parameters(params)
        elif species == "Carnivore":
            c = Carn()
            c.set_parameters(params)
        else:
            raise ValueError(f'Got non existing species {species} ')
示例#5
0
    def test_carnivore_weight_and_fitness_updates_after_feeding(self, mocker):
        """Tests that the carnivore weight and fitness updates after feeding.

        Mocker is used to ensure that the carnivore eats.
        """
        a = Herb()
        mocker.patch("random.uniform", return_value=0)
        c = Carn()
        c.F = 10000
        c.fitness = 1
        herb_list = [a]
        herb_list.sort(key=lambda x: x.fitness, reverse=True)
        herb_weight_list = [h.weight for h in herb_list]
        carn_weight = c.weight + c.beta * herb_weight_list[0]
        c.feeding(herb_list)
        assert c.weight == pytest.approx(carn_weight)
        assert c.fitness == pytest.approx(
            1 / (1 + m.exp(c.phi_age * (c.a - c.a_half))) * 1 /
            (1 + m.exp(-c.phi_age * (c.weight - c.w_half))))
        c = Carn()
        c.fitness = 1
        c.F = 0.001
        herb_list[0].weight = 5
        carn_weight = c.weight + c.beta * c.F
        c.feeding(herb_list)
        assert c.weight == pytest.approx(carn_weight), "Weight doesn't update"
示例#6
0
 def test_death(self, mocker):
     h = Herb()
     c = Carn()
     h.fitness = 0
     c.fitness = 0
     assert h.death() is True
     assert c.death() is True
     h = Herb()
     c = Carn()
     mocker.patch('random.uniform', return_value=1)
     assert h.death() is False
     assert c.death() is False
     h = Herb()
     c = Carn()
     mocker.patch('random.uniform', return_value=0)
     assert h.death() is True
     assert c.death() is True
示例#7
0
    def test_weightloss(self, mocker):
        """Tests that the weight is updated according to the weightloss method.

        The mocker is used to give spesific values from random functions used
        in the module.
        """
        mocker.patch("numpy.random.normal", return_value=1)
        h = Herb()
        h.weightloss()
        assert h.weight == 1 - h.eta * 1
        h.weight = 100000
        c = Carn()
        c.weight = 100000
        weight_list_herb = []
        weight_list_carn = []
        for _ in range(100):
            weight_list_herb.append(h.weight)
            weight_list_carn.append(c.weight)
            h.weightloss()
            c.weightloss()
        assert all([
            weight1 > weight2 for weight1, weight2 in zip(
                weight_list_herb[:-1], weight_list_herb[1:])
        ]), "The weight does not decrease in the list when weightloss is used"
        assert all([
            weight1 > weight2 for weight1, weight2 in zip(
                weight_list_carn[:-1], weight_list_carn[1:])
        ]), "The weight does not decrease in the list when weightloss is used"
示例#8
0
 def test_feeding_herb(self, mocker):
     mocker.patch('numpy.random.normal', return_value=3)
     h = Herb()
     return_fodder = h.feeding(300)
     assert h.weight == 3 + h.beta * h.F
     assert return_fodder == h.F
     h = Herb()
     return_fodder = h.feeding(5)
     assert h.weight == 3 + h.beta * 5
     assert return_fodder == 5
     with pytest.raises(ValueError):
         h.feeding(-5)
示例#9
0
 def test_carnivore_continues_to_feed_if_it_has_appetite(self, mocker):
     herb_list = [Herb() for _ in range(100)]
     herb_list.sort(key=lambda x: x.fitness, reverse=True)
     mocker.patch('random.uniform', return_value=0)
     c = Carn()
     c.F = 100000
     c.fitness = 0.5
     for herb in herb_list:
         herb.fitness = 0
     killed_herbs = len(c.feeding(herb_list))
     assert killed_herbs == len(herb_list)
示例#10
0
 def test_BaseAnimal_init_function_inherits_correctly_to_subclass(
         self, herb, carn):
     assert herb.a == 0
     assert carn.a == 0
     assert carn.weight > 0
     assert herb.weight > 0
     assert 0 <= herb.fitness <= 1, 'Fitness needs to be in interval [0,1]'
     assert 0 <= carn.fitness <= 1, 'Fitness needs to be in interval [0,1]'
     with pytest.raises(ValueError):
         h = Herb(age=-1)
     with pytest.raises(ValueError):
         c = Carn(age=-300)
     with pytest.raises(ValueError):
         h = Herb(weight=-1)
     with pytest.raises(ValueError):
         c = Carn(weight=-1)
     with pytest.raises(ValueError):
         h = Herb(age="hello")
     with pytest.raises(ValueError):
         c = Carn(weight='hi')
     with pytest.raises(ValueError):
         h = Herb(weight=[1, 2, 3])
示例#11
0
 def test_migrate_all_animals_equal_prob(self, jungle, herb_list_big,
                                         carn_list_big, tear_down_params):
     """
     Test to see that animal is equally likely to migrate to any square if
     move propensity is equal for all squares.
     """
     j = jungle
     j.herb_list = herb_list_big
     j.carn_list = carn_list_big
     neighbors = (Jungle(), Jungle(), Jungle(), Jungle())
     Herb.set_parameters({"mu": 100, "F": 0})
     Carn.set_parameters({"mu": 100, "F": 0})
     j.migrate_all_animals(neighbors)
     num_moved = np.array((
         len(neighbors[0].herb_move_to_list),
         len(neighbors[1].herb_move_to_list),
         len(neighbors[2].herb_move_to_list),
         len(neighbors[3].herb_move_to_list),
     ))
     num_expected = np.array((250, 250, 250, 250))
     _, pvalue = chisquare(num_moved, num_expected)
     assert pvalue > 0.001
示例#12
0
 def test_death(self, mocker):
     """Tests that the death method works correctly.
     The fitness of herbivores and carnivores are set as needed to have the
     correct return object from the death method.
     The mocker is used to give spesific values from random functions used
     in the module.
     """
     h = Herb()
     c = Carn()
     h.fitness = 0
     c.fitness = 0
     assert h.death() is True
     assert c.death() is True
     h = Herb()
     c = Carn()
     mocker.patch("random.uniform", return_value=1)
     assert h.death() is False
     assert c.death() is False
     h = Herb()
     c = Carn()
     mocker.patch("random.uniform", return_value=0)
     assert h.death() is True
     assert c.death() is True
示例#13
0
    def test_feeding_herb(self, mocker):
        """Tests that the feeding of herbivores are done correctly.

        The mocker is used to give spesific values from random functions used
        in the module.
        """
        mocker.patch("numpy.random.normal", return_value=3)
        h = Herb()
        return_fodder = h.feeding(300)
        assert h.weight == 3 + h.beta * h.F
        assert return_fodder == h.F
        h = Herb()
        return_fodder = h.feeding(5)
        assert h.weight == 3 + h.beta * 5
        assert return_fodder == 5
        with pytest.raises(ValueError):
            h.feeding(-5)
示例#14
0
 def test_will_birth(self, mocker):
     h = Herb()
     c = Carn()
     h.weight = 0
     c.weight = 0
     return_object_herb = h.will_birth(10)
     return_object_carn = c.will_birth(10)
     assert return_object_herb is None
     assert return_object_carn is None
     mocker.patch('random.uniform', return_value=0)
     h = Herb()
     c = Carn()
     h.weight = 100
     c.weight = 100
     return_object_herb = h.will_birth(10000)
     return_object_carn = c.will_birth(10000)
     assert isinstance(return_object_herb, Herb)
     assert isinstance(return_object_carn, Carn)
示例#15
0
 def test_migrate(self, mocker):
     mocker.patch('random.uniform', return_value=0)
     h = Herb()
     c = Carn()
     assert h.migrate() is True
     assert c.migrate() is True
     mocker.patch('random.uniform', return_value=1)
     h = Herb()
     c = Carn()
     assert h.migrate() is False
     assert c.migrate() is False
示例#16
0
    def test_carnivore_continues_to_feed_if_it_has_appetite(self, mocker):
        """Tests that the carnivores continues to feed.

        Tests that the carnivore feeds until there are no more herbivores on
        the nature square, given that its appetite and fitness is high.
        Mocker is used to ensure that the carnivore eats for certain.
        """
        herb_list = [Herb() for _ in range(100)]
        herb_list.sort(key=lambda x: x.fitness, reverse=True)
        mocker.patch("random.uniform", return_value=0)
        c = Carn()
        c.F = 100000
        c.fitness = 0.5
        for herb in herb_list:
            herb.fitness = 0.1
        killed_herbs = len(c.feeding(herb_list))
        assert killed_herbs == len(herb_list)
示例#17
0
 def test_weight_follows_normal_distribution(self):
     #np.random.seed(123)  # need this?
     # Using D.agostinos K^2 test which is accessed from the normaltest in scipy
     n_trials = 10000
     some_herb_list = [Herb() for _ in range(n_trials)]
     some_carn_list = [Carn() for _ in range(n_trials)]
     weight_data_herb = [h.weight for h in some_herb_list]
     weight_data_carn = [c.weight for c in some_carn_list]
     stat, p_value1 = normaltest(weight_data_herb)
     stat, p_value2 = normaltest(weight_data_carn)
     # Significance level 0.01.
     alpha = 0.01
     # Nullhypothesis is that the weight follows a normal distribution.
     # Weight follows a normal distribution if the test is passed.
     # If it doesnt pass, we reject H0 on a 0.01 significance level. This
     # means that it is more likely that the data doesnt follow a normal
     # distribution. If it passes it means that its probable that it follows
     # a normal distribution but we cant say for sure.
     assert p_value1 > alpha
     assert p_value2 > alpha
示例#18
0
    def test_migrate(self, mocker):
        """Tests that the migrate method works properly.

        The mocker is used to give spesific values from random functions used
        in the module.
        """
        mocker.patch("random.uniform", return_value=0)
        h = Herb()
        c = Carn()
        assert h.migrate() is True
        assert c.migrate() is True
        mocker.patch("random.uniform", return_value=1)
        h = Herb()
        c = Carn()
        assert h.migrate() is False
        assert c.migrate() is False
示例#19
0
 def test_carnivore_weight_and_fitness_updates_after_feeding(self, mocker):
     # usikker på denne testen.
     a = Herb()
     mocker.patch("random.uniform", return_value=0)
     c = Carn()
     c.F = 10000
     c.fitness = 1
     herb_list = [a]
     herb_list.sort(key=lambda x: x.fitness, reverse=True)
     herb_weight_list = [h.weight for h in herb_list]
     carn_weight = c.weight + c.beta * herb_weight_list[0]
     c.feeding(herb_list)
     assert c.weight == pytest.approx(carn_weight)
     assert c.fitness == pytest.approx(
         1 / (1 + m.exp(c.phi_age * (c.a - c.a_half))) * 1 /
         (1 + m.exp(-c.phi_age * (c.weight - c.w_half))))
     c = Carn()
     c.fitness = 1
     c.F = 0.001
     herb_list[0].weight = 5
     carn_weight = c.weight + c.beta * c.F
     c.feeding(herb_list)
     assert c.weight == pytest.approx(carn_weight)
示例#20
0
    def test_weight_follows_normal_distribution(self):
        """A statistical test for the distribution of animal weights.

        The test determines whether it is probable that the drawn weights of
        the animals follows a normal distribution.
        The test uses  the normaltest from scipy, which is based on D.agostinos
        K^2 test. The nullhypothesis for the test is that the weight follows a
        normal distribution. If the assertion fails we reject the
        nullhypothesis on the given significance level, which is defined
        as alpha.
        """
        n_trials = 10000
        some_herb_list = [Herb() for _ in range(n_trials)]
        some_carn_list = [Carn() for _ in range(n_trials)]
        weight_data_herb = [h.weight for h in some_herb_list]
        weight_data_carn = [c.weight for c in some_carn_list]
        stat, p_value1 = normaltest(weight_data_herb)
        stat, p_value2 = normaltest(weight_data_carn)
        alpha = 0.001
        assert p_value1 > alpha, ("Herbivore weight probably "
                                  "doesn't follow a normal distribution")
        assert p_value2 > alpha, ("Carnivore weight probably "
                                  "doesn't follow a normal distribution")
示例#21
0
 def test_weightloss(self, mocker):
     mocker.patch('numpy.random.normal', return_value=1)
     h = Herb()
     h.weightloss()
     assert h.weight == 1 - h.eta * 1
     h.weight = 100000
     c = Carn()
     c.weight = 100000
     weight_list_herb = []
     weight_list_carn = []
     for _ in range(100):
         weight_list_herb.append(h.weight)
         weight_list_carn.append(c.weight)
         h.weightloss()
         c.weightloss()
     assert all([
         weight1 > weight2 for weight1, weight2 in zip(
             weight_list_herb[:-1], weight_list_herb[1:])
     ])
     assert all([
         weight1 > weight2 for weight1, weight2 in zip(
             weight_list_carn[:-1], weight_list_carn[1:])
     ])
示例#22
0
 def herb_list(self):
     """Creates a fixture that returns list with 100 herbivores.
     """
     return [Herb() for _ in range(100)]
示例#23
0
 def herb_list_big(self):
     """Creates a fixture that returns a list of 1000 herbivores.
     """
     return [Herb() for _ in range(1000)]
示例#24
0
 def herb_params(self):
     """Creates a fixture of the herbivore default parameters.
     """
     return Herb().DEFAULT_PARAMETERS
示例#25
0
 def herb_list(self):
     return [Herb() for _ in range(100)]
示例#26
0
 def tear_down_params(self):
     """Creates a tear_down fixture that resets the parameters.
     """
     yield None
     Herb().set_default_parameters_for_species()
     Carn().set_default_parameters_for_species()
示例#27
0
 def herb_list_gen(self):
     """Creates a fixture which returns a list of 100 herbivores.
     """
     return [Herb() for _ in range(100)]
示例#28
0
 def herb(self):
     """Creates a fixture of the herbivore class instance.
     """
     return Herb()
示例#29
0
    def test_fitness_update(self, mocker):
        """Tests that the fitness gets updated for given values of weight.

        The mocker is used to give spesific values from random functions used
        in the module.
        """
        h = Herb()
        c = Carn()
        h.weight = -3
        c.weight = -2
        h.fitness_update()
        c.fitness_update()
        assert h.fitness == 0
        assert c.fitness == 0
        mocker.patch("numpy.random.normal", return_value=0)
        h = Herb()
        c = Carn()
        h.fitness_update()
        c.fitness_update()
        assert h.fitness == 0
        assert c.fitness == 0
        mocker.patch("numpy.random.normal", return_value=1)
        h = Herb()
        c = Carn()
        h.fitness_update()
        c.fitness_update()
        assert h.fitness == pytest.approx(
            1 / (1 + m.exp(h.phi_age * (h.a - h.a_half))) * 1 /
            (1 + m.exp(-h.phi_weight * (1 - h.w_half))))
        assert c.fitness == pytest.approx(
            1 / (1 + m.exp(c.phi_age * (c.a - c.a_half))) * 1 /
            (1 + m.exp(-c.phi_weight * (1 - c.w_half))))
示例#30
0
 def tear_down_params(self):
     yield None
     Herb().set_default_parameters_for_species()
     Carn().set_default_parameters_for_species()