eckity.creators.gp_creators.ramped_hh

  1from eckity.creators.gp_creators.full import FullCreator
  2from eckity.creators.gp_creators.grow import GrowCreator
  3from eckity.creators.gp_creators.tree_creator import GPTreeCreator
  4from eckity.genetic_encodings.gp.tree.tree_individual import Tree
  5from eckity.fitness.gp_fitness import GPFitness
  6
  7
  8class RampedHalfAndHalfCreator(GPTreeCreator):
  9	def __init__(self,
 10				 grow_creator=None,
 11				 full_creator=None,
 12				 init_depth=None,
 13				 function_set=None,
 14				 terminal_set=None,
 15				 erc_range=None,
 16				 bloat_weight=0.1,
 17				 events=None):
 18		"""
 19		Tree creator that creates trees using the Ramped Half and Half method
 20
 21		Parameters
 22		----------
 23		grow_creator: GrowCreator
 24			a tree creator that creates trees using the grow method
 25
 26		full_creator: FullCreator
 27			a tree creator that creates trees using the full method
 28
 29		init_depth : (int, int)
 30		Min and max depths of initial random trees. The default is None.
 31
 32		function_set : list
 33			List of functions used as internal nodes in the GP tree. The default is None.
 34
 35		terminal_set : list
 36			List of terminals used in the GP-tree leaves. The default is None.
 37
 38		erc_range : (float, float)
 39			Range of values for ephemeral random constant (ERC). The default is None.
 40
 41		bloat_weight : float
 42			Bloat control weight to punish large trees. Bigger values make a bigger punish.
 43
 44		events : list
 45			List of events related to this class
 46		"""
 47		super().__init__(init_depth=init_depth,
 48						 function_set=function_set,
 49						 terminal_set=terminal_set,
 50						 erc_range=erc_range,
 51						 bloat_weight=bloat_weight,
 52						 events=events)
 53
 54		# assign default creators
 55		if grow_creator is None:
 56			grow_creator = GrowCreator(init_depth=self.init_depth, function_set=self.function_set,
 57									   terminal_set=self.terminal_set, erc_range=self.erc_range, events=self.events)
 58		if full_creator is None:
 59			full_creator = FullCreator(init_depth=self.init_depth, function_set=self.function_set,
 60									   terminal_set=self.terminal_set, erc_range=self.erc_range, events=self.events)
 61
 62		self.grow_creator, self.full_creator = grow_creator, full_creator
 63
 64		self.init_method = None  # current creator in use (either grow or full)
 65
 66	def create_individuals(self, n_individuals, higher_is_better):
 67		"""
 68		Initialize the subpopulation individuals using ramped half-and-half method.
 69
 70		Parameters
 71		----------
 72		n_individuals: int
 73			number of individuals to create
 74
 75		higher_is_better: bool
 76			determines if the fitness of the created individuals should be minimized or maximized
 77
 78		Returns
 79		-------
 80
 81		"""
 82
 83		min_depth, max_depth = self.init_depth[0], self.init_depth[1]
 84
 85		# if pop size is 100 and we want depths 2,3,4,5,6 then group_size is 10:
 86		# 10 'grow' with depth 2, 10 'full' with depth 2, 10 'grow' with depth 3, 10 'full' with depth 3, etc.
 87		group_size = int(n_individuals / (max_depth + 1 - min_depth) / 2)
 88
 89		individuals = []
 90
 91		for depth in range(min_depth, max_depth + 1):
 92			for i in range(group_size):
 93				# as explained above, first create (group_size) individuals using grow method
 94				self.init_method = self.grow_creator
 95				self._create_individuals(individuals, depth, higher_is_better)
 96
 97				# then create (group_size) individuals using full method
 98				self.init_method = self.full_creator
 99				self._create_individuals(individuals, depth, higher_is_better)
