Exemplo n.º 1
0
    def test_should_detect_correct_anticipation_6(self, cfg):
        # Case when effect predicts situation incorrectly

        # given
        cls = Classifier(effect=Effect(
            ['#', '#', '1', '#', '0', '#', '0', '#']),
                         cfg=cfg)
        p0 = Perception(['0', '0', '0', '1', '1', '0', '1', '0'])
        p1 = Perception(['0', '0', '1', '1', '0', '0', '0', '0'])

        # then
        assert cls.does_anticipate_correctly(p0, p1) is True
Exemplo n.º 2
0
    def test_should_exploit_using_majority_voting(self, cfg):
        # given
        cl1 = Classifier(action=1,
                         effect='1###0###',
                         reward=0.1,
                         quality=0.7,
                         numerosity=9,
                         cfg=cfg)

        cl2 = Classifier(action=2,
                         effect='1###0###',
                         reward=0.1,
                         quality=0.7,
                         numerosity=10,
                         cfg=cfg)
        population = ClassifiersList(*[cl1, cl2])

        # when
        action = exploit(population, cfg.number_of_possible_actions)

        # then
        assert action == 2
Exemplo n.º 3
0
    def test_should_predict_successfully_1(self, cfg):
        # given
        action = 5
        cls = Classifier(condition='1#0111#1',
                         action=action,
                         effect='0#1000#0',
                         quality=0.94,
                         cfg=cfg)
        p0 = Perception('11011101')
        p1 = Perception('01100000')

        # then
        assert cls.predicts_successfully(p0, action, p1) is True
Exemplo n.º 4
0
    def test_should_detect_correct_anticipation_2(self, cfg):
        # Introduce two changes into situation and effect (should
        # also predict correctly)

        # given
        cls = Classifier(effect=Effect(
            ['#', '1', '#', '#', '#', '#', '0', '#']),
                         cfg=cfg)
        p0 = Perception(['0', '0', '0', '0', '1', '1', '1', '1'])
        p1 = Perception(['0', '1', '0', '0', '1', '1', '0', '1'])

        # then
        assert cls.does_anticipate_correctly(p0, p1) is True
Exemplo n.º 5
0
    def test_should_exploit_with_single_classifier(self, cfg):
        # given
        cl = Classifier(action=2,
                        effect='1###0###',
                        reward=0.25,
                        cfg=cfg)
        population = ClassifiersList(*[cl])

        # when
        action = exploit(population, cfg.number_of_possible_actions)

        # then
        assert action == 2
Exemplo n.º 6
0
    def test_find_old_classifier_similar_and_subsumer_subsumer_returned(
            self, cfg):
        # given
        subsumer = Classifier(condition='1#######',
                              action=1,
                              experience=21,
                              quality=0.95,
                              cfg=cfg)

        similar = Classifier(condition='10######', action=1, cfg=cfg)

        existing_classifiers = ClassifiersList(*[similar, subsumer], cfg=cfg)

        classifier = Classifier(condition='10######', action=1, cfg=cfg)

        # when
        old_cls = existing_classifiers.find_old_classifier(classifier)

        # then
        assert subsumer.does_subsume(classifier) is True
        assert similar == classifier
        assert subsumer == old_cls
Exemplo n.º 7
0
    def test_should_handle_pass_through_symbol(self, cfg):
        # A case when there was no change in perception but effect has no
        # pass-through symbol

        # given
        cls = Classifier(effect=Effect(
            ['#', '0', '#', '#', '#', '#', '#', '#']),
                         cfg=cfg)
        p0 = Perception(['0', '0', '0', '0', '1', '1', '1', '1'])
        p1 = Perception(['0', '0', '0', '0', '1', '1', '1', '1'])

        # then
        assert cls.does_anticipate_correctly(p0, p1) is False
Exemplo n.º 8
0
    def test_should_return_best_fitness_action(self, cfg):
        # given
        all_actions = cfg.number_of_possible_actions
        population = ClassifiersList(cfg=cfg)

        # when & then
        # C1 - does not anticipate change
        c1 = Classifier(action=1, cfg=cfg)
        population.append(c1)

        # Some random action should be selected here
        best_action = exploit(population, all_actions)
        assert best_action is not None

        # when & then
        # C2 - does anticipate some change
        c2 = Classifier(action=2,
                        effect='1###0###',
                        reward=0.25,
                        cfg=cfg)
        population.append(c2)

        # Here C2 action should be selected
        best_action = exploit(population, all_actions)
        assert best_action == 2

        # when & then
        # C3 - does anticipate change and is quite good
        c3 = Classifier(action=3,
                        effect='1#######',
                        quality=0.8,
                        reward=5,
                        cfg=cfg)
        population.append(c3)

        # Here C3 has the biggest fitness score
        best_action = exploit(population, all_actions)
        assert best_action == 3
