def apply_alp(population: ClassifierList, match_set: ClassifierList, action_set: ClassifierList, p0: Perception, action: int, p1: Perception, time: int, theta_exp: int, cfg: Configuration) -> None: new_list = ClassifierList() new_cl: Optional[Classifier] = None was_expected_case = False delete_counter = 0 for cl in action_set: cl.increase_experience() cl.set_alp_timestamp(time) if cl.does_anticipate_correctly(p0, p1): new_cl = alp_racs.expected_case(cl, p0, time) was_expected_case = True else: new_cl = alp_racs.unexpected_case(cl, p0, p1, time) if cl.is_inadequate(): delete_counter += 1 lists = [x for x in [population, match_set, action_set] if x] for lst in lists: lst.safe_remove(cl) if new_cl is not None: new_cl.tga = time alp.add_classifier(new_cl, action_set, new_list, theta_exp) # No classifier anticipated correctly - generate new one if not was_expected_case: new_cl = alp_racs.cover(p0, action, p1, time, cfg) alp.add_classifier(new_cl, action_set, new_list, theta_exp) # Merge classifiers from new_list into self and population action_set.extend(new_list) population.extend(new_list) if match_set is not None: new_matching = [cl for cl in new_list if cl.condition.does_match(p1)] match_set.extend(new_matching)
def apply_alp(population: ClassifiersList, match_set: ClassifiersList, action_set: ClassifiersList, p0: Perception, action: int, p1: Perception, time: int, theta_exp: int, cfg: Configuration) -> None: """ The Anticipatory Learning Process. Handles all updates by the ALP, insertion of new classifiers in pop and possibly matchSet, and deletion of inadequate classifiers in pop and possibly matchSet. Parameters ---------- population match_set action_set p0: Perception action: int p1: Perception time: int theta_exp cfg: Configuration Returns ------- """ new_list = ClassifiersList() new_cl: Optional[Classifier] = None was_expected_case = False delete_count = 0 for cl in action_set: cl.increase_experience() cl.update_application_average(time) if cl.does_anticipate_correctly(p0, p1): new_cl = alp_acs2.expected_case(cl, p0, time) was_expected_case = True else: new_cl = alp_acs2.unexpected_case(cl, p0, p1, time) if cl.is_inadequate(): # Removes classifier from population, match set # and current list delete_count += 1 lists = [ x for x in [population, match_set, action_set] if x ] for lst in lists: lst.safe_remove(cl) if new_cl is not None: new_cl.tga = time alp.add_classifier(new_cl, action_set, new_list, theta_exp) # No classifier anticipated correctly - generate new one if not was_expected_case: new_cl = alp_acs2.cover(p0, action, p1, time, cfg) alp.add_classifier(new_cl, action_set, new_list, theta_exp) # Merge classifiers from new_list into self and population action_set.extend(new_list) population.extend(new_list) if match_set is not None: new_matching = [ cl for cl in new_list if matching(cl.condition, p1) ] match_set.extend(new_matching)
def test_should_insert_alp_offspring_2(self): # given cfg = acs2.Configuration(8, 8) population = acs2.ClassifiersList() new_list = acs2.ClassifiersList() child = acs2.Classifier( condition='#1O##O##', action=0, reward=18.206, immediate_reward=0, experience=1, tga=747, talp=747, tav=22.0755, cfg=cfg ) c1 = acs2.Classifier( condition='#1O#O###', action=0, quality=0.650831, reward=14.8323, immediate_reward=0, experience=5, tga=531, talp=747, tav=48.3562, cfg=cfg ) c2 = acs2.Classifier( condition='##O#O###', action=0, quality=0.79094, reward=9.97782, immediate_reward=0, experience=10, tga=330, talp=747, tav=43.7171, cfg=cfg ) c3 = acs2.Classifier( condition='#1O###1O', action=0, effect='#O1####1', quality=0.515369, reward=8.3284, immediate_reward=0, experience=8, tga=316, talp=747, tav=57.8883, cfg=cfg ) c3.mark[0].update(['1']) c3.mark[3].update(['0']) c3.mark[4].update(['0']) c3.mark[5].update(['0']) c4 = acs2.Classifier( condition='####O###', action=0, quality=0.903144, reward=14.8722, immediate_reward=0, experience=25, tga=187, talp=747, tav=23.0038, cfg=cfg ) c5 = acs2.Classifier( condition='#1O####O', action=0, effect='#O1####1', quality=0.647915, reward=9.24712, immediate_reward=0, experience=14, tga=154, talp=747, tav=44.5457, cfg=cfg ) c5.mark[0].update(['1']) c5.mark[3].update(['0', '1']) c5.mark[4].update(['0', '1']) c5.mark[5].update(['0', '1']) c5.mark[6].update(['0', '1']) c6 = acs2.Classifier( condition='#1O#####', action=0, quality=0.179243, reward=18.206, immediate_reward=0, experience=29, tga=104, talp=747, tav=22.0755, cfg=cfg ) c6.mark[0].update(['1']) c6.mark[3].update(['1']) c6.mark[4].update(['1']) c6.mark[5].update(['1']) c6.mark[6].update(['0', '1']) c6.mark[7].update(['0', '1']) c7 = acs2.Classifier( condition='##O#####', action=0, quality=0.100984, reward=15.91, immediate_reward=0, experience=44, tga=58, talp=747, tav=14.4171, cfg=cfg ) c7.mark[0].update(['0', '1']) c7.mark[1].update(['0', '1']) c7.mark[3].update(['0', '1']) c7.mark[5].update(['0', '1']) c7.mark[6].update(['0', '1']) c7.mark[7].update(['0', '1']) # Add classifiers into current ClassifierList population.extend([c1, c2, c3, c4, c5, c6, c7]) # When alp.add_classifier(child, population, new_list, cfg.theta_exp) # Then assert 7 == len(population) assert 0 == len(new_list) assert c1 in population assert c2 in population assert c3 in population assert c4 in population assert c5 in population assert c6 in population assert c7 in population # `C4` should be subsumer of `child` assert abs(0.907987 - c4.q) < 0.01
def test_should_insert_alp_offspring_1(self): # given cfg = acs2.Configuration(8, 8) population = acs2.ClassifiersList() new_list = acs2.ClassifiersList() child = acs2.Classifier( condition='1##1#010', action=0, effect='0####101', quality=0.5, reward=8.96245, immediate_reward=0, experience=1, tga=423, talp=423, tav=27.3182, cfg=cfg ) c1 = acs2.Classifier( condition='1##1#010', action=0, effect='0####101', quality=0.571313, reward=7.67011, immediate_reward=0, experience=3, tga=225, talp=423, tav=70.881, cfg=cfg ) c2 = acs2.Classifier( condition='1####010', action=0, effect='0####101', quality=0.462151, reward=8.96245, immediate_reward=0, experience=11, tga=143, talp=423, tav=27.3182, cfg=cfg ) c3 = acs2.Classifier( condition='1####0##', action=0, effect='0####1##', quality=0.31452, reward=9.04305, immediate_reward=0, experience=19, tga=49, talp=423, tav=19.125, cfg=cfg ) population.extend([c1, c2, c3]) # when alp.add_classifier(child, population, new_list, cfg.theta_exp) # then assert len(population) == 3 assert c1 in population assert c2 in population assert c3 in population assert abs(0.592747 - c1.q) < 0.01