Class: Wallace::Operators::GaussianMutationOperator
- Inherits:
-
Wallace::Operator
- Object
- Wallace::Operator
- Wallace::Operators::GaussianMutationOperator
- 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
-
#distribution ⇒ Object
Allow the distribution to be dynamically adjusted.
-
#probability ⇒ Object
Allow the mutation probability to be dynamically adjusted.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ GaussianMutationOperator
constructor
Constructs a new gaussian mutation operator.
- #operate(rng, inputs) ⇒ Object
Methods inherited from Wallace::Operator
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
#distribution ⇒ Object
Allow the distribution to be dynamically adjusted.
11 12 13 |
# File 'lib/operators/gaussian_mutation_operation.rb', line 11 def distribution @distribution end |
#probability ⇒ Object
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 |