Class: Selection::Truncation

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

Overview

The truncation selection method. This method selects only the best individuals from the population.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Truncation) initialize(ranker)

Set the ranker object (the instance of Ranking class) necessary for comparision of individuals.



10
11
12
13
14
# File '../lib/truncation.rb', line 10

def initialize ranker
  raise "Truncation: invalid Ranking object" unless ranker.kind_of? Ranking     
  @ranker = ranker
  @population = nil     
end

Instance Attribute Details

- (Object) population

The population to select from.



17
18
19
# File '../lib/truncation.rb', line 17

def population
  @population
end

Instance Method Details

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

Select N-best individuals from the population (N==how_much)



20
21
22
23
24
25
# File '../lib/truncation.rb', line 20

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

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

Select only the best individual from the population.



28
29
30
# File '../lib/truncation.rb', line 28

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