Exemplo n.º 9
0
    def test_form_sequence_forwards_2(self, cfg):
        # given
        gs = GoalSequenceSearcher()
        cl0 = Classifier(condition="01010101",
                         action=2,
                         effect="0000000",
                         cfg=cfg)
        cl1 = Classifier(condition="11111111",
                         action=0,
                         effect="0000000",
                         cfg=cfg)
        gs.forward_classifiers = [ClassifiersList(cl0)]
        i = 1
        idx = 0

        # when
        seq = gs._form_sequence_forwards(i, idx, cl1)

        # then
        assert len(seq) == 2
        assert cl0.action in seq
        assert cl1.action in seq
        assert seq == [cl0.action, cl1.action]
Exemplo n.º 10
0
    def test_should_subsume_another_classifier_2(self, cfg):
        # given
        cls = Classifier(quality=0.84, reward=0.33, experience=3, cfg=cfg)
        cls.condition[0] = '1'
        cls.condition[1] = '0'
        cls.condition[4] = '0'
        cls.condition[6] = '1'
        cls.action = 6
        cls.effect[0] = '0'
        cls.effect[1] = '1'
        cls.effect[6] = '0'

        other = Classifier(quality=0.5, reward=0.41, experience=1, cfg=cfg)
        other.condition[0] = '1'
        other.condition[1] = '0'
        other.condition[6] = '2'
        other.action = 3
        other.effect[0] = '0'
        other.effect[1] = '1'
        other.effect[6] = '0'

        # when & then
        assert cls.does_subsume(other) is False
Exemplo n.º 11
0
    def test_search_goal_sequence_3(self, cfg):
        # given
        gs = GoalSequenceSearcher()
        start = "01111111"
        goal = "10111111"

        reliable_classifiers = ClassifiersList(
            Classifier(condition="#1######",
                       action=1,
                       effect="#0######",
                       cfg=cfg),
            Classifier(condition="#0######",
                       action=3,
                       effect="#1######",
                       cfg=cfg),
        )

        # when
        result = gs.search_goal_sequence(reliable_classifiers,
                                         start=start,
                                         goal=goal)
        # then
        assert result == []
Exemplo n.º 12
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
Exemplo n.º 13
0
    def test_should_apply_reinforcement_learning(self, cfg):
        # given
        population = ClassifiersList(cfg=cfg)
        c1 = Classifier(cfg=cfg)
        c1.r = 34.29
        c1.ir = 11.29
        population.append(c1)

        # when
        population.apply_reinforcement_learning(0, 28.79)

        # then
        assert abs(33.94 - population[0].r) < 0.1
        assert abs(10.74 - population[0].ir) < 0.1
Exemplo n.º 14
0
    def test_should_select_parents1(self, cfg):
        # given
        population = ClassifiersList(cfg=cfg)
        c0 = Classifier(condition='######00', cfg=cfg)
        c1 = Classifier(condition='######01', cfg=cfg)
        c2 = Classifier(condition='######10', cfg=cfg)
        c3 = Classifier(condition='######11', cfg=cfg)
        population.append(c0)
        population.append(c1)
        population.append(c2)
        population.append(c3)

        # when
        p1, p2 = roulette_wheel_parents_selection(population,
                                                  randomfunc=(RandomMock(
                                                      [0.7, 0.1])))

        # then
        assert c0 == p1

        # when
        p1, p2 = roulette_wheel_parents_selection(population,
                                                  randomfunc=(RandomMock(
                                                      [0.3, 0.6])))

        # then
        assert c1 == p1
        assert c2 == p2

        # when
        p1, p2 = roulette_wheel_parents_selection(population,
                                                  randomfunc=(RandomMock(
                                                      [0.2, 0.8])))

        # then
        assert c0 == p1
        assert c3 == p2
Exemplo n.º 15
0
    def test_find_subsumer_finds_selects_more_general_subsumer1(self, cfg):
        # given
        subsumer1 = Classifier(condition='1##0####',
                               action=3,
                               effect='##1#####',
                               quality=0.93,
                               reward=1.35,
                               experience=23,
                               cfg=cfg)
        subsumer2 = Classifier(condition='###0####',
                               action=3,
                               effect='##1#####',
                               quality=0.93,
                               reward=1.35,
                               experience=23,
                               cfg=cfg)

        nonsubsumer = Classifier(cfg=cfg)

        classifier = Classifier(condition='11#0####',
                                action=3,
                                effect='##1#####',
                                quality=0.5,
                                reward=0.35,
                                experience=1,
                                cfg=cfg)

        classifiers_list = ClassifiersList(
            *[nonsubsumer, subsumer2, subsumer1, nonsubsumer], cfg=cfg)

        # when
        actual_subsumer = classifiers_list.find_subsumer(
            classifier, choice_func=lambda l: l[0])

        # then
        assert actual_subsumer == subsumer2
