Class: EvoSynth::Mutations::OneGeneFlipping

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

Overview

This mutations flips (inverts) one gene in the genome of a given individual using the given flip function (a lambda) 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 EIN-BIT-BINAERE-MUTATION (Weicker 2007, page 48).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flip_function) ⇒ OneGeneFlipping

:call-seq: OneGeneFlipping.new(Lambda) -> OneGeneFlipping

Returns a new OneGeneFlipping.

custom_flip_function = lambda { |gene| EvoSynth.rand(42 * gene) }
OneGeneFlipping.new(custom_flip_function)


47
48
49
# File 'lib/evosynth/operators/mutations/one_gene_flipping.rb', line 47

def initialize(flip_function)
	@flip_function = flip_function
end

Instance Attribute Details

#flip_functionObject

This function is used to flip each gene



37
38
39
# File 'lib/evosynth/operators/mutations/one_gene_flipping.rb', line 37

def flip_function
  @flip_function
end

Instance Method Details

#mutate(individual) ⇒ Object

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

Returns the mutation (one gene will be flipped with the given flip function) of a given individual.

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


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/evosynth/operators/mutations/one_gene_flipping.rb', line 59

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

	rand_index = EvoSynth.rand(genome.size)
	if @flip_function.arity == 1
		genome[rand_index] = @flip_function.call(genome[rand_index])
	else
		genome[rand_index] = @flip_function.call
	end

	mutated
end

#to_sObject

:call-seq: to_s -> string

Returns description of this mutation

m = OneGeneFlipping.new
m.to_s                   #=> "one-gene-flipping"


81
82
83
# File 'lib/evosynth/operators/mutations/one_gene_flipping.rb', line 81

def to_s
	"one-gene-flipping"
end