Class: Wallace::Operators::SwapMutationOperator
- Inherits:
-
Wallace::Operator
- Object
- Wallace::Operator
- Wallace::Operators::SwapMutationOperator
- 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
-
#probability ⇒ Object
Allow the swap probability to be dynamically adjusted.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ SwapMutationOperator
constructor
Constructs a new swap mutation operator.
- #operate(rng, inputs) ⇒ Object
Methods inherited from Wallace::Operator
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
#probability ⇒ Object
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 |