Class: Newral::Genetic::Tree
- Inherits:
-
Object
- Object
- Newral::Genetic::Tree
- Defined in:
- lib/newral/genetic/tree.rb
Constant Summary collapse
- OPERANDS =
{ '+' => 2, '*' => 2, '/' => 2, '-' => 2, 'pow' => 2, 'sqrt' => 1 }
Instance Attribute Summary collapse
-
#left_child ⇒ Object
readonly
Returns the value of attribute left_child.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#right_child ⇒ Object
readonly
Returns the value of attribute right_child.
-
#sub_node_count ⇒ Object
readonly
Returns the value of attribute sub_node_count.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #eval ⇒ Object
-
#initialize(parent: nil, value: nil, right_child: nil, left_child: nil) ⇒ Tree
constructor
A new instance of Tree.
- #node_count ⇒ Object
- #set_child_trees(left_child: nil, right_child: nil, force: false) ⇒ Object
- #update_node_count ⇒ Object
Constructor Details
#initialize(parent: nil, value: nil, right_child: nil, left_child: nil) ⇒ Tree
Returns a new instance of Tree.
14 15 16 17 18 19 20 |
# File 'lib/newral/genetic/tree.rb', line 14 def initialize( parent: nil, value:nil, right_child: nil, left_child: nil ) @parent = parent @left_child = left_child @right_child = right_child update_node_count @value = value end |
Instance Attribute Details
#left_child ⇒ Object (readonly)
Returns the value of attribute left_child.
4 5 6 |
# File 'lib/newral/genetic/tree.rb', line 4 def left_child @left_child end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/newral/genetic/tree.rb', line 5 def parent @parent end |
#right_child ⇒ Object (readonly)
Returns the value of attribute right_child.
4 5 6 |
# File 'lib/newral/genetic/tree.rb', line 4 def right_child @right_child end |
#sub_node_count ⇒ Object (readonly)
Returns the value of attribute sub_node_count.
4 5 6 |
# File 'lib/newral/genetic/tree.rb', line 4 def sub_node_count @sub_node_count end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
4 5 6 |
# File 'lib/newral/genetic/tree.rb', line 4 def value @value end |
Class Method Details
.full_tree(depth: 3, allowed_operands: OPERANDS.keys, terminal_nodes: []) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/newral/genetic/tree.rb', line 54 def self.full_tree( depth:3, allowed_operands: OPERANDS.keys, terminal_nodes:[] ) if depth > 1 value = allowed_operands[ rand(allowed_operands.size) ] tree = Tree.new( value: value ) tree.set_child_trees( left_child: Tree.full_tree( depth: depth-1, allowed_operands: allowed_operands, terminal_nodes: terminal_nodes )) if OPERANDS[value] == 2 tree.set_child_trees( right_child: Tree.full_tree( depth: depth-1, allowed_operands: allowed_operands, terminal_nodes: terminal_nodes )) end tree else Tree.new( value: terminal_nodes[rand(terminal_nodes.size)]) end end |
Instance Method Details
#eval ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/newral/genetic/tree.rb', line 41 def eval case @value when '+' then @left_child.eval+@right_child.eval when '-' then @left_child.eval-@right_child.eval when '*' then @left_child.eval*@right_child.eval when '/' then @left_child.eval/@right_child.eval when 'pow' then @left_child.eval**@right_child.eval when 'sqrt' then @left_child.eval**0.5 else @value end end |
#node_count ⇒ Object
28 29 30 |
# File 'lib/newral/genetic/tree.rb', line 28 def node_count @sub_node_count+1 end |
#set_child_trees(left_child: nil, right_child: nil, force: false) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/newral/genetic/tree.rb', line 32 def set_child_trees( left_child: nil, right_child: nil, force: false ) @left_child = left_child if left_child || force @right_child = right_child if right_child || force @right_child.parent = self if @right_child @left_child.parent = self if @left_child update_node_count self end |
#update_node_count ⇒ Object
22 23 24 25 26 |
# File 'lib/newral/genetic/tree.rb', line 22 def update_node_count @sub_node_count = ( @left_child && 1 ).to_i+( @left_child && @left_child.sub_node_count ).to_i+( @right_child && 1 ).to_i+( @right_child && @right_child.sub_node_count ).to_i @parent.update_node_count if @parent @sub_node_count end |