示例#1
0
    def apply_alp(self, previous_situation: Perception, action: int,
                  situation: Perception, time: int,
                  population: ClassifiersList,
                  match_set: ClassifiersList) -> None:
        """
        The Anticipatory Learning Process. Handles all updates by the ALP,
        insertion of new classifiers in pop and possibly matchSet, and
        deletion of inadequate classifiers in pop and possibly matchSet.

        :param previous_situation:
        :param action:
        :param situation:
        :param time:
        :param population:
        :param match_set:
        """
        new_list = ClassifiersList(cfg=self.cfg)
        new_cl: Optional[Classifier] = None
        was_expected_case = False
        delete_count = 0

        for cl in self:
            cl.increase_experience()
            cl.set_alp_timestamp(time)

            if cl.does_anticipate_correctly(previous_situation, situation):
                new_cl = expected_case(cl, previous_situation, time)
                was_expected_case = True
            else:
                new_cl = unexpected_case(cl, previous_situation, situation,
                                         time)

                if cl.is_inadequate():
                    # Removes classifier from population, match set
                    # and current list
                    delete_count += 1
                    lists = [x for x in [population, match_set, self] if x]
                    for lst in lists:
                        lst.safe_remove(cl)

            if new_cl is not None:
                new_cl.tga = time
                self.add_alp_classifier(new_cl, new_list)

        # No classifier anticipated correctly - generate new one
        if not was_expected_case:
            new_cl = cover(previous_situation, action, situation, time,
                           self.cfg)
            self.add_alp_classifier(new_cl, new_list)

        # Merge classifiers from new_list into self and population
        self.extend(new_list)
        population.extend(new_list)

        if match_set is not None:
            new_matching = [
                cl for cl in new_list if cl.condition.does_match(situation)
            ]
            match_set.extend(new_matching)
示例#2
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
示例#3
0
    def apply_alp(population: ClassifiersList, match_set: ClassifiersList,
                  action_set: ClassifiersList, p0: Perception, action: int,
                  p1: Perception, time: int, theta_exp: int,
                  cfg: Configuration) -> None:
        """
        The Anticipatory Learning Process. Handles all updates by the ALP,
        insertion of new classifiers in pop and possibly matchSet, and
        deletion of inadequate classifiers in pop and possibly matchSet.

        Parameters
        ----------
        population
        match_set
        action_set
        p0: Perception
        action: int
        p1: Perception
        time: int
        theta_exp
        cfg: Configuration

        Returns
        -------

        """
        new_list = ClassifiersList()
        new_cl: Optional[Classifier] = None
        was_expected_case = False
        delete_count = 0

        for cl in action_set:
            cl.increase_experience()
            cl.update_application_average(time)

            if cl.does_anticipate_correctly(p0, p1):
                new_cl = alp_acs2.expected_case(cl, p0, time)
                was_expected_case = True
            else:
                new_cl = alp_acs2.unexpected_case(cl, p0, p1, time)

                if cl.is_inadequate():
                    # Removes classifier from population, match set
                    # and current list
                    delete_count += 1
                    lists = [
                        x for x in [population, match_set, action_set] if x
                    ]
                    for lst in lists:
                        lst.safe_remove(cl)

            if new_cl is not None:
                new_cl.tga = time
                alp.add_classifier(new_cl, action_set, new_list, theta_exp)

        # No classifier anticipated correctly - generate new one
        if not was_expected_case:
            new_cl = alp_acs2.cover(p0, action, p1, time, cfg)
            alp.add_classifier(new_cl, action_set, new_list, theta_exp)

        # Merge classifiers from new_list into self and population
        action_set.extend(new_list)
        population.extend(new_list)

        if match_set is not None:
            new_matching = [
                cl for cl in new_list if matching(cl.condition, p1)
            ]
            match_set.extend(new_matching)