eckity.breeders.simple_breeder
1from eckity.breeders.breeder import Breeder 2from eckity.genetic_operators.selections.elitism_selection import ( 3 ElitismSelection, 4) 5 6 7class SimpleBreeder(Breeder): 8 """ 9 A Simple version of Breeder class. 10 All simple classes assume there is only one sub-population in population. 11 """ 12 13 def __init__(self, events=None): 14 super().__init__(events=events) 15 self.selected_individuals = ( 16 [] 17 ) # TODO why do we need this field? what about applied_individuals? 18 self.best_of_run = [] # TODO this field isn't used 19 20 def apply_breed(self, population): 21 """ 22 Apply elitism, selection and operator sequence on the sub-populations. 23 In simple case, the operator sequence is applied on one sub-population. 24 25 Parameters 26 ---------- 27 population: 28 Population of sub-populations of individuals. 29 The operators will be applied on those individuals. 30 31 Returns 32 ------- 33 None. 34 """ 35 for subpopulation in population.sub_populations: 36 # Assert that operator arities are compatible with pop size 37 selection_methods = subpopulation.get_selection_methods() 38 39 operators_sequence = subpopulation.get_operators_sequence() 40 operators = selection_methods + operators_sequence 41 42 for oper in operators: 43 if subpopulation.population_size % oper.arity != 0: 44 raise ValueError( 45 f"Operator {oper} arity must be " 46 f"dividable by population size" 47 ) 48 49 nextgen_population = [] 50 51 num_elites = subpopulation.n_elite 52 if num_elites > 0: 53 elitism_sel = ElitismSelection( 54 num_elites=num_elites 55 ) 56 elitism_sel.apply_operator( 57 (subpopulation.individuals, nextgen_population) 58 ) 59 60 # consider only first selection method 61 selection = subpopulation.get_selection_methods()[0] 62 self.selected_individuals = selection.select( 63 subpopulation.individuals, nextgen_population 64 ) 65 66 # then runs all operators on next_gen 67 nextgen_population = self._apply_operators( 68 subpopulation.get_operators_sequence(), 69 self.selected_individuals, 70 ) 71 72 subpopulation.individuals = nextgen_population 73 74 def _apply_operators(self, operator_seq, individuals_to_apply_on): 75 """ 76 Apply a given operator sequence on a given list of individuals. 77 The operators are done sequentially. 78 79 Parameters 80 ---------- 81 operator_seq: list of operators 82 Operator sequence. Applied sequentially on the individuals. 83 individuals_to_apply_on: list of individuals 84 The individuals to apply the operator sequence on. 85 86 Returns 87 ------- 88 list of individuals 89 The individuals list after the operators were applied on them. 90 """ 91 for operator in operator_seq: 92 operator_arity = operator.get_operator_arity() 93 for i in range(0, len(individuals_to_apply_on), operator_arity): 94 op_res = operator.apply_operator( 95 individuals_to_apply_on[i: i + operator_arity] 96 ) 97 individuals_to_apply_on[i: i + operator_arity] = op_res 98 return individuals_to_apply_on 99 100 def event_name_to_data(self, event_name): 101 if event_name == "after_selection": 102 return { 103 "selected_individuals": self.selected_individuals, 104 "best_of_run": self.best_of_run, 105 }
8class SimpleBreeder(Breeder): 9 """ 10 A Simple version of Breeder class. 11 All simple classes assume there is only one sub-population in population. 12 """ 13 14 def __init__(self, events=None): 15 super().__init__(events=events) 16 self.selected_individuals = ( 17 [] 18 ) # TODO why do we need this field? what about applied_individuals? 19 self.best_of_run = [] # TODO this field isn't used 20 21 def apply_breed(self, population): 22 """ 23 Apply elitism, selection and operator sequence on the sub-populations. 24 In simple case, the operator sequence is applied on one sub-population. 25 26 Parameters 27 ---------- 28 population: 29 Population of sub-populations of individuals. 30 The operators will be applied on those individuals. 31 32 Returns 33 ------- 34 None. 35 """ 36 for subpopulation in population.sub_populations: 37 # Assert that operator arities are compatible with pop size 38 selection_methods = subpopulation.get_selection_methods() 39 40 operators_sequence = subpopulation.get_operators_sequence() 41 operators = selection_methods + operators_sequence 42 43 for oper in operators: 44 if subpopulation.population_size % oper.arity != 0: 45 raise ValueError( 46 f"Operator {oper} arity must be " 47 f"dividable by population size" 48 ) 49 50 nextgen_population = [] 51 52 num_elites = subpopulation.n_elite 53 if num_elites > 0: 54 elitism_sel = ElitismSelection( 55 num_elites=num_elites 56 ) 57 elitism_sel.apply_operator( 58 (subpopulation.individuals, nextgen_population) 59 ) 60 61 # consider only first selection method 62 selection = subpopulation.get_selection_methods()[0] 63 self.selected_individuals = selection.select( 64 subpopulation.individuals, nextgen_population 65 ) 66 67 # then runs all operators on next_gen 68 nextgen_population = self._apply_operators( 69 subpopulation.get_operators_sequence(), 70 self.selected_individuals, 71 ) 72 73 subpopulation.individuals = nextgen_population 74 75 def _apply_operators(self, operator_seq, individuals_to_apply_on): 76 """ 77 Apply a given operator sequence on a given list of individuals. 78 The operators are done sequentially. 79 80 Parameters 81 ---------- 82 operator_seq: list of operators 83 Operator sequence. Applied sequentially on the individuals. 84 individuals_to_apply_on: list of individuals 85 The individuals to apply the operator sequence on. 86 87 Returns 88 ------- 89 list of individuals 90 The individuals list after the operators were applied on them. 91 """ 92 for operator in operator_seq: 93 operator_arity = operator.get_operator_arity() 94 for i in range(0, len(individuals_to_apply_on), operator_arity): 95 op_res = operator.apply_operator( 96 individuals_to_apply_on[i: i + operator_arity] 97 ) 98 individuals_to_apply_on[i: i + operator_arity] = op_res 99 return individuals_to_apply_on 100 101 def event_name_to_data(self, event_name): 102 if event_name == "after_selection": 103 return { 104 "selected_individuals": self.selected_individuals, 105 "best_of_run": self.best_of_run, 106 }
A Simple version of Breeder class. All simple classes assume there is only one sub-population in population.
def
apply_breed(self, population):
21 def apply_breed(self, population): 22 """ 23 Apply elitism, selection and operator sequence on the sub-populations. 24 In simple case, the operator sequence is applied on one sub-population. 25 26 Parameters 27 ---------- 28 population: 29 Population of sub-populations of individuals. 30 The operators will be applied on those individuals. 31 32 Returns 33 ------- 34 None. 35 """ 36 for subpopulation in population.sub_populations: 37 # Assert that operator arities are compatible with pop size 38 selection_methods = subpopulation.get_selection_methods() 39 40 operators_sequence = subpopulation.get_operators_sequence() 41 operators = selection_methods + operators_sequence 42 43 for oper in operators: 44 if subpopulation.population_size % oper.arity != 0: 45 raise ValueError( 46 f"Operator {oper} arity must be " 47 f"dividable by population size" 48 ) 49 50 nextgen_population = [] 51 52 num_elites = subpopulation.n_elite 53 if num_elites > 0: 54 elitism_sel = ElitismSelection( 55 num_elites=num_elites 56 ) 57 elitism_sel.apply_operator( 58 (subpopulation.individuals, nextgen_population) 59 ) 60 61 # consider only first selection method 62 selection = subpopulation.get_selection_methods()[0] 63 self.selected_individuals = selection.select( 64 subpopulation.individuals, nextgen_population 65 ) 66 67 # then runs all operators on next_gen 68 nextgen_population = self._apply_operators( 69 subpopulation.get_operators_sequence(), 70 self.selected_individuals, 71 ) 72 73 subpopulation.individuals = nextgen_population
Apply elitism, selection and operator sequence on the sub-populations. In simple case, the operator sequence is applied on one sub-population.
Parameters
- population:: Population of sub-populations of individuals. The operators will be applied on those individuals.
Returns
- None.