Пример #1
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))))
Пример #2
0
 def test_fitness_update(self, mocker):
     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))))