Class: Selection::Sampling
Overview
Stochastic Universal Sampling selection method. The probability of the individual selection is proportional to some (usually fitness) non-negative value (as in Roulette selection). However, more individuals can be selected at once by the single run of a wheel, which brings a better spread of the results (in comparision with Roulette method).
Instance Attribute Summary
Attributes inherited from Roulette
#population, #proportional_by, #random
Attributes included from SelectMore
Instance Method Summary (collapse)
-
- (Sampling) initialize(proportional_by = nil, &block)
constructor
Set the proportional_by or the block for obtaining invividual’s proportion.
-
- (Object) select(how_much, population = self.population)
Select individuals from the population.
-
- (Object) select_one(population = self.population)
Select one individual from the population (same as Roulette#select_one, but a bit less effective).
Constructor Details
- (Sampling) initialize(proportional_by = nil, &block)
Set the proportional_by or the block for obtaining invividual’s proportion. See Roulette#proportional_by
17 18 19 |
# File '../lib/sampling.rb', line 17 def initialize( proportional_by=nil, &block ) super end |
Instance Method Details
- (Object) select(how_much, population = self.population)
Select individuals from the population. It can be specified how_much individuals will be selected.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File '../lib/sampling.rb', line 23 def select( how_much, population=self.population ) raise "Sampling: cannot select from an empty population" if population.empty? raise "Sampling: cannot select more than population.size" if how_much > population.size return [] if how_much == 0 @sum,@wheel = wheel_core population @population = population step = @sum.to_f/how_much ballot = step * @random.rand width = 0.0 winners = [] @wheel.each_with_index do |slot,index| width += slot.width next if ballot > width winners.push slot.original ballot += step end winners end |
- (Object) select_one(population = self.population)
Select one individual from the population (same as Roulette#select_one, but a bit less effective).
47 48 49 |
# File '../lib/sampling.rb', line 47 def select_one population=self.population select( 1, population ).first end |