Class: Wallace::Operators::SpliceCrossoverOperator
- Inherits:
-
Wallace::Operator
- Object
- Wallace::Operator
- Wallace::Operators::SpliceCrossoverOperator
- Defined in:
- lib/operators/splice_crossover_operation.rb
Overview
The splice crossover operator takes two parents (which may have different length genomes), splits each into two substrings at random points (using a different point for each parent) before combining the four substrings into a pair of two substrings at random to produce two 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
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/operators/splice_crossover_operation.rb', line 8 def operate(rng, inputs) # Do nothing if either string is shorter than length 2. return inputs if inputs[0].length <= 1 or inputs[1].length <= 1 # Cut the two strings into four strings at a random # point such that the recombination of any two strings will result in # a legal string (a string whose length does not exceed the bounds). x, y = rng.rand(1...inputs[0].length), rng.rand(1...inputs[1].length) strings = [ inputs[0][0...x], inputs[1][0...y], inputs[0][x...inputs[0].length], inputs[1][y...inputs[1].length] ] # given any A, B, C and D, it is given that: # min <= #(A+B) <= max # min <= #(C+D) <= max # we want to find an A, B, C, D, such that: # min <= #(A+D) <= max # min <= #(C+B) <= max # Randomly re-attach the four strings into two strings # and return the resulting individuals. strings.shuffle!(random: rng) return [ strings.pop + strings.pop, strings.pop + strings.pop ] end |