eckity.multi_objective_evolution.nsga2_breeder

 1from eckity.breeders.simple_breeder import SimpleBreeder
 2from eckity.genetic_operators.selections.elitism_selection import (
 3    ElitismSelection,
 4)
 5
 6
 7class NSGA2Breeder(SimpleBreeder):
 8    def __init__(self, events=None):
 9        super().__init__(events=events)
10        self.selected_individuals = (
11            []
12        )  # TODO why do we need this field? what about applied_individuals?
13
14    def apply_breed(self, population):
15        """
16        Apply elitism, selection method and the sub-population's operator sequence on each sub-population.
17        In simple case, the operator sequence is applied on the one and only sub-population.
18
19        adds the current generation to the next generation
20
21        Parameters
22        ----------
23        population:
24            Population of sub-populations of individuals. The operators will be applied on those individuals.
25
26        Returns
27        -------
28        None.
29        """
30        for subpopulation in population.sub_populations:
31            nextgen_population = []
32
33            num_elites = subpopulation.n_elite
34            if num_elites > 0:
35                elitism_sel = ElitismSelection(num_elites=num_elites)
36                elitism_sel.apply_operator(
37                    (subpopulation.individuals, nextgen_population)
38                )
39
40            nextgen_population = self._create_next_gen(subpopulation)
41
42            selection = subpopulation.get_selection_methods()[0]
43            self.selected_individuals = selection.select(
44                subpopulation.individuals, nextgen_population
45            )
46
47            subpopulation.individuals = nextgen_population
48
49    def _create_next_gen(self, subpopulation):
50        oldgen_population = [ind.clone() for ind in subpopulation.individuals]
51
52        nextgen_population = self._apply_operators(
53            subpopulation.get_operators_sequence(), subpopulation.individuals
54        )
55
56        oldgen_population += nextgen_population
57        nextgen_population = oldgen_population
58
59        for ind in nextgen_population:
60            ind.fitness.set_not_evaluated()
61
62        return nextgen_population
class NSGA2Breeder(eckity.breeders.simple_breeder.SimpleBreeder):
 8class NSGA2Breeder(SimpleBreeder):
 9    def __init__(self, events=None):
10        super().__init__(events=events)
11        self.selected_individuals = (
12            []
13        )  # TODO why do we need this field? what about applied_individuals?
14
15    def apply_breed(self, population):
16        """
17        Apply elitism, selection method and the sub-population's operator sequence on each sub-population.
18        In simple case, the operator sequence is applied on the one and only sub-population.
19
20        adds the current generation to the next generation
21
22        Parameters
23        ----------
24        population:
25            Population of sub-populations of individuals. The operators will be applied on those individuals.
26
27        Returns
28        -------
29        None.
30        """
31        for subpopulation in population.sub_populations:
32            nextgen_population = []
33
34            num_elites = subpopulation.n_elite
35            if num_elites > 0:
36                elitism_sel = ElitismSelection(num_elites=num_elites)
37                elitism_sel.apply_operator(
38                    (subpopulation.individuals, nextgen_population)
39                )
40
41            nextgen_population = self._create_next_gen(subpopulation)
42
43            selection = subpopulation.get_selection_methods()[0]
44            self.selected_individuals = selection.select(
45                subpopulation.individuals, nextgen_population
46            )
47
48            subpopulation.individuals = nextgen_population
49
50    def _create_next_gen(self, subpopulation):
51        oldgen_population = [ind.clone() for ind in subpopulation.individuals]
52
53        nextgen_population = self._apply_operators(
54            subpopulation.get_operators_sequence(), subpopulation.individuals
55        )
56
57        oldgen_population += nextgen_population
58        nextgen_population = oldgen_population
59
60        for ind in nextgen_population:
61            ind.fitness.set_not_evaluated()
62
63        return nextgen_population

A Simple version of Breeder class. All simple classes assume there is only one sub-population in population.

NSGA2Breeder(events=None)
 9    def __init__(self, events=None):
10        super().__init__(events=events)
11        self.selected_individuals = (
12            []
13        )  # TODO why do we need this field? what about applied_individuals?
selected_individuals
def apply_breed(self, population):
15    def apply_breed(self, population):
16        """
17        Apply elitism, selection method and the sub-population's operator sequence on each sub-population.
18        In simple case, the operator sequence is applied on the one and only sub-population.
19
20        adds the current generation to the next generation
21
22        Parameters
23        ----------
24        population:
25            Population of sub-populations of individuals. The operators will be applied on those individuals.
26
27        Returns
28        -------
29        None.
30        """
31        for subpopulation in population.sub_populations:
32            nextgen_population = []
33
34            num_elites = subpopulation.n_elite
35            if num_elites > 0:
36                elitism_sel = ElitismSelection(num_elites=num_elites)
37                elitism_sel.apply_operator(
38                    (subpopulation.individuals, nextgen_population)
39                )
40
41            nextgen_population = self._create_next_gen(subpopulation)
42
43            selection = subpopulation.get_selection_methods()[0]
44            self.selected_individuals = selection.select(
45                subpopulation.individuals, nextgen_population
46            )
47
48            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.