Class: Wallace::Operators::UniformCrossoverOperator
- Inherits:
-
Wallace::Operator
- Object
- Wallace::Operator
- Wallace::Operators::UniformCrossoverOperator
- Defined in:
- lib/operators/uniform_crossover_operation.rb
Overview
Uniform crossover takes two individuals as parents, A and B, and produces two children, C and D, by swapping genes according to the mixing ratio probability. Using a mixing ratio of 0.5 should result in individuals which share half of their genes with a given parent.
Instance Attribute Summary collapse
-
#ratio ⇒ Object
Allow the mixing ratio to be dynamically adjusted.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ UniformCrossoverOperator
constructor
Constructs a new uniform crossover operator.
- #operate(rng, inputs) ⇒ Object
Methods inherited from Wallace::Operator
Constructor Details
#initialize(opts = {}) ⇒ UniformCrossoverOperator
Constructs a new uniform crossover 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. -> ratio, the mixing ratio to use during crossover. (default = 0.5).
18 19 20 21 |
# File 'lib/operators/uniform_crossover_operation.rb', line 18 def initialize(opts = {}) super(opts) @ratio = opts[:ratio] || 0.5 end |
Instance Attribute Details
#ratio ⇒ Object
Allow the mixing ratio to be dynamically adjusted.
9 10 11 |
# File 'lib/operators/uniform_crossover_operation.rb', line 9 def ratio @ratio end |
Instance Method Details
#operate(rng, inputs) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/operators/uniform_crossover_operation.rb', line 23 def operate(rng, inputs) (0...inputs[0].length).each do |i| inputs[0][i], inputs[1][i] = inputs[1][i], inputs[0][i] if rng.rand <= @ratio end return inputs end |