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  {
289  OCTREE
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
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
Exposes the FirstLeafExact() method of the given RAType.
Definition: ra_model.hpp:177
.hpp
Definition: add_to_po.hpp:21
ReferenceSetVisitor exposes the referenceSet of the given NSType.
Definition: ns_model.hpp:218
void RandomBasis(arma::mat &basis, const size_t d)
Create a random d-dimensional orthogonal basis, storing it in the given matrix.
Exposes the SingleMode() method of the given RAType.
Definition: ra_model.hpp:220
BiSearchVisitor executes a bichromatic neighbor search on the given NSType.
Definition: ns_model.hpp:80
Exposes the Tau() method of the given RAType.
Definition: ra_model.hpp:209
TrainVisitor sets the reference set to a new reference set on the given NSType.
RASearch< SortPolicy, metric::EuclideanDistance, arma::mat, TreeType > RAType
Alias template for RASearch.
Definition: ra_model.hpp:37
void operator()(NSType *ns) const
Perform monochromatic nearest neighbor search.
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:71
Exposes the SampleAtLeaves() method of the given RAType.
Definition: ra_model.hpp:187
DeleteVisitor deletes the given NSType instance.
Definition: ns_model.hpp:229
TreeTypes
The list of tree types we can use with RASearch.
Definition: ra_model.hpp:278
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
Exposes the SingleSampleLimit() method of the given RAType.
Definition: ra_model.hpp:167