Esempio n. 1
0
    def test_copy_from_and_mutate_does_not_influence_another_condition(
            self, cfg):
        """ Verify that not just reference to Condition copied (changing which
        will change the original - definitily not original C++ code did). """
        # given
        s = cfg.mu * 0.5  # less then MU
        b = 1 - (1 - cfg.mu) * 0.5  # more then MU

        operation_time = 123
        original_cl = Classifier(condition='1###1011', cfg=cfg)

        copied_cl = Classifier.copy_from(original_cl, operation_time)

        # when
        mutate(copied_cl, cfg.mu, RandomMock([s, b, b, b, b]))

        # then
        assert Condition('####1011') == copied_cl.condition
        assert Condition('1###1011') == original_cl.condition

        # when
        mutate(original_cl, cfg.mu, RandomMock([b, s, b, b, b]))

        # then
        assert Condition('1####011') == original_cl.condition
        assert Condition('####1011') == copied_cl.condition
Esempio n. 2
0
    def test_should_match_perception(self, _c, _p, _result):
        # given
        c = Condition(_c)
        p = Perception(_p)

        # then
        assert c.does_match(p) is _result
Esempio n. 3
0
    def test_should_initialize_two_times_the_same_way(self):
        # given
        c1 = Condition("#1O##O##")
        c2 = Condition(['#', '1', 'O', '#', '#', 'O', '#', '#'])

        # then
        assert c1 == c2
Esempio n. 4
0
    def test_should_match_condition(self, _c, _other, _result):
        # given
        c = Condition(_c)
        other = Condition(_other)

        # then
        assert c.does_match(other) is _result
Esempio n. 5
0
    def test_generalize_decrements_specificity(self):
        # given
        condition = Condition('#11#####')
        assert 2 == condition.specificity

        # when
        condition.generalize(1)

        # then
        assert 1 == condition.specificity
Esempio n. 6
0
    def test_should_generalize(self):
        # given
        s = "#1O##O##"
        cond = Condition(s)

        # when
        cond.generalize(position=1)

        # then
        assert Condition("##O##O##") == cond
Esempio n. 7
0
    def test_crossover_allows_to_change_last_element(self, cfg):
        # given
        cl1 = Classifier(condition='0##10###', cfg=cfg)
        cl2 = Classifier(condition='#10##011', cfg=cfg)

        # when
        two_point_crossover(cl1, cl2, samplefunc=SampleMock([5, 8]))

        # then
        assert Condition('0##10011') == cl1.condition
        assert Condition('#10#####') == cl2.condition
Esempio n. 8
0
    def test_mutate_2(self, cfg):
        # given
        cl = Classifier(Condition('##011###'), cfg=cfg)
        s = cfg.mu * 0.5  # less then MU
        b = 1 - (1 - cfg.mu) * 0.5  # more then MU

        # when
        mutate(cl, cfg.mu, randomfunc=RandomMock([b, b, s]))

        # then
        assert Condition('##01####') == cl.condition
Esempio n. 9
0
    def test_crossover(self, cfg):
        # given
        cl1 = Classifier(condition='0##10###', cfg=cfg)
        cl2 = Classifier(condition='#10##0##', cfg=cfg)

        # when
        two_point_crossover(cl1, cl2, samplefunc=SampleMock([1, 4]))

        # then
        assert Condition('010#0###') == cl1.condition
        assert Condition('###1#0##') == cl2.condition
Esempio n. 10
0
    def test_should_generalize_specific_attributes_randomly(
            self, _condition, _spec_before, _spec_after):

        # given
        condition = Condition(_condition)
        assert condition.specificity == _spec_before

        # when
        condition.generalize_specific_attribute_randomly()

        # then
        assert condition.specificity == _spec_after
Esempio n. 11
0
    def test_should_specialize(self, _p0, _p1, _init_cond, _init_effect,
                               _res_cond, _res_effect, cfg):
        # given
        cls = Classifier(condition=Condition(_init_cond),
                         effect=Effect(_init_effect),
                         cfg=cfg)
        p0 = Perception(_p0)
        p1 = Perception(_p1)

        # when
        cls.specialize(p0, p1, leave_specialized=False)

        # then
        assert cls.condition == Condition(_res_cond)
        assert cls.effect == Effect(_res_effect)
