eckity.creators.gp_creators.tree_creator
1from eckity.creators.creator import Creator 2from eckity.fitness.simple_fitness import SimpleFitness 3from eckity.genetic_encodings.gp.tree.functions import f_add, f_sub, f_mul, f_div 4from eckity.fitness.gp_fitness import GPFitness 5from eckity.genetic_encodings.gp.tree.tree_individual import Tree 6 7from abc import abstractmethod 8 9 10class GPTreeCreator(Creator): 11 def __init__(self, 12 init_depth=None, 13 function_set=None, 14 terminal_set=None, 15 erc_range=None, 16 fitness_type=SimpleFitness, 17 bloat_weight=0.1, 18 events=None): 19 if events is None: 20 events = ["after_creation"] 21 super().__init__(events, fitness_type) 22 23 if init_depth is None: 24 init_depth = (2, 4) 25 26 if function_set is None: 27 function_set = [f_add, f_sub, f_mul, f_div] 28 29 if terminal_set is None: 30 terminal_set = ['x', 'y', 'z', 0, 1, -1] 31 32 self.init_depth = init_depth 33 self.function_set = function_set 34 self.terminal_set = terminal_set 35 self.erc_range = erc_range 36 self.bloat_weight = bloat_weight 37 38 def create_individuals(self, n_individuals, higher_is_better): 39 individuals = [Tree(function_set=self.function_set, 40 terminal_set=self.terminal_set, 41 erc_range=self.erc_range, 42 fitness=GPFitness(bloat_weight=self.bloat_weight, higher_is_better=higher_is_better), 43 init_depth=self.init_depth) 44 for _ in range(n_individuals)] 45 for ind in individuals: 46 self.create_tree(ind, self.init_depth[1]) 47 self.created_individuals = individuals 48 49 # TODO do we need this event? creator is instance of Operator and already has before/after operator 50 # self.publish("after_creation") 51 52 return individuals 53 54 @abstractmethod 55 def create_tree(self, tree_ind, max_depth): 56 pass
11class GPTreeCreator(Creator): 12 def __init__(self, 13 init_depth=None, 14 function_set=None, 15 terminal_set=None, 16 erc_range=None, 17 fitness_type=SimpleFitness, 18 bloat_weight=0.1, 19 events=None): 20 if events is None: 21 events = ["after_creation"] 22 super().__init__(events, fitness_type) 23 24 if init_depth is None: 25 init_depth = (2, 4) 26 27 if function_set is None: 28 function_set = [f_add, f_sub, f_mul, f_div] 29 30 if terminal_set is None: 31 terminal_set = ['x', 'y', 'z', 0, 1, -1] 32 33 self.init_depth = init_depth 34 self.function_set = function_set 35 self.terminal_set = terminal_set 36 self.erc_range = erc_range 37 self.bloat_weight = bloat_weight 38 39 def create_individuals(self, n_individuals, higher_is_better): 40 individuals = [Tree(function_set=self.function_set, 41 terminal_set=self.terminal_set, 42 erc_range=self.erc_range, 43 fitness=GPFitness(bloat_weight=self.bloat_weight, higher_is_better=higher_is_better), 44 init_depth=self.init_depth) 45 for _ in range(n_individuals)] 46 for ind in individuals: 47 self.create_tree(ind, self.init_depth[1]) 48 self.created_individuals = individuals 49 50 # TODO do we need this event? creator is instance of Operator and already has before/after operator 51 # self.publish("after_creation") 52 53 return individuals 54 55 @abstractmethod 56 def create_tree(self, tree_ind, max_depth): 57 pass
GPTreeCreator( init_depth=None, function_set=None, terminal_set=None, erc_range=None, fitness_type=<class 'eckity.fitness.simple_fitness.SimpleFitness'>, bloat_weight=0.1, events=None)
12 def __init__(self, 13 init_depth=None, 14 function_set=None, 15 terminal_set=None, 16 erc_range=None, 17 fitness_type=SimpleFitness, 18 bloat_weight=0.1, 19 events=None): 20 if events is None: 21 events = ["after_creation"] 22 super().__init__(events, fitness_type) 23 24 if init_depth is None: 25 init_depth = (2, 4) 26 27 if function_set is None: 28 function_set = [f_add, f_sub, f_mul, f_div] 29 30 if terminal_set is None: 31 terminal_set = ['x', 'y', 'z', 0, 1, -1] 32 33 self.init_depth = init_depth 34 self.function_set = function_set 35 self.terminal_set = terminal_set 36 self.erc_range = erc_range 37 self.bloat_weight = bloat_weight
def
create_individuals(self, n_individuals, higher_is_better):
39 def create_individuals(self, n_individuals, higher_is_better): 40 individuals = [Tree(function_set=self.function_set, 41 terminal_set=self.terminal_set, 42 erc_range=self.erc_range, 43 fitness=GPFitness(bloat_weight=self.bloat_weight, higher_is_better=higher_is_better), 44 init_depth=self.init_depth) 45 for _ in range(n_individuals)] 46 for ind in individuals: 47 self.create_tree(ind, self.init_depth[1]) 48 self.created_individuals = individuals 49 50 # TODO do we need this event? creator is instance of Operator and already has before/after operator 51 # self.publish("after_creation") 52 53 return individuals