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 "ra_search.hpp"
22 
23 namespace mlpack {
24 namespace neighbor {
25 
33 {
34  public:
38 
41  virtual RAWrapperBase* Clone() const = 0;
42 
44  virtual ~RAWrapperBase() { }
45 
47  virtual const arma::mat& Dataset() const = 0;
48 
50  virtual size_t SingleSampleLimit() const = 0;
52  virtual size_t& SingleSampleLimit() = 0;
53 
55  virtual bool FirstLeafExact() const = 0;
57  virtual bool& FirstLeafExact() = 0;
58 
60  virtual bool SampleAtLeaves() const = 0;
62  virtual bool& SampleAtLeaves() = 0;
63 
65  virtual double Alpha() const = 0;
67  virtual double& Alpha() = 0;
68 
70  virtual double Tau() const = 0;
72  virtual double& Tau() = 0;
73 
75  virtual bool SingleMode() const = 0;
77  virtual bool& SingleMode() = 0;
78 
80  virtual bool Naive() const = 0;
82  virtual bool& Naive() = 0;
83 
85  virtual void Train(util::Timers& timers,
86  arma::mat&& referenceSet,
87  const size_t leafSize) = 0;
88 
91  virtual void Search(util::Timers& timers,
92  arma::mat&& querySet,
93  const size_t k,
94  arma::Mat<size_t>& neighbors,
95  arma::mat& distances,
96  const size_t leafSize) = 0;
97 
100  virtual void Search(util::Timers& timers,
101  const size_t k,
102  arma::Mat<size_t>& neighbors,
103  arma::mat& distances) = 0;
104 };
105 
109 template<template<typename TreeMetricType,
110  typename TreeStatType,
111  typename TreeMatType> class TreeType>
112 class RAWrapper : public RAWrapperBase
113 {
114  public:
117  RAWrapper(const bool singleMode, const bool naive) :
118  ra(singleMode, naive)
119  {
120  // Nothing else to do.
121  }
122 
124  virtual ~RAWrapper() { }
125 
128  virtual RAWrapper* Clone() const { return new RAWrapper(*this); }
129 
131  const arma::mat& Dataset() const { return ra.ReferenceSet(); }
132 
134  size_t SingleSampleLimit() const { return ra.SingleSampleLimit(); }
136  size_t& SingleSampleLimit() { return ra.SingleSampleLimit(); }
137 
139  bool FirstLeafExact() const { return ra.FirstLeafExact(); }
141  bool& FirstLeafExact() { return ra.FirstLeafExact(); }
142 
144  bool SampleAtLeaves() const { return ra.SampleAtLeaves(); }
146  bool& SampleAtLeaves() { return ra.SampleAtLeaves(); }
147 
149  double Alpha() const { return ra.Alpha(); }
151  double& Alpha() { return ra.Alpha(); }
152 
154  double Tau() const { return ra.Tau(); }
156  double& Tau() { return ra.Tau(); }
157 
159  bool SingleMode() const { return ra.SingleMode(); }
161  bool& SingleMode() { return ra.SingleMode(); }
162 
164  bool Naive() const { return ra.Naive(); }
166  bool& Naive() { return ra.Naive(); }
167 
169  virtual void Train(util::Timers& timers,
170  arma::mat&& referenceSet,
171  const size_t /* leafSize */);
172 
175  virtual void Search(util::Timers& timers,
176  arma::mat&& querySet,
177  const size_t k,
178  arma::Mat<size_t>& neighbors,
179  arma::mat& distances,
180  const size_t /* leafSize */);
181 
184  virtual void Search(util::Timers& timers,
185  const size_t k,
186  arma::Mat<size_t>& neighbors,
187  arma::mat& distances);
188 
190  template<typename Archive>
191  void serialize(Archive& ar, const uint32_t /* version */)
192  {
193  ar(CEREAL_NVP(ra));
194  }
195 
196  protected:
199  arma::mat,
200  TreeType> RAType;
201 
203  RAType ra;
204 };
205 
211 template<template<typename TreeMetricType,
212  typename TreeStatType,
213  typename TreeMatType> class TreeType>
214 class LeafSizeRAWrapper : public RAWrapper<TreeType>
215 {
216  public:
219  LeafSizeRAWrapper(const bool singleMode, const bool naive) :
220  RAWrapper<TreeType>(singleMode, naive)
221  {
222  // Nothing else to do.
223  }
224 
226  virtual ~LeafSizeRAWrapper() { }
227 
229  virtual LeafSizeRAWrapper* Clone() const
230  {
231  return new LeafSizeRAWrapper(*this);
232  }
233 
235  virtual void Train(util::Timers& timers,
236  arma::mat&& referenceSet,
237  const size_t leafSize);
238 
241  virtual void Search(util::Timers& timers,
242  arma::mat&& querySet,
243  const size_t k,
244  arma::Mat<size_t>& neighbors,
245  arma::mat& distances,
246  const size_t leafSize);
247 
249  template<typename Archive>
250  void serialize(Archive& ar, const uint32_t /* version */)
251  {
252  ar(CEREAL_NVP(ra));
253  }
254 
255  protected:
257 };
258 
265 class RAModel
266 {
267  public:
273  {
283  OCTREE
284  };
285 
286  private:
288  TreeTypes treeType;
290  size_t leafSize;
291 
293  bool randomBasis;
295  arma::mat q;
296 
298  RAWrapperBase* raSearch;
299 
300  public:
305  RAModel(TreeTypes treeType = TreeTypes::KD_TREE, bool randomBasis = false);
306 
312  RAModel(const RAModel& other);
313 
319  RAModel(RAModel&& other);
320 
326  RAModel& operator=(const RAModel& other);
327 
333  RAModel& operator=(RAModel&& other);
334 
336  ~RAModel();
337 
339  template<typename Archive>
340  void serialize(Archive& ar, const uint32_t /* version */);
341 
343  const arma::mat& Dataset() const { return raSearch->Dataset(); }
344 
346  bool SingleMode() const { return raSearch->SingleMode(); }
348  bool& SingleMode() { return raSearch->SingleMode(); }
349 
351  bool Naive() const { return raSearch->Naive(); }
353  bool& Naive() { return raSearch->Naive(); }
354 
356  double Tau() const { return raSearch->Tau(); }
358  double& Tau() { return raSearch->Tau(); }
359 
361  double Alpha() const { return raSearch->Alpha(); }
363  double& Alpha() { return raSearch->Alpha(); }
364 
366  bool SampleAtLeaves() const { return raSearch->SampleAtLeaves(); }
368  bool& SampleAtLeaves() { return raSearch->SampleAtLeaves(); }
369 
371  bool FirstLeafExact() const { return raSearch->FirstLeafExact(); }
373  bool& FirstLeafExact() { return raSearch->FirstLeafExact(); }
374 
376  size_t SingleSampleLimit() const { return raSearch->SingleSampleLimit(); }
378  size_t& SingleSampleLimit() { return raSearch->SingleSampleLimit(); }
379 
381  size_t LeafSize() const { return leafSize; }
383  size_t& LeafSize() { return leafSize; }
384 
386  TreeTypes TreeType() const { return treeType; }
388  TreeTypes& TreeType() { return treeType; }
389 
391  bool RandomBasis() const { return randomBasis; }
394  bool& RandomBasis() { return randomBasis; }
395 
397  void InitializeModel(const bool naive, const bool singleMode);
398 
400  void BuildModel(util::Timers& timers,
401  arma::mat&& referenceSet,
402  const size_t leafSize,
403  const bool naive,
404  const bool singleMode);
405 
408  void Search(util::Timers& timers,
409  arma::mat&& querySet,
410  const size_t k,
411  arma::Mat<size_t>& neighbors,
412  arma::mat& distances);
413 
418  void Search(util::Timers& timers,
419  const size_t k,
420  arma::Mat<size_t>& neighbors,
421  arma::mat& distances);
422 
424  std::string TreeName() const;
425 };
426 
427 } // namespace neighbor
428 } // namespace mlpack
429 
430 #include "ra_model_impl.hpp"
431 
432 #endif
LeafSizeRAWrapper(const bool singleMode, const bool naive)
Construct the LeafSizeRAWrapper by delegating to the RAWrapper constructor.
Definition: ra_model.hpp:219
TreeTypes TreeType() const
Get the type of tree being used.
Definition: ra_model.hpp:386
size_t & SingleSampleLimit()
Modify the single sample limit.
Definition: ra_model.hpp:136
virtual size_t SingleSampleLimit() const =0
Get the single sample limit.
double Alpha() const
Get the value of alpha.
Definition: ra_model.hpp:149
size_t & SingleSampleLimit()
Modify the limit on the size of a node that can be approximation.
Definition: ra_model.hpp:378
double & Tau()
Modify the value of tau.
Definition: ra_model.hpp:156
virtual RAWrapperBase * Clone() const =0
Create a new RAWrapperBase that is the same as this one.
bool RandomBasis() const
Get whether or not a random basis is being used.
Definition: ra_model.hpp:391
virtual bool Naive() const =0
Get whether naive search is being used.
bool & SampleAtLeaves()
Modify whether or not sampling is done at the leaves.
Definition: ra_model.hpp:368
Linear algebra utility functions, generally performed on matrices or vectors.
virtual void Search(util::Timers &timers, arma::mat &&querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances, const size_t leafSize)=0
Perform bichromatic rank-approximate nearest neighbor search (i.e.
const arma::mat & Dataset() const
Expose the dataset.
Definition: ra_model.hpp:343
size_t SingleSampleLimit() const
Get the single sample limit.
Definition: ra_model.hpp:134
virtual bool SampleAtLeaves() const =0
Get whether to do sampling at leaves.
bool FirstLeafExact() const
Get whether to do exact search at the first leaf.
Definition: ra_model.hpp:139
bool & Naive()
Modify whether naive search is being used.
Definition: ra_model.hpp:166
virtual ~RAWrapperBase()
Destruct the RAWrapperBase (nothing to do).
Definition: ra_model.hpp:44
virtual ~LeafSizeRAWrapper()
Delete the LeafSizeRAWrapper.
Definition: ra_model.hpp:226
const arma::mat & Dataset() const
Get a reference to the reference set.
Definition: ra_model.hpp:131
bool & SingleMode()
Modify whether or not single-tree search is being used.
Definition: ra_model.hpp:348
virtual bool SingleMode() const =0
Get whether single-tree search is being used.
double Tau() const
Get the rank-approximation in percentile of the data.
Definition: ra_model.hpp:356
RAWrapper(const bool singleMode, const bool naive)
Construct the RAWrapper object, initializing the internally-held RASearch object. ...
Definition: ra_model.hpp:117
double Tau() const
Get the value of tau.
Definition: ra_model.hpp:154
virtual void Train(util::Timers &timers, arma::mat &&referenceSet, const size_t leafSize)=0
Train the RASearch model with the given parameters.
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
Definition: ra_model.hpp:376
bool & SingleMode()
Modify whether single-tree search is being used.
Definition: ra_model.hpp:161
bool & RandomBasis()
Modify whether or not a random basis is being used.
Definition: ra_model.hpp:394
bool SingleMode() const
Get whether single-tree search is being used.
Definition: ra_model.hpp:159
double & Alpha()
Modify the desired success probability.
Definition: ra_model.hpp:363
bool SampleAtLeaves() const
Get whether to do sampling at leaves.
Definition: ra_model.hpp:144
bool FirstLeafExact() const
Get whether or not we traverse to the first leaf without approximation.
Definition: ra_model.hpp:371
virtual ~RAWrapper()
Delete the RAWrapper object.
Definition: ra_model.hpp:124
double & Alpha()
Modify the value of alpha.
Definition: ra_model.hpp:151
virtual bool FirstLeafExact() const =0
Get whether to do exact search at the first leaf.
void serialize(Archive &ar, const uint32_t)
Serialize the RASearch model.
Definition: ra_model.hpp:191
virtual double Alpha() const =0
Get the value of alpha.
RAWrapperBase()
Create the RAWrapperBase object.
Definition: ra_model.hpp:37
TreeTypes & TreeType()
Modify the type of tree being used.
Definition: ra_model.hpp:388
virtual double Tau() const =0
Get the value of tau.
virtual const arma::mat & Dataset() const =0
Return a reference to the dataset.
LeafSizeRAWrapper wraps any RASearch type that needs to be able to take the leaf size into account wh...
Definition: ra_model.hpp:214
virtual RAWrapper * Clone() const
Create a copy of this RAWrapper object.
Definition: ra_model.hpp:128
RAType ra
The instantiated RASearch object that we are wrapping.
Definition: ra_model.hpp:203
bool & FirstLeafExact()
Modify whether or not we traverse to the first leaf without approximation.
Definition: ra_model.hpp:373
virtual LeafSizeRAWrapper * Clone() const
Return a copy of the LeafSizeRAWrapper.
Definition: ra_model.hpp:229
bool SingleMode() const
Get whether or not single-tree search is being used.
Definition: ra_model.hpp:346
double & Tau()
Modify the rank-approximation in percentile of the data.
Definition: ra_model.hpp:358
The RASearch class: This class provides a generic manner to perform rank-approximate search via rando...
Definition: ra_search.hpp:77
void serialize(Archive &ar, const uint32_t)
Serialize the RASearch model.
Definition: ra_model.hpp:250
RAWrapper is a wrapper class for most RASearch types.
Definition: ra_model.hpp:112
bool & FirstLeafExact()
Modify whether to do exact search at the first leaf.
Definition: ra_model.hpp:141
bool & SampleAtLeaves()
Modify whether to do sampling at leaves.
Definition: ra_model.hpp:146
RAWrapperBase is a base wrapper class for holding all RASearch types supported by RAModel...
Definition: ra_model.hpp:32
RASearch< NearestNeighborSort, metric::EuclideanDistance, arma::mat, TreeType > RAType
Definition: ra_model.hpp:200
size_t LeafSize() const
Get the leaf size (only relevant when the kd-tree is used).
Definition: ra_model.hpp:381
TreeTypes
The list of tree types we can use with RASearch.
Definition: ra_model.hpp:272
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
Definition: ra_model.hpp:366
bool Naive() const
Get whether naive search is being used.
Definition: ra_model.hpp:164
bool Naive() const
Get whether or not naive search is being used.
Definition: ra_model.hpp:351
The RAModel class provides an abstraction for the RASearch class, abstracting away the TreeType param...
Definition: ra_model.hpp:265
double Alpha() const
Get the desired success probability.
Definition: ra_model.hpp:361
size_t & LeafSize()
Modify the leaf size (only relevant when the kd-tree is used).
Definition: ra_model.hpp:383
bool & Naive()
Modify whether or not naive search is being used.
Definition: ra_model.hpp:353