mlpack  3.4.2
ra_search.hpp
Go to the documentation of this file.
1 
25 #ifndef MLPACK_METHODS_RANN_RA_SEARCH_HPP
26 #define MLPACK_METHODS_RANN_RA_SEARCH_HPP
27 
28 #include <mlpack/prereqs.hpp>
29 
31 
34 
35 #include "ra_query_stat.hpp"
36 #include "ra_util.hpp"
37 
38 namespace mlpack {
39 namespace neighbor {
40 
41 // Forward declaration.
42 template<typename SortPolicy>
43 class TrainVisitor;
44 
69 template<typename SortPolicy = NearestNeighborSort,
70  typename MetricType = metric::EuclideanDistance,
71  typename MatType = arma::mat,
72  template<typename TreeMetricType,
73  typename TreeStatType,
74  typename TreeMatType> class TreeType = tree::KDTree>
75 class RASearch
76 {
77  public:
79  typedef TreeType<MetricType, RAQueryStat<SortPolicy>, MatType> Tree;
80 
126  RASearch(MatType referenceSet,
127  const bool naive = false,
128  const bool singleMode = false,
129  const double tau = 5,
130  const double alpha = 0.95,
131  const bool sampleAtLeaves = false,
132  const bool firstLeafExact = false,
133  const size_t singleSampleLimit = 20,
134  const MetricType metric = MetricType());
135 
183  RASearch(Tree* referenceTree,
184  const bool singleMode = false,
185  const double tau = 5,
186  const double alpha = 0.95,
187  const bool sampleAtLeaves = false,
188  const bool firstLeafExact = false,
189  const size_t singleSampleLimit = 20,
190  const MetricType metric = MetricType());
191 
211  RASearch(const bool naive = false,
212  const bool singleMode = false,
213  const double tau = 5,
214  const double alpha = 0.95,
215  const bool sampleAtLeaves = false,
216  const bool firstLeafExact = false,
217  const size_t singleSampleLimit = 20,
218  const MetricType metric = MetricType());
219 
225 
235  void Train(MatType referenceSet);
236 
240  void Train(Tree* referenceTree);
241 
258  void Search(const MatType& querySet,
259  const size_t k,
260  arma::Mat<size_t>& neighbors,
261  arma::mat& distances);
262 
285  void Search(Tree* queryTree,
286  const size_t k,
287  arma::Mat<size_t>& neighbors,
288  arma::mat& distances);
289 
302  void Search(const size_t k,
303  arma::Mat<size_t>& neighbors,
304  arma::mat& distances);
305 
319  void ResetQueryTree(Tree* queryTree) const;
320 
322  const MatType& ReferenceSet() const { return *referenceSet; }
323 
325  bool Naive() const { return naive; }
327  bool& Naive() { return naive; }
328 
330  bool SingleMode() const { return singleMode; }
332  bool& SingleMode() { return singleMode; }
333 
335  double Tau() const { return tau; }
337  double& Tau() { return tau; }
338 
340  double Alpha() const { return alpha; }
342  double& Alpha() { return alpha; }
343 
345  bool SampleAtLeaves() const { return sampleAtLeaves; }
347  bool& SampleAtLeaves() { return sampleAtLeaves; }
348 
350  bool FirstLeafExact() const { return firstLeafExact; }
352  bool& FirstLeafExact() { return firstLeafExact; }
353 
355  size_t SingleSampleLimit() const { return singleSampleLimit; }
357  size_t& SingleSampleLimit() { return singleSampleLimit; }
358 
360  template<typename Archive>
361  void serialize(Archive& ar, const unsigned int /* version */);
362 
363  private:
365  std::vector<size_t> oldFromNewReferences;
367  Tree* referenceTree;
369  const MatType* referenceSet;
370 
372  bool treeOwner;
374  bool setOwner;
375 
377  bool naive;
379  bool singleMode;
380 
382  double tau;
384  double alpha;
386  bool sampleAtLeaves;
388  bool firstLeafExact;
391  size_t singleSampleLimit;
392 
394  MetricType metric;
395 
397  template<typename SortPol>
398  friend class TrainVisitor;
399 }; // class RASearch
400 
401 } // namespace neighbor
402 } // namespace mlpack
403 
404 // Include implementation.
405 #include "ra_search_impl.hpp"
406 
407 // Include convenient typedefs.
408 #include "ra_typedef.hpp"
409 
410 #endif
The RASearch class: This class provides a generic manner to perform rank-approximate search via rando...
Definition: ra_search.hpp:76
const MatType & ReferenceSet() const
Access the reference set.
Definition: ra_search.hpp:322
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...
bool FirstLeafExact() const
Get whether or not we traverse to the first leaf without approximation.
Definition: ra_search.hpp:350
TreeType< MetricType, RAQueryStat< SortPolicy >, MatType > Tree
Convenience typedef.
Definition: ra_search.hpp:79
void Train(MatType referenceSet)
"Train" the model on the given reference set.
RASearch(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 Naive() const
Get whether or not naive (brute-force) search is used.
Definition: ra_search.hpp:325
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
Definition: ra_search.hpp:355
void Train(Tree *referenceTree)
Set the reference tree to a new reference tree.
double Tau() const
Get the rank-approximation in percentile of the data.
Definition: ra_search.hpp:335
double Alpha() const
Get the desired success probability.
Definition: ra_search.hpp:340
void Search(const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
Compute the rank approximate nearest neighbors of each point in the reference set (that is,...
~RASearch()
Delete the RASearch object.
RASearch(Tree *referenceTree, 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 with the given pre-constructed reference tree.
bool SingleMode() const
Get whether or not single-tree search is used.
Definition: ra_search.hpp:330
void ResetQueryTree(Tree *queryTree) const
This function recursively resets the RAQueryStat of the given query tree to set 'bound' to SortPolicy...
RASearch(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())
Create an RASearch object with no reference data.
void Search(Tree *queryTree, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
Compute the rank approximate nearest neighbors of each point in the pre-built query tree and store th...
bool & SampleAtLeaves()
Modify whether or not sampling is done at the leaves.
Definition: ra_search.hpp:347
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
Definition: ra_search.hpp:345
bool & FirstLeafExact()
Modify whether or not we traverse to the first leaf without approximation.
Definition: ra_search.hpp:352
double & Alpha()
Modify the desired success probability.
Definition: ra_search.hpp:342
double & Tau()
Modify the rank-approximation in percentile of the data.
Definition: ra_search.hpp:337
bool & SingleMode()
Modify whether or not single-tree search is used.
Definition: ra_search.hpp:332
size_t & SingleSampleLimit()
Modify the limit on the size of a node that can be approximation.
Definition: ra_search.hpp:357
void serialize(Archive &ar, const unsigned int)
Serialize the object.
bool & Naive()
Modify whether or not naive (brute-force) search is used.
Definition: ra_search.hpp:327
TrainVisitor sets the reference set to a new reference set on the given NSType.
Definition: ra_model.hpp:128
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
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.
The core includes that mlpack expects; standard C++ includes and Armadillo.