Beispiel #1
0
    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
Beispiel #2
0
    def test_find_old_classifier_similar_and_subsumer_subsumer_returned(self):
        # given
        cfg = acs2.Configuration(
            classifier_length=8, number_of_possible_actions=4)

        subsumer = acs2.Classifier(
            condition='1#######',
            action=1,
            experience=21,
            quality=0.95,
            cfg=cfg)

        similar = acs2.Classifier(
            condition='10######',
            action=1,
            cfg=cfg)

        population = acs2.ClassifiersList(*[similar, subsumer])

        cl = acs2.Classifier(
            condition='10######',
            action=1,
            cfg=cfg)

        # when
        old_cls = ga._find_old_classifier(population, cl, True, cfg.theta_exp)

        # then
        assert similar == cl
        assert subsumer == old_cls
Beispiel #3
0
    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
Beispiel #4
0
    def test_should_delete_ga_classifier(self):
        # given
        cfg = acs2.Configuration(
            classifier_length=4, number_of_possible_actions=2)
        clss = []

        # Total of 20 micro-classifiers
        action_cl_num_fixtures = {
            1: [1, 5, 4],
            2: [1, 1, 6, 2]
        }

        for action, num_lst in action_cl_num_fixtures.items():
            for num in num_lst:
                clss.append(acs2.Classifier(
                    action=action, numerosity=num, cfg=cfg))

        insize = 2  # space needed for new classifiers
        theta_as = 10  # maximum size of the action set

        # when
        population = acs2.ClassifiersList(*clss)
        action_set = population.form_action_set(1)

        ga.delete_classifiers(population, None, action_set, insize, theta_as)

        # then
        assert sum(cl.num for cl in population) == 18
        assert sum(cl.num for cl in action_set) == 8
Beispiel #5
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)
Beispiel #6
0
    def test_should_not_find_old_classifier(self):
        # given
        cfg = acs2.Configuration(
            classifier_length=4, number_of_possible_actions=2)
        cl = acs2.Classifier(cfg=cfg)
        population = acs2.ClassifiersList()

        # when
        old_cl = ga._find_old_classifier(population, cl, True, cfg.theta_exp)

        # then
        assert old_cl is None
Beispiel #7
0
    def test_should_find_old_classifier_only_subsumer(self):
        # given
        cfg = acs2.Configuration(
            classifier_length=8, number_of_possible_actions=4)

        subsumer1 = acs2.Classifier(
            condition='1##0####',
            action=3,
            effect='##1#####',
            quality=0.93,
            reward=1.35,
            experience=23,
            cfg=cfg)

        subsumer2 = acs2.Classifier(
            condition='#1#0####',
            action=3,
            effect='##1#####',
            quality=0.93,
            reward=1.35,
            experience=23,
            cfg=cfg)

        most_general = acs2.Classifier(
            condition='###0####',
            action=3,
            effect='##1#####',
            quality=0.93,
            reward=1.35,
            experience=23,
            cfg=cfg)

        nonsubsumer = acs2.Classifier(cfg=cfg)

        cl = acs2.Classifier(
            condition='11#0####',
            action=3,
            effect='##1#####',
            quality=0.5,
            reward=0.35,
            experience=1,
            cfg=cfg)

        population = acs2.ClassifiersList(
            *[nonsubsumer, subsumer1, nonsubsumer, most_general,
              subsumer2, nonsubsumer])

        # when
        old_cl = ga._find_old_classifier(population, cl, True, cfg.theta_exp)

        # then
        assert old_cl == most_general
Beispiel #8
0
    def test_two_point_crossover(self, _seed, _c1, _c2, _rc1, _rc2):
        # given
        cfg = acs2.Configuration(
            classifier_length=4, number_of_possible_actions=2)
        cl1 = acs2.Classifier(condition=_c1, cfg=cfg)
        cl2 = acs2.Classifier(condition=_c2, cfg=cfg)

        # when
        np.random.seed(_seed)
        ga.two_point_crossover(cl1, cl2)

        # then
        assert cl1.condition == acs.Condition(_rc1)
        assert cl2.condition == acs.Condition(_rc2)
Beispiel #9
0
    def test_find_old_classifier_only_similar(self):
        # given
        cfg = acs2.Configuration(
            classifier_length=4, number_of_possible_actions=2)

        cl_1 = acs2.Classifier(action=1, experience=32, cfg=cfg)
        cl_2 = acs2.Classifier(action=1, cfg=cfg)
        population = acs2.ClassifiersList(
            *[cl_1,
              acs2.Classifier(action=2, cfg=cfg),
              acs2.Classifier(action=3, cfg=cfg),
              cl_2])

        # when
        cl = acs2.Classifier(action=1, cfg=cfg)
        old_cl = ga._find_old_classifier(population, cl, True, cfg.theta_exp)

        # then
        assert old_cl == cl_1
Beispiel #10
0
    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
Beispiel #11
0
    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
Beispiel #12
0
 def acs2_cfg(self):
     return acs2.Configuration(8, 8)
def run_experiments_alternating(env, trials, params):
    """
    Function running experiments in explore-exploit fashion using
    3 algorithms - ACS2, AACS2-v1, AACS2-v2
    """

    logging.info('Starting ACS2 experiments')
    acs2_cfg = acs2.Configuration(
        params['perception_bits'],
        params['possible_actions'],
        do_ga=params['do_ga'],
        beta=params['beta'],
        epsilon=params['epsilon'],
        gamma=params['gamma'],
        environment_adapter=params['environment_adapter'],
        user_metrics_collector_fcn=params['user_metrics_collector_fcn'],
        biased_exploration_prob=params['biased_exploration_prob'],
        metrics_trial_frequency=params['metrics_trial_freq'])

    acs2_agent = acs2.ACS2(acs2_cfg)
    pop_acs2, metrics_acs2 = acs2_agent.explore_exploit(env, trials)

    logging.info('Starting AACS2-v1 experiments')
    aacs2v1_cfg = aacs2.Configuration(
        params['perception_bits'],
        params['possible_actions'],
        do_ga=params['do_ga'],
        beta=params['beta'],
        epsilon=params['epsilon'],
        gamma=params['gamma'],
        zeta=params['zeta'],
        rho_update_version='1',
        environment_adapter=params['environment_adapter'],
        user_metrics_collector_fcn=params['user_metrics_collector_fcn'],
        biased_exploration_prob=params['biased_exploration_prob'],
        metrics_trial_frequency=params['metrics_trial_freq'])

    aacs2v1_agent = aacs2.AACS2(aacs2v1_cfg)
    pop_aacs2v1, metrics_aacs2v1 = aacs2v1_agent.explore_exploit(env, trials)

    logging.info('Starting AACS2-v2 experiments')
    aacs2v2_cfg = aacs2.Configuration(
        params['perception_bits'],
        params['possible_actions'],
        do_ga=params['do_ga'],
        beta=params['beta'],
        epsilon=params['epsilon'],
        gamma=params['gamma'],
        zeta=params['zeta'],
        rho_update_version='2',
        environment_adapter=params['environment_adapter'],
        user_metrics_collector_fcn=params['user_metrics_collector_fcn'],
        biased_exploration_prob=params['biased_exploration_prob'],
        metrics_trial_frequency=params['metrics_trial_freq'])

    aacs2v2_agent = aacs2.AACS2(aacs2v2_cfg)
    pop_aacs2v2, metrics_aacs2v2 = aacs2v2_agent.explore_exploit(env, trials)

    # Join metrics together
    m = []
    m.extend(metrics_acs2)
    m.extend(metrics_aacs2v1)
    m.extend(metrics_aacs2v2)

    return pop_acs2, pop_aacs2v1, pop_aacs2v2, m
Beispiel #14
0
 def acs2_cfg(self):
     return acs2.Configuration(4, 4, epsilon=0.5)