Class: Vptree::VPNode
Overview
Implementation of node of VP-tree
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#distance_measure ⇒ Object
Returns the value of attribute distance_measure.
-
#is_block ⇒ Object
Returns the value of attribute is_block.
-
#left_node ⇒ Object
Returns the value of attribute left_node.
-
#mu ⇒ Object
Returns the value of attribute mu.
-
#right_node ⇒ Object
Returns the value of attribute right_node.
-
#vp_point ⇒ Object
Returns the value of attribute vp_point.
Instance Method Summary collapse
-
#initialize(data, options = {}, &block) ⇒ VPNode
constructor
A new instance of VPNode.
- #separate ⇒ Object
- #setup(other_node) ⇒ Object
Methods included from CalcDistance
Constructor Details
#initialize(data, options = {}, &block) ⇒ VPNode
Returns a new instance of VPNode.
51 52 53 54 55 56 57 58 |
# File 'lib/vptree.rb', line 51 def initialize(data, = {}, &block) @data = data @is_block = block_given? @distance_measure = block || [:distance_measure] || :euclidean_distance @left_node = nil @right_node = nil @mu = 0 end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
48 49 50 |
# File 'lib/vptree.rb', line 48 def data @data end |
#distance_measure ⇒ Object
Returns the value of attribute distance_measure.
48 49 50 |
# File 'lib/vptree.rb', line 48 def distance_measure @distance_measure end |
#is_block ⇒ Object
Returns the value of attribute is_block.
48 49 50 |
# File 'lib/vptree.rb', line 48 def is_block @is_block end |
#left_node ⇒ Object
Returns the value of attribute left_node.
49 50 51 |
# File 'lib/vptree.rb', line 49 def left_node @left_node end |
#mu ⇒ Object
Returns the value of attribute mu.
49 50 51 |
# File 'lib/vptree.rb', line 49 def mu @mu end |
#right_node ⇒ Object
Returns the value of attribute right_node.
49 50 51 |
# File 'lib/vptree.rb', line 49 def right_node @right_node end |
#vp_point ⇒ Object
Returns the value of attribute vp_point.
49 50 51 |
# File 'lib/vptree.rb', line 49 def vp_point @vp_point end |
Instance Method Details
#separate ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vptree.rb', line 67 def separate if @data.size <= 2 @vp_point = @data.first @right_node = nil if @data.size == 2 @mu = calc_dist(@data[0], @data[1]) / 2 @left_node = VPNode.new([@data[1]]) end else @vp_point = @data.sample # all sorted nodes next_node_data = @data.sort_by { |a| calc_dist(a, @vp_point) }[1..-1] len = next_node_data.size r_points = next_node_data[0..(len / 2 - 1)] l_points = next_node_data[(len / 2)..-1] @mu = calc_dist(r_points.last, @vp_point) @mu += calc_dist(l_points.first, @vp_point) @mu /= 2.0 @right_node = VPNode.new(r_points) @left_node = VPNode.new(l_points) end @right_node.setup(self) if @right_node @left_node.setup(self) if @left_node @right_node.separate if @right_node @left_node.separate if @left_node @data = nil end |
#setup(other_node) ⇒ Object
60 61 62 63 |
# File 'lib/vptree.rb', line 60 def setup(other_node) @is_block = other_node.is_block @distance_measure = other_node.distance_measure end |