Class: Evolvable::Selection
- Inherits:
-
Object
- Object
- Evolvable::Selection
- Extended by:
- Forwardable
- Defined in:
- lib/evolvable/selection.rb
Overview
Selection determines which evolvables will serve as parents for the next generation. You can control the selection process in several ways:
Set the selection size during population initialization:
population = MyEvolvable.new_population(
selection: { size: 3 }
)
Adjust the selection size after initialization:
population.selection_size = 4
Manually assign the selected evolvables:
population.selected_evolvables = [evolvable1, evolvable2]
Or evolve a custom selection directly:
population.evolve_selected([evolvable1, evolvable2])
This flexibility lets you implement custom selection strategies, overriding or augmenting the built-in behavior.
Instance Attribute Summary collapse
-
#size ⇒ Integer
The number of evolvables to select as parents for the next generation.
Instance Method Summary collapse
-
#call(population) ⇒ Evolvable::Population
Performs selection on the population.
-
#initialize(size: 2) ⇒ Selection
constructor
Initializes a new selection object.
-
#select_evolvables(evolvables) ⇒ Array<Evolvable>
Selects the best evolvables from the given collection.
Constructor Details
#initialize(size: 2) ⇒ Selection
Initializes a new selection object.
48 49 50 |
# File 'lib/evolvable/selection.rb', line 48 def initialize(size: 2) @size = size end |
Instance Attribute Details
#size ⇒ Integer
The number of evolvables to select as parents for the next generation.
56 57 58 |
# File 'lib/evolvable/selection.rb', line 56 def size @size end |
Instance Method Details
#call(population) ⇒ Evolvable::Population
Performs selection on the population. Takes the evaluated and sorted evolvables and selects a subset to become parents for the next generation.
66 67 68 69 70 71 |
# File 'lib/evolvable/selection.rb', line 66 def call(population) population.parent_evolvables = population.selected_evolvables.empty? ? select_evolvables(population.evolvables) : population.selected_evolvables population.selected_evolvables = [] population.evolvables = [] population end |
#select_evolvables(evolvables) ⇒ Array<Evolvable>
Selects the best evolvables from the given collection. By default, selects the last N evolvables, where N is the selection size. This assumes evolvables are already sorted in the evaluation step, with the best at the end.
Override this method in a subclass to implement different selection strategies such as tournament selection or roulette wheel selection.
95 96 97 |
# File 'lib/evolvable/selection.rb', line 95 def select_evolvables(evolvables) evolvables.last(@size) end |