Last change
on this file since 1297 was
1185,
checked in by Maciej Komosinski, 2 years ago
|
Renamed a module; new name is "evolalg_steps"
|
File size:
671 bytes
|
Rev | Line | |
---|
[1185] | 1 | from evolalg_steps.base.individual import Individual |
---|
[1113] | 2 | from typing import List |
---|
| 3 | import random |
---|
| 4 | |
---|
[1185] | 5 | from evolalg_steps.base.step import Step |
---|
| 6 | from evolalg_steps.selection.selection import Selection |
---|
[1113] | 7 | |
---|
| 8 | |
---|
| 9 | class TournamentSelection(Selection): |
---|
[1139] | 10 | def __init__(self, tournament_size: int, fit_attr="fitness", copy=False, *args, **kwargs): |
---|
| 11 | super(TournamentSelection, self).__init__(copy, *args, **kwargs) |
---|
[1113] | 12 | self.tournament_size = tournament_size |
---|
| 13 | self.fit_attr = fit_attr |
---|
| 14 | |
---|
| 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.