mlpack
single_tree_traverser_impl.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_CORE_TREE_RECTANGLE_TREE_SINGLE_TREE_TRAVERSER_IMPL_HPP
15 #define MLPACK_CORE_TREE_RECTANGLE_TREE_SINGLE_TREE_TRAVERSER_IMPL_HPP
16 
18 
19 #include <algorithm>
20 #include <stack>
21 
22 namespace mlpack {
23 namespace tree {
24 
25 template<typename MetricType,
26  typename StatisticType,
27  typename MatType,
28  typename SplitType,
29  typename DescentType,
30  template<typename> class AuxiliaryInformationType>
31 template<typename RuleType>
32 RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType,
33  AuxiliaryInformationType>::
34 SingleTreeTraverser<RuleType>::SingleTreeTraverser(RuleType& rule) :
35  rule(rule),
36  numPrunes(0)
37 { /* Nothing to do */ }
38 
39 template<typename MetricType,
40  typename StatisticType,
41  typename MatType,
42  typename SplitType,
43  typename DescentType,
44  template<typename> class AuxiliaryInformationType>
45 template<typename RuleType>
46 void RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType,
47  AuxiliaryInformationType>::
49  const size_t queryIndex,
50  const RectangleTree& referenceNode)
51 {
52  // If we reach a leaf node, we need to run the base case.
53  if (referenceNode.IsLeaf())
54  {
55  for (size_t i = 0; i < referenceNode.Count(); ++i)
56  rule.BaseCase(queryIndex, referenceNode.Point(i));
57 
58  return;
59  }
60 
61  // This is not a leaf node so we sort the children of this node by their
62  // scores.
63  std::vector<NodeAndScore> nodesAndScores(referenceNode.NumChildren());
64  for (size_t i = 0; i < referenceNode.NumChildren(); ++i)
65  {
66  nodesAndScores[i].node = &(referenceNode.Child(i));
67  nodesAndScores[i].score = rule.Score(queryIndex, *nodesAndScores[i].node);
68  }
69 
70  std::sort(nodesAndScores.begin(), nodesAndScores.end(), NodeComparator);
71 
72  // Now iterate through them starting with the best and stopping when we reach
73  // one that isn't good enough.
74  for (size_t i = 0; i < referenceNode.NumChildren(); ++i)
75  {
76  if (rule.Rescore(queryIndex, *nodesAndScores[i].node,
77  nodesAndScores[i].score) != DBL_MAX)
78  {
79  Traverse(queryIndex, *nodesAndScores[i].node);
80  }
81  else
82  {
83  numPrunes += referenceNode.NumChildren() - i;
84  return;
85  }
86  }
87 }
88 
89 } // namespace tree
90 } // namespace mlpack
91 
92 #endif
size_t Point(const size_t index) const
Return the index (with reference to the dataset) of a particular point in this node.
Definition: rectangle_tree.hpp:480
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void Traverse(const size_t queryIndex, const RectangleTree &referenceNode)
Traverse the tree with the given point.
Definition: single_tree_traverser_impl.hpp:48
friend DescentType
Give friend access for DescentType.
Definition: rectangle_tree.hpp:580
size_t NumChildren() const
Return the number of child nodes. (One level beneath this one only.)
Definition: rectangle_tree.hpp:371
friend SplitType
Give friend access for SplitType.
Definition: rectangle_tree.hpp:583
A rectangle type tree tree, such as an R-tree or X-tree.
Definition: rectangle_tree.hpp:54
A single traverser for rectangle type trees.
Definition: rectangle_tree.hpp:112
bool IsLeaf() const
Return whether or not this node is a leaf (true if it has no children).
Definition: rectangle_tree_impl.hpp:760
RectangleTree & Child(const size_t child) const
Get the specified child.
Definition: rectangle_tree.hpp:437
size_t Count() const
Return the number of points in this subset.
Definition: rectangle_tree.hpp:548