Class: BinaryDecisionTree::MarshalledTree

Inherits:
Object
  • Object
show all
Defined in:
lib/binary_decision_tree/marshalled_tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(depth, decisions, mask) ⇒ MarshalledTree

Returns a new instance of MarshalledTree.



7
8
9
10
11
# File 'lib/binary_decision_tree/marshalled_tree.rb', line 7

def initialize(depth, decisions, mask)
  @depth = depth
  @decisions = decisions
  @mask = mask
end

Instance Attribute Details

#decisionsObject (readonly)

Returns the value of attribute decisions.



4
5
6
# File 'lib/binary_decision_tree/marshalled_tree.rb', line 4

def decisions
  @decisions
end

#depthObject (readonly)

Returns the value of attribute depth.



3
4
5
# File 'lib/binary_decision_tree/marshalled_tree.rb', line 3

def depth
  @depth
end

#maskObject (readonly)

Returns the value of attribute mask.



5
6
7
# File 'lib/binary_decision_tree/marshalled_tree.rb', line 5

def mask
  @mask
end

Class Method Details

.from_tree(tree) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#==(obj) ⇒ Object Also known as: eql?



47
48
49
# File 'lib/binary_decision_tree/marshalled_tree.rb', line 47

def ==(obj)
  obj.class == self.class && obj.state == state
end

#hashObject



53
54
55
# File 'lib/binary_decision_tree/marshalled_tree.rb', line 53

def hash
  state.hash
end

#to_tree(tree_class: Tree) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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