Module: InsertionMutator

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

Overview

Takes a random element of the permutation, and inserts it at a random position.

  • Example: [1 2 3 4 5] to [1 4 2 3 5]

Instance Method Summary collapse

Instance Method Details

#mutate!Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/charlie/permutation/permutation.rb', line 54

def mutate!
  from, to = @genes.rand_index, @genes.rand_index
  @genes = if to >= from
    to += 1  # add end of array as possibility 
    (@genes[0...from] + @genes[from+1...to] << @genes[from])  + @genes[to..-1]
  else
    (@genes[0...to] << @genes[from]) + @genes[to...from] + @genes[from+1..-1]
  end
  self
end