Class: Wallace::Operators::UniformMutationOperator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Wallace::Operator

#produce

Constructor Details

#initialize(opts = {}) ⇒ UniformMutationOperator

Constructs a new uniform 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. -> values, the range of values that this mutator should sample from (can be biased by using a weighted array). -> probability, the probability that a given bit should be mutated. (default = 0.01).



16
17
18
19
20
# File 'lib/operators/uniform_mutation_operation.rb', line 16

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

Instance Attribute Details

#probabilityObject

Allow the mutation probability to be dynamically adjusted.



6
7
8
# File 'lib/operators/uniform_mutation_operation.rb', line 6

def probability
  @probability
end

Instance Method Details

#operate(rng, inputs) ⇒ Object



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

def operate(rng, inputs)
  (0...inputs[0].length).each do |i|
    if rng.rand <= @probability
      inputs[0][i] = @values.sample(random: rng)
    end
  end
  return inputs
end