Class: Evolvable::Selection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/evolvable/selection.rb

Overview

The selection object assumes that a population's evolvables have already been sorted by the evaluation object. It selects "parent" evolvables to undergo combination and thereby produce the next generation of evolvables.

Only two evolvables are selected as parents for each generation by default. The selection size is configurable.

Examples:

# TODO: Show how to add/change population's selection object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: 2) ⇒ Selection

Initializes a new selection object.

Keyword arguments:

size

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



29
30
31
# File 'lib/evolvable/selection.rb', line 29

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

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



33
34
35
# File 'lib/evolvable/selection.rb', line 33

def size
  @size
end

Instance Method Details

#call(population) ⇒ Object



35
36
37
38
39
# File 'lib/evolvable/selection.rb', line 35

def call(population)
  population.parent_evolvables = select_evolvables(population.evolvables)
  population.evolvables = []
  population
end

#select_evolvables(evolvables) ⇒ Object



41
42
43
# File 'lib/evolvable/selection.rb', line 41

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