Exemplo n.º 16
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)
Exemplo n.º 17
0
    def test_form_new_classifiers_1(self, cfg):
        # given
        gs = GoalSequenceSearcher()
        cls_list = [ClassifiersList()]
        cl = Classifier(condition="01010101",
                        action=2,
                        effect="0000000",
                        cfg=cfg)
        i = 0

        # when
        new_classifiers = gs._form_new_classifiers(cls_list, i, cl)

        # then
        assert len(new_classifiers) == 1
        assert cl in new_classifiers
Exemplo n.º 18
0
    def test_form_sequence_forwards_1(self, cfg):
        # given
        gs = GoalSequenceSearcher()
        cl0 = Classifier(condition="01010101",
                         action=2,
                         effect="0000000",
                         cfg=cfg)
        i = 0
        idx = 0

        # when
        seq = gs._form_sequence_forwards(i, idx, cl0)

        # then
        assert len(seq) == 1
        assert cl0.action in seq
Exemplo n.º 19
0
    def test_apply_ga(self, cfg):
        # given
        cl_1 = Classifier(condition='#1#1#1#1', numerosity=12, cfg=cfg)
        cl_2 = Classifier(condition='0#0#0#0#', numerosity=9, cfg=cfg)
        action_set = ClassifiersList(*[cl_1, cl_2], cfg=cfg)
        match_set = ClassifiersList(*[cl_1, cl_2], cfg=cfg)
        population = ClassifiersList(*[cl_1, cl_2], cfg=cfg)

        random_sequence = \
            [
                0.1, 0.6,  # parent selection
                0.1, 0.5, 0.5, 0.5,  # mutation of child1
                0.5, 0.1, 0.5, 0.5,  # mutation of child2
                0.1,  # do crossover
            ] + [0.5] * 12 + [0.2] + [0.5] * 8 + \
            [0.2] + [0.5] * 20 + [0.2] + [0.5] * 20

        # when
        action_set.apply_ga(101,
                            population,
                            match_set,
                            None,
                            randomfunc=RandomMock(random_sequence),
                            samplefunc=SampleMock([0, 4]))

        # then
        modified_parent1 = Classifier(condition='#1#1#1#1',
                                      numerosity=10,
                                      tga=101,
                                      cfg=cfg)

        modified_parent2 = Classifier(condition='0#0#0#0#',
                                      numerosity=8,
                                      tga=101,
                                      cfg=cfg)

        child1 = Classifier(condition='0####1#1',
                            quality=0.25,
                            talp=101,
                            tga=101,
                            cfg=cfg)

        child2 = Classifier(condition='###10#0#',
                            quality=0.25,
                            talp=101,
                            tga=101,
                            cfg=cfg)

        expected_population = ClassifiersList(
            *[modified_parent1, modified_parent2, child1, child2], cfg=cfg)

        # it might sometime fails because one function RNDG is not mocked
        assert expected_population == population
        assert expected_population == match_set
        assert expected_population == action_set
Exemplo n.º 20
0
    def test_should_set_mark_from_condition_3(self, cfg):
        # given
        p0 = Perception('11111010')
        cls = Classifier(condition='11#11##0', cfg=cfg)

        # when
        cls.set_mark(p0)

        # Then
        assert 1 == len(cls.mark[2])
        assert '1' in cls.mark[2]

        assert 1 == len(cls.mark[5])
        assert '0' in cls.mark[5]

        assert 1 == len(cls.mark[6])
        assert '1' in cls.mark[6]
Exemplo n.º 21
0
    def test_copy_from_and_change_does_not_influence_another_effect(self, cfg):
        """ Verify that not just reference to Condition copied (changing which
        will change the original - definitily not original C++ code did). """
        # given
        operation_time = 123
        original_cl = Classifier(effect='10####1#', cfg=cfg)

        # when
        copied_cl = Classifier.copy_from(original_cl, operation_time)

        # when & then
        copied_cl.effect[2] = '1'
        assert Effect('101###1#') == copied_cl.effect
        assert Effect('10####1#') == original_cl.effect

        # when & then
        original_cl.effect[3] = '0'
        assert Effect('101###1#') == copied_cl.effect
        assert Effect('10#0##1#') == original_cl.effect
