Class: Wallace::Operators::GaussianMutationOperator

Inherits:
Wallace::Operator show all
Defined in:
lib/operators/gaussian_mutation_operation.rb

Overview

The Gaussian mutation operator mutates the genes of an individual with a given probability (mutation rate)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Wallace::Operator

#produce

Constructor Details

#initialize(opts = {}) ⇒ GaussianMutationOperator

Constructs a new gaussian mutation operator.

Parameters:

  • opts, a hash of keyword options for this method. -> id, the unique identifier for this operator. -> inputs, an array of inputs (OperatorInput) to this operator. -> probability, the probability that a given bit should be mutated. (default = 0.01). -> distribution, the gaussian distribution which should be used. -> bounds, the bounds for all genes in the chromosome.



22
23
24
25
26
27
# File 'lib/operators/gaussian_mutation_operation.rb', line 22

def initialize(opts = {})
  super(opts)
  @probability = opts[:probability] || 0.01
  @distribution = opts[:distribution]
  @bounds = opts[:bounds]
end

Instance Attribute Details

#distributionObject

Allow the distribution to be dynamically adjusted.



11
12
13
# File 'lib/operators/gaussian_mutation_operation.rb', line 11

def distribution
  @distribution
end

#probabilityObject

Allow the mutation probability to be dynamically adjusted.



8
9
10
# File 'lib/operators/gaussian_mutation_operation.rb', line 8

def probability
  @probability
end

Instance Method Details

#operate(rng, inputs) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/operators/gaussian_mutation_operation.rb', line 29

def operate(rng, inputs)
  (0...inputs[0].length).each do |i|
    if rng.rand <= @probability
      inputs[0][i] += @distribution.sample(random: rng)
      inputs[0][i] = [@bounds.first, inputs[0][i], @bounds.last].sort[1] unless @bounds.nil?
    end
  end
  return inputs
end