Esempio n. 12
0
    def test_should_handle_unexpected_case_2(self, cfg):
        # given
        cls = Classifier(condition='#######0', action=4, quality=0.4, cfg=cfg)
        cls.mark[0].update([0, 1])
        cls.mark[1].update([0, 1])
        cls.mark[2].update([0, 1])
        cls.mark[3].update([0, 1])
        cls.mark[4].update([1])
        cls.mark[5].update([0, 1])
        cls.mark[6].update([0, 1])

        p0 = Perception('11101010')
        p1 = Perception('10011101')
        time = 94

        # when
        new_cl = unexpected_case(cls, p0, p1, time)

        # then
        assert new_cl.condition == Condition('#110#010')
        assert new_cl.effect == Effect('#001#101')
        assert new_cl.is_marked() is False
        assert time == new_cl.tga
        assert time == new_cl.talp
        assert abs(cls.q - 0.38) < 0.01
Esempio n. 13
0
    def test_should_return_number_of_specified_elements(self):
        # given
        condition = Condition.empty(8)
        assert 0 == condition.specificity

        # when
        condition[2] = '1'
        condition[5] = '0'

        # then
        assert condition.specificity == 2
Esempio n. 14
0
    def test_should_get_differences_1(self, _p0, cfg):
        # given
        generic_condition = Condition.empty(length=cfg.classifier_length)
        p0 = Perception(_p0)
        mark = PMark(cfg)

        # when
        diff = mark.get_differences(p0)

        # then
        assert diff == generic_condition
Esempio n. 15
0
    def test_should_generalize_second_unchanging_attribute(self, cfg):
        # given
        cls = Classifier(condition='#####0#0', effect='########', cfg=cfg)

        assert len(cls.specified_unchanging_attributes) == 2

        # when
        generalized = cls.generalize_unchanging_condition_attribute(
            lambda x: 7)

        # then
        assert generalized is True
        assert len(cls.specified_unchanging_attributes) == 1
        assert Condition('#####0##') == cls.condition
Esempio n. 16
0
    def test_should_specialize_with_condition(self, _condition, _diff,
                                              _result):

        # given
        cond = Condition(_condition)
        diff = Condition(_diff)

        # when
        cond.specialize_with_condition(diff)

        # then
        assert cond == Condition(_result)
Esempio n. 17
0
    def test_should_handle_unexpected_case_1(self, cfg):
        # given
        cls = Classifier(action=2, cfg=cfg)

        p0 = Perception('01100000')
        p1 = Perception('10100010')
        time = 14

        new_cls = unexpected_case(cls, p0, p1, time)

        # Quality should be decreased
        assert 0.475 == cls.q

        # Should be marked with previous perception
        for mark_attrib in cls.mark:
            assert 1 == len(mark_attrib)

        assert '0' in cls.mark[0]
        assert '1' in cls.mark[1]
        assert '1' in cls.mark[2]
        assert '0' in cls.mark[3]
        assert '0' in cls.mark[4]
        assert '0' in cls.mark[5]
        assert '0' in cls.mark[6]
        assert '0' in cls.mark[7]

        # New classifier should not be the same object
        assert cls is not new_cls

        # Check attributes of a new classifier
        assert Condition('01####0#') == new_cls.condition
        assert 2 == new_cls.action
        assert Effect('10####1#') == new_cls.effect

        # There should be no mark
        for mark_attrib in new_cls.mark:
            assert 0 == len(mark_attrib)

        assert 0.5 == new_cls.q
        assert cls.r == new_cls.r
        assert time == new_cls.tga
        assert time == new_cls.talp
Esempio n. 18
0
    def test_add_ga_classifier_increase_numerosity(self, cfg):
        # given
        cl_1 = Classifier(action=2, condition='1#######', cfg=cfg)
        cl_2 = Classifier(action=2, condition='1#######', cfg=cfg)
        cl_3 = Classifier(action=3, cfg=cfg)
        cl_4 = Classifier(action=4, cfg=cfg)

        action_set = ClassifiersList(*[cl_1], cfg=cfg)
        match_set = ClassifiersList(*[cl_1], cfg=cfg)
        population = ClassifiersList(*[cl_1, cl_3, cl_4], cfg=cfg)

        # when
        action_set.add_ga_classifier(cl_2, match_set, population)
        new_classifier = Classifier(action=2,
                                    condition=Condition('1#######'),
                                    numerosity=2,
                                    cfg=cfg)

        # then
        assert ClassifiersList(*[new_classifier, cl_3, cl_4],
                               cfg=cfg) == population
