eckity.creators.gp_creators.half
1from typing import Any, Callable, Dict, List, Optional, Tuple, Union 2 3from overrides import override 4 5from eckity.creators.gp_creators.full import FullCreator 6from eckity.creators.gp_creators.grow import GrowCreator 7from eckity.creators.gp_creators.tree_creator import GPTreeCreator 8from eckity.genetic_encodings.gp.tree.tree_individual import Tree 9 10 11class HalfCreator(GPTreeCreator): 12 def __init__( 13 self, 14 grow_creator: GrowCreator = None, 15 full_creator: FullCreator = None, 16 init_depth: Tuple[int, int] = None, 17 function_set: List[Callable] = None, 18 terminal_set: Union[Dict[Any, type], List[Any]] = None, 19 bloat_weight: float = 0.0, 20 erc_range: Union[Tuple[int, int], Tuple[float, float]] = None, 21 events: List[str] = None, 22 root_type: Optional[type] = None, 23 ): 24 """ 25 Tree creator that creates trees using the Ramped Half and Half method 26 27 Parameters 28 ---------- 29 grow_creator: GrowCreator 30 a tree creator that creates trees using the grow method 31 32 full_creator: FullCreator 33 a tree creator that creates trees using the full method 34 35 init_depth : (int, int) 36 Min and max depths of initial random trees. The default is None. 37 38 function_set : list 39 List of functions used as internal nodes in the GP tree. The default is None. 40 41 terminal_set : list or dict 42 List of terminals used in the GP-tree leaves. The default is None. 43 44 bloat_weight : float 45 Bloat control weight to punish large trees. Bigger values make a bigger punish. 46 47 events : list 48 List of events related to this class 49 """ 50 super().__init__( 51 init_depth=init_depth, 52 function_set=function_set, 53 terminal_set=terminal_set, 54 bloat_weight=bloat_weight, 55 erc_range=erc_range, 56 events=events, 57 root_type=root_type, 58 ) 59 60 # assign default creators 61 if grow_creator is None: 62 grow_creator = GrowCreator( 63 init_depth=self.init_depth, 64 function_set=self.function_set, 65 terminal_set=self.terminal_set, 66 events=self.events, 67 root_type=root_type, 68 erc_range=self.erc_range, 69 ) 70 if full_creator is None: 71 full_creator = FullCreator( 72 init_depth=self.init_depth, 73 function_set=self.function_set, 74 terminal_set=self.terminal_set, 75 events=self.events, 76 root_type=root_type, 77 erc_range=self.erc_range, 78 ) 79 80 self.grow_creator = grow_creator 81 self.full_creator = full_creator 82 83 @override 84 def create_individuals( 85 self, n_individuals: int, higher_is_better: bool 86 ) -> List[Tree]: 87 """ 88 Initialize the subpopulation individuals using ramped half-and-half method. 89 90 Parameters 91 ---------- 92 n_individuals: int 93 number of individuals to create 94 95 higher_is_better: bool 96 determines if the fitness of the created individuals should be minimized or maximized 97 98 Returns 99 ------- 100 101 """ 102 min_depth, max_depth = self.init_depth 103 104 # if pop size is 100 and we want depths 2,3,4,5,6 then group_size is 10: 105 # 10 'grow' with depth 2, 10 'full' with depth 2, 10 'grow' with depth 3, 10 'full' with depth 3, etc. 106 group_size = int(n_individuals / (2 * (max_depth + 1 - min_depth))) 107 108 if group_size == 0: 109 raise ValueError( 110 "Incompatible population size and init_depth. " 111 "Population size must be " 112 f"at least {2 * (max_depth + 1 - min_depth)}." 113 ) 114 115 individuals = [] 116 117 for depth in range(min_depth, max_depth + 1): 118 # first create `group_size` individuals using grow method 119 self.grow_creator.init_depth = (min_depth, depth) 120 grown_inds = self.grow_creator.create_individuals( 121 group_size, higher_is_better 122 ) 123 124 # then create `group_size` individuals using full method 125 self.full_creator.init_depth = (min_depth, depth) 126 full_inds = self.full_creator.create_individuals( 127 group_size, higher_is_better 128 ) 129 130 individuals.extend(grown_inds + full_inds) 131 132 # might need to add a few because 'group_size' may have been a float that was truncated 133 n_missing = n_individuals - len(individuals) 134 if n_missing > 0: 135 missing_inds = self.full_creator.create_individuals( 136 n_missing, higher_is_better 137 ) 138 individuals.extend(missing_inds) 139 140 self.created_individuals = individuals 141 return individuals
12class HalfCreator(GPTreeCreator): 13 def __init__( 14 self, 15 grow_creator: GrowCreator = None, 16 full_creator: FullCreator = None, 17 init_depth: Tuple[int, int] = None, 18 function_set: List[Callable] = None, 19 terminal_set: Union[Dict[Any, type], List[Any]] = None, 20 bloat_weight: float = 0.0, 21 erc_range: Union[Tuple[int, int], Tuple[float, float]] = None, 22 events: List[str] = None, 23 root_type: Optional[type] = None, 24 ): 25 """ 26 Tree creator that creates trees using the Ramped Half and Half method 27 28 Parameters 29 ---------- 30 grow_creator: GrowCreator 31 a tree creator that creates trees using the grow method 32 33 full_creator: FullCreator 34 a tree creator that creates trees using the full method 35 36 init_depth : (int, int) 37 Min and max depths of initial random trees. The default is None. 38 39 function_set : list 40 List of functions used as internal nodes in the GP tree. The default is None. 41 42 terminal_set : list or dict 43 List of terminals used in the GP-tree leaves. The default is None. 44 45 bloat_weight : float 46 Bloat control weight to punish large trees. Bigger values make a bigger punish. 47 48 events : list 49 List of events related to this class 50 """ 51 super().__init__( 52 init_depth=init_depth, 53 function_set=function_set, 54 terminal_set=terminal_set, 55 bloat_weight=bloat_weight, 56 erc_range=erc_range, 57 events=events, 58 root_type=root_type, 59 ) 60 61 # assign default creators 62 if grow_creator is None: 63 grow_creator = GrowCreator( 64 init_depth=self.init_depth, 65 function_set=self.function_set, 66 terminal_set=self.terminal_set, 67 events=self.events, 68 root_type=root_type, 69 erc_range=self.erc_range, 70 ) 71 if full_creator is None: 72 full_creator = FullCreator( 73 init_depth=self.init_depth, 74 function_set=self.function_set, 75 terminal_set=self.terminal_set, 76 events=self.events, 77 root_type=root_type, 78 erc_range=self.erc_range, 79 ) 80 81 self.grow_creator = grow_creator 82 self.full_creator = full_creator 83 84 @override 85 def create_individuals( 86 self, n_individuals: int, higher_is_better: bool 87 ) -> List[Tree]: 88 """ 89 Initialize the subpopulation individuals using ramped half-and-half method. 90 91 Parameters 92 ---------- 93 n_individuals: int 94 number of individuals to create 95 96 higher_is_better: bool 97 determines if the fitness of the created individuals should be minimized or maximized 98 99 Returns 100 ------- 101 102 """ 103 min_depth, max_depth = self.init_depth 104 105 # if pop size is 100 and we want depths 2,3,4,5,6 then group_size is 10: 106 # 10 'grow' with depth 2, 10 'full' with depth 2, 10 'grow' with depth 3, 10 'full' with depth 3, etc. 107 group_size = int(n_individuals / (2 * (max_depth + 1 - min_depth))) 108 109 if group_size == 0: 110 raise ValueError( 111 "Incompatible population size and init_depth. " 112 "Population size must be " 113 f"at least {2 * (max_depth + 1 - min_depth)}." 114 ) 115 116 individuals = [] 117 118 for depth in range(min_depth, max_depth + 1): 119 # first create `group_size` individuals using grow method 120 self.grow_creator.init_depth = (min_depth, depth) 121 grown_inds = self.grow_creator.create_individuals( 122 group_size, higher_is_better 123 ) 124 125 # then create `group_size` individuals using full method 126 self.full_creator.init_depth = (min_depth, depth) 127 full_inds = self.full_creator.create_individuals( 128 group_size, higher_is_better 129 ) 130 131 individuals.extend(grown_inds + full_inds) 132 133 # might need to add a few because 'group_size' may have been a float that was truncated 134 n_missing = n_individuals - len(individuals) 135 if n_missing > 0: 136 missing_inds = self.full_creator.create_individuals( 137 n_missing, higher_is_better 138 ) 139 individuals.extend(missing_inds) 140 141 self.created_individuals = individuals 142 return individuals
Helper class that provides a standard way to create an ABC using inheritance.
HalfCreator( grow_creator: eckity.creators.gp_creators.grow.GrowCreator = None, full_creator: eckity.creators.gp_creators.full.FullCreator = None, init_depth: Tuple[int, int] = None, function_set: List[Callable] = None, terminal_set: Union[Dict[Any, type], List[Any]] = None, bloat_weight: float = 0.0, erc_range: Union[Tuple[int, int], Tuple[float, float]] = None, events: List[str] = None, root_type: Optional[type] = None)
13 def __init__( 14 self, 15 grow_creator: GrowCreator = None, 16 full_creator: FullCreator = None, 17 init_depth: Tuple[int, int] = None, 18 function_set: List[Callable] = None, 19 terminal_set: Union[Dict[Any, type], List[Any]] = None, 20 bloat_weight: float = 0.0, 21 erc_range: Union[Tuple[int, int], Tuple[float, float]] = None, 22 events: List[str] = None, 23 root_type: Optional[type] = None, 24 ): 25 """ 26 Tree creator that creates trees using the Ramped Half and Half method 27 28 Parameters 29 ---------- 30 grow_creator: GrowCreator 31 a tree creator that creates trees using the grow method 32 33 full_creator: FullCreator 34 a tree creator that creates trees using the full method 35 36 init_depth : (int, int) 37 Min and max depths of initial random trees. The default is None. 38 39 function_set : list 40 List of functions used as internal nodes in the GP tree. The default is None. 41 42 terminal_set : list or dict 43 List of terminals used in the GP-tree leaves. The default is None. 44 45 bloat_weight : float 46 Bloat control weight to punish large trees. Bigger values make a bigger punish. 47 48 events : list 49 List of events related to this class 50 """ 51 super().__init__( 52 init_depth=init_depth, 53 function_set=function_set, 54 terminal_set=terminal_set, 55 bloat_weight=bloat_weight, 56 erc_range=erc_range, 57 events=events, 58 root_type=root_type, 59 ) 60 61 # assign default creators 62 if grow_creator is None: 63 grow_creator = GrowCreator( 64 init_depth=self.init_depth, 65 function_set=self.function_set, 66 terminal_set=self.terminal_set, 67 events=self.events, 68 root_type=root_type, 69 erc_range=self.erc_range, 70 ) 71 if full_creator is None: 72 full_creator = FullCreator( 73 init_depth=self.init_depth, 74 function_set=self.function_set, 75 terminal_set=self.terminal_set, 76 events=self.events, 77 root_type=root_type, 78 erc_range=self.erc_range, 79 ) 80 81 self.grow_creator = grow_creator 82 self.full_creator = full_creator
Tree creator that creates trees using the Ramped Half and Half method
Parameters
- grow_creator (GrowCreator): a tree creator that creates trees using the grow method
- full_creator (FullCreator): a tree creator that creates trees using the full method
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 or dict): List of terminals used in the GP-tree leaves. 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
@override
def
create_individuals( self, n_individuals: int, higher_is_better: bool) -> List[eckity.genetic_encodings.gp.tree.tree_individual.Tree]:
84 @override 85 def create_individuals( 86 self, n_individuals: int, higher_is_better: bool 87 ) -> List[Tree]: 88 """ 89 Initialize the subpopulation individuals using ramped half-and-half method. 90 91 Parameters 92 ---------- 93 n_individuals: int 94 number of individuals to create 95 96 higher_is_better: bool 97 determines if the fitness of the created individuals should be minimized or maximized 98 99 Returns 100 ------- 101 102 """ 103 min_depth, max_depth = self.init_depth 104 105 # if pop size is 100 and we want depths 2,3,4,5,6 then group_size is 10: 106 # 10 'grow' with depth 2, 10 'full' with depth 2, 10 'grow' with depth 3, 10 'full' with depth 3, etc. 107 group_size = int(n_individuals / (2 * (max_depth + 1 - min_depth))) 108 109 if group_size == 0: 110 raise ValueError( 111 "Incompatible population size and init_depth. " 112 "Population size must be " 113 f"at least {2 * (max_depth + 1 - min_depth)}." 114 ) 115 116 individuals = [] 117 118 for depth in range(min_depth, max_depth + 1): 119 # first create `group_size` individuals using grow method 120 self.grow_creator.init_depth = (min_depth, depth) 121 grown_inds = self.grow_creator.create_individuals( 122 group_size, higher_is_better 123 ) 124 125 # then create `group_size` individuals using full method 126 self.full_creator.init_depth = (min_depth, depth) 127 full_inds = self.full_creator.create_individuals( 128 group_size, higher_is_better 129 ) 130 131 individuals.extend(grown_inds + full_inds) 132 133 # might need to add a few because 'group_size' may have been a float that was truncated 134 n_missing = n_individuals - len(individuals) 135 if n_missing > 0: 136 missing_inds = self.full_creator.create_individuals( 137 n_missing, higher_is_better 138 ) 139 individuals.extend(missing_inds) 140 141 self.created_individuals = individuals 142 return individuals
Initialize the subpopulation individuals using ramped half-and-half method.
Parameters
- n_individuals (int): number of individuals to create
- higher_is_better (bool): determines if the fitness of the created individuals should be minimized or maximized
- Returns
- -------