Class: EvoSynth::Mutations::MixingMutation

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

Overview

The MixingMutation shuffles the order of a random range of genes in the genome. It is based on MISCHENDE-MUTATION (Weicker 2007, page 132).

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 shuffles 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
57
58
# File 'lib/evosynth/operators/mutations/mixing_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_one, index_two = index_two, index_one if index_one > index_two
  return mutated if index_one == index_two

  subsection = genome[index_one..index_two]
  subsection.sort! { EvoSynth.rand(2) }
  genome[index_one..index_two] = subsection

  mutated
end

#to_sObject

:call-seq: to_s -> string

Returns description of this mutation

m = MixingMutation.new
m.to_s                   #=> "mixing muation"


68
69
70
# File 'lib/evosynth/operators/mutations/mixing_mutation.rb', line 68

def to_s
  "mixing muation"
end