Ejemplo n.º 1
0
    def test_should_create_generic_condition(self, cfg):
        # when
        cond = Condition.generic(cfg)

        # then
        assert len(cond) == cfg.classifier_length
        for allele in cond:
            assert allele == cfg.classifier_wildcard
Ejemplo n.º 2
0
    def get_differences(self, p0: Perception) -> Condition:
        """
        Difference determination is run when the classifier anticipated the
        change correctly.

        If it's marked we want to find if we can propose differences that
        will be applied to new condition part (specialization).

        There can be two types of differences:
        1) unique - one or more attributes in mark does not contain given
        perception attribute
        2) fuzzy - there is no unique difference - one or more attributes in
        the mark specify more than one value in perception attribute.

        If only unique differences are present - one random one get specified.
        If there are fuzzy differences everyone is specified.

        Parameters
        ----------
        p0: Perception

        Returns
        -------
        Condition
            differences between mark and perception that can form
            a new classifier

        """
        diff = Condition.generic(self.cfg)

        if self.is_marked():
            enc_p0 = list(map(self.cfg.encoder.encode, p0))

            # Unique and fuzzy difference counts
            nr1, nr2 = 0, 0

            # Count difference types
            for idx, item in enumerate(self):
                if len(item) > 0 and enc_p0[idx] not in item:
                    nr1 += 1
                elif len(item) > 1:
                    nr2 += 1

            if nr1 > 0:
                possible_idx = [
                    pi for pi, p in enumerate(enc_p0)
                    if p not in self[pi] and len(self[pi]) > 0
                ]
                rand_idx = random.choice(possible_idx)
                p = enc_p0[rand_idx]
                diff[rand_idx] = UBR(p, p)
            elif nr2 > 0:
                for pi, p in enumerate(enc_p0):
                    if len(self[pi]) > 1:
                        diff[pi] = UBR(p, p)

        return diff
Ejemplo n.º 3
0
    def test_should_get_no_differences(self, cfg):
        # given
        p0 = Perception([.5, .5], oktypes=(float, ))
        mark = self._init_mark([], cfg)

        # when
        diff = mark.get_differences(p0)

        # then
        assert diff == Condition.generic(cfg)
Ejemplo n.º 4
0
    def test_should_initialize_without_arguments(self, cfg):
        # when
        c = Classifier(cfg=cfg)

        # then
        assert c.condition == Condition.generic(cfg=cfg)
        assert c.action is None
        assert c.effect == Effect.pass_through(cfg=cfg)
        assert c.exp == 1
        assert c.talp is None
        assert c.tav == 0.0
Ejemplo n.º 5
0
    def _init_condition(vals, cfg):
        if len(vals) == 0:
            return Condition.generic(cfg)

        return Condition(vals, cfg)