Class: Evolvable::PointCrossover

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

Overview

Supports single and multi-point crossover. The default is single-point crossover via a points_count of 1 which can be changed on an existing population (population.crossover.points_count = 5) or during initialization (Evolvable::PointCrossover.new(5))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points_count: 1) ⇒ PointCrossover

Returns a new instance of PointCrossover.


11
12
13
# File 'lib/evolvable/point_crossover.rb', line 11

def initialize(points_count: 1)
  @points_count = points_count
end

Instance Attribute Details

#points_countObject

Returns the value of attribute points_count.


15
16
17
# File 'lib/evolvable/point_crossover.rb', line 15

def points_count
  @points_count
end

Instance Method Details

#call(population) ⇒ Object


17
18
19
20
# File 'lib/evolvable/point_crossover.rb', line 17

def call(population)
  population.evolvables = new_evolvables(population, population.size)
  population
end

#new_evolvables(population, count) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/evolvable/point_crossover.rb', line 22

def new_evolvables(population, count)
  parent_genome_cycle = population.new_parent_genome_cycle
  evolvables = []
  loop do
    genome_1, genome_2 = parent_genome_cycle.next
    crossover_genomes(genome_1, genome_2).each do |genome|
      evolvable = population.new_evolvable(genome: genome)
      evolvables << evolvable
      return evolvables if evolvable.generation_index == count
    end
  end
end