eckity.genetic_operators.selections.fp_selection

 1from typing import List
 2
 3import numpy as np
 4from overrides import override
 5from scipy.special import softmax
 6
 7from eckity.genetic_operators import SelectionMethod
 8from eckity.individual import Individual
 9
10
11class FitnessProportionateSelection(SelectionMethod):
12    def __init__(
13        self,
14        events=None,
15    ):
16        """
17        Fitness Proportionate Selection, AKA Roulette Wheel Selection.
18        In this method, the probability of selecting an individual from the
19        population is proportional to its fitness score.
20        Individuals with higher fitness have a higher chance of being
21        selected as parents. This selection method simulates a roulette wheel,
22        where the size of the slice for each individual on the wheel is
23        determined by its fitness score
24
25        Parameters
26        ----------
27        events : List[str], optional
28            selection events, by default None
29        """
30        super().__init__(events=events)
31
32    @override
33    def select(
34        self, source_inds: List[Individual], dest_inds: List[Individual]
35    ) -> List[Individual]:
36        n_selected = len(source_inds) - len(dest_inds)
37
38        fitness_scores = np.array(
39            [ind.get_augmented_fitness() for ind in source_inds]
40        )
41
42        min_val = np.min(fitness_scores)
43
44        if min_val < 0:
45            raise ValueError(
46                "Fitness scores must be non-negative for FP Selection"
47            )
48
49        # assumes higher_is_better the same for all individuals
50        higher_is_better = source_inds[0].higher_is_better
51
52        # convert higher fitness scores to be better
53        if not higher_is_better:
54            # add smoothing (if necessary) to avoid division by zero
55            smoothing = 1 if min_val == 0 else 0
56            fitness_scores = 1 / (fitness_scores + smoothing)
57
58        # generate a distribution of fitness scores
59        fit_p = (
60            softmax(fitness_scores)
61            if np.sum(fitness_scores) != 1
62            else fitness_scores
63        )
64
65        # select individuals proportionate to fitness
66        selected_inds = np.random.choice(
67            source_inds, size=n_selected, replace=True, p=fit_p
68        )
69
70        for selected_ind in selected_inds:
71            clone = selected_ind.clone()
72            clone.selected_by.append(type(self).__name__)
73            dest_inds.append(clone)
74
75        self.selected_individuals = dest_inds
76
77        return dest_inds
class FitnessProportionateSelection(eckity.genetic_operators.selections.selection_method.SelectionMethod):
12class FitnessProportionateSelection(SelectionMethod):
13    def __init__(
14        self,
15        events=None,
16    ):
17        """
18        Fitness Proportionate Selection, AKA Roulette Wheel Selection.
19        In this method, the probability of selecting an individual from the
20        population is proportional to its fitness score.
21        Individuals with higher fitness have a higher chance of being
22        selected as parents. This selection method simulates a roulette wheel,
23        where the size of the slice for each individual on the wheel is
24        determined by its fitness score
25
26        Parameters
27        ----------
28        events : List[str], optional
29            selection events, by default None
30        """
31        super().__init__(events=events)
32
33    @override
34    def select(
35        self, source_inds: List[Individual], dest_inds: List[Individual]
36    ) -> List[Individual]:
37        n_selected = len(source_inds) - len(dest_inds)
38
39        fitness_scores = np.array(
40            [ind.get_augmented_fitness() for ind in source_inds]
41        )
42
43        min_val = np.min(fitness_scores)
44
45        if min_val < 0:
46            raise ValueError(
47                "Fitness scores must be non-negative for FP Selection"
48            )
49
50        # assumes higher_is_better the same for all individuals
51        higher_is_better = source_inds[0].higher_is_better
52
53        # convert higher fitness scores to be better
54        if not higher_is_better:
55            # add smoothing (if necessary) to avoid division by zero
56            smoothing = 1 if min_val == 0 else 0
57            fitness_scores = 1 / (fitness_scores + smoothing)
58
59        # generate a distribution of fitness scores
60        fit_p = (
61            softmax(fitness_scores)
62            if np.sum(fitness_scores) != 1
63            else fitness_scores
64        )
65
66        # select individuals proportionate to fitness
67        selected_inds = np.random.choice(
68            source_inds, size=n_selected, replace=True, p=fit_p
69        )
70
71        for selected_ind in selected_inds:
72            clone = selected_ind.clone()
73            clone.selected_by.append(type(self).__name__)
74            dest_inds.append(clone)
75
76        self.selected_individuals = dest_inds
77
78        return dest_inds

Helper class that provides a standard way to create an ABC using inheritance.

FitnessProportionateSelection(events=None)
13    def __init__(
14        self,
15        events=None,
16    ):
17        """
18        Fitness Proportionate Selection, AKA Roulette Wheel Selection.
19        In this method, the probability of selecting an individual from the
20        population is proportional to its fitness score.
21        Individuals with higher fitness have a higher chance of being
22        selected as parents. This selection method simulates a roulette wheel,
23        where the size of the slice for each individual on the wheel is
24        determined by its fitness score
25
26        Parameters
27        ----------
28        events : List[str], optional
29            selection events, by default None
30        """
31        super().__init__(events=events)

Fitness Proportionate Selection, AKA Roulette Wheel Selection. In this method, the probability of selecting an individual from the population is proportional to its fitness score. Individuals with higher fitness have a higher chance of being selected as parents. This selection method simulates a roulette wheel, where the size of the slice for each individual on the wheel is determined by its fitness score

Parameters
  • events (List[str], optional): selection events, by default None
@override
def select( self, source_inds: List[eckity.individual.Individual], dest_inds: List[eckity.individual.Individual]) -> List[eckity.individual.Individual]:
33    @override
34    def select(
35        self, source_inds: List[Individual], dest_inds: List[Individual]
36    ) -> List[Individual]:
37        n_selected = len(source_inds) - len(dest_inds)
38
39        fitness_scores = np.array(
40            [ind.get_augmented_fitness() for ind in source_inds]
41        )
42
43        min_val = np.min(fitness_scores)
44
45        if min_val < 0:
46            raise ValueError(
47                "Fitness scores must be non-negative for FP Selection"
48            )
49
50        # assumes higher_is_better the same for all individuals
51        higher_is_better = source_inds[0].higher_is_better
52
53        # convert higher fitness scores to be better
54        if not higher_is_better:
55            # add smoothing (if necessary) to avoid division by zero
56            smoothing = 1 if min_val == 0 else 0
57            fitness_scores = 1 / (fitness_scores + smoothing)
58
59        # generate a distribution of fitness scores
60        fit_p = (
61            softmax(fitness_scores)
62            if np.sum(fitness_scores) != 1
63            else fitness_scores
64        )
65
66        # select individuals proportionate to fitness
67        selected_inds = np.random.choice(
68            source_inds, size=n_selected, replace=True, p=fit_p
69        )
70
71        for selected_ind in selected_inds:
72            clone = selected_ind.clone()
73            clone.selected_by.append(type(self).__name__)
74            dest_inds.append(clone)
75
76        self.selected_individuals = dest_inds
77
78        return dest_inds