Class: Vptree::VPNode

Inherits:
Object
  • Object
show all
Includes:
CalcDistance
Defined in:
lib/vptree.rb

Overview

Implementation of node of VP-tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CalcDistance

#calc_dist

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, options = {}, &block)
  @data = data
  @is_block = block_given?
  @distance_measure = block || options[:distance_measure] || :euclidean_distance
  @left_node = nil
  @right_node = nil
  @mu = 0
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



48
49
50
# File 'lib/vptree.rb', line 48

def data
  @data
end

#distance_measureObject

Returns the value of attribute distance_measure.



48
49
50
# File 'lib/vptree.rb', line 48

def distance_measure
  @distance_measure
end

#is_blockObject

Returns the value of attribute is_block.



48
49
50
# File 'lib/vptree.rb', line 48

def is_block
  @is_block
end

#left_nodeObject

Returns the value of attribute left_node.



49
50
51
# File 'lib/vptree.rb', line 49

def left_node
  @left_node
end

#muObject

Returns the value of attribute mu.



49
50
51
# File 'lib/vptree.rb', line 49

def mu
  @mu
end

#right_nodeObject

Returns the value of attribute right_node.



49
50
51
# File 'lib/vptree.rb', line 49

def right_node
  @right_node
end

#vp_pointObject

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

#separateObject



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