Class: Wallace::Operators::HalfUniformCrossoverOperator

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

Overview

Half uniform crossover takes two individuals as parents, A and B, and produces two children, C and D, by swapping exactly half of their non-matching bits.

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/operators/half_uniform_crossover_operation.rb', line 7

def operate(rng, inputs)

  # Find all indices where bits do not match between A and B.
  indices = []
  (0...inputs[0].length).each do |i|
    indices << i if inputs[0][i] != inputs[1][i]
  end

  # Swap exactly half of the non-matching bits.
  indices.shuffle(random: rng).first((indices.length/2).to_i).each do |i|
    inputs[0][i], inputs[1][i] = inputs[1][i], inputs[0][i]
  end

  return inputs

end