Ejemplo n.º 1
0
    def test_should_not_generalize_unchanging_attribute(
            self, _condition, _effect, _sua_before, _sua_after, cfg):

        # given
        cls = Classifier(condition=_condition, effect=_effect, cfg=cfg)
        assert len(cls.specified_unchanging_attributes) == _sua_before

        # when
        cls.generalize_unchanging_condition_attribute()

        # then
        assert len(cls.specified_unchanging_attributes) == _sua_after
Ejemplo n.º 2
0
def expected_case(cl: Classifier, p0: Perception,
                  time: int) -> Optional[Classifier]:
    """
    Controls the expected case of a classifier. If the classifier
    is too specific it tries to add some randomness to it by
    generalizing some attributes.

    :param cl:
    :param p0:
    :param time:
    :return: new classifier or None
    """
    if cl.cfg.do_pee:
        cl.effect.update_enhanced_effect_probs(p0, cl.cfg.beta)

    diff = cl.mark.get_differences(p0)

    if diff.specificity == 0:
        if cl.cfg.do_pee and cl.is_marked():
            cl.ee = True

        cl.increase_quality()

        return None

    no_spec = len(cl.specified_unchanging_attributes)
    no_spec_new = diff.specificity
    child = cl.copy_from(cl, time)

    if no_spec >= cl.cfg.u_max:
        while no_spec >= cl.cfg.u_max:
            res = cl.generalize_unchanging_condition_attribute()
            assert res is True
            no_spec -= 1

        while no_spec + no_spec_new > cl.cfg.u_max:
            if random() < 0.5:
                diff.generalize_specific_attribute_randomly()
                no_spec_new -= 1
            else:
                if cl.generalize_unchanging_condition_attribute():
                    no_spec -= 1
    else:
        while no_spec + no_spec_new > cl.cfg.u_max:
            diff.generalize_specific_attribute_randomly()
            no_spec_new -= 1

    child.condition.specialize_with_condition(diff)

    if child.q < 0.5:
        child.q = 0.5

    return child
Ejemplo n.º 3
0
    def test_should_generalize_second_unchanging_attribute(self, cfg):
        # given
        cls = Classifier(condition='#####0#0', effect='########', cfg=cfg)

        assert len(cls.specified_unchanging_attributes) == 2

        # when
        generalized = cls.generalize_unchanging_condition_attribute(
            lambda x: 7)

        # then
        assert generalized is True
        assert len(cls.specified_unchanging_attributes) == 1
        assert Condition('#####0##') == cls.condition