Class: Wallace::Selectors::RouletteSelector

Inherits:
Wallace::Selector show all
Defined in:
lib/selectors/roulette_selector.rb

Overview

NOT READY!

Instance Method Summary collapse

Methods inherited from Wallace::Selector

#produce

Instance Method Details

#prepare(rng, candidates) ⇒ Object

Precomputes the probability distribution prior to selection.



7
8
9
10
11
12
13
# File 'lib/selectors/roulette_selector.rb', line 7

def prepare(rng, candidates)
  sum_fitness = candidates.reduce(0) {|sum, ind| sum += 1.0 / ind.fitness.value.to_f }
  distribution = Array.new(candidates.length) do |i|
    (1.0 / candidates[i].fitness.value.to_f) / sum_fitness
  end
  @candidates = Wallace::Utility::ScaledArray.new(candidates, distribution)
end

#select(rng) ⇒ Object

Selects an individual at random from the list of candidates.

Arguments

  • rng, random number generator to use to select index of individual.



19
20
21
# File 'lib/selectors/roulette_selector.rb', line 19

def select(rng)
  return @candidates.sample(random: rng)
end