Class: EvoSynth::Mutations::EfficientBinaryMutation

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

Overview

This mutation is basically a optimized version of the BinaryMutation and should have a better performance on long genomes.

It flips each gene in the genome of a given individual using the given flip function (a lambda) with a given probability and returns a mutated individual. To use this mutation the flip_function has to return the negation/inverse of a given gene. This mutations is based on EFFIZIENTE-BINAERE-MUTATION (Weicker 2007, page 130).

Constant Summary collapse

DEFAULT_PROBABILITY =

the default mutation probability

0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flip_function, probability = DEFAULT_PROBABILITY) ⇒ EfficientBinaryMutation

:call-seq: EfficientBinaryMutation.new(Lambda) -> EfficientBinaryMutation EfficientBinaryMutation.new(Lambda, Float) -> EfficientBinaryMutation (overrides default probability)

Returns a new EfficientBinaryMutation. In the first form, the default mutation probability BinaryMutation::DEFAULT_PROBABILITY (0.1) is used. In the second it creates a BinaryMutation with the given probability.

custom_flip_function = lambda { |gene| EvoSynth.rand(42 * gene) }
EfficientBinaryMutation.new(custom_flip_function)
EfficientBinaryMutation.new(EvoSynth::Mutations::Functions::FLIP_BOOLEAN, 0.01)


59
60
61
62
# File 'lib/evosynth/operators/mutations/efficient_binary_mutation.rb', line 59

def initialize(flip_function, probability = DEFAULT_PROBABILITY)
  @flip_function = flip_function
  @probability = probability
end

Instance Attribute Details

#flip_functionObject

This function is used to flip each gene



42
43
44
# File 'lib/evosynth/operators/mutations/efficient_binary_mutation.rb', line 42

def flip_function
  @flip_function
end

#probabilityObject

Each gene is flipped with this probability (should be between 0 and 1)



38
39
40
# File 'lib/evosynth/operators/mutations/efficient_binary_mutation.rb', line 38

def probability
  @probability
end

Instance Method Details

#mutate(individual) ⇒ Object

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

Returns the mutation (with the given flip function) of a given individual.

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


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/evosynth/operators/mutations/efficient_binary_mutation.rb', line 72

def mutate(individual)
  mutated = individual.deep_clone
  @next_index = EvoSynth.rand(mutated.genome.size) unless defined? @next_index

  while @next_index < mutated.genome.size
    if @flip_function.arity == 1
      mutated.genome[@next_index] = @flip_function.call(mutated.genome[@next_index])
    else
      mutated.genome[@next_index] = @flip_function.call
    end
    
    @next_index += (Math.log(EvoSynth.rand) / Math.log(1 - @probability)).ceil
  end

  @next_index -= mutated.genome.size
  mutated
end

#to_sObject

:call-seq: to_s -> string

Returns description of this mutation

m = EfficientBinaryMutation.new(0.91)
m.to_s                   #=> "efficient binary muation <probability: 0.01>"


98
99
100
# File 'lib/evosynth/operators/mutations/efficient_binary_mutation.rb', line 98

def to_s
  "efficient binary muation <probability: #{@probability}>"
end