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 TrainVisitor;
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 
302  void Train(Tree* referenceTree);
303 
320  void Search(const MatType& querySet,
321  const size_t k,
322  arma::Mat<size_t>& neighbors,
323  arma::mat& distances);
324 
348  void Search(Tree* queryTree,
349  const size_t k,
350  arma::Mat<size_t>& neighbors,
351  arma::mat& distances);
352 
365  void Search(const size_t k,
366  arma::Mat<size_t>& neighbors,
367  arma::mat& distances);
368 
382  void ResetQueryTree(Tree* queryTree) const;
383 
385  const MatType& ReferenceSet() const { return *referenceSet; }
386 
388  bool Naive() const { return naive; }
390  bool& Naive() { return naive; }
391 
393  bool SingleMode() const { return singleMode; }
395  bool& SingleMode() { return singleMode; }
396 
398  double Tau() const { return tau; }
400  double& Tau() { return tau; }
401 
403  double Alpha() const { return alpha; }
405  double& Alpha() { return alpha; }
406 
408  bool SampleAtLeaves() const { return sampleAtLeaves; }
410  bool& SampleAtLeaves() { return sampleAtLeaves; }
411 
413  bool FirstLeafExact() const { return firstLeafExact; }
415  bool& FirstLeafExact() { return firstLeafExact; }
416 
418  size_t SingleSampleLimit() const { return singleSampleLimit; }
420  size_t& SingleSampleLimit() { return singleSampleLimit; }
421 
423  template<typename Archive>
424  void serialize(Archive& ar, const unsigned int /* version */);
425 
426  private:
428  std::vector<size_t> oldFromNewReferences;
430  Tree* referenceTree;
432  const MatType* referenceSet;
433 
435  bool treeOwner;
437  bool setOwner;
438 
440  bool naive;
442  bool singleMode;
443 
445  double tau;
447  double alpha;
449  bool sampleAtLeaves;
451  bool firstLeafExact;
454  size_t singleSampleLimit;
455 
457  MetricType metric;
458 
460  template<typename SortPol>
461  friend class TrainVisitor;
462 }; // class RASearch
463 
464 } // namespace neighbor
465 } // namespace mlpack
466 
467 // Include implementation.
468 #include "ra_search_impl.hpp"
469 
470 // Include convenient typedefs.
471 #include "ra_typedef.hpp"
472 
473 #endif
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
Definition: ra_search.hpp:418
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
Definition: ra_search.hpp:408
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
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...
bool & SingleMode()
Modify whether or not single-tree search is used.
Definition: ra_search.hpp:395
void serialize(Archive &ar, const unsigned int)
Serialize the object.
size_t & SingleSampleLimit()
Modify the limit on the size of a node that can be approximation.
Definition: ra_search.hpp:420
double & Tau()
Modify the rank-approximation in percentile of the data.
Definition: ra_search.hpp:400
TreeType< MetricType, RAQueryStat< SortPolicy >, MatType > Tree
Convenience typedef.
Definition: ra_search.hpp:75
bool Naive() const
Get whether or not naive (brute-force) search is used.
Definition: ra_search.hpp:388
TrainVisitor sets the reference set to a new reference set on the given NSType.
bool FirstLeafExact() const
Get whether or not we traverse to the first leaf without approximation.
Definition: ra_search.hpp:413
void ResetQueryTree(Tree *queryTree) const
This function recursively resets the RAQueryStat of the given query tree to set &#39;bound&#39; to SortPolicy...
bool SingleMode() const
Get whether or not single-tree search is used.
Definition: ra_search.hpp:393
bool & Naive()
Modify whether or not naive (brute-force) search is used.
Definition: ra_search.hpp:390
bool & FirstLeafExact()
Modify whether or not we traverse to the first leaf without approximation.
Definition: ra_search.hpp:415
The RASearch class: This class provides a generic manner to perform rank-approximate search via rando...
Definition: ra_search.hpp:71
bool & SampleAtLeaves()
Modify whether or not sampling is done at the leaves.
Definition: ra_search.hpp:410
const MatType & ReferenceSet() const
Access the reference set.
Definition: ra_search.hpp:385
void Train(const MatType &referenceSet)
"Train" the model on the given reference set.
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...
BinarySpaceTree< MetricType, StatisticType, MatType, bound::HRectBound, MidpointSplit > KDTree
The standard midpoint-split kd-tree.
Definition: typedef.hpp:63
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
~RASearch()
Delete the RASearch object.
double & Alpha()
Modify the desired success probability.
Definition: ra_search.hpp:405
double Tau() const
Get the rank-approximation in percentile of the data.
Definition: ra_search.hpp:398
double Alpha() const
Get the desired success probability.
Definition: ra_search.hpp:403