class BinaryDecisionTree::MarshalledTree

Attributes

decisions[R]
depth[R]
mask[R]

Public Class Methods

from_tree(tree) click to toggle source
# File lib/binary_decision_tree/marshalled_tree.rb, line 13
def self.from_tree(tree)
  depth = tree.depth
  decisions = 0
  mask = 0

  (2**tree.depth).times do |i|
    next if i == 0
    node = tree.at(i)
    if !node.decision.nil?
      mask |= 1 << i
      decisions |= node.decision << i
    end
  end

  new(depth, decisions, mask)
end
new(depth, decisions, mask) click to toggle source
# File lib/binary_decision_tree/marshalled_tree.rb, line 7
def initialize(depth, decisions, mask)
  @depth = depth
  @decisions = decisions
  @mask = mask
end

Public Instance Methods

==(obj) click to toggle source
# File lib/binary_decision_tree/marshalled_tree.rb, line 47
def ==(obj)
  obj.class == self.class && obj.state == state
end
Also aliased as: eql?
eql?(obj)
Alias for: ==
hash() click to toggle source
# File lib/binary_decision_tree/marshalled_tree.rb, line 53
def hash
  state.hash
end
to_tree(tree_class: Tree) click to toggle source
# File lib/binary_decision_tree/marshalled_tree.rb, line 30
def to_tree(tree_class: Tree)
  tree = tree_class.new(depth)

  (2**tree.depth).times do |i|
    next if i == 0

    current_position = 1 << i

    if (mask & current_position) != 0
      node = tree.at(i)
      node.decision = (decisions & current_position) == 0 ? 0 : 1
    end
  end

  tree
end

Protected Instance Methods

state() click to toggle source
# File lib/binary_decision_tree/marshalled_tree.rb, line 59
def state
  [depth, decisions, mask]
end