mlpack  3.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ra_model.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_RANN_RA_MODEL_HPP
15 #define MLPACK_METHODS_RANN_RA_MODEL_HPP
16 
21 #include <boost/variant.hpp>
22 #include "ra_search.hpp"
23 
24 namespace mlpack {
25 namespace neighbor {
26 
30 template<typename SortPolicy,
31  template<typename TreeMetricType,
32  typename TreeStatType,
33  typename TreeMatType> class TreeType>
34 using RAType = RASearch<SortPolicy,
36  arma::mat,
37  TreeType>;
38 
43 class MonoSearchVisitor : public boost::static_visitor<void>
44 {
45  private:
47  const size_t k;
49  arma::Mat<size_t>& neighbors;
51  arma::mat& distances;
52 
53  public:
55  template<typename RAType>
56  void operator()(RAType* ra) const;
57 
59  MonoSearchVisitor(const size_t k,
60  arma::Mat<size_t>& neighbors,
61  arma::mat& distances) :
62  k(k),
63  neighbors(neighbors),
64  distances(distances)
65  {};
66 };
67 
74 template<typename SortPolicy>
75 class BiSearchVisitor : public boost::static_visitor<void>
76 {
77  private:
79  const arma::mat& querySet;
81  const size_t k;
83  arma::Mat<size_t>& neighbors;
85  arma::mat& distances;
87  const size_t leafSize;
88 
90  template<typename RAType>
91  void SearchLeaf(RAType* ra) const;
92 
93  public:
95  template<template<typename TreeMetricType,
96  typename TreeStatType,
97  typename TreeMatType> class TreeType>
99 
101  template<template<typename TreeMetricType,
102  typename TreeStatType,
103  typename TreeMatType> class TreeType>
104  void operator()(RATypeT<TreeType>* ra) const;
105 
107  void operator()(RATypeT<tree::KDTree>* ra) const;
108 
110  void operator()(RATypeT<tree::Octree>* ra) const;
111 
113  BiSearchVisitor(const arma::mat& querySet,
114  const size_t k,
115  arma::Mat<size_t>& neighbors,
116  arma::mat& distances,
117  const size_t leafSize);
118 };
119 
126 template<typename SortPolicy>
127 class TrainVisitor : public boost::static_visitor<void>
128 {
129  private:
131  arma::mat&& referenceSet;
133  size_t leafSize;
134 
136  template<typename RAType>
137  void TrainLeaf(RAType* ra) const;
138 
139  public:
141  template<template<typename TreeMetricType,
142  typename TreeStatType,
143  typename TreeMatType> class TreeType>
145 
147  template<template<typename TreeMetricType,
148  typename TreeStatType,
149  typename TreeMatType> class TreeType>
150  void operator()(RATypeT<TreeType>* ra) const;
151 
153  void operator()(RATypeT<tree::KDTree>* ra) const;
154 
156  void operator()(RATypeT<tree::Octree>* ra) const;
157 
160  TrainVisitor(arma::mat&& referenceSet,
161  const size_t leafSize);
162 };
163 
167 class SingleSampleLimitVisitor : public boost::static_visitor<size_t&>
168 {
169  public:
170  template<typename RAType>
171  size_t& operator()(RAType* ra) const;
172 };
173 
177 class FirstLeafExactVisitor : public boost::static_visitor<bool&>
178 {
179  public:
180  template<typename RAType>
181  bool& operator()(RAType* ra) const;
182 };
183 
187 class SampleAtLeavesVisitor : public boost::static_visitor<bool&>
188 {
189  public:
191  template<typename RAType>
192  bool& operator()(RAType *) const;
193 };
194 
198 class AlphaVisitor : public boost::static_visitor<double&>
199 {
200  public:
202  template<typename RAType>
203  double& operator()(RAType* ra) const;
204 };
205 
209 class TauVisitor : public boost::static_visitor<double&>
210 {
211  public:
213  template<typename RAType>
214  double& operator()(RAType* ra) const;
215 };
216 
220 class SingleModeVisitor : public boost::static_visitor<bool&>
221 {
222  public:
224  template<typename RAType>
225  bool& operator()(RAType* ra) const;
226 };
227 
231 class ReferenceSetVisitor : public boost::static_visitor<const arma::mat&>
232 {
233  public:
235  template<typename RAType>
236  const arma::mat& operator()(RAType* ra) const;
237 };
238 
242 class DeleteVisitor : public boost::static_visitor<void>
243 {
244  public:
246  template<typename RAType> void operator()(RAType* ra) const;
247 };
248 
252 class NaiveVisitor : public boost::static_visitor<bool&>
253 {
254  public:
258  template<typename RAType>
259  bool& operator()(RAType* ra) const;
260 };
261 
270 template<typename SortPolicy>
271 class RAModel
272 {
273  public:
279  {
290  };
291 
292  private:
294  TreeTypes treeType;
296  size_t leafSize;
297 
299  bool randomBasis;
301  arma::mat q;
302 
304  boost::variant<RAType<SortPolicy, tree::KDTree>*,
314 
315  public:
320  RAModel(TreeTypes treeType = TreeTypes::KD_TREE, bool randomBasis = false);
321 
327  RAModel(const RAModel& other);
328 
334  RAModel(RAModel&& other);
335 
341  RAModel& operator=(const RAModel& other);
342 
348  RAModel& operator=(RAModel&& other);
349 
351  ~RAModel();
352 
354  template<typename Archive>
355  void serialize(Archive& ar, const unsigned int /* version */);
356 
358  const arma::mat& Dataset() const;
359 
361  bool SingleMode() const;
363  bool& SingleMode();
364 
366  bool Naive() const;
368  bool& Naive();
369 
371  double Tau() const;
373  double& Tau();
374 
376  double Alpha() const;
378  double& Alpha();
379 
381  bool SampleAtLeaves() const;
383  bool& SampleAtLeaves();
384 
386  bool FirstLeafExact() const;
388  bool& FirstLeafExact();
389 
391  size_t SingleSampleLimit() const;
393  size_t& SingleSampleLimit();
394 
396  size_t LeafSize() const;
398  size_t& LeafSize();
399 
401  TreeTypes TreeType() const;
403  TreeTypes& TreeType();
404 
406  bool RandomBasis() const;
409  bool& RandomBasis();
410 
412  void BuildModel(arma::mat&& referenceSet,
413  const size_t leafSize,
414  const bool naive,
415  const bool singleMode);
416 
419  void Search(arma::mat&& querySet,
420  const size_t k,
421  arma::Mat<size_t>& neighbors,
422  arma::mat& distances);
423 
428  void Search(const size_t k,
429  arma::Mat<size_t>& neighbors,
430  arma::mat& distances);
431 
433  std::string TreeName() const;
434 };
435 
436 } // namespace neighbor
437 } // namespace mlpack
438 
439 #include "ra_model_impl.hpp"
440 
441 #endif
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
MonoSearchVisitor(const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
Construct the MonoSearchVisitor object with the given parameters.
Definition: ra_model.hpp:59
NaiveVisitor exposes the Naive() method of the given RAType.
Definition: ra_model.hpp:252
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
void serialize(Archive &ar, const unsigned int)
Serialize the model.
void BuildModel(arma::mat &&referenceSet, const size_t leafSize, const bool naive, const bool singleMode)
Build the reference tree.
TrainVisitor(arma::mat &&referenceSet, const size_t leafSize, const double tau, const double rho)
Construct the TrainVisitor object with the given reference set, leafSize for BinarySpaceTrees, and tau and rho for spill trees.
Exposes the FirstLeafExact() method of the given RAType.
Definition: ra_model.hpp:177
void operator()(NSType *ns) const
Delete the NSType object.
ReferenceSetVisitor exposes the referenceSet of the given NSType.
Definition: ns_model.hpp:218
void operator()(NSTypeT< TreeType > *ns) const
Default Train on the given NSType instance.
size_t LeafSize() const
Get the leaf size (only relevant when the kd-tree is used).
bool & operator()(RAType *ra) const
Exposes the SingleMode() method of the given RAType.
Definition: ra_model.hpp:220
void operator()(NSType *ns) const
Perform monochromatic nearest neighbor search.
double & operator()(RAType *ra) const
Return Alpha parameter.
bool & operator()(RAType *) const
Return SampleAtLeaves (whether or not sampling is done at leaves).
RAModel(TreeTypes treeType=TreeTypes::KD_TREE, bool randomBasis=false)
Initialize the RAModel with the given type and whether or not a random basis should be used...
const arma::mat & operator()(NSType *ns) const
Return the reference set.
bool FirstLeafExact() const
Get whether or not we traverse to the first leaf without approximation.
bool Naive() const
Get whether or not naive search is being used.
double Alpha() const
Get the desired success probability.
Exposes the Tau() method of the given RAType.
Definition: ra_model.hpp:209
const arma::mat & Dataset() const
Expose the dataset.
RAModel & operator=(const RAModel &other)
Copy the given RAModel.
double & operator()(RAType *ra) const
Get a reference to the Tau parameter.
void Search(arma::mat &&querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
Perform rank-approximate neighbor search, taking ownership of the query set.
std::string TreeName() const
Get the name of the tree type.
TrainVisitor sets the reference set to a new reference set on the given NSType.
void operator()(NSTypeT< TreeType > *ns) const
Default Bichromatic neighbor search on the given NSType instance.
BiSearchVisitor(const arma::mat &querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances, const size_t leafSize, const double tau, const double rho)
Construct the BiSearchVisitor.
bool SingleMode() const
Get whether or not single-tree search is being used.
RASearch< SortPolicy, metric::EuclideanDistance, arma::mat, TreeType > RAType
Alias template for RASearch.
Definition: ra_model.hpp:37
~RAModel()
Clean memory, if necessary.
bool & operator()(RAType *ra) const
Get a reference to the SingleMode parameter of the given RASearch object.
size_t & operator()(RAType *ra) const
MonoSearchVisitor executes a monochromatic neighbor search on the given NSType.
Definition: ns_model.hpp:48
Exposes the Alpha() method of the given RAType.
Definition: ra_model.hpp:198
The RASearch class: This class provides a generic manner to perform rank-approximate search via rando...
Definition: ra_search.hpp:75
Exposes the SampleAtLeaves() method of the given RAType.
Definition: ra_model.hpp:187
TreeTypes
The list of tree types we can use with RASearch.
Definition: ra_model.hpp:278
double Tau() const
Get the rank-approximation in percentile of the data.
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
The RAModel class provides an abstraction for the RASearch class, abstracting away the TreeType param...
Definition: ra_model.hpp:271
bool & operator()(RAType *ra) const
Get a reference to the naive parameter of the given RASearch object.
src mlpack core util version hpp VERSION_HPP_CONTENTS string(REGEX REPLACE".*#define MLPACK_VERSION_MAJOR ([0-9]+).*""\\1"MLPACK_VERSION_MAJOR"${VERSION_HPP_CONTENTS}") string(REGEX REPLACE".* MLPACK_VERSION_MINOR "$
Definition: CMakeLists.txt:79
bool RandomBasis() const
Get whether or not a random basis is being used.
TreeTypes TreeType() const
Get the type of tree being used.
Exposes the SingleSampleLimit() method of the given RAType.
Definition: ra_model.hpp:167