eckity.genetic_operators.mutations.subtree_mutation
1from random import randint 2 3from eckity.creators.gp_creators.grow import GrowCreator 4from eckity.genetic_operators.genetic_operator import GeneticOperator 5 6 7class SubtreeMutation(GeneticOperator): 8 def __init__(self, probability=1, arity=1, init_depth=None, events=None): 9 super().__init__(probability=probability, arity=arity, events=events) 10 self.init_depth = init_depth 11 12 def apply(self, individuals): 13 """ 14 Perform subtree mutation: select a subtree at random to be replaced by a new, randomly generated subtree. 15 16 Returns 17 ------- 18 None. 19 """ 20 21 for ind in individuals: 22 init_depth = (ind.init_depth[0], randint(ind.init_depth[0], ind.init_depth[1])) \ 23 if self.init_depth is None \ 24 else self.init_depth 25 tree_creator = GrowCreator(init_depth=init_depth, 26 function_set=ind.function_set, terminal_set=ind.terminal_set, 27 erc_range=ind.erc_range) 28 29 # TODO refactor dummy individual creation, only the tree should be generated 30 subtree_individual = tree_creator.create_individuals(1, None)[0] 31 ind.replace_subtree(subtree_individual.tree) 32 33 self.applied_individuals = individuals 34 return individuals
8class SubtreeMutation(GeneticOperator): 9 def __init__(self, probability=1, arity=1, init_depth=None, events=None): 10 super().__init__(probability=probability, arity=arity, events=events) 11 self.init_depth = init_depth 12 13 def apply(self, individuals): 14 """ 15 Perform subtree mutation: select a subtree at random to be replaced by a new, randomly generated subtree. 16 17 Returns 18 ------- 19 None. 20 """ 21 22 for ind in individuals: 23 init_depth = (ind.init_depth[0], randint(ind.init_depth[0], ind.init_depth[1])) \ 24 if self.init_depth is None \ 25 else self.init_depth 26 tree_creator = GrowCreator(init_depth=init_depth, 27 function_set=ind.function_set, terminal_set=ind.terminal_set, 28 erc_range=ind.erc_range) 29 30 # TODO refactor dummy individual creation, only the tree should be generated 31 subtree_individual = tree_creator.create_individuals(1, None)[0] 32 ind.replace_subtree(subtree_individual.tree) 33 34 self.applied_individuals = individuals 35 return individuals
def
apply(self, individuals):
13 def apply(self, individuals): 14 """ 15 Perform subtree mutation: select a subtree at random to be replaced by a new, randomly generated subtree. 16 17 Returns 18 ------- 19 None. 20 """ 21 22 for ind in individuals: 23 init_depth = (ind.init_depth[0], randint(ind.init_depth[0], ind.init_depth[1])) \ 24 if self.init_depth is None \ 25 else self.init_depth 26 tree_creator = GrowCreator(init_depth=init_depth, 27 function_set=ind.function_set, terminal_set=ind.terminal_set, 28 erc_range=ind.erc_range) 29 30 # TODO refactor dummy individual creation, only the tree should be generated 31 subtree_individual = tree_creator.create_individuals(1, None)[0] 32 ind.replace_subtree(subtree_individual.tree) 33 34 self.applied_individuals = individuals 35 return individuals
Perform subtree mutation: select a subtree at random to be replaced by a new, randomly generated subtree.
Returns
- None.