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(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 
180  RASearch(Tree* referenceTree,
181  const bool singleMode = false,
182  const double tau = 5,
183  const double alpha = 0.95,
184  const bool sampleAtLeaves = false,
185  const bool firstLeafExact = false,
186  const size_t singleSampleLimit = 20,
187  const MetricType metric = MetricType());
188 
208  RASearch(const bool naive = false,
209  const bool singleMode = false,
210  const double tau = 5,
211  const double alpha = 0.95,
212  const bool sampleAtLeaves = false,
213  const bool firstLeafExact = false,
214  const size_t singleSampleLimit = 20,
215  const MetricType metric = MetricType());
216 
221  ~RASearch();
222 
232  void Train(MatType referenceSet);
233 
237  void Train(Tree* referenceTree);
238 
255  void Search(const MatType& querySet,
256  const size_t k,
257  arma::Mat<size_t>& neighbors,
258  arma::mat& distances);
259 
283  void Search(Tree* queryTree,
284  const size_t k,
285  arma::Mat<size_t>& neighbors,
286  arma::mat& distances);
287 
300  void Search(const size_t k,
301  arma::Mat<size_t>& neighbors,
302  arma::mat& distances);
303 
317  void ResetQueryTree(Tree* queryTree) const;
318 
320  const MatType& ReferenceSet() const { return *referenceSet; }
321 
323  bool Naive() const { return naive; }
325  bool& Naive() { return naive; }
326 
328  bool SingleMode() const { return singleMode; }
330  bool& SingleMode() { return singleMode; }
331 
333  double Tau() const { return tau; }
335  double& Tau() { return tau; }
336 
338  double Alpha() const { return alpha; }
340  double& Alpha() { return alpha; }
341 
343  bool SampleAtLeaves() const { return sampleAtLeaves; }
345  bool& SampleAtLeaves() { return sampleAtLeaves; }
346 
348  bool FirstLeafExact() const { return firstLeafExact; }
350  bool& FirstLeafExact() { return firstLeafExact; }
351 
353  size_t SingleSampleLimit() const { return singleSampleLimit; }
355  size_t& SingleSampleLimit() { return singleSampleLimit; }
356 
358  template<typename Archive>
359  void serialize(Archive& ar, const unsigned int /* version */);
360 
361  private:
363  std::vector<size_t> oldFromNewReferences;
365  Tree* referenceTree;
367  const MatType* referenceSet;
368 
370  bool treeOwner;
372  bool setOwner;
373 
375  bool naive;
377  bool singleMode;
378 
380  double tau;
382  double alpha;
384  bool sampleAtLeaves;
386  bool firstLeafExact;
389  size_t singleSampleLimit;
390 
392  MetricType metric;
393 
395  template<typename SortPol>
396  friend class TrainVisitor;
397 }; // class RASearch
398 
399 } // namespace neighbor
400 } // namespace mlpack
401 
402 // Include implementation.
403 #include "ra_search_impl.hpp"
404 
405 // Include convenient typedefs.
406 #include "ra_typedef.hpp"
407 
408 #endif
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...
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
Definition: ra_search.hpp:353
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
Definition: ra_search.hpp:343
void Train(MatType referenceSet)
"Train" the model on the given reference set.
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool & SingleMode()
Modify whether or not single-tree search is used.
Definition: ra_search.hpp:330
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:355
double & Tau()
Modify the rank-approximation in percentile of the data.
Definition: ra_search.hpp:335
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:323
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:348
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:328
bool & Naive()
Modify whether or not naive (brute-force) search is used.
Definition: ra_search.hpp:325
bool & FirstLeafExact()
Modify whether or not we traverse to the first leaf without approximation.
Definition: ra_search.hpp:350
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:345
const MatType & ReferenceSet() const
Access the reference set.
Definition: ra_search.hpp:320
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:340
double Tau() const
Get the rank-approximation in percentile of the data.
Definition: ra_search.hpp:333
double Alpha() const
Get the desired success probability.
Definition: ra_search.hpp:338