Class: Wallace::Operators::PointMutationOperator

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

Overview

Point mutation selects a prespecified number of points of a provided chromosome at random and replaces them with a value sampled from the provided distribution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Wallace::Operator

#produce

Constructor Details

#initialize(opts = {}) ⇒ PointMutationOperator

Constructs a new point 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). -> points, the number of points which should be mutated. (default = 1).



18
19
20
21
22
# File 'lib/operators/point_mutation_operation.rb', line 18

def initialize(opts = {})
  super(opts)
  @values = opts[:values] # could gather this information from the species?
  @points = opts[:points] || 1
end

Instance Attribute Details

#pointsObject

Allow the number of points to be dynamically adjusted.



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

def points
  @points
end

Instance Method Details

#operate(rng, inputs) ⇒ Object



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

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