Class: Wallace::Operator

Inherits:
Object
  • Object
show all
Defined in:
lib/core/operator.rb

Overview

Operators are used to produce individuals for the next generation using members of the current population as their inputs.

TODO: Feed state data into the operator (size of population, fitness, generations, etc).

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Operator

Constructs an instance of an Operator.

Parameters:

  • opts, a hash of keyword options for this method. -> id, the unique identifier for this operator.



18
19
20
# File 'lib/core/operator.rb', line 18

def initialize(opts = {})
  @id = opts[:id]
end

Class Attribute Details

.nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/core/operator.rb', line 10

def name
  @name
end

Instance Method Details

#operate(rng, inputs) ⇒ Object

Performs this genetic operation on the genetic data of a given number of individuals to return the genetic data for their offspring.

Unlike ‘produce’, this method operates only on the genetic data (we refrain from the term chromosome since an individual may have multiple chromosomes).

Parameters:

  • rng, the RNG to use within this operation.

  • inputs, input individuals to the operation.

Returns: An array containing the genetic data for each produced offspring.

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/core/operator.rb', line 50

def operate(rng, inputs)
  raise NotImplementedError, "No 'operate' function was implemented by this operator."
end

#produce(rng, inputs) ⇒ Object

Produces an arbitrary number of individuals according to the rules of this operator using

Parameters:

  • rng, random number generator to use within the operation.

  • inputs, input individuals to the operation.

Returns: An array of the offspring individuals produced by this operation.



31
32
33
34
35
36
# File 'lib/core/operator.rb', line 31

def produce(rng, inputs)
  operate(rng, inputs.map { |i| i.data }).each_with_index do |data, i|
    inputs[i].data = data
  end
  return inputs
end