Class: Wallace::Koza::Builder::HalfBuilder
- Inherits:
-
Wallace::Koza::Builder
- Object
- Wallace::Koza::Builder
- Wallace::Koza::Builder::HalfBuilder
- Defined in:
- lib/modules/koza/builder/half_builder.rb
Overview
The Half Builder is a (slightly modified) implementation of Koza’s Ramped Half-and-Half builder.
It works by selecting a random integer d, between the minimum and maximum depth inclusively. probability_grow of the time the grow method is used to generate a tree of up to depth d using the GROW builder. (1-probability_grow) of the time the FULL method is used to generate trees of exactly depth d.
Instance Attribute Summary collapse
-
#probability_grow ⇒ Object
readonly
The probability that the GROW method will be used to generate a tree.
-
#probability_terminal ⇒ Object
The probability that a terminal will be selected when constructing a node using the GROW method.
Attributes inherited from Wallace::Koza::Builder
#depth_limits, #non_terminals, #terminals
Instance Method Summary collapse
-
#build_subtree(depth, opts = {}) ⇒ Object
Builds a new subtree of a maximum given depth using either the GROW or FULL method.
-
#build_tree(opts = {}) ⇒ Object
Builds a new Koza Tree of depth d, where d is within the allowed minimum and maximum tree depths, using either the GROW or FULL method.
-
#initialize(probability_grow, probability_terminal) ⇒ HalfBuilder
constructor
Constructs a new HalfBuilder.
-
#probability_full ⇒ Object
Returns the probability that the FULL method will be used to generate a tree.
Methods inherited from Wallace::Koza::Builder
Constructor Details
#initialize(probability_grow, probability_terminal) ⇒ HalfBuilder
Constructs a new HalfBuilder.
Parameters:
-
probability_grow, the probability that the GROW method will be used when generating a tree.
-
probability_terminal, the probability that a terminal node will be built when using the GROW method.
20 21 22 23 |
# File 'lib/modules/koza/builder/half_builder.rb', line 20 def initialize(probability_grow, probability_terminal) @probability_grow = probability_grow @probability_terminal = probability_terminal end |
Instance Attribute Details
#probability_grow ⇒ Object (readonly)
The probability that the GROW method will be used to generate a tree.
10 11 12 |
# File 'lib/modules/koza/builder/half_builder.rb', line 10 def probability_grow @probability_grow end |
#probability_terminal ⇒ Object
The probability that a terminal will be selected when constructing a node using the GROW method.
13 14 15 |
# File 'lib/modules/koza/builder/half_builder.rb', line 13 def probability_terminal @probability_terminal end |
Instance Method Details
#build_subtree(depth, opts = {}) ⇒ Object
Builds a new subtree of a maximum given depth using either the GROW or FULL method.
Parameters:
-
depth, the (maximum) depth of the subtree.
-
opts, a hash of keyword options for this method. -> random, the RNG to use for the process.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/modules/koza/builder/half_builder.rb', line 49 def build_subtree(depth, opts = {}) opts[:random] ||= Random.new return Wallace::Koza::Builder::GrowBuilder.grow_subtree( @terminals, @non_terminals, depth, @probability_terminal, random: opts[:random] ) if @probability_grow <= opts[:random].rand return Wallace::Koza::Builder::FullBuilder.full_subtree( @terminals, @non_terminals, depth, random: opts[:random] ) end |
#build_tree(opts = {}) ⇒ Object
Builds a new Koza Tree of depth d, where d is within the allowed minimum and maximum tree depths, using either the GROW or FULL method.
Parameters:
-
opts, a hash of keyword options for this method. -> random, the RNG to use for the process.
Returns: A new Koza tree.
39 40 41 |
# File 'lib/modules/koza/builder/half_builder.rb', line 39 def build_tree(opts = {}) Wallace::Koza::KozaTree.new(build_subtree(@depth_limits.sample, random: opts[:random])) end |
#probability_full ⇒ Object
Returns the probability that the FULL method will be used to generate a tree.
26 27 28 |
# File 'lib/modules/koza/builder/half_builder.rb', line 26 def probability_full 1.0 - @probability_grow end |