Class: Evolvable::Selection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(size: 2) ⇒ Selection

Initializes a new selection object.

Parameters:

  • size (Integer) (defaults to: 2)

    The number of instances to select from each generation to perform crossover and generate or "breed" the next generation. The default is 2.



48
49
50
# File 'lib/evolvable/selection.rb', line 48

def initialize(size: 2)
  @size = size
end

Instance Attribute Details

#sizeInteger

The number of evolvables to select as parents for the next generation.

Returns:

  • (Integer)

    The selection size



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.

Parameters:

Returns:



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.

Examples:

# A custom selection strategy using tournament selection
class TournamentSelection < Evolvable::Selection
  def select_evolvables(evolvables)
    Array.new(size) do
      # Randomly select 3 individuals and pick the best one
      evolvables.sample(3).max_by(&:fitness)
    end
  end
end

Parameters:

  • evolvables (Array<Evolvable>)

    The evolvables to select from

Returns:

  • (Array<Evolvable>)

    The selected evolvables to use as parents



95
96
97
# File 'lib/evolvable/selection.rb', line 95

def select_evolvables(evolvables)
  evolvables.last(@size)
end