eckity.genetic_operators.selections.tournament_selection

 1from random import choices
 2
 3from eckity.genetic_operators.selections.selection_method import SelectionMethod
 4
 5
 6class TournamentSelection(SelectionMethod):
 7    def __init__(self, tournament_size, higher_is_better=False, events=None):
 8        super().__init__(events=events, higher_is_better=higher_is_better)
 9        self.tournament_size = tournament_size
10
11    def select(self, source_inds, dest_inds):
12        # the selection should add len(source_inds) individuals to dest_inds, so the required number of tournaments
13        # is the size of source individuals divided by the number of winners per tournament
14        # n_tournaments = len(source_inds) // self.operator_arity
15        n_tournaments = (len(source_inds) - len(dest_inds)) // self.arity
16
17        # create all tournaments beforehand
18        tournaments = [choices(source_inds, k=self.tournament_size) for _ in range(n_tournaments)]
19
20        # pick the winner of each tournament and add all winners to dest_inds
21        winners = [self._pick_tournament_winner(tour) for tour in tournaments]
22        dest_inds.extend(winners)
23
24        self.selected_individuals = dest_inds
25        # self.publish("after_selection")   # TODO this already publishes 'after_operator' event
26
27        return dest_inds
28
29    def _pick_tournament_winner(self, tournament):
30        winner = tournament[0]
31        for participant in tournament[1:]:
32            if participant.fitness.better_than(participant, winner.fitness, winner):
33                winner = participant
34        return winner.clone()
 8class TournamentSelection(SelectionMethod):
 9    def __init__(self, tournament_size, higher_is_better=False, events=None):
10        super().__init__(events=events, higher_is_better=higher_is_better)
11        self.tournament_size = tournament_size
12
13    def select(self, source_inds, dest_inds):
14        # the selection should add len(source_inds) individuals to dest_inds, so the required number of tournaments
15        # is the size of source individuals divided by the number of winners per tournament
16        # n_tournaments = len(source_inds) // self.operator_arity
17        n_tournaments = (len(source_inds) - len(dest_inds)) // self.arity
18
19        # create all tournaments beforehand
20        tournaments = [choices(source_inds, k=self.tournament_size) for _ in range(n_tournaments)]
21
22        # pick the winner of each tournament and add all winners to dest_inds
23        winners = [self._pick_tournament_winner(tour) for tour in tournaments]
24        dest_inds.extend(winners)
25
26        self.selected_individuals = dest_inds
27        # self.publish("after_selection")   # TODO this already publishes 'after_operator' event
28
29        return dest_inds
30
31    def _pick_tournament_winner(self, tournament):
32        winner = tournament[0]
33        for participant in tournament[1:]:
34            if participant.fitness.better_than(participant, winner.fitness, winner):
35                winner = participant
36        return winner.clone()
TournamentSelection(tournament_size, higher_is_better=False, events=None)
 9    def __init__(self, tournament_size, higher_is_better=False, events=None):
10        super().__init__(events=events, higher_is_better=higher_is_better)
11        self.tournament_size = tournament_size
tournament_size
def select(self, source_inds, dest_inds):
13    def select(self, source_inds, dest_inds):
14        # the selection should add len(source_inds) individuals to dest_inds, so the required number of tournaments
15        # is the size of source individuals divided by the number of winners per tournament
16        # n_tournaments = len(source_inds) // self.operator_arity
17        n_tournaments = (len(source_inds) - len(dest_inds)) // self.arity
18
19        # create all tournaments beforehand
20        tournaments = [choices(source_inds, k=self.tournament_size) for _ in range(n_tournaments)]
21
22        # pick the winner of each tournament and add all winners to dest_inds
23        winners = [self._pick_tournament_winner(tour) for tour in tournaments]
24        dest_inds.extend(winners)
25
26        self.selected_individuals = dest_inds
27        # self.publish("after_selection")   # TODO this already publishes 'after_operator' event
28
29        return dest_inds