Class: Operator::Selector::TournamentSelection
- Inherits:
-
Object
- Object
- Operator::Selector::TournamentSelection
- Defined in:
- lib/opt_alg_framework/operator/selector/tournament_selection.rb
Instance Method Summary collapse
-
#initialize(size) ⇒ TournamentSelection
constructor
Initialize informing the tournament size.
-
#select(population) ⇒ Object
Main method.
Constructor Details
#initialize(size) ⇒ TournamentSelection
Initialize informing the tournament size.
6 7 8 |
# File 'lib/opt_alg_framework/operator/selector/tournament_selection.rb', line 6 def initialize(size) @size = size end |
Instance Method Details
#select(population) ⇒ Object
Main method.
11 12 13 14 15 16 17 18 |
# File 'lib/opt_alg_framework/operator/selector/tournament_selection.rb', line 11 def select(population) best_individual = population[Random.rand(0...population.size)] 0.upto(@size) do individual = population[Random.rand(0...population.size)] best_individual = individual if individual[:fitness] < best_individual[:fitness] end best_individual end |