neighbor_search.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_NEIGHBOR_SEARCH_NEIGHBOR_SEARCH_HPP
14 #define MLPACK_METHODS_NEIGHBOR_SEARCH_NEIGHBOR_SEARCH_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 #include <vector>
18 #include <string>
19 
23 
24 #include "neighbor_search_stat.hpp"
27 
28 namespace mlpack {
29 // Neighbor-search routines. These include all-nearest-neighbors and
30 // all-furthest-neighbors searches.
31 namespace neighbor {
32 
33 // Forward declaration.
34 template<typename SortPolicy,
35  template<typename TreeMetricType,
36  typename TreeStatType,
37  typename TreeMatType> class TreeType,
38  template<typename RuleType> class DualTreeTraversalType,
39  template<typename RuleType> class SingleTreeTraversalType>
41 
44 {
49 };
50 
74 template<typename SortPolicy = NearestNeighborSort,
75  typename MetricType = mlpack::metric::EuclideanDistance,
76  typename MatType = arma::mat,
77  template<typename TreeMetricType,
78  typename TreeStatType,
79  typename TreeMatType> class TreeType = tree::KDTree,
80  template<typename RuleType> class DualTreeTraversalType =
81  TreeType<MetricType,
83  MatType>::template DualTreeTraverser,
84  template<typename RuleType> class SingleTreeTraversalType =
85  TreeType<MetricType,
86  NeighborSearchStat<SortPolicy>,
87  MatType>::template SingleTreeTraverser>
89 {
90  public:
92  typedef TreeType<MetricType, NeighborSearchStat<SortPolicy>, MatType> Tree;
93 
110  NeighborSearch(MatType referenceSet,
111  const NeighborSearchMode mode = DUAL_TREE_MODE,
112  const double epsilon = 0,
113  const MetricType metric = MetricType());
114 
138  NeighborSearch(Tree referenceTree,
139  const NeighborSearchMode mode = DUAL_TREE_MODE,
140  const double epsilon = 0,
141  const MetricType metric = MetricType());
142 
153  const double epsilon = 0,
154  const MetricType metric = MetricType());
155 
162  NeighborSearch(const NeighborSearch& other);
163 
171 
177  NeighborSearch& operator=(const NeighborSearch& other);
178 
185 
190  ~NeighborSearch();
191 
201  void Train(MatType referenceSet);
202 
212  void Train(Tree referenceTree);
213 
231  void Search(const MatType& querySet,
232  const size_t k,
233  arma::Mat<size_t>& neighbors,
234  arma::mat& distances);
235 
256  void Search(Tree& queryTree,
257  const size_t k,
258  arma::Mat<size_t>& neighbors,
259  arma::mat& distances,
260  bool sameSet = false);
261 
276  void Search(const size_t k,
277  arma::Mat<size_t>& neighbors,
278  arma::mat& distances);
279 
295  static double EffectiveError(arma::mat& foundDistances,
296  arma::mat& realDistances);
297 
309  static double Recall(arma::Mat<size_t>& foundNeighbors,
310  arma::Mat<size_t>& realNeighbors);
311 
314  size_t BaseCases() const { return baseCases; }
315 
317  size_t Scores() const { return scores; }
318 
320  NeighborSearchMode SearchMode() const { return searchMode; }
322  NeighborSearchMode& SearchMode() { return searchMode; }
323 
325  double Epsilon() const { return epsilon; }
327  double& Epsilon() { return epsilon; }
328 
330  const MatType& ReferenceSet() const { return *referenceSet; }
331 
333  const Tree& ReferenceTree() const { return *referenceTree; }
335  Tree& ReferenceTree() { return *referenceTree; }
336 
338  template<typename Archive>
339  void serialize(Archive& ar, const uint32_t version);
340 
341  private:
343  std::vector<size_t> oldFromNewReferences;
345  Tree* referenceTree;
347  const MatType* referenceSet;
348 
350  NeighborSearchMode searchMode;
352  double epsilon;
353 
355  MetricType metric;
356 
358  size_t baseCases;
360  size_t scores;
361 
364  bool treeNeedsReset;
365 
367  friend class LeafSizeNSWrapper<SortPolicy, TreeType, DualTreeTraversalType,
368  SingleTreeTraversalType>;
369 }; // class NeighborSearch
370 
371 } // namespace neighbor
372 } // namespace mlpack
373 
374 // Include implementation.
375 #include "neighbor_search_impl.hpp"
376 
377 // Include convenience typedefs.
378 #include "typedef.hpp"
379 
380 #endif
const MatType & ReferenceSet() const
Access the reference dataset.
double Epsilon() const
Access the relative error to be considered in approximate search.
size_t Scores() const
Return the number of node combination scores during the last search.
const Tree & ReferenceTree() const
Access the reference tree.
Linear algebra utility functions, generally performed on matrices or vectors.
Extra data for each node in the tree.
The core includes that mlpack expects; standard C++ includes and Armadillo.
NeighborSearch(MatType referenceSet, const NeighborSearchMode mode=DUAL_TREE_MODE, const double epsilon=0, const MetricType metric=MetricType())
Initialize the NeighborSearch object, passing a reference dataset (this is the dataset which is searc...
LeafSizeNSWrapper wraps any NeighborSearch types that take a leaf size for tree construction.
The NeighborSearch class is a template class for performing distance-based neighbor searches...
static double EffectiveError(arma::mat &foundDistances, arma::mat &realDistances)
Calculate the average relative error (effective error) between the distances calculated and the true ...
double & Epsilon()
Modify the relative error to be considered in approximate search.
Tree & ReferenceTree()
Modify the reference tree.
static double Recall(arma::Mat< size_t > &foundNeighbors, arma::Mat< size_t > &realNeighbors)
Calculate the recall (% of neighbors found) given the list of found neighbors and the true set of nei...
NeighborSearchMode & SearchMode()
Modify the search mode.
NeighborSearchMode SearchMode() const
Access the search mode.
NeighborSearch & operator=(const NeighborSearch &other)
Copy the given NeighborSearch object.
TreeType< MetricType, NeighborSearchStat< SortPolicy >, MatType > Tree
Convenience typedef.
Definition of generalized binary space partitioning tree (BinarySpaceTree).
void Search(const MatType &querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
For each point in the query set, compute the nearest neighbors and store the output in the given matr...
size_t BaseCases() const
Return the total number of base case evaluations performed during the last search.
void Train(MatType referenceSet)
Set the reference set to a new reference set, and build a tree if necessary.
BinarySpaceTree< MetricType, StatisticType, MatType, bound::HRectBound, MidpointSplit > KDTree
The standard midpoint-split kd-tree.
Definition: typedef.hpp:63
void serialize(Archive &ar, const uint32_t version)
Serialize the NeighborSearch model.
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
NeighborSearchMode
NeighborSearchMode represents the different neighbor search modes available.
~NeighborSearch()
Delete the NeighborSearch object.