eckity.multi_objective_evolution.nsga2_breeder
1from eckity.breeders.simple_breeder import SimpleBreeder 2from eckity.genetic_operators.selections.elitism_selection import ElitismSelection 3 4 5class NSGA2Breeder(SimpleBreeder): 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 11 def apply_breed(self, population): 12 """ 13 Apply elitism, selection method and the sub-population's operator sequence on each sub-population. 14 In simple case, the operator sequence is applied on the one and only sub-population. 15 16 adds the current generation to the next generation 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 nextgen_population = self._create_next_gen(subpopulation) 36 37 self.selected_individuals = subpopulation.get_selection_methods()[0][0] \ 38 .select(subpopulation.individuals, nextgen_population) 39 40 subpopulation.individuals = nextgen_population 41 42 def _create_next_gen(self, subpopulation): 43 # oldgen_population = deepcopy(subpopulation.individuals) # needed since apply operator changes the values of 44 oldgen_population = [ind.clone() for ind in subpopulation.individuals] 45 46 nextgen_population = self._apply_operators(subpopulation.get_operators_sequence(), 47 subpopulation.individuals) # self.selected_individuals) 48 49 oldgen_population += nextgen_population 50 nextgen_population = oldgen_population 51 52 for ind in nextgen_population: 53 ind.fitness.set_not_evaluated() 54 55 return nextgen_population
6class NSGA2Breeder(SimpleBreeder): 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 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 adds the current generation to the next generation 18 19 Parameters 20 ---------- 21 population: 22 Population of sub-populations of individuals. The operators will be applied on those individuals. 23 24 Returns 25 ------- 26 None. 27 """ 28 for subpopulation in population.sub_populations: 29 nextgen_population = [] 30 31 num_elites = subpopulation.n_elite 32 if num_elites > 0: 33 elitism_sel = ElitismSelection(num_elites=num_elites, higher_is_better=subpopulation.higher_is_better) 34 elitism_sel.apply_operator((subpopulation.individuals, nextgen_population)) 35 36 nextgen_population = self._create_next_gen(subpopulation) 37 38 self.selected_individuals = subpopulation.get_selection_methods()[0][0] \ 39 .select(subpopulation.individuals, nextgen_population) 40 41 subpopulation.individuals = nextgen_population 42 43 def _create_next_gen(self, subpopulation): 44 # oldgen_population = deepcopy(subpopulation.individuals) # needed since apply operator changes the values of 45 oldgen_population = [ind.clone() for ind in subpopulation.individuals] 46 47 nextgen_population = self._apply_operators(subpopulation.get_operators_sequence(), 48 subpopulation.individuals) # self.selected_individuals) 49 50 oldgen_population += nextgen_population 51 nextgen_population = oldgen_population 52 53 for ind in nextgen_population: 54 ind.fitness.set_not_evaluated() 55 56 return nextgen_population
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
def
apply_breed(self, population):
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 adds the current generation to the next generation 18 19 Parameters 20 ---------- 21 population: 22 Population of sub-populations of individuals. The operators will be applied on those individuals. 23 24 Returns 25 ------- 26 None. 27 """ 28 for subpopulation in population.sub_populations: 29 nextgen_population = [] 30 31 num_elites = subpopulation.n_elite 32 if num_elites > 0: 33 elitism_sel = ElitismSelection(num_elites=num_elites, higher_is_better=subpopulation.higher_is_better) 34 elitism_sel.apply_operator((subpopulation.individuals, nextgen_population)) 35 36 nextgen_population = self._create_next_gen(subpopulation) 37 38 self.selected_individuals = subpopulation.get_selection_methods()[0][0] \ 39 .select(subpopulation.individuals, nextgen_population) 40 41 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.
adds the current generation to the next generation
Parameters
- population:: Population of sub-populations of individuals. The operators will be applied on those individuals.
Returns
- None.