source: framspy/evolalg/selection/ @ 1114

Last change on this file since 1114 was 1113, checked in by Maciej Komosinski, 2 years ago

Added a framework for evolutionary algorithms cooperating with FramsticksLib?.py

File size: 619 bytes
1from evolalg.base.individual import Individual
2from typing import List
3import random
5from evolalg.base.step import Step
6from evolalg.selection.selection import Selection
9class TournamentSelection(Selection):
10    def __init__(self, tournament_size: int, fit_attr="fitness", copy=False):
11        super(TournamentSelection, self).__init__(copy)
12        self.tournament_size = tournament_size
13        self.fit_attr = fit_attr
15    def select_next(self, population):
16        selected = [random.choice(population) for i in range(self.tournament_size)]
17        return max(selected, key=lambda x: getattr(x, self.fit_attr))
Note: See TracBrowser for help on using the repository browser.