Beispiel #1
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"
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
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)