Exemple #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
Exemple #2
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
Exemple #3
0
    def apply_ga(self,
                 time: int,
                 population: ClassifiersList,
                 match_set: ClassifiersList,
                 situation: Perception,
                 randomfunc=random,
                 samplefunc=sample) -> None:

        if self.should_apply_ga(time):
            self.set_ga_timestamp(time)
            parent1, parent2 = roulette_wheel_parents_selection(
                self, randomfunc=randomfunc)

            child1 = Classifier.copy_from(parent1, time)
            child2 = Classifier.copy_from(parent2, time)

            mutate(child1, child1.cfg.mu, randomfunc=randomfunc)
            mutate(child2, child2.cfg.mu, randomfunc=randomfunc)

            if randomfunc() < self.cfg.chi:
                if child1.effect == child2.effect:
                    two_point_crossover(child1, child2, samplefunc=samplefunc)

                    # Update quality and reward
                    # TODO: check if needed
                    child2.q = float(sum([child1.q, child2.q]) / 2)
                    child2.r = float(sum([child1.r, child2.r]) / 2)

            child1.q /= 2
            child2.q /= 2

            children = [
                child for child in [child1, child2]
                if child.condition.specificity > 0
            ]

            # if two classifiers are identical, leave only one
            unique_children = set(children)

            self.delete_ga_classifiers(population,
                                       match_set,
                                       len(unique_children),
                                       randomfunc=randomfunc)

            # check for subsumers / similar classifiers
            for child in unique_children:
                self.add_ga_classifier(child, match_set, population)