mlpack
r_tree_descent_heuristic_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_TREE_RECTANGLE_TREE_R_TREE_DESCENT_HEURISTIC_IMPL_HPP
14 #define MLPACK_CORE_TREE_RECTANGLE_TREE_R_TREE_DESCENT_HEURISTIC_IMPL_HPP
15 
17 
18 namespace mlpack {
19 namespace tree {
20 
21 template<typename TreeType>
22 inline size_t RTreeDescentHeuristic::ChooseDescentNode(const TreeType* node,
23  const size_t point)
24 {
25  // Convenience typedef.
26  typedef typename TreeType::ElemType ElemType;
27 
28  ElemType minScore = std::numeric_limits<ElemType>::max();
29  int bestIndex = 0;
30  ElemType bestVol = 0.0;
31 
32  for (size_t i = 0; i < node->NumChildren(); ++i)
33  {
34  ElemType v1 = 1.0;
35  ElemType v2 = 1.0;
36  for (size_t j = 0; j < node->Child(i).Bound().Dim(); ++j)
37  {
38  v1 *= node->Child(i).Bound()[j].Width();
39  v2 *= node->Child(i).Bound()[j].Contains(node->Dataset().col(point)[j]) ?
40  node->Child(i).Bound()[j].Width() :
41  (node->Child(i).Bound()[j].Hi() < node->Dataset().col(point)[j] ?
42  (node->Dataset().col(point)[j] - node->Child(i).Bound()[j].Lo()) :
43  (node->Child(i).Bound()[j].Hi() - node->Dataset().col(point)[j]));
44  }
45 
46  assert(v2 - v1 >= 0);
47 
48  if ((v2 - v1) < minScore)
49  {
50  minScore = v2 - v1;
51  bestVol = v1;
52  bestIndex = i;
53  }
54  else if ((v2 - v1) == minScore && v1 < bestVol)
55  {
56  bestVol = v1;
57  bestIndex = i;
58  }
59  }
60 
61  return bestIndex;
62 }
63 
64 template<typename TreeType>
66  const TreeType* node,
67  const TreeType* insertedNode)
68 {
69  // Convenience typedef.
70  typedef typename TreeType::ElemType ElemType;
71 
72  ElemType minScore = std::numeric_limits<ElemType>::max();
73  int bestIndex = 0;
74  ElemType bestVol = 0.0;
75 
76  for (size_t i = 0; i < node->NumChildren(); ++i)
77  {
78  ElemType v1 = 1.0;
79  ElemType v2 = 1.0;
80  for (size_t j = 0; j < node->Child(i).Bound().Dim(); ++j)
81  {
82  v1 *= node->Child(i).Bound()[j].Width();
83  v2 *= node->Child(i).Bound()[j].Contains(insertedNode->Bound()[j]) ?
84  node->Child(i).Bound()[j].Width() :
85  (insertedNode->Bound()[j].Contains(node->Child(i).Bound()[j]) ?
86  insertedNode->Bound()[j].Width() :
87  (insertedNode->Bound()[j].Lo() < node->Child(i).Bound()[j].Lo()
88  ? (node->Child(i).Bound()[j].Hi() -
89  insertedNode->Bound()[j].Lo()) : (insertedNode->Bound()[j].Hi() -
90  node->Child(i).Bound()[j].Lo())));
91  }
92 
93  assert(v2 - v1 >= 0);
94 
95  if ((v2 - v1) < minScore)
96  {
97  minScore = v2 - v1;
98  bestVol = v1;
99  bestIndex = i;
100  }
101  else if ((v2 - v1) == minScore && v1 < bestVol)
102  {
103  bestVol = v1;
104  bestIndex = i;
105  }
106  }
107 
108  return bestIndex;
109 }
110 
111 } // namespace tree
112 } // namespace mlpack
113 
114 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
static size_t ChooseDescentNode(const TreeType *node, const size_t point)
Evaluate the node using a heuristic.
Definition: r_tree_descent_heuristic_impl.hpp:22