def test_should_add_classifier(self): # given cfg = acs2.Configuration( classifier_length=8, number_of_possible_actions=4) cl_1 = acs2.Classifier(action=1, cfg=cfg) cl_3 = acs2.Classifier(action=3, cfg=cfg) cl_4 = acs2.Classifier(action=4, cfg=cfg) population = acs2.ClassifiersList(*[cl_1, cl_3, cl_4]) match_set = acs2.ClassifiersList() action_set = acs2.ClassifiersList(*[cl_1]) p0 = Perception('10101010') cl = acs2.Classifier( action=1, condition='1#######', cfg=cfg) # when ga.add_classifier(cl, p0, population, match_set, action_set, do_subsumption=True, theta_exp=cfg.theta_exp) # then assert acs2.ClassifiersList(*[cl_1, cl_3, cl_4, cl]) == population assert acs2.ClassifiersList(*[cl]) == match_set assert acs2.ClassifiersList(*[cl_1, cl]) == action_set
def test_add_ga_classifier_increase_numerosity(self): # given cfg = acs2.Configuration( classifier_length=8, number_of_possible_actions=4) cl_1 = acs2.Classifier(action=2, condition='1#######', cfg=cfg) cl_2 = acs2.Classifier(action=3, cfg=cfg) cl_3 = acs2.Classifier(action=4, cfg=cfg) population = acs2.ClassifiersList(*[cl_1, cl_2, cl_3]) match_set = acs2.ClassifiersList(*[cl_1]) action_set = acs2.ClassifiersList(*[cl_1]) cl = acs2.Classifier(action=2, condition='1#######', cfg=cfg) # when p0 = Perception('10101010') ga.add_classifier(cl, p0, population, match_set, action_set, do_subsumption=True, theta_exp=cfg.theta_exp) # then assert cl_1.num == 2 assert acs2.ClassifiersList(*[cl_1, cl_2, cl_3]) == population assert acs2.ClassifiersList(*[cl_1]) == match_set assert acs2.ClassifiersList(*[cl_1]) == action_set
def apply_ga(time: int, population: ClassifierList, match_set: ClassifierList, action_set: ClassifierList, 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 mutate(child1, mu) mutate(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)