Class: EvoSynth::Mutations::InversionMutation

Inherits:
Object
  • Object
show all
Defined in:
lib/evosynth/operators/mutations/inversion_mutation.rb

Overview

The InversionMutation inverts the order of a random range of genes in the genome. It is based on INVERTIERENDE-MUTATION (Weicker 2007, page 28).

This mutations does not destroy permutations.

Instance Method Summary collapse

Instance Method Details

#mutate(individual) ⇒ Object

:call-seq: mutate(Individual) -> Individual

Returns a clone of a given individual and inverts the order of a random range of genes in the genome of this clone.

m = InversionMutation.new
m.mutate(a_individual)   #=> a_new_individual


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/evosynth/operators/mutations/inversion_mutation.rb', line 44

def mutate(individual)
  mutated = individual.deep_clone
  genome = mutated.genome

  index_one = EvoSynth.rand(genome.size)
  index_two = EvoSynth.rand(genome.size)
  index_two = EvoSynth.rand(genome.size) while index_two == index_one
  index_one, index_two = index_two, index_one if index_one > index_two

  genome[index_one..index_two] = genome[index_one..index_two].reverse

  mutated
end

#to_sObject

:call-seq: to_s -> string

Returns description of this mutation

m = Identity.new
m.to_s                   #=> "exchange mutation"


66
67
68
# File 'lib/evosynth/operators/mutations/inversion_mutation.rb', line 66

def to_s
  "exchange mutation"
end