mlpack  master
ra_search.hpp
Go to the documentation of this file.
1 
23 #ifndef MLPACK_METHODS_RANN_RA_SEARCH_HPP
24 #define MLPACK_METHODS_RANN_RA_SEARCH_HPP
25 
26 #include <mlpack/prereqs.hpp>
27 
29 
32 
33 #include "ra_query_stat.hpp"
34 #include "ra_util.hpp"
35 
36 namespace mlpack {
37 namespace neighbor {
38 
39 // Forward declaration.
40 template<typename SortPolicy>
41 class RAModel;
42 
65 template<typename SortPolicy = NearestNeighborSort,
66  typename MetricType = metric::EuclideanDistance,
67  typename MatType = arma::mat,
68  template<typename TreeMetricType,
69  typename TreeStatType,
70  typename TreeMatType> class TreeType = tree::KDTree>
71 class RASearch
72 {
73  public:
75  typedef TreeType<MetricType, RAQueryStat<SortPolicy>, MatType> Tree;
76 
122  RASearch(const MatType& referenceSet,
123  const bool naive = false,
124  const bool singleMode = false,
125  const double tau = 5,
126  const double alpha = 0.95,
127  const bool sampleAtLeaves = false,
128  const bool firstLeafExact = false,
129  const size_t singleSampleLimit = 20,
130  const MetricType metric = MetricType());
131 
176  RASearch(MatType&& referenceSet,
177  const bool naive = false,
178  const bool singleMode = false,
179  const double tau = 5,
180  const double alpha = 0.95,
181  const bool sampleAtLeaves = false,
182  const bool firstLeafExact = false,
183  const size_t singleSampleLimit = 20,
184  const MetricType metric = MetricType());
185 
234  RASearch(Tree* referenceTree,
235  const bool singleMode = false,
236  const double tau = 5,
237  const double alpha = 0.95,
238  const bool sampleAtLeaves = false,
239  const bool firstLeafExact = false,
240  const size_t singleSampleLimit = 20,
241  const MetricType metric = MetricType());
242 
262  RASearch(const bool naive = false,
263  const bool singleMode = false,
264  const double tau = 5,
265  const double alpha = 0.95,
266  const bool sampleAtLeaves = false,
267  const bool firstLeafExact = false,
268  const size_t singleSampleLimit = 20,
269  const MetricType metric = MetricType());
270 
275  ~RASearch();
276 
286  void Train(const MatType& referenceSet);
287 
297  void Train(MatType&& referenceSet);
298 
315  void Search(const MatType& querySet,
316  const size_t k,
317  arma::Mat<size_t>& neighbors,
318  arma::mat& distances);
319 
343  void Search(Tree* queryTree,
344  const size_t k,
345  arma::Mat<size_t>& neighbors,
346  arma::mat& distances);
347 
360  void Search(const size_t k,
361  arma::Mat<size_t>& neighbors,
362  arma::mat& distances);
363 
377  void ResetQueryTree(Tree* queryTree) const;
378 
380  const MatType& ReferenceSet() const { return *referenceSet; }
381 
383  bool Naive() const { return naive; }
385  bool& Naive() { return naive; }
386 
388  bool SingleMode() const { return singleMode; }
390  bool& SingleMode() { return singleMode; }
391 
393  double Tau() const { return tau; }
395  double& Tau() { return tau; }
396 
398  double Alpha() const { return alpha; }
400  double& Alpha() { return alpha; }
401 
403  bool SampleAtLeaves() const { return sampleAtLeaves; }
405  bool& SampleAtLeaves() { return sampleAtLeaves; }
406 
408  bool FirstLeafExact() const { return firstLeafExact; }
410  bool& FirstLeafExact() { return firstLeafExact; }
411 
413  size_t SingleSampleLimit() const { return singleSampleLimit; }
415  size_t& SingleSampleLimit() { return singleSampleLimit; }
416 
418  template<typename Archive>
419  void Serialize(Archive& ar, const unsigned int /* version */);
420 
421  private:
423  std::vector<size_t> oldFromNewReferences;
427  const MatType* referenceSet;
428 
430  bool treeOwner;
432  bool setOwner;
433 
435  bool naive;
438 
440  double tau;
442  double alpha;
450 
452  MetricType metric;
453 
455  friend class RAModel<SortPolicy>;
456 }; // class RASearch
457 
458 } // namespace neighbor
459 } // namespace mlpack
460 
461 // Include implementation.
462 #include "ra_search_impl.hpp"
463 
464 // Include convenient typedefs.
465 #include "ra_typedef.hpp"
466 
467 #endif
TreeType< MetricType, RAQueryStat< SortPolicy >, MatType > Tree
Convenience typedef.
Definition: ra_search.hpp:75
BinarySpaceTree< MetricType, StatisticType, MatType, bound::HRectBound, MidpointSplit > KDTree
The standard midpoint-split kd-tree.
Definition: typedef.hpp:63
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
bool singleMode
Indicates if single-tree search is being used (opposed to dual-tree).
Definition: ra_search.hpp:437
bool & SampleAtLeaves()
Modify whether or not sampling is done at the leaves.
Definition: ra_search.hpp:405
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
Definition: ra_search.hpp:403
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
double Tau() const
Get the rank-approximation in percentile of the data.
Definition: ra_search.hpp:393
bool SingleMode() const
Get whether or not single-tree search is used.
Definition: ra_search.hpp:388
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool & FirstLeafExact()
Modify whether or not we traverse to the first leaf without approximation.
Definition: ra_search.hpp:410
bool firstLeafExact
If true, we will traverse to the first leaf without approximation.
Definition: ra_search.hpp:446
size_t & SingleSampleLimit()
Modify the limit on the size of a node that can be approximation.
Definition: ra_search.hpp:415
size_t singleSampleLimit
The limit on the number of points in the largest node that can be approximated by sampling...
Definition: ra_search.hpp:449
Tree * referenceTree
Pointer to the root of the reference tree.
Definition: ra_search.hpp:425
bool & Naive()
Modify whether or not naive (brute-force) search is used.
Definition: ra_search.hpp:385
void Train(const MatType &referenceSet)
"Train" the model on the given reference set.
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
Definition: ra_search.hpp:413
MetricType metric
Instantiation of kernel.
Definition: ra_search.hpp:452
bool sampleAtLeaves
Whether or not sampling is done at the leaves. Faster, but less accurate.
Definition: ra_search.hpp:444
const MatType & ReferenceSet() const
Access the reference set.
Definition: ra_search.hpp:380
bool setOwner
If true, we are responsible for deleting the dataset.
Definition: ra_search.hpp:432
bool naive
Indicates if naive random sampling on the set is being used.
Definition: ra_search.hpp:435
double & Tau()
Modify the rank-approximation in percentile of the data.
Definition: ra_search.hpp:395
void Search(const MatType &querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
Compute the rank approximate nearest neighbors of each query point in the query set and store the out...
double alpha
The desired success probability (between 0 and 1).
Definition: ra_search.hpp:442
const MatType * referenceSet
Reference dataset. In some situations we may own this dataset.
Definition: ra_search.hpp:427
~RASearch()
Delete the RASearch object.
double Alpha() const
Get the desired success probability.
Definition: ra_search.hpp:398
void Serialize(Archive &ar, const unsigned int)
Serialize the object.
bool & SingleMode()
Modify whether or not single-tree search is used.
Definition: ra_search.hpp:390
std::vector< size_t > oldFromNewReferences
Permutations of reference points during tree building.
Definition: ra_search.hpp:423
void ResetQueryTree(Tree *queryTree) const
This function recursively resets the RAQueryStat of the given query tree to set &#39;bound&#39; to SortPolicy...
bool Naive() const
Get whether or not naive (brute-force) search is used.
Definition: ra_search.hpp:383
bool FirstLeafExact() const
Get whether or not we traverse to the first leaf without approximation.
Definition: ra_search.hpp:408
RASearch(const MatType &referenceSet, const bool naive=false, const bool singleMode=false, const double tau=5, const double alpha=0.95, const bool sampleAtLeaves=false, const bool firstLeafExact=false, const size_t singleSampleLimit=20, const MetricType metric=MetricType())
Initialize the RASearch object, passing both a reference dataset (this is the dataset that will be se...
double & Alpha()
Modify the desired success probability.
Definition: ra_search.hpp:400
The RASearch class: This class provides a generic manner to perform rank-approximate search via rando...
Definition: ra_search.hpp:71
bool treeOwner
If true, this object created the trees and is responsible for them.
Definition: ra_search.hpp:430
The RAModel class provides an abstraction for the RASearch class, abstracting away the TreeType param...
Definition: ra_model.hpp:36
double tau
The rank-approximation in percentile of the data (between 0 and 100).
Definition: ra_search.hpp:440