Class: Wallace::BreedingGraph

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

Defined Under Namespace

Classes: InputNode, Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BreedingGraph

Constructs a new BreedingGraph.

Parameters:

  • opts, a hash of keyword options. -> outputs, an array of terminal nodes in this breeding graph (can be weighted). -> buffered, flag indicating whether the process should be buffered.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/core/breeding_graph.rb', line 13

def initialize(opts = {})

  @outputs = opts[:outputs]
  @buffered = opts[:buffered] || true

  # Produce a list of nodes in the graph and find all inputs to the graph.
  @nodes = []
  @inputs = []
  queue = @outputs.clone

  until queue.empty?
    node = queue.shift
    @nodes << node
    if node.is_a? Wallace::BreedingGraph::InputNode
      @inputs << node
    else
      queue += node.inputs.map{ |i| i.source }
    end
  end

end

Instance Attribute Details

#inputsObject (readonly)

Returns the value of attribute inputs.



3
4
5
# File 'lib/core/breeding_graph.rb', line 3

def inputs
  @inputs
end

#nodesObject (readonly)

Returns the value of attribute nodes.



3
4
5
# File 'lib/core/breeding_graph.rb', line 3

def nodes
  @nodes
end

#outputsObject (readonly)

Returns the value of attribute outputs.



3
4
5
# File 'lib/core/breeding_graph.rb', line 3

def outputs
  @outputs
end

Instance Method Details

#breed!(rng, buffer, range) ⇒ Object

Breeds a portion of the associated given sub-population for the next generation.

Parameters:

  • rng, the RNG to use when breeding individuals.

  • buffer, the buffer for the contents of the sub-population of the next generation.

  • range, the range of indices to be populated in the buffer by newly bred individuals.



47
48
49
# File 'lib/core/breeding_graph.rb', line 47

def breed!(rng, buffer, range)
  range.each { |i| buffer[i] = @outputs.sample(random: rng).take!(rng) }
end

#clean!Object

Discards all temporary information regarding the last breeding cycle and clears all the buffers.



37
38
39
# File 'lib/core/breeding_graph.rb', line 37

def clean!
  @nodes.each { |n| n.clean! }
end

#cloneObject

Creates a clone of this breeding graph.



52
53
54
55
56
57
# File 'lib/core/breeding_graph.rb', line 52

def clone
  Wallace::BreedingGraph.new(
    outputs: @outputs.map { |n| n.clone },
    buffered: @buffered
  )
end