Class: Wallace::Operators::OrderCrossoverOperator
- Inherits:
-
Wallace::Operator
- Object
- Wallace::Operator
- Wallace::Operators::OrderCrossoverOperator
- Defined in:
- lib/operators/order_crossover_operation.rb
Overview
The order crossover operator takes two parents as input and creates two offspring.
Two random crossover points on the chromosome (the same for each parent) are selected and the genes at all loci in between are carried to each child (i.e. P0 -> C0, P1 -> C1).
The remaining genes of each child are determined by inserting all genes that are not covered within their central subsequence (copied from the parent) in the order that they appear in the other parent. Once the order of genes has been calculated, the genes are inserted after the second crossover point and are wrapped around the start and to the first crossover point.
Instance Method Summary collapse
Methods inherited from Wallace::Operator
Constructor Details
This class inherits a constructor from Wallace::Operator
Instance Method Details
#operate(rng, inputs) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/operators/order_crossover_operation.rb', line 14 def operate(rng, inputs) # Select two random crossover points. # All genes between these points are left untouched. len = inputs[0].length i, j = Array.new(2) { rng.rand(len - 1) }.sort # Find the alleles that are not within the selected subsequence of each # parent in the order of the other parents chromosome. insert = Array.new(2) do |input| inputs[(input+1)%2].select { |g| inputs[input][i..j].exclude? g } end # Inject the alleles in the gathered order starting at the second # crossover point before wrapping around to the first. (0..1).each do |input| inputs[input][j+1...len] = insert[input].shift(len-j-1) inputs[input][0...i] = insert[input] end return inputs end |