Module InversionMutator
In: lib/charlie/permutation/permutation.rb

Inversion mutator for PermutationGenotype. Takes two random indices, and reverses the elements in between (includes possible wrapping if index2 < index1)

Methods

mutate!  

Public Instance methods

Inverts parts of the genes

[Source]

    # File lib/charlie/permutation/permutation.rb, line 37
37:   def mutate!
38:     i1, i2 = @genes.rand_index,@genes.rand_index 
39:     if i2 >= i1
40:       @genes[i1..i2] = @genes[i1..i2].reverse unless i1==i2
41:     else
42:       reversed = (@genes[i1..-1] + @genes[0..i2]).reverse
43:       @genes[i1..-1] = reversed.slice!(0,@genes.size-i1)
44:       @genes[0..i2] = reversed
45:     end
46:     self
47:   end

[Validate]