def test_should_detect_correctable_classifier(self, _c, _e, _p0, _p1, _result, cfg): cl = Classifier(condition=_c, effect=_e, cfg=cfg) p0 = Perception(_p0) p1 = Perception(_p1) assert cl.can_be_corrected(p0, p1) is _result
def apply(p0: Perception, p1: Perception, cl: Classifier, population: ClassifiersList): if not _perception_changed(p0, p1): handle_useless_case(cl) elif cl.does_anticipate_correctly(p0, p1): handle_expected_case(cl) elif not cl.does_anticipate_correctly(p0, p1) and _perception_changed( p0, p1) and cl.can_be_corrected(p0, p1): handle_correctable_case(p0, p1, cl, population) else: handle_not_correctable_case(cl, p0) # Remove inadequate classifiers for cl in population: if cl.is_inadequate() and not cl.is_general(): population.remove(cl)
def handle_correctable_case(p0: Perception, p1: Perception, cl: Classifier, population: ClassifiersList): new_cl = Classifier.build_corrected(cl, p0, p1) existing = [cl for cl in population if cl == new_cl] if len(existing) == 0: population.append(new_cl)
def test_should_construct_correct_classifier(self, cfg): cl = Classifier(condition="000#", effect="000#", quality=0.2, cfg=cfg) p0 = Perception("0000") p1 = Perception("0001") assert cl.does_match(p0) new_cl = Classifier.build_corrected(cl, p0, p1) assert new_cl is not cl assert new_cl.condition is not cl.condition assert new_cl.effect is not cl.effect assert new_cl.condition == Condition("0000") assert new_cl.action == cl.action assert new_cl.effect == Effect("0001") assert new_cl.q == 0.5 assert new_cl.does_match(p0)
def handle_not_correctable_case(cl: Classifier, p0: Perception): cl.decrease_quality() cl.set_mark(p0)
def handle_expected_case(cl: Classifier): cl.increase_quality()
def handle_useless_case(cl: Classifier): cl.decrease_quality()
def test_should_increase_quality(self, cfg): cl = Classifier(cfg=cfg) cl.increase_quality() assert cl.q == 0.525
def test_should_decrease_quality(self, cfg): cl = Classifier(cfg=cfg) cl.decrease_quality() assert cl.q == 0.475
def test_distinguish_general_classifier(self, _c, _e, _result, cfg): cl = Classifier(condition=Condition(_c), effect=Effect(_e), cfg=cfg) assert cl.is_general() == _result
def test_should_create_general_classifier(self, cfg): cl = Classifier.general(action=1, cfg=cfg) assert cl.condition == Condition("####") assert cl.action == 1 assert cl.condition == Effect("####")
def _initial_population(self): cls = [] for action in range(0, self.cfg.number_of_possible_actions): cls.append(Classifier.general(action, cfg=self.cfg)) return ClassifiersList(*cls)