100
101		# might need to add a few because 'group_size' may have been a float that was truncated
102		self.init_method = self.full_creator
103		for i in range(n_individuals - len(individuals)):
104			self._create_individuals(individuals, max_depth, higher_is_better)
105
106		# TODO we don't need this event since creators have before/after operator events
107		# self.publish("after_creation")
108
109		self.created_individuals = individuals
110		return individuals
111
112	def _create_individuals(self, individuals, max_depth, higher_is_better):
113		t = Tree(init_depth=self.init_depth, function_set=self.function_set,
114				 terminal_set=self.terminal_set, erc_range=self.erc_range,
115				 fitness=GPFitness(bloat_weight=self.bloat_weight, higher_is_better=higher_is_better))
116		self.create_tree(t, max_depth=max_depth)
117		individuals.append(t)
118
119	def create_tree(self, tree_ind, max_depth):
120		self.init_method.create_tree(tree_ind, max_depth)
class RampedHalfAndHalfCreator(eckity.creators.gp_creators.tree_creator.GPTreeCreator):
  9class RampedHalfAndHalfCreator(GPTreeCreator):
 10	def __init__(self,
 11				 grow_creator=None,
 12				 full_creator=None,
 13				 init_depth=None,
 14				 function_set=None,
 15				 terminal_set=None,
 16				 erc_range=None,
 17				 bloat_weight=0.1,
 18				 events=None):
 19		"""
 20		Tree creator that creates trees using the Ramped Half and Half method
 21
 22		Parameters
 23		----------
 24		grow_creator: GrowCreator
 25			a tree creator that creates trees using the grow method
 26
 27		full_creator: FullCreator
 28			a tree creator that creates trees using the full method
 29
 30		init_depth : (int, int)
 31		Min and max depths of initial random trees. The default is None.
 32
 33		function_set : list
 34			List of functions used as internal nodes in the GP tree. The default is None.
 35
 36		terminal_set : list
 37			List of terminals used in the GP-tree leaves. The default is None.
 38
 39		erc_range : (float, float)
 40			Range of values for ephemeral random constant (ERC). The default is None.
 41
 42		bloat_weight : float
 43			Bloat control weight to punish large trees. Bigger values make a bigger punish.
 44
 45		events : list
 46			List of events related to this class
 47		"""
 48		super().__init__(init_depth=init_depth,
 49						 function_set=function_set,
 50						 terminal_set=terminal_set,
 51						 erc_range=erc_range,
 52						 bloat_weight=bloat_weight,
 53						 events=events)
 54
 55		# assign default creators
 56		if grow_creator is None:
 57			grow_creator = GrowCreator(init_depth=self.init_depth, function_set=self.function_set,
 58									   terminal_set=self.terminal_set, erc_range=self.erc_range, events=self.events)
 59		if full_creator is None:
 60			full_creator = FullCreator(init_depth=self.init_depth, function_set=self.function_set,
 61									   terminal_set=self.terminal_set, erc_range=self.erc_range, events=self.events)
 62
 63		self.grow_creator, self.full_creator = grow_creator, full_creator
 64
 65		self.init_method = None  # current creator in use (either grow or full)
 66
 67	def create_individuals(self, n_individuals, higher_is_better):
 68		"""
 69		Initialize the subpopulation individuals using ramped half-and-half method.
 70
 71		Parameters
 72		----------
 73		n_individuals: int
 74			number of individuals to create
 75
 76		higher_is_better: bool
 77			determines if the fitness of the created individuals should be minimized or maximized
 78
 79		Returns
 80		-------
 81
 82		"""
 83
 84		min_depth, max_depth = self.init_depth[0], self.init_depth[1]
 85
 86		# if pop size is 100 and we want depths 2,3,4,5,6 then group_size is 10:
 87		# 10 'grow' with depth 2, 10 'full' with depth 2, 10 'grow' with depth 3, 10 'full' with depth 3, etc.
 88		group_size = int(n_individuals / (max_depth + 1 - min_depth) / 2)
 89
 90		individuals = []
 91
 92		for depth in range(min_depth, max_depth + 1):
 93			for i in range(group_size):
 94				# as explained above, first create (group_size) individuals using grow method
 95				self.init_method = self.grow_creator
 96				self._create_individuals(individuals, depth, higher_is_better)
 97
 98				# then create (group_size) individuals using full method
 99				self.init_method = self.full_creator
