示例#1
0
    def test_generalizing_mutation(self, _mu, _cond1, _cond2):
        # given
        cfg = acs2.Configuration(
            classifier_length=4, number_of_possible_actions=2)
        cl = acs2.Classifier(condition=_cond1, cfg=cfg)

        # when
        ga.generalizing_mutation(cl, mu=_mu)

        # then
        assert cl.condition == acs.Condition(_cond2)
示例#2
0
    def apply_ga(time: int,
                 population: ClassifiersList,
                 match_set: ClassifiersList,
                 action_set: ClassifiersList,
                 p: Perception,
                 theta_ga: int,
                 mu: float,
                 chi: float,
                 theta_as: int,
                 do_subsumption: bool,
                 theta_exp: int) -> None:

        if ga.should_apply(action_set, time, theta_ga):
            ga.set_timestamps(action_set, time)

            # Select parents
            parent1, parent2 = ga.roulette_wheel_selection(
                action_set, lambda cl: pow(cl.q, 3) * cl.num)

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

            # Execute mutation
            ga.generalizing_mutation(child1, mu)
            ga.generalizing_mutation(child2, mu)

            # Execute cross-over
            if random.random() < chi:
                if child1.effect == child2.effect:
                    ga.two_point_crossover(child1, child2)

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

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

            # We are interested only in classifiers with specialized condition
            unique_children = {cl for cl in [child1, child2]
                               if cl.condition.specificity > 0}

            ga.delete_classifiers(
                population, match_set, action_set,
                len(unique_children), theta_as)

            # check for subsumers / similar classifiers
            for child in unique_children:
                ga.add_classifier(child, p,
                                  population, match_set, action_set,
                                  do_subsumption, theta_exp)