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
def test_quality_and_numerosity_influence_parent_selection(self, cfg): # given population = ClassifiersList(cfg=cfg) c0 = Classifier(condition='######00', quality=1, numerosity=1, cfg=cfg) c1 = Classifier(condition='######01', cfg=cfg) c2 = Classifier(condition='######10', cfg=cfg) population.append(c0) # q3num = 1 population.append(c1) # q3num = 0.0625 population.append(c2) # q3num = 0.0625 # when p1, p2 = roulette_wheel_parents_selection(population, randomfunc=(RandomMock( [0.888, 0.999]))) # then assert c1 == p1 assert c2 == p2 # when p1, p2 = roulette_wheel_parents_selection(population, randomfunc=(RandomMock( [0.888, 0.777]))) # then assert c0 == p1 assert c1 == p2
def test_delete_ga_classifiers(self, cfg): # given cl_1 = Classifier(action=1, cfg=cfg) cl_2 = Classifier(action=2, numerosity=20, cfg=cfg) cl_3 = Classifier(action=3, cfg=cfg) cl_4 = Classifier(action=4, cfg=cfg) action_set = ClassifiersList(*[cl_1, cl_2], cfg=cfg) match_set = ClassifiersList(*[cl_2], cfg=cfg) population = ClassifiersList(*[cl_1, cl_2, cl_3, cl_4], cfg=cfg) # when action_set.delete_ga_classifiers(population, match_set, 2, randomfunc=RandomMock( ([0.5, 0.1] + [0.5] * 19) * 3)) expected_action_set = ClassifiersList( *[cl_1, Classifier(action=2, numerosity=17, cfg=cfg)], cfg=cfg) expected_match_set = ClassifiersList( *[Classifier(action=2, numerosity=17, cfg=cfg)], cfg=cfg) expected_population = ClassifiersList( *[cl_1, Classifier(action=2, numerosity=17, cfg=cfg), cl_3, cl_4], cfg=cfg) # then assert expected_action_set == action_set assert expected_match_set == match_set assert expected_population == population
def test_delete_a_classifier_decrease_numerosity(self, cfg): # given cl_1 = Classifier(action=1, cfg=cfg) cl_2 = Classifier(action=2, numerosity=3, cfg=cfg) cl_3 = Classifier(action=3, cfg=cfg) cl_4 = Classifier(action=4, cfg=cfg) action_set = ClassifiersList(*[cl_1, cl_2], cfg=cfg) match_set = ClassifiersList(*[cl_2], cfg=cfg) population = ClassifiersList(*[cl_1, cl_2, cl_3, cl_4], cfg=cfg) # when action_set.delete_a_classifier(match_set, population, randomfunc=RandomMock( [0.5, 0.1, 0.5, 0.5])) expected_action_set = ClassifiersList( *[cl_1, Classifier(action=2, numerosity=2, cfg=cfg)], cfg=cfg) expected_match_set = ClassifiersList( *[Classifier(action=2, numerosity=2, cfg=cfg)], cfg=cfg) expected_population = ClassifiersList( *[cl_1, Classifier(action=2, numerosity=2, cfg=cfg), cl_3, cl_4], cfg=cfg) # then assert expected_action_set == action_set assert expected_match_set == match_set assert expected_population == population
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
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
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
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
def test_delete_a_classifier_delete(self, cfg): # given cl_1 = Classifier(action=1, cfg=cfg) cl_2 = Classifier(action=2, cfg=cfg) cl_3 = Classifier(action=3, cfg=cfg) cl_4 = Classifier(action=4, cfg=cfg) action_set = ClassifiersList(*[cl_1, cl_2], cfg=cfg) match_set = ClassifiersList(*[cl_2], cfg=cfg) population = ClassifiersList(*[cl_1, cl_2, cl_3, cl_4], cfg=cfg) # when action_set.delete_a_classifier(match_set, population, randomfunc=RandomMock( [0.5, 0.1, 0.5, 0.5])) # then assert ClassifiersList(*[cl_1], cfg=cfg) == action_set assert ClassifiersList(cfg=cfg) == match_set assert ClassifiersList(*[cl_1, cl_3, cl_4], cfg=cfg) == population
def test_randommock_returns_values_in_a_given_sequence(self): f = RandomMock([0.1, 0.2, 0.3]) assert 0.1 == f() assert 0.2 == f() assert 0.3 == f()