Class: Selection::RankSampling

Inherits:
Object
  • Object
show all
Defined in:
../lib/rank_sampling.rb

Overview

“Stochastic Universal Sampling” selection using given Ranking instance. Selection operator which uses SUS selection applied on :proportion value of pre-ranked individuals.

See en.wikipedia.org/wiki/Stochastic_universal_sampling

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RankSampling) initialize(ranker)

Set the instance of the Ranking object which will be used for ranking the population before the selection.



16
17
18
19
20
21
# File '../lib/rank_sampling.rb', line 16

def initialize ranker
  raise "RankSampling: invalid Ranking object" unless ranker.kind_of? Ranking     
  @ranker = ranker
  @sampling = Sampling.new :proportion
  @population = nil     
end

Instance Attribute Details

- (Object) population

The population to select from.



24
25
26
# File '../lib/rank_sampling.rb', line 24

def population
  @population
end

Instance Method Details

- (Object) random=(rnd)

Set the source of randomness for the roulette, using Sampling#random=



27
28
29
# File '../lib/rank_sampling.rb', line 27

def random= rnd
  @sampling.random = rnd
end

- (Object) select(how_much, population = self.population)

Rank the population and then select individuals from it. The stochastic “slots” are :proportion-al. It can be specified how_much individuals will be selected.



33
34
35
36
37
38
# File '../lib/rank_sampling.rb', line 33

def select( how_much, population=self.population )
  ranked = @ranker.rank( population ) 
  @population = population
 
  @sampling.select( how_much, ranked ).map { |individual| individual.original }
end

- (Object) select_one(population = self.population)

Rank the population and then select one individual from it. The stochastic “slots” are :proportion-al.



42
43
44
# File '../lib/rank_sampling.rb', line 42

def select_one population=self.population
  select( 1, population ).first
end