Class: Wallace::BreedingGraph::Node

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

Overview

Represents a node within the breeding graph.

Each node corresponds to a stage within the breeding process and is associated with a given operator or selection method which is used on the inputs to that node to produce some individuals for the next generation or for further processing by successive nodes in the graph.

Direct Known Subclasses

InputNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function, inputs) ⇒ Node

Constructs a node for the breeding graph.

Parameters:

  • function, the operator or selection method associated to this node.

  • inputs, an array of inputs to this node.



17
18
19
20
21
# File 'lib/core/breeding_graph/node.rb', line 17

def initialize(function, inputs)
  @inputs = inputs
  @function = function
  @buffer = []
end

Instance Attribute Details

#functionObject (readonly)

Returns the value of attribute function.



9
10
11
# File 'lib/core/breeding_graph/node.rb', line 9

def function
  @function
end

#inputsObject (readonly)

Returns the value of attribute inputs.



9
10
11
# File 'lib/core/breeding_graph/node.rb', line 9

def inputs
  @inputs
end

Instance Method Details

#clean!Object

Clears the buffer of this node and discards any temporary information relating to the breeding processs.



40
41
42
# File 'lib/core/breeding_graph/node.rb', line 40

def clean!
  @buffer.clear
end

#cloneObject

Creates a clone of this node and its inputs.



45
46
47
48
49
50
# File 'lib/core/breeding_graph/node.rb', line 45

def clone
  Wallace::BreedingGraph::Node.new(
    @function,
    @inputs.map { |i| Wallace::BreedingGraph::NodeInput.new(i.source.clone, i.size) }
  )
end

#take!(rng) ⇒ Object

Takes and returns a single individual produced (or selected) by this node.

If there are no individuals held in the buffer then a number are produced (or selected) using the operator (or selection method) attached to this node.

Parameters:

  • rng, the RNG to use if new individuals are bred.

Returns:

An array of individuals produced at this node.


33
34
35
36
# File 'lib/core/breeding_graph/node.rb', line 33

def take!(rng)
  @buffer += @function.produce(rng, generate_inputs!(rng)) if @buffer.empty?
  return @buffer.pop
end