eckity.creators.gp_creators.grow
1from random import random 2 3from overrides import overrides 4 5from eckity.creators.gp_creators.tree_creator import GPTreeCreator 6 7 8class GrowCreator(GPTreeCreator): 9 def __init__(self, 10 init_depth=None, 11 function_set=None, 12 terminal_set=None, 13 erc_range=None, 14 bloat_weight=0.1, 15 events=None): 16 """ 17 Tree creator using the grow method 18 19 Parameters 20 ---------- 21 init_depth : (int, int) 22 Min and max depths of initial random trees. The default is None. 23 24 function_set : list 25 List of functions used as internal nodes in the GP-tree. The default is None. 26 27 terminal_set : list 28 List of terminals used in the GP-tree leaves. The default is None. 29 30 erc_range : (float, float) 31 Range of values for ephemeral random constant (ERC). The default is None. 32 33 bloat_weight : float 34 Bloat control weight to punish large trees. Bigger values make a bigger punish. 35 36 events : list 37 List of events related to this class 38 """ 39 super().__init__(init_depth=init_depth, function_set=function_set, terminal_set=terminal_set, 40 erc_range=erc_range, bloat_weight=bloat_weight, events=events) 41 42 @overrides 43 def create_tree(self, tree_ind, max_depth=5): 44 """ 45 Create a random tree using the grow method, and assign it to the given individual. 46 47 Parameters 48 ---------- 49 tree_ind: Tree 50 An individual of GP Tree representation with an initially empty tree 51 52 max_depth: int 53 Maximum depth of tree. The default is 5. 54 55 Returns 56 ------- 57 None. 58 """ 59 self._create_tree(tree_ind, max_depth, 0) 60 61 def _create_tree(self, tree_ind, max_depth=5, depth=0): 62 """ 63 Recursively create a random tree using the grow method 64 65 Parameters 66 ---------- 67 max_depth : int 68 Maximum depth of tree. The default is 2. 69 depth : int, optional 70 Current depth in recursive process. The default is 0. 71 72 Returns 73 ------- 74 None. 75 76 """ 77 is_func = False 78 79 if depth < self.init_depth[0]: 80 node = tree_ind.random_function() 81 is_func = True 82 elif depth >= max_depth: 83 node = tree_ind.random_terminal() 84 else: # intermediate depth, grow 85 if random() > 0.5: 86 node = tree_ind.random_function() 87 is_func = True 88 else: 89 node = tree_ind.random_terminal() 90 91 # add the new node to the tree of the given individual 92 tree_ind.add_tree(node) 93 94 if is_func: 95 # recursively add arguments to the function node, according to its arity 96 for i in range(tree_ind.arity[node]): 97 self._create_tree(tree_ind, max_depth, depth=depth + 1)
9class GrowCreator(GPTreeCreator): 10 def __init__(self, 11 init_depth=None, 12 function_set=None, 13 terminal_set=None, 14 erc_range=None, 15 bloat_weight=0.1, 16 events=None): 17 """ 18 Tree creator using the grow method 19 20 Parameters 21 ---------- 22 init_depth : (int, int) 23 Min and max depths of initial random trees. The default is None. 24 25 function_set : list 26 List of functions used as internal nodes in the GP-tree. The default is None. 27 28 terminal_set : list 29 List of terminals used in the GP-tree leaves. The default is None. 30 31 erc_range : (float, float) 32 Range of values for ephemeral random constant (ERC). The default is None. 33 34 bloat_weight : float 35 Bloat control weight to punish large trees. Bigger values make a bigger punish. 36 37 events : list 38 List of events related to this class 39 """ 40 super().__init__(init_depth=init_depth, function_set=function_set, terminal_set=terminal_set, 41 erc_range=erc_range, bloat_weight=bloat_weight, events=events) 42 43 @overrides 44 def create_tree(self, tree_ind, max_depth=5): 45 """ 46 Create a random tree using the grow method, and assign it to the given individual. 47 48 Parameters 49 ---------- 50 tree_ind: Tree 51 An individual of GP Tree representation with an initially empty tree 52 53 max_depth: int 54 Maximum depth of tree. The default is 5. 55 56 Returns 57 ------- 58 None. 59 """ 60 self._create_tree(tree_ind, max_depth, 0) 61 62 def _create_tree(self, tree_ind, max_depth=5, depth=0): 63 """ 64 Recursively create a random tree using the grow method 65 66 Parameters 67 ---------- 68 max_depth : int 69 Maximum depth of tree. The default is 2. 70 depth : int, optional 71 Current depth in recursive process. The default is 0. 72 73 Returns 74 ------- 75 None. 76 77 """ 78 is_func = False 79 80 if depth < self.init_depth[0]: 81 node = tree_ind.random_function() 82 is_func = True 83 elif depth >= max_depth: 84 node = tree_ind.random_terminal() 85 else: # intermediate depth, grow 86 if random() > 0.5: 87 node = tree_ind.random_function() 88 is_func = True 89 else: 90 node = tree_ind.random_terminal() 91 92 # add the new node to the tree of the given individual 93 tree_ind.add_tree(node) 94 95 if is_func: 96 # recursively add arguments to the function node, according to its arity 97 for i in range(tree_ind.arity[node]): 98 self._create_tree(tree_ind, max_depth, depth=depth + 1)
GrowCreator( init_depth=None, function_set=None, terminal_set=None, erc_range=None, bloat_weight=0.1, events=None)
10 def __init__(self, 11 init_depth=None, 12 function_set=None, 13 terminal_set=None, 14 erc_range=None, 15 bloat_weight=0.1, 16 events=None): 17 """ 18 Tree creator using the grow method 19 20 Parameters 21 ---------- 22 init_depth : (int, int) 23 Min and max depths of initial random trees. The default is None. 24 25 function_set : list 26 List of functions used as internal nodes in the GP-tree. The default is None. 27 28 terminal_set : list 29 List of terminals used in the GP-tree leaves. The default is None. 30 31 erc_range : (float, float) 32 Range of values for ephemeral random constant (ERC). The default is None. 33 34 bloat_weight : float 35 Bloat control weight to punish large trees. Bigger values make a bigger punish. 36 37 events : list 38 List of events related to this class 39 """ 40 super().__init__(init_depth=init_depth, function_set=function_set, terminal_set=terminal_set, 41 erc_range=erc_range, bloat_weight=bloat_weight, events=events)
Tree creator using the grow method
Parameters
init_depth ((int, int)):
Min and max depths of initial random trees. The default is None.
- function_set (list): List of functions used as internal nodes in the GP-tree. The default is None.
- terminal_set (list): List of terminals used in the GP-tree leaves. The default is None.
- erc_range ((float, float)): Range of values for ephemeral random constant (ERC). The default is None.
- bloat_weight (float): Bloat control weight to punish large trees. Bigger values make a bigger punish.
- events (list): List of events related to this class
@overrides
def
create_tree(self, tree_ind, max_depth=5):
43 @overrides 44 def create_tree(self, tree_ind, max_depth=5): 45 """ 46 Create a random tree using the grow method, and assign it to the given individual. 47 48 Parameters 49 ---------- 50 tree_ind: Tree 51 An individual of GP Tree representation with an initially empty tree 52 53 max_depth: int 54 Maximum depth of tree. The default is 5. 55 56 Returns 57 ------- 58 None. 59 """ 60 self._create_tree(tree_ind, max_depth, 0)
Create a random tree using the grow method, and assign it to the given individual.
Parameters
- tree_ind (Tree): An individual of GP Tree representation with an initially empty tree
- max_depth (int): Maximum depth of tree. The default is 5.
Returns
- None.