Class: Wallace::Operators::SwapMutationOperator

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

Overview

The swap mutation operator swaps pairs of consecutive genes according to an associated probability.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Wallace::Operator

#produce

Constructor Details

#initialize(opts = {}) ⇒ SwapMutationOperator

Constructs a new swap 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 pair (of consecutive genes) should be swapped. (default = 0.05).



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

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

Instance Attribute Details

#probabilityObject

Allow the swap probability to be dynamically adjusted.



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

def probability
  @probability
end

Instance Method Details

#operate(rng, inputs) ⇒ Object



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

def operate(rng, inputs)
    (0...inputs[0].length).each_cons(2) do |g1, g2|
      inputs[0][g1], inputs[0][g2] = inputs[0][g2], inputs[0][g1] if rng.rand <= @probability
    end
    return inputs
end