Skip to content

Commit 8bbe8ed

Browse files
committed
fix merge add_clip_clean and develop
2 parents 864528c + 5b6a502 commit 8bbe8ed

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

ontolearn/concept_learner.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ class EvoLearner(BaseConceptLearner[EvoLearnerNode]):
692692
__slots__ = 'fitness_func', 'init_method', 'algorithm', 'value_splitter', 'tournament_size', \
693693
'population_size', 'num_generations', 'height_limit', 'use_data_properties', 'pset', 'toolbox', \
694694
'_learning_problem', '_result_population', 'mut_uniform_gen', '_dp_to_prim_type', '_dp_splits', \
695-
'_split_properties', '_cache', 'use_card_restrictions', 'card_limit', 'use_inverse'
695+
'_split_properties', '_cache', 'use_card_restrictions', 'card_limit', 'use_inverse', 'total_fits'
696696

697697
name = 'evolearner'
698698

@@ -791,11 +791,12 @@ def __init__(self,
791791
self.population_size = population_size
792792
self.num_generations = num_generations
793793
self.height_limit = height_limit
794+
self.total_fits = 0
794795
self.__setup()
795796

796797
def __setup(self):
798+
self.clean(partial=True)
797799
self._cache = dict()
798-
self.clean()
799800
if self.fitness_func is None:
800801
self.fitness_func = LinearPressureFitness()
801802

@@ -974,7 +975,11 @@ def fit(self, *args, **kwargs) -> 'EvoLearner':
974975
"""
975976
Find hypotheses that explain pos and neg.
976977
"""
977-
self.clean()
978+
# Don't reset everything if the user is just using this model for 1 learning problem, since he may use the
979+
# register_op method, else-wise we need to `clean` before fitting to get a fresh fit.
980+
if self.total_fits > 0:
981+
self.clean()
982+
self.total_fits += 1
978983
learning_problem = self.construct_learning_problem(PosNegLPStandard, args, kwargs)
979984
self._learning_problem = learning_problem.encode_kb(self.kb)
980985

@@ -1052,18 +1057,30 @@ def _fitness_func(self, individual: Tree):
10521057
self._cache[ind_str] = (e.q, individual.fitness.values[0])
10531058
self._number_of_tested_concepts += 1
10541059

1055-
def clean(self):
1056-
self._result_population = None
1057-
1060+
def clean(self, partial: bool = False):
10581061
# Resets classes if they already exist, names must match the ones that were created in the toolbox
10591062
try:
10601063
del creator.Fitness
10611064
del creator.Individual
10621065
del creator.Quality
10631066
except AttributeError:
10641067
pass
1065-
self._cache.clear()
10661068
super().clean()
1069+
if not partial:
1070+
# Reset everything if fitting more than one lp. Tests have shown that this is necessary to get the
1071+
# best performance of EvoLearner.
1072+
self._result_population = None
1073+
self._cache.clear()
1074+
self.fitness_func = LinearPressureFitness()
1075+
self.init_method = EARandomWalkInitialization()
1076+
self.algorithm = EASimple()
1077+
self.mut_uniform_gen = EARandomInitialization(min_height=1, max_height=3)
1078+
self.value_splitter = EntropyValueSplitter()
1079+
self._dp_to_prim_type = dict()
1080+
self._dp_splits = dict()
1081+
self._split_properties = []
1082+
self.pset = self.__build_primitive_set()
1083+
self.toolbox = self.__build_toolbox()
10671084

10681085

10691086
class CLIP(CELOE):

tests/test_evolearner.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def test_regression_family(self):
1818
kb = KnowledgeBase(path=settings['data_path'][3:])
1919
model = EvoLearner(knowledge_base=kb, max_runtime=10)
2020

21-
regression_test_evolearner = {'Aunt': 0.9, 'Brother': 1.0,
22-
'Cousin': 0.9, 'Granddaughter': 1.0,
23-
'Uncle': 0.9, 'Grandgrandfather': 0.94}
21+
regression_test_evolearner = {'Aunt': 1.0, 'Brother': 1.0,
22+
'Cousin': 1.0, 'Granddaughter': 1.0,
23+
'Uncle': 1.0, 'Grandgrandfather': 1.0}
2424
for str_target_concept, examples in settings['problems'].items():
2525
pos = set(map(OWLNamedIndividual, map(IRI.create, set(examples['positive_examples']))))
2626
neg = set(map(OWLNamedIndividual, map(IRI.create, set(examples['negative_examples']))))
@@ -31,8 +31,12 @@ def test_regression_family(self):
3131
self.assertEqual(returned_model, model)
3232
hypotheses = list(returned_model.best_hypotheses(n=3))
3333
self.assertGreaterEqual(hypotheses[0].quality, regression_test_evolearner[str_target_concept])
34-
self.assertGreaterEqual(hypotheses[0].quality, hypotheses[1].quality)
35-
self.assertGreaterEqual(hypotheses[1].quality, hypotheses[2].quality)
34+
# best_hypotheses returns distinct hypotheses and sometimes the model will not find 'n' distinct hypothesis,
35+
# hence the checks
36+
if len(hypotheses) == 2:
37+
self.assertGreaterEqual(hypotheses[0].quality, hypotheses[1].quality)
38+
if len(hypotheses) == 3:
39+
self.assertGreaterEqual(hypotheses[1].quality, hypotheses[2].quality)
3640

3741
def test_regression_mutagenesis_multiple_fits(self):
3842
kb = KnowledgeBase(path='KGs/Mutagenesis/mutagenesis.owl')

0 commit comments

Comments
 (0)