Esempio n. 19
0
    def test_should_create_new_classifier_using_covering(self, cfg):
        # given
        action = random.randint(0, cfg.number_of_possible_actions)
        time = random.randint(0, 1000)
        p0 = Perception('01001101')
        p1 = Perception('00011111')

        # when
        new_cl = cover(p0, action, p1, time, cfg)

        # then
        assert new_cl.condition == Condition('#1#0##0#')
        assert new_cl.action == action
        assert new_cl.effect == Effect('#0#1##1#')
        assert new_cl.q == .5
        assert new_cl.r == 0
        assert new_cl.ir == 0
        assert new_cl.tav == 0
        assert new_cl.tga == time
        assert new_cl.talp == time
        assert new_cl.num == 1
        assert new_cl.exp == 0
Esempio n. 20
0
    def test_should_handle_unexpected_case_6(self, cfg):
        # given
        cls = Classifier(condition='0#1####1',
                         action=2,
                         effect='1#0####0',
                         quality=0.38505,
                         reward=1.20898,
                         immediate_reward=0,
                         experience=11,
                         tga=95,
                         talp=873,
                         tav=71.3967,
                         cfg=cfg)
        cls.mark[1].update(['1'])
        cls.mark[3].update(['1'])
        cls.mark[4].update(['0', '1'])
        cls.mark[5].update(['1'])
        cls.mark[6].update(['0', '1'])

        p0 = Perception('01111101')
        p1 = Perception('11011110')
        time = 873

        # when
        new_cls = unexpected_case(cls, p0, p1, time)

        # then
        assert new_cls is not None
        assert Condition('0#1###01') == new_cls.condition
        assert Effect('1#0###10') == new_cls.effect
        assert abs(0.5 - new_cls.q) < 0.1
        assert abs(1.20898 - new_cls.r) < 0.1
        assert abs(0 - new_cls.ir) < 0.1
        assert abs(71.3967 - new_cls.tav) < 0.1
        assert 1 == new_cls.exp
        assert 1 == new_cls.num
        assert time == new_cls.tga
        assert time == new_cls.talp
Esempio n. 21
0
    def test_should_handle_unexpected_case_5(self, cfg):
        # given
        cls = Classifier(condition='00####1#',
                         action=2,
                         effect='########',
                         quality=0.129,
                         reward=341.967,
                         immediate_reward=130.369,
                         experience=201,
                         tga=129,
                         talp=9628,
                         tav=25.08,
                         cfg=cfg)
        cls.mark[2].add('2')
        cls.mark[3].add('1')
        cls.mark[4].add('1')
        cls.mark[5].add('0')
        cls.mark[7].add('0')

        p0 = Perception('00211010')
        p1 = Perception('00001110')
        time = 9628

        # when
        new_cls = unexpected_case(cls, p0, p1, time)

        # then
        assert new_cls is not None
        assert Condition('0021#01#') == new_cls.condition
        assert Effect('##00#1##') == new_cls.effect
        assert abs(0.5 - new_cls.q) < 0.1
        assert abs(341.967 - new_cls.r) < 0.1
        assert abs(130.369 - new_cls.ir) < 0.1
        assert abs(25.08 - new_cls.tav) < 0.1
        assert 1 == new_cls.exp
        assert 1 == new_cls.num
        assert time == new_cls.tga
        assert time == new_cls.talp
Esempio n. 22
0
 def test_equal(self):
     assert Condition('########') == Condition('########')
     assert Condition('1#######') != Condition('########')
     assert Condition('########') != Condition('#######1')
     assert Condition('1111####') == Condition('1111####')
     assert Condition('1111####') != Condition('1011####')
     assert Condition('1101####') != Condition('1111####')
     assert Condition('00001###') == Condition('00001###')
Esempio n. 23
0
    def test_should_only_accept_strings(self):
        condition = Condition([])

        with pytest.raises(TypeError):
            # Try to store an integer
            condition[0] = 1
Esempio n. 24
0
    def test_should_calculate_specificity(self, _condition, _specificity, cfg):

        cls = Classifier(Condition(_condition), cfg=cfg)
        assert cls.specificity == _specificity
Esempio n. 25
0
    def test_should_get_initialized_with_str(self):
        # given
        condition = Condition("#1O##O##")

        # then
        assert len(condition) == 8