Module: InversionMutator

Defined in:
lib/charlie/permutation/permutation.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#mutate!Object

Inverts parts of the genes



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/charlie/permutation/permutation.rb', line 37

def mutate!
  i1, i2 = @genes.rand_index,@genes.rand_index 
  if i2 >= i1
    @genes[i1..i2] = @genes[i1..i2].reverse unless i1==i2
  else
    reversed = (@genes[i1..-1] + @genes[0..i2]).reverse
    @genes[i1..-1] = reversed.slice!(0,@genes.size-i1)
    @genes[0..i2] = reversed
  end
  self
end