Exemplo n.º 22
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
Exemplo n.º 23
0
    def test_should_set_mark_from_condition_2(self, cfg):
        # given
        p0 = Perception('12101101')
        cls = Classifier(condition='###0#101', cfg=cfg)

        # when
        cls.set_mark(p0)

        # then
        assert 1 == len(cls.mark[0])
        assert '1' in cls.mark[0]

        assert 1 == len(cls.mark[1])
        assert '2' in cls.mark[1]

        assert 1 == len(cls.mark[2])
        assert '1' in cls.mark[2]

        assert 1 == len(cls.mark[4])
        assert '1' in cls.mark[4]
Exemplo n.º 24
0
    def test_should_return_worst_quality_action(self, cfg):
        # given
        all_actions = cfg.number_of_possible_actions
        population = ClassifiersList()
        c0 = Classifier(action=0, cfg=cfg)
        population.append(c0)

        # Should return C1 (because it's first not mentioned)
        assert choose_action_from_knowledge_array(population, all_actions) == 1

        # Add rest of classifiers
        c1 = Classifier(action=1, numerosity=31, quality=0.72, cfg=cfg)
        c2 = Classifier(action=2, numerosity=2, quality=0.6, cfg=cfg)
        c3 = Classifier(action=3, numerosity=2, quality=0.63, cfg=cfg)
        c4 = Classifier(action=4, numerosity=7, quality=0.75, cfg=cfg)
        c5 = Classifier(action=5, numerosity=1, quality=0.63, cfg=cfg)
        c6 = Classifier(action=6, numerosity=6, quality=0.52, cfg=cfg)
        c7 = Classifier(action=7, numerosity=10, quality=0.36, cfg=cfg)
        population += ClassifiersList(*[c1, c2, c3, c4, c5, c6, c7])

        # then
        # Classifier C7 should be the worst here
        assert choose_action_from_knowledge_array(population, all_actions) == 7
Exemplo n.º 25
0
    def test_search_one_backward_step_1(self, cfg):
        # given
        gs = GoalSequenceSearcher()
        start = "01111111"
        goal = "00111111"
        gs.forward_perceptions.append(Perception(start))
        gs.backward_perceptions.append(Perception(goal))
        reliable_classifiers = ClassifiersList(
            Classifier(condition="#1######",
                       action=1,
                       effect="#0######",
                       cfg=cfg))
        forward_size = 1
        forward_point = 0

        # when
        act_seq, size = gs._search_one_backward_step(reliable_classifiers,
                                                     forward_size,
                                                     forward_point)

        # then
        assert act_seq == [1]
Exemplo n.º 26
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
Exemplo n.º 27
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
Exemplo n.º 28
0
    def test_should_set_mark_from_condition_1(self, cfg):
        # given
        p0 = Perception('00001111')
        cls = Classifier(condition='##0#1#1#', cfg=cfg)
        cls.mark[0].add('0')
        cls.mark[1].add('0')
        cls.mark[3].add('0')
        cls.mark[5].add('1')
        cls.mark[7].add('1')

        # when
        cls.set_mark(p0)

        # then
        assert 8 == len(cls.mark)
        assert 1 == len(cls.mark[0])  # 0
        assert 1 == len(cls.mark[1])  # 0
        assert 0 == len(cls.mark[2])
        assert 1 == len(cls.mark[3])  # 0
        assert 0 == len(cls.mark[4])
        assert 1 == len(cls.mark[5])  # 1
        assert 0 == len(cls.mark[6])
        assert 1 == len(cls.mark[7])  # 1
Exemplo n.º 29
0
    def test_select_classifier_to_delete(self, cfg):
        # given
        selected_first = Classifier(quality=0.5, cfg=cfg)
        much_worse = Classifier(quality=0.2, cfg=cfg)
        yet_another_to_consider = Classifier(quality=0.2, cfg=cfg)
        classifiers = ClassifiersList(*[
            Classifier(cfg=cfg), selected_first,
            Classifier(cfg=cfg), much_worse, yet_another_to_consider,
            Classifier(cfg=cfg)
        ],
                                      cfg=cfg)

        # when
        actual_selected = classifiers.select_classifier_to_delete(
            randomfunc=RandomMock([0.5, 0.1, 0.5, 0.1, 0.1, 0.5]))

        # then
        assert much_worse == actual_selected
Exemplo n.º 30
0
    def test_should_handle_unexpected_case_3(self, cfg):
        cls = Classifier(condition='#####1#0',
                         effect='#####0#1',
                         quality=0.475,
                         cfg=cfg)

        cls.mark[0].add('1')
        cls.mark[1].add('1')
        cls.mark[2].add('0')
        cls.mark[3].add('1')
        cls.mark[5].add('1')
        cls.mark[7].add('1')

        p0 = Perception('11001110')
        p1 = Perception('01110000')
        time = 20

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

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

        # No classifier should be generated here
        assert new_cls is None