Class: Evolvable::PointCrossover
- Inherits:
-
Object
- Object
- Evolvable::PointCrossover
- Defined in:
- lib/evolvable/point_crossover.rb
Overview
A classic genetic algorithm strategy that performs single or multi-point crossover by selecting random positions in the genome and swapping gene segments between parents.
- Single-point crossover (default): Swaps all genes after a randomly chosen position.
- Multi-point crossover: Alternates segments between multiple randomly chosen points.
Best for:
- Preserving beneficial gene blocks
- Problems where related traits are located near each other
Set your population to use this strategy during initialization with:
population = MyEvolvable.new_population(
combination: Evolvable::PointCrossover.new(points_count: 2)
)
Or update an existing population:
population.combination = Evolvable::PointCrossover.new(points_count: 3)
Instance Attribute Summary collapse
-
#points_count ⇒ Object
Returns the value of attribute points_count.
Instance Method Summary collapse
- #call(population) ⇒ Object
-
#initialize(points_count: 1) ⇒ PointCrossover
constructor
A new instance of PointCrossover.
- #new_evolvables(population, count) ⇒ Object
Constructor Details
#initialize(points_count: 1) ⇒ PointCrossover
Returns a new instance of PointCrossover.
31 32 33 |
# File 'lib/evolvable/point_crossover.rb', line 31 def initialize(points_count: 1) @points_count = points_count end |
Instance Attribute Details
#points_count ⇒ Object
Returns the value of attribute points_count.
35 36 37 |
# File 'lib/evolvable/point_crossover.rb', line 35 def points_count @points_count end |
Instance Method Details
#call(population) ⇒ Object
37 38 39 40 |
# File 'lib/evolvable/point_crossover.rb', line 37 def call(population) population.evolvables = new_evolvables(population, population.size) population end |
#new_evolvables(population, count) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/evolvable/point_crossover.rb', line 42 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 |