Class: Wallace::Operators::MergingCrossoverOperator

Inherits:
Wallace::Operator show all
Defined in:
lib/operators/merging_crossover_operation.rb

Overview

Merging crossover takes two parents as input and randomly merges their chromosomes into a dual-length chromosome, randomly choosing between each parent when adding each gene.

The merged chromosome is then split into two child chromosomes by using the first occurrence of each allele as the order of genes in the first child, and the second occurence as the order of genes in the second child.

Instance Method Summary collapse

Methods inherited from Wallace::Operator

#initialize, #produce

Constructor Details

This class inherits a constructor from Wallace::Operator

Instance Method Details

#operate(rng, inputs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/operators/merging_crossover_operation.rb', line 11

def operate(rng, inputs)

  # Rather than progressively building up the merged chromosome, we can quickly
  # add the two parent chromosomes together and shuffle the resulting sequence.
  merged_chromosome = (inputs[0] + inputs[1]).shuffle(random: rng)

  # Split the merged chromosome into two child chromosomes.
  # We check whether it is the first occurence of a given allele by seeing if the
  # allele exists within the first child's chromosome.
  child_chromosomes = [ [], [] ]
  merged_chromosome.each { |g| child_chromosomes[(child_chromosomes[0].include? g) ? 1 : 0] << g }

  return child_chromosomes

end