eckity.breeders.simple_breeder

 1from eckity.breeders.breeder import Breeder
 2from eckity.genetic_operators.selections.elitism_selection import ElitismSelection
 3
 4
 5class SimpleBreeder(Breeder):
 6	def __init__(self,
 7				 events=None):
 8		super().__init__(events=events)
 9		self.selected_individuals = []  # TODO why do we need this field? what about applied_individuals?
10		self.best_of_run = []  # TODO this field isn't used
11
12	def apply_breed(self, population):
13		"""
14		Apply elitism, selection method and the sub-population's operator sequence on each sub-population.
15		In simple case, the operator sequence is applied on the one and only sub-population.
16
17		Parameters
18		----------
19		population:
20			Population of sub-populations of individuals. The operators will be applied on those individuals.
21
22		Returns
23		-------
24		None.
25		"""
26		for subpopulation in population.sub_populations:
27			nextgen_population = []
28
29			num_elites = subpopulation.n_elite
30			if num_elites > 0:
31				elitism_sel = ElitismSelection(num_elites=num_elites, higher_is_better=subpopulation.higher_is_better)
32				elitism_sel.apply_operator((subpopulation.individuals, nextgen_population))
33
34			self.selected_individuals = subpopulation.get_selection_methods()[0][0] \
35				.select(subpopulation.individuals, nextgen_population)
36
37			# then runs all operators on next_gen
38			nextgen_population = self._apply_operators(subpopulation.get_operators_sequence(),
39													   self.selected_individuals)
40			# TODO assert simple operators the has %0 with pop size
41
42			subpopulation.individuals = nextgen_population
43
44	def _apply_operators(self, operator_seq, individuals_to_apply_on):
45		"""
46		Apply a given operator sequence on a given list of individuals.
47		The operators are done sequentially.
48
49		Parameters
50		----------
51		operator_seq: list of operators
52			Operator sequence. The operators will be applied sequentially on the given individuals.
53		individuals_to_apply_on: list of individuals
54			The individuals to apply the operator sequence on.
55
56		Returns
57		-------
58		list of individuals
59			The individuals list after the operators were applied on them.
60		"""
61		for operator in operator_seq:
62			operator_arity = operator.get_operator_arity()
63			for i in range(0, len(individuals_to_apply_on), operator_arity):
64				op_res = operator.apply_operator(individuals_to_apply_on[i:i + operator_arity])
65				individuals_to_apply_on[i:i + operator_arity] = op_res
66		return individuals_to_apply_on
67
68	def event_name_to_data(self, event_name):
69		if event_name == "after_selection":
70			return {"selected_individuals": self.selected_individuals,
71					"best_of_run": self.best_of_run}
class SimpleBreeder(eckity.breeders.breeder.Breeder):
 6class SimpleBreeder(Breeder):
 7	def __init__(self,
 8				 events=None):
 9		super().__init__(events=events)
10		self.selected_individuals = []  # TODO why do we need this field? what about applied_individuals?
11		self.best_of_run = []  # TODO this field isn't used
12
13	def apply_breed(self, population):
14		"""
15		Apply elitism, selection method and the sub-population's operator sequence on each sub-population.
16		In simple case, the operator sequence is applied on the one and only sub-population.
17
18		Parameters
19		----------
20		population:
21			Population of sub-populations of individuals. The operators will be applied on those individuals.
22
23		Returns
24		-------
25		None.
26		"""
27		for subpopulation in population.sub_populations:
28			nextgen_population = []
29
30			num_elites = subpopulation.n_elite
31			if num_elites > 0:
32				elitism_sel = ElitismSelection(num_elites=num_elites, higher_is_better=subpopulation.higher_is_better)
33				elitism_sel.apply_operator((subpopulation.individuals, nextgen_population))
34
35			self.selected_individuals = subpopulation.get_selection_methods()[0][0] \
36				.select(subpopulation.individuals, nextgen_population)
37
38			# then runs all operators on next_gen
39			nextgen_population = self._apply_operators(subpopulation.get_operators_sequence(),
40													   self.selected_individuals)
41			# TODO assert simple operators the has %0 with pop size
42
43			subpopulation.individuals = nextgen_population
44
45	def _apply_operators(self, operator_seq, individuals_to_apply_on):
46		"""
47		Apply a given operator sequence on a given list of individuals.
48		The operators are done sequentially.
49
50		Parameters
51		----------
52		operator_seq: list of operators
53			Operator sequence. The operators will be applied sequentially on the given individuals.
54		individuals_to_apply_on: list of individuals
55			The individuals to apply the operator sequence on.
56
57		Returns
58		-------
59		list of individuals
60			The individuals list after the operators were applied on them.
61		"""
62		for operator in operator_seq:
63			operator_arity = operator.get_operator_arity()
64			for i in range(0, len(individuals_to_apply_on), operator_arity):
65				op_res = operator.apply_operator(individuals_to_apply_on[i:i + operator_arity])
66				individuals_to_apply_on[i:i + operator_arity] = op_res
67		return individuals_to_apply_on
68
69	def event_name_to_data(self, event_name):
70		if event_name == "after_selection":
71			return {"selected_individuals": self.selected_individuals,
72					"best_of_run": self.best_of_run}

The Breeder is responsible to activate the genetic operators (selection, crossover, mutation) on the existing population

Parameters
  • events (dict(str, dict(object, function))): dictionary of event names to dictionary of subscribers to callback methods
SimpleBreeder(events=None)
 7	def __init__(self,
 8				 events=None):
 9		super().__init__(events=events)
10		self.selected_individuals = []  # TODO why do we need this field? what about applied_individuals?
11		self.best_of_run = []  # TODO this field isn't used
selected_individuals
best_of_run
def apply_breed(self, population):
13	def apply_breed(self, population):
14		"""
15		Apply elitism, selection method and the sub-population's operator sequence on each sub-population.
16		In simple case, the operator sequence is applied on the one and only sub-population.
17
18		Parameters
19		----------
20		population:
21			Population of sub-populations of individuals. The operators will be applied on those individuals.
22
23		Returns
24		-------
25		None.
26		"""
27		for subpopulation in population.sub_populations:
28			nextgen_population = []
29
30			num_elites = subpopulation.n_elite
31			if num_elites > 0:
32				elitism_sel = ElitismSelection(num_elites=num_elites, higher_is_better=subpopulation.higher_is_better)
33				elitism_sel.apply_operator((subpopulation.individuals, nextgen_population))
34
35			self.selected_individuals = subpopulation.get_selection_methods()[0][0] \
36				.select(subpopulation.individuals, nextgen_population)
37
38			# then runs all operators on next_gen
39			nextgen_population = self._apply_operators(subpopulation.get_operators_sequence(),
40													   self.selected_individuals)
41			# TODO assert simple operators the has %0 with pop size
42
43			subpopulation.individuals = nextgen_population

Apply elitism, selection method and the sub-population's operator sequence on each sub-population. In simple case, the operator sequence is applied on the one and only sub-population.

Parameters
  • population:: Population of sub-populations of individuals. The operators will be applied on those individuals.
Returns
  • None.
def event_name_to_data(self, event_name):
69	def event_name_to_data(self, event_name):
70		if event_name == "after_selection":
71			return {"selected_individuals": self.selected_individuals,
72					"best_of_run": self.best_of_run}