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_should_return_latest_action(self, cfg): # given all_actions = cfg.number_of_possible_actions population = ClassifiersList() c0 = Classifier(action=0, cfg=cfg) c0.talp = 1 # when population.append(c0) # Should return first action with no classifiers assert 1 == choose_latest_action(population, all_actions) # Add rest of classifiers population.append(Classifier(action=3, cfg=cfg)) population.append(Classifier(action=7, cfg=cfg)) population.append(Classifier(action=5, cfg=cfg)) population.append(Classifier(action=1, cfg=cfg)) population.append(Classifier(action=4, cfg=cfg)) population.append(Classifier(action=2, cfg=cfg)) population.append(Classifier(action=6, cfg=cfg)) # Assign each classifier random talp from certain range for cl in population: cl.talp = randint(70, 100) # But third classifier (action 7) will be the executed long time ago population[2].talp = randint(10, 20) # then assert choose_latest_action(population, all_actions) == 7
def test_should_form_match_set_backwards(self, cfg): # given population = ClassifiersList() situation = Perception('11110000') # C1 - general condition c1 = Classifier(cfg=cfg) # C2 - matching c2 = Classifier(condition='0##0####', effect='1##1####', cfg=cfg) # C3 - non-matching c3 = Classifier(condition='0###1###', effect='1######0', cfg=cfg) # C4 - non-matching c4 = Classifier(condition='0###0###', effect='1###1###', cfg=cfg) population.append(c1) population.append(c2) population.append(c3) population.append(c4) # when match_set = ClassifiersList.form_match_set_backwards( population, situation) # then assert 2 == len(match_set) assert c1 in match_set assert c2 in match_set
def test_get_quality_classifiers_list(self, cfg): # given population = ClassifiersList() # C1 - matching c1 = Classifier(quality=0.9, cfg=cfg) # C2 - matching c2 = Classifier(quality=0.7, cfg=cfg) # C3 - non-matching c3 = Classifier(quality=0.5, cfg=cfg) # C4 - non-matching c4 = Classifier(quality=0.1, cfg=cfg) population.append(c1) population.append(c2) population.append(c3) population.append(c4) # when match_set = get_quality_classifiers_list(population, 0.5) # then assert 2 == len(match_set) assert c1 in match_set assert c2 in match_set
def test_should_insert_classifier_2(self, cfg): # given population = ClassifiersList(cfg=cfg) # when population.append(Classifier(cfg=cfg)) # then assert 1 == len(population)
def test_should_insert_classifier(self, cfg): # given population = ClassifiersList() cl = Classifier(cfg=cfg) # when population.append(cl) # then assert len(population) == 1
def test_should_apply_reinforcement_learning(self, cfg): # given population = ClassifiersList(cfg=cfg) c1 = Classifier(cfg=cfg) c1.r = 34.29 c1.ir = 11.29 population.append(c1) # when population.apply_reinforcement_learning(0, 28.79) # then assert abs(33.94 - population[0].r) < 0.1 assert abs(10.74 - population[0].ir) < 0.1
def test_should_get_similar_classifier(self, cfg): # given pop = ClassifiersList(cfg=cfg) pop.append(Classifier(action=1, cfg=cfg)) pop.append(Classifier(action=2, cfg=cfg)) pop.append(Classifier(action=3, cfg=cfg)) # when & then # No similar classifiers exist assert pop.get_similar(Classifier(action=4, cfg=cfg)) is None # when & then # Should find similar classifier assert pop.get_similar(Classifier(action=2, cfg=cfg)) is not None
def test_should_expand(self, cfg): # given population = ClassifiersList(cfg=cfg) c0 = Classifier(action=0, cfg=cfg) c1 = Classifier(action=1, numerosity=2, cfg=cfg) c2 = Classifier(action=2, numerosity=3, cfg=cfg) population.append(c0) population.append(c1) population.append(c2) # when expanded = population.expand() # then assert 6 == len(expanded) assert c0 in expanded assert c1 in expanded assert c2 in expanded
def test_should_return_best_fitness_action(self, cfg): # given population = ClassifiersList(cfg=cfg) # when & then # C1 - does not anticipate change c1 = Classifier(action=1, cfg=cfg) population.append(c1) # Some random action should be selected here best_action = exploit(population) assert best_action is not None # when & then # C2 - does anticipate some change c2 = Classifier(action=2, effect='1###0###', reward=0.25, cfg=cfg) population.append(c2) # Here C2 action should be selected best_action = exploit(population) assert 2 == best_action # when & then # C3 - does anticipate change and is quite good c3 = Classifier(action=3, effect='1#######', quality=0.8, reward=5, cfg=cfg) population.append(c3) # Here C3 has the biggest fitness score best_action = exploit(population) assert 3 == best_action
def get_quality_classifiers_list(classifiers: ClassifiersList, quality: float) -> ClassifiersList: """ Constructs classifier list out of a list with q > quality. Parameters ---------- classifiers quality Returns ------- """ # TODO: refactor - list comprehension listp = ClassifiersList() for item in classifiers: if item.q > quality: listp.append(item) return listp
def test_should_return_worst_quality_action(self, cfg): # given all_actions = cfg.number_of_possible_actions population = ClassifiersList() c0 = Classifier(action=0, cfg=cfg) population.append(c0) # Should return C1 (because it's first not mentioned) assert choose_action_from_knowledge_array(population, all_actions) == 1 # Add rest of classifiers c1 = Classifier(action=1, numerosity=31, quality=0.72, cfg=cfg) c2 = Classifier(action=2, numerosity=2, quality=0.6, cfg=cfg) c3 = Classifier(action=3, numerosity=2, quality=0.63, cfg=cfg) c4 = Classifier(action=4, numerosity=7, quality=0.75, cfg=cfg) c5 = Classifier(action=5, numerosity=1, quality=0.63, cfg=cfg) c6 = Classifier(action=6, numerosity=6, quality=0.52, cfg=cfg) c7 = Classifier(action=7, numerosity=10, quality=0.36, cfg=cfg) population += ClassifiersList(*[c1, c2, c3, c4, c5, c6, c7]) # then # Classifier C7 should be the worst here assert choose_action_from_knowledge_array(population, all_actions) == 7
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_overall_numerosity(self, cfg): population = ClassifiersList(cfg=cfg) assert 0 == population.overall_numerosity() population.append(Classifier(numerosity=2, cfg=cfg)) assert 2 == population.overall_numerosity() population.append(Classifier(numerosity=1, cfg=cfg)) assert 3 == population.overall_numerosity() population.append(Classifier(numerosity=3, cfg=cfg)) assert 6 == population.overall_numerosity()
def test_exists_classifier(self, cfg): # given population = ClassifiersList() prev_situation = Perception('01100000') situation = Perception('11110000') act = 0 q = 0.5 # C1 - OK c1 = Classifier(condition='0##0####', action=0, effect='1##1####', quality=0.7, cfg=cfg) # C2 - wrong action c2 = Classifier(condition='0##0####', action=1, effect='1##1####', quality=0.7, cfg=cfg) # C3 - wrong condition c3 = Classifier(condition='0##1####', action=0, effect='1##1####', quality=0.7, cfg=cfg) # C4 - wrong effect c4 = Classifier(condition='0##0####', action=0, effect='1##0####', quality=0.7, cfg=cfg) # C5 - wrong quality c5 = Classifier(condition='0##0####', action=0, effect='1##1####', quality=0.25, cfg=cfg) population.append(c2) population.append(c3) population.append(c4) population.append(c5) # when result0 = exists_classifier(population, p0=prev_situation, p1=situation, action=act, quality=q) population.append(c1) result1 = exists_classifier(population, p0=prev_situation, p1=situation, action=act, quality=q) # then assert result0 is False assert result1 is True
def test_should_form_action_set(self, cfg): # given population = ClassifiersList(cfg=cfg) c0 = Classifier(action=0, cfg=cfg) c01 = Classifier(action=0, cfg=cfg) c1 = Classifier(action=1, cfg=cfg) population.append(c0) population.append(c01) population.append(c1) # when & then action_set = ClassifiersList.form_action_set(population, 0, cfg) assert 2 == len(action_set) assert c0 in action_set assert c01 in action_set # when & then action_set = ClassifiersList.form_action_set(population, 1, cfg) assert 1 == len(action_set) assert c1 in action_set
def test_should_calculate_maximum_fitness(self, cfg): # given population = ClassifiersList() # when & then # C1 - does not anticipate change c1 = Classifier(cfg=cfg) population.append(c1) assert 0.0 == population.get_maximum_fitness() # when & then # C2 - does anticipate some change c2 = Classifier(effect='1###0###', reward=0.25, cfg=cfg) population.append(c2) assert 0.125 == population.get_maximum_fitness() # when & then # C3 - does anticipate change and is quite good c3 = Classifier(effect='1#######', quality=0.8, reward=5, cfg=cfg) population.append(c3) assert 4 == population.get_maximum_fitness()
def test_should_insert_classifier_1(self, cfg): population = ClassifiersList(cfg=cfg) with pytest.raises(TypeError): # Try to insert an integer instead of classifier object population.append(4)
def test_should_deny_insertion_illegal_types(self, cfg): population = ClassifiersList() with pytest.raises(TypeError): # Try to insert an integer instead of classifier object population.append(4)
def test_should_return_worst_quality_action(self, cfg): # given population = ClassifiersList(cfg=cfg) c0 = Classifier(action=0, cfg=cfg) population.append(c0) # Should return C1 (because it's first not mentioned) assert 1 == choose_action_from_knowledge_array(population) # Add rest of classifiers c1 = Classifier(action=1, numerosity=31, quality=0.72, cfg=cfg) population.append(c1) c2 = Classifier(action=2, numerosity=2, quality=0.6, cfg=cfg) population.append(c2) c3 = Classifier(action=3, numerosity=2, quality=0.63, cfg=cfg) population.append(c3) c4 = Classifier(action=4, numerosity=7, quality=0.75, cfg=cfg) population.append(c4) c5 = Classifier(action=5, numerosity=1, quality=0.63, cfg=cfg) population.append(c5) c6 = Classifier(action=6, numerosity=6, quality=0.52, cfg=cfg) population.append(c6) c7 = Classifier(action=7, numerosity=10, quality=0.36, cfg=cfg) population.append(c7) # then # Classifier C7 should be the worst here assert 7 == choose_action_from_knowledge_array(population)