Module: Algorithm::Genetic::Mutation::Shift

Defined in:
lib/algorithm/genetic/mutation/shift.rb

Instance Method Summary collapse

Instance Method Details

#mutate(code) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/algorithm/genetic/mutation/shift.rb', line 4

def mutate(code)
  index = (rand * code.length).to_i
  direction = rand <= 0.5 ? -1 : 1
  begin
    new_char = (code[index].ord + direction).chr
  rescue
    return(code)
  end
  code[index] = new_char
  return code
end