Class: Wallace::BreedingGraph
- Inherits:
-
Object
- Object
- Wallace::BreedingGraph
- Defined in:
- lib/core/breeding_graph.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
Instance Method Summary collapse
-
#breed!(rng, buffer, range) ⇒ Object
Breeds a portion of the associated given sub-population for the next generation.
-
#clean! ⇒ Object
Discards all temporary information regarding the last breeding cycle and clears all the buffers.
-
#clone ⇒ Object
Creates a clone of this breeding graph.
-
#initialize(opts = {}) ⇒ BreedingGraph
constructor
Constructs a new BreedingGraph.
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
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
3 4 5 |
# File 'lib/core/breeding_graph.rb', line 3 def inputs @inputs end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
3 4 5 |
# File 'lib/core/breeding_graph.rb', line 3 def nodes @nodes end |
#outputs ⇒ Object (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 |
#clone ⇒ Object
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 |