100				self._create_individuals(individuals, depth, higher_is_better)
101
102		# might need to add a few because 'group_size' may have been a float that was truncated
103		self.init_method = self.full_creator
104		for i in range(n_individuals - len(individuals)):
105			self._create_individuals(individuals, max_depth, higher_is_better)
106
107		# TODO we don't need this event since creators have before/after operator events
108		# self.publish("after_creation")
109
110		self.created_individuals = individuals
111		return individuals
112
113	def _create_individuals(self, individuals, max_depth, higher_is_better):
114		t = Tree(init_depth=self.init_depth, function_set=self.function_set,
115				 terminal_set=self.terminal_set, erc_range=self.erc_range,
116				 fitness=GPFitness(bloat_weight=self.bloat_weight, higher_is_better=higher_is_better))
117		self.create_tree(t, max_depth=max_depth)
118		individuals.append(t)
119
120	def create_tree(self, tree_ind, max_depth):
121		self.init_method.create_tree(tree_ind, max_depth)
RampedHalfAndHalfCreator( grow_creator=None, full_creator=None, init_depth=None, function_set=None, terminal_set=None, erc_range=None, bloat_weight=0.1, events=None)
10	def __init__(self,
11				 grow_creator=None,
12				 full_creator=None,
13				 init_depth=None,
14				 function_set=None,
15				 terminal_set=None,
16				 erc_range=None,
17				 bloat_weight=0.1,
18				 events=None):
19		"""
20		Tree creator that creates trees using the Ramped Half and Half method
21
22		Parameters
23		----------
24		grow_creator: GrowCreator
25			a tree creator that creates trees using the grow method
26
27		full_creator: FullCreator
28			a tree creator that creates trees using the full method
29
30		init_depth : (int, int)
31		Min and max depths of initial random trees. The default is None.
32
33		function_set : list
34			List of functions used as internal nodes in the GP tree. The default is None.
35
36		terminal_set : list
37			List of terminals used in the GP-tree leaves. The default is None.
38
39		erc_range : (float, float)
40			Range of values for ephemeral random constant (ERC). The default is None.
41
42		bloat_weight : float
43			Bloat control weight to punish large trees. Bigger values make a bigger punish.
44
45		events : list
46			List of events related to this class
47		"""
48		super().__init__(init_depth=init_depth,
49						 function_set=function_set,
50						 terminal_set=terminal_set,
51						 erc_range=erc_range,
52						 bloat_weight=bloat_weight,
53						 events=events)
54
55		# assign default creators
56		if grow_creator is None:
57			grow_creator = GrowCreator(init_depth=self.init_depth, function_set=self.function_set,
58									   terminal_set=self.terminal_set, erc_range=self.erc_range, events=self.events)
59		if full_creator is None:
60			full_creator = FullCreator(init_depth=self.init_depth, function_set=self.function_set,
61									   terminal_set=self.terminal_set, erc_range=self.erc_range, events=self.events)
62
63		self.grow_creator, self.full_creator = grow_creator, full_creator
64
65		self.init_method = None  # current creator in use (either grow or full)

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): 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
init_method
def create_individuals(self, n_individuals, higher_is_better):
 67	def create_individuals(self, n_individuals, higher_is_better):
 68		"""
 69		Initialize the subpopulation individuals using ramped half-and-half method.
 70
 71		Parameters
 72		----------
 73		n_individuals: int
 74			number of individuals to create
 75
 76		higher_is_better: bool
 77			determines if the fitness of the created individuals should be minimized or maximized
 78
 79		Returns
 80		-------
 81
 82		"""
 83
 84		min_depth, max_depth = self.init_depth[0], self.init_depth[1]
 85
 86		# if pop size is 100 and we want depths 2,3,4,5,6 then group_size is 10:
 87		# 10 'grow' with depth 2, 10 'full' with depth 2, 10 'grow' with depth 3, 10 'full' with depth 3, etc.
 88		group_size = int(n_individuals / (max_depth + 1 - min_depth) / 2)
 89
 90		individuals = []
 91
 92		for depth in range(min_depth, max_depth + 1):
 93			for i in range(group_size):
 94				# as explained above, first create (group_size) individuals using grow method
 95				self.init_method = self.grow_creator
 96				self._create_individuals(individuals, depth, higher_is_better)
 97
 98				# then create (group_size) individuals using full method
 99				self.init_method = self.full_creator
100				self._create_individuals(individuals, depth, higher_is_better)
101
102		# might need to add a few because 'group_size' may have been a float that was truncated
103		self.init_method = self.full_creator
104		for i in range(n_individuals - len(individuals)):
105			self._create_individuals(individuals, max_depth, higher_is_better)
106
107		# TODO we don't need this event since creators have before/after operator events
108		# self.publish("after_creation")
109
110		self.created_individuals = individuals
111		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
  • -------
def create_tree(self, tree_ind, max_depth):
120	def create_tree(self, tree_ind, max_depth):
121		self.init_method.create_tree(tree_ind, max_depth)