@@ -689,7 +689,7 @@ class EvoLearner(BaseConceptLearner[EvoLearnerNode]):
689
689
__slots__ = 'fitness_func' , 'init_method' , 'algorithm' , 'value_splitter' , 'tournament_size' , \
690
690
'population_size' , 'num_generations' , 'height_limit' , 'use_data_properties' , 'pset' , 'toolbox' , \
691
691
'_learning_problem' , '_result_population' , 'mut_uniform_gen' , '_dp_to_prim_type' , '_dp_splits' , \
692
- '_split_properties' , '_cache' , 'use_card_restrictions' , 'card_limit' , 'use_inverse'
692
+ '_split_properties' , '_cache' , 'use_card_restrictions' , 'card_limit' , 'use_inverse' , 'total_fits'
693
693
694
694
name = 'evolearner'
695
695
@@ -788,11 +788,12 @@ def __init__(self,
788
788
self .population_size = population_size
789
789
self .num_generations = num_generations
790
790
self .height_limit = height_limit
791
+ self .total_fits = 0
791
792
self .__setup ()
792
793
793
794
def __setup (self ):
795
+ self .clean (partial = True )
794
796
self ._cache = dict ()
795
- self .clean ()
796
797
if self .fitness_func is None :
797
798
self .fitness_func = LinearPressureFitness ()
798
799
@@ -971,7 +972,11 @@ def fit(self, *args, **kwargs) -> 'EvoLearner':
971
972
"""
972
973
Find hypotheses that explain pos and neg.
973
974
"""
974
- self .clean ()
975
+ # Don't reset everything if the user is just using this model for 1 learning problem, since he may use the
976
+ # register_op method, else-wise we need to `clean` before fitting to get a fresh fit.
977
+ if self .total_fits > 0 :
978
+ self .clean ()
979
+ self .total_fits += 1
975
980
learning_problem = self .construct_learning_problem (PosNegLPStandard , args , kwargs )
976
981
self ._learning_problem = learning_problem .encode_kb (self .kb )
977
982
@@ -1049,18 +1054,30 @@ def _fitness_func(self, individual: Tree):
1049
1054
self ._cache [ind_str ] = (e .q , individual .fitness .values [0 ])
1050
1055
self ._number_of_tested_concepts += 1
1051
1056
1052
- def clean (self ):
1053
- self ._result_population = None
1054
-
1057
+ def clean (self , partial : bool = False ):
1055
1058
# Resets classes if they already exist, names must match the ones that were created in the toolbox
1056
1059
try :
1057
1060
del creator .Fitness
1058
1061
del creator .Individual
1059
1062
del creator .Quality
1060
1063
except AttributeError :
1061
1064
pass
1062
- self ._cache .clear ()
1063
1065
super ().clean ()
1066
+ if not partial :
1067
+ # Reset everything if fitting more than one lp. Tests have shown that this is necessary to get the
1068
+ # best performance of EvoLearner.
1069
+ self ._result_population = None
1070
+ self ._cache .clear ()
1071
+ self .fitness_func = LinearPressureFitness ()
1072
+ self .init_method = EARandomWalkInitialization ()
1073
+ self .algorithm = EASimple ()
1074
+ self .mut_uniform_gen = EARandomInitialization (min_height = 1 , max_height = 3 )
1075
+ self .value_splitter = EntropyValueSplitter ()
1076
+ self ._dp_to_prim_type = dict ()
1077
+ self ._dp_splits = dict ()
1078
+ self ._split_properties = []
1079
+ self .pset = self .__build_primitive_set ()
1080
+ self .toolbox = self .__build_toolbox ()
1064
1081
1065
1082
1066
1083
class NCES (BaseNCES ):
0 commit comments