mlpack  2.0.1
neighbor_search.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_METHODS_NEIGHBOR_SEARCH_NEIGHBOR_SEARCH_HPP
16 #define __MLPACK_METHODS_NEIGHBOR_SEARCH_NEIGHBOR_SEARCH_HPP
17 
18 #include <mlpack/core.hpp>
19 #include <vector>
20 #include <string>
21 
25 
27 #include "neighbor_search_stat.hpp"
30 
31 namespace mlpack {
32 namespace neighbor {
35 
36 // Forward declaration.
37 template<typename SortPolicy>
38 class NSModel;
39 
61 template<typename SortPolicy = NearestNeighborSort,
62  typename MetricType = mlpack::metric::EuclideanDistance,
63  typename MatType = arma::mat,
64  template<typename TreeMetricType,
65  typename TreeStatType,
66  typename TreeMatType> class TreeType = tree::KDTree,
67  template<typename RuleType> class TraversalType =
68  TreeType<MetricType,
70  MatType>::template DualTreeTraverser>
72 {
73  public:
75  typedef TreeType<MetricType, NeighborSearchStat<SortPolicy>, MatType> Tree;
76 
96  NeighborSearch(const MatType& referenceSet,
97  const bool naive = false,
98  const bool singleMode = false,
99  const MetricType metric = MetricType());
100 
120  NeighborSearch(MatType&& referenceSet,
121  const bool naive = false,
122  const bool singleMode = false,
123  const MetricType metric = MetricType());
124 
151  const bool singleMode = false,
152  const MetricType metric = MetricType());
153 
164  NeighborSearch(const bool naive = false,
165  const bool singleMode = false,
166  const MetricType metric = MetricType());
167 
168 
173  ~NeighborSearch();
174 
183  void Train(const MatType& referenceSet);
184 
193  void Train(MatType&& referenceSet);
194 
198  void Train(Tree* referenceTree);
199 
217  void Search(const MatType& querySet,
218  const size_t k,
219  arma::Mat<size_t>& neighbors,
220  arma::mat& distances);
221 
235  void Search(Tree* queryTree,
236  const size_t k,
237  arma::Mat<size_t>& neighbors,
238  arma::mat& distances);
239 
254  void Search(const size_t k,
255  arma::Mat<size_t>& neighbors,
256  arma::mat& distances);
257 
260  size_t BaseCases() const { return baseCases; }
261 
263  size_t Scores() const { return scores; }
264 
266  bool Naive() const { return naive; }
268  bool& Naive() { return naive; }
269 
271  bool SingleMode() const { return singleMode; }
273  bool& SingleMode() { return singleMode; }
274 
276  const MatType& ReferenceSet() const { return *referenceSet; }
277 
279  template<typename Archive>
280  void Serialize(Archive& ar, const unsigned int /* version */);
281 
282  private:
284  std::vector<size_t> oldFromNewReferences;
288  const MatType* referenceSet;
289 
291  bool treeOwner;
293  bool setOwner;
294 
296  bool naive;
299 
301  MetricType metric;
302 
304  size_t baseCases;
306  size_t scores;
307 
311 
313  friend class NSModel<SortPolicy>;
314 }; // class NeighborSearch
315 
316 } // namespace neighbor
317 } // namespace mlpack
318 
319 // Include implementation.
320 #include "neighbor_search_impl.hpp"
321 
322 // Include convenience typedefs.
323 #include "typedef.hpp"
324 
325 #endif
bool Naive() const
Access whether or not search is done in naive linear scan mode.
BinarySpaceTree< MetricType, StatisticType, MatType, bound::HRectBound, MidpointSplit > KDTree
The standard midpoint-split kd-tree.
Definition: typedef.hpp:65
void Train(const MatType &referenceSet)
Set the reference set to a new reference set, and build a tree if necessary.
const MatType & ReferenceSet() const
Access the reference dataset.
std::vector< size_t > oldFromNewReferences
Permutations of reference points during tree building.
Linear algebra utility functions, generally performed on matrices or vectors.
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:113
bool treeNeedsReset
If this is true, the reference tree bounds need to be reset on a call to Search() without a query set...
size_t BaseCases() const
Return the total number of base case evaluations performed during the last search.
Extra data for each node in the tree.
The NeighborSearch class is a template class for performing distance-based neighbor searches...
const MatType * referenceSet
Reference dataset. In some situations we may be the owner of this.
size_t scores
The total number of scores (applicable for non-naive search).
size_t baseCases
The total number of base cases.
bool & SingleMode()
Modify whether or not search is done in single-tree mode.
void Serialize(Archive &ar, const unsigned int)
Serialize the NeighborSearch model.
Tree * referenceTree
Pointer to the root of the reference tree.
This class implements the necessary methods for the SortPolicy template parameter of the NeighborSear...
bool singleMode
Indicates if single-tree search is being used (as opposed to dual-tree).
size_t Scores() const
Return the number of node combination scores during the last search.
bool setOwner
If true, we own the reference set.
bool naive
Indicates if O(n^2) naive search is being used.
bool treeOwner
If true, this object created the trees and is responsible for them.
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...
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
TreeType< MetricType, NeighborSearchStat< SortPolicy >, MatType > Tree
Convenience typedef.
bool SingleMode() const
Access whether or not search is done in single-tree mode.
MetricType metric
Instantiation of metric.
NeighborSearch(const MatType &referenceSet, const bool naive=false, const bool singleMode=false, const MetricType metric=MetricType())
Initialize the NeighborSearch object, passing a reference dataset (this is the dataset which is searc...
~NeighborSearch()
Delete the NeighborSearch object.
bool & Naive()
Modify whether or not search is done in naive linear scan mode.