Class: Wallace::Operators::OnePointCrossoverOperator
- Inherits:
-
Wallace::Operator
- Object
- Wallace::Operator
- Wallace::Operators::OnePointCrossoverOperator
- Defined in:
- lib/operators/one_point_crossover_operation.rb
Overview
One-point crossover (also known as single-point crossover) takes two individuals, A and B, as parents, and produces two children, formed by the combination of sub-strings from A and B.
Firstly a single crossover point, common to the two individuals is selected, effectively splitting each parent, A and B, into two substrings (A1, A2; B1, B2). The resulting substrings (A1+B2 and B1+A2) are combined to form two (potentially) unique children.
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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/operators/one_point_crossover_operation.rb', line 11 def operate(rng, inputs) a, b = individuals # Ensure that the individuals are longer than length 1! return inputs if inputs[0].length <= 1 or inputs[1].length <= 1 # Calculate the crossover point, split A and B into four substrings # and combine those substrings to form two children. x = rng.rand((1...([inputs[0].length, inputs[1].length].min))) return [ inputs[0][0...x] + inputs[0][x...inputs[0].length], inputs[1][0...x] + inputs[1][x...inputs[1].length] ] end |