Exemple #1
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)
Exemple #2
0
    def copy_from(cls, old_cls: Classifier, time: int):
        """
        Copies old classifier with given time (tga, talp).
        Old tav gets replaced with new value.
        New classifier also has no mark.

        Parameters
        ----------
        old_cls: Classifier
            classifier to copy from
        time: int
            time of creation / current epoch

        Returns
        -------
        Classifier
            copied classifier
        """
        new_cls = cls(
            condition=acs.Condition(old_cls.condition),
            action=old_cls.action,
            effect=old_cls.effect,
            quality=old_cls.q,
            reward=old_cls.r,
            immediate_reward=old_cls.ir,
            cfg=old_cls.cfg)

        new_cls.tga = time
        new_cls.talp = time
        new_cls.tav = old_cls.tav

        return new_cls
Exemple #3
0
    def merge_with(self, other_classifier, perception, time):
        assert self.cfg.do_pee

        result = Classifier(cfg=self.cfg)

        result.condition = acs.Condition(self.condition)
        result.condition.specialize_with_condition(other_classifier.condition)

        # action is an int, so we can assign directly
        result.action = self.action

        result.effect = Effect.enhanced_effect(
            self.effect, other_classifier.effect,
            self.q, other_classifier.q,
            perception)

        result.mark = acs.PMark(cfg=self.cfg)

        result.r = (self.r + other_classifier.r) / 2.0
        result.q = (self.q + other_classifier.q) / 2.0

        # This 0.5 is Q_INI constant in the original C++ code
        if result.q < 0.5:
            result.q = 0.5

        result.num = 1
        result.tga = time
        result.talp = time
        result.tav = 0
        result.exp = 1

        result.ee = False

        return result
Exemple #4
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)