@@ -692,7 +692,7 @@ class EvoLearner(BaseConceptLearner[EvoLearnerNode]):
692
692
__slots__ = 'fitness_func' , 'init_method' , 'algorithm' , 'value_splitter' , 'tournament_size' , \
693
693
'population_size' , 'num_generations' , 'height_limit' , 'use_data_properties' , 'pset' , 'toolbox' , \
694
694
'_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'
696
696
697
697
name = 'evolearner'
698
698
@@ -791,11 +791,12 @@ def __init__(self,
791
791
self .population_size = population_size
792
792
self .num_generations = num_generations
793
793
self .height_limit = height_limit
794
+ self .total_fits = 0
794
795
self .__setup ()
795
796
796
797
def __setup (self ):
798
+ self .clean (partial = True )
797
799
self ._cache = dict ()
798
- self .clean ()
799
800
if self .fitness_func is None :
800
801
self .fitness_func = LinearPressureFitness ()
801
802
@@ -974,7 +975,11 @@ def fit(self, *args, **kwargs) -> 'EvoLearner':
974
975
"""
975
976
Find hypotheses that explain pos and neg.
976
977
"""
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
978
983
learning_problem = self .construct_learning_problem (PosNegLPStandard , args , kwargs )
979
984
self ._learning_problem = learning_problem .encode_kb (self .kb )
980
985
@@ -1052,18 +1057,30 @@ def _fitness_func(self, individual: Tree):
1052
1057
self ._cache [ind_str ] = (e .q , individual .fitness .values [0 ])
1053
1058
self ._number_of_tested_concepts += 1
1054
1059
1055
- def clean (self ):
1056
- self ._result_population = None
1057
-
1060
+ def clean (self , partial : bool = False ):
1058
1061
# Resets classes if they already exist, names must match the ones that were created in the toolbox
1059
1062
try :
1060
1063
del creator .Fitness
1061
1064
del creator .Individual
1062
1065
del creator .Quality
1063
1066
except AttributeError :
1064
1067
pass
1065
- self ._cache .clear ()
1066
1068
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 ()
1067
1084
1068
1085
1069
1086
class CLIP (CELOE ):
0 commit comments