mlpack
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(arma::mat&& referenceSet,
86  const size_t leafSize) = 0;
87 
90  virtual void Search(arma::mat&& querySet,
91  const size_t k,
92  arma::Mat<size_t>& neighbors,
93  arma::mat& distances,
94  const size_t leafSize) = 0;
95 
98  virtual void Search(const size_t k,
99  arma::Mat<size_t>& neighbors,
100  arma::mat& distances) = 0;
101 };
102 
106 template<template<typename TreeMetricType,
107  typename TreeStatType,
108  typename TreeMatType> class TreeType>
109 class RAWrapper : public RAWrapperBase
110 {
111  public:
114  RAWrapper(const bool singleMode, const bool naive) :
115  ra(singleMode, naive)
116  {
117  // Nothing else to do.
118  }
119 
121  virtual ~RAWrapper() { }
122 
125  virtual RAWrapper* Clone() const { return new RAWrapper(*this); }
126 
128  const arma::mat& Dataset() const { return ra.ReferenceSet(); }
129 
131  size_t SingleSampleLimit() const { return ra.SingleSampleLimit(); }
133  size_t& SingleSampleLimit() { return ra.SingleSampleLimit(); }
134 
136  bool FirstLeafExact() const { return ra.FirstLeafExact(); }
138  bool& FirstLeafExact() { return ra.FirstLeafExact(); }
139 
141  bool SampleAtLeaves() const { return ra.SampleAtLeaves(); }
143  bool& SampleAtLeaves() { return ra.SampleAtLeaves(); }
144 
146  double Alpha() const { return ra.Alpha(); }
148  double& Alpha() { return ra.Alpha(); }
149 
151  double Tau() const { return ra.Tau(); }
153  double& Tau() { return ra.Tau(); }
154 
156  bool SingleMode() const { return ra.SingleMode(); }
158  bool& SingleMode() { return ra.SingleMode(); }
159 
161  bool Naive() const { return ra.Naive(); }
163  bool& Naive() { return ra.Naive(); }
164 
166  virtual void Train(arma::mat&& referenceSet,
167  const size_t /* leafSize */);
168 
171  virtual void Search(arma::mat&& querySet,
172  const size_t k,
173  arma::Mat<size_t>& neighbors,
174  arma::mat& distances,
175  const size_t /* leafSize */);
176 
179  virtual void Search(const size_t k,
180  arma::Mat<size_t>& neighbors,
181  arma::mat& distances);
182 
184  template<typename Archive>
185  void serialize(Archive& ar, const uint32_t /* version */)
186  {
187  ar(CEREAL_NVP(ra));
188  }
189 
190  protected:
193  arma::mat,
194  TreeType> RAType;
195 
197  RAType ra;
198 };
199 
205 template<template<typename TreeMetricType,
206  typename TreeStatType,
207  typename TreeMatType> class TreeType>
208 class LeafSizeRAWrapper : public RAWrapper<TreeType>
209 {
210  public:
213  LeafSizeRAWrapper(const bool singleMode, const bool naive) :
214  RAWrapper<TreeType>(singleMode, naive)
215  {
216  // Nothing else to do.
217  }
218 
220  virtual ~LeafSizeRAWrapper() { }
221 
223  virtual LeafSizeRAWrapper* Clone() const
224  {
225  return new LeafSizeRAWrapper(*this);
226  }
227 
229  virtual void Train(arma::mat&& referenceSet,
230  const size_t leafSize);
231 
234  virtual void Search(arma::mat&& querySet,
235  const size_t k,
236  arma::Mat<size_t>& neighbors,
237  arma::mat& distances,
238  const size_t leafSize);
239 
241  template<typename Archive>
242  void serialize(Archive& ar, const uint32_t /* version */)
243  {
244  ar(CEREAL_NVP(ra));
245  }
246 
247  protected:
249 };
250 
257 class RAModel
258 {
259  public:
265  {
266  KD_TREE,
267  COVER_TREE,
268  R_TREE,
269  R_STAR_TREE,
270  X_TREE,
271  HILBERT_R_TREE,
272  R_PLUS_TREE,
273  R_PLUS_PLUS_TREE,
274  UB_TREE,
275  OCTREE
276  };
277 
278  private:
280  TreeTypes treeType;
282  size_t leafSize;
283 
285  bool randomBasis;
287  arma::mat q;
288 
290  RAWrapperBase* raSearch;
291 
292  public:
297  RAModel(TreeTypes treeType = TreeTypes::KD_TREE, bool randomBasis = false);
298 
304  RAModel(const RAModel& other);
305 
311  RAModel(RAModel&& other);
312 
318  RAModel& operator=(const RAModel& other);
319 
325  RAModel& operator=(RAModel&& other);
326 
328  ~RAModel();
329 
331  template<typename Archive>
332  void serialize(Archive& ar, const uint32_t /* version */);
333 
335  const arma::mat& Dataset() const { return raSearch->Dataset(); }
336 
338  bool SingleMode() const { return raSearch->SingleMode(); }
340  bool& SingleMode() { return raSearch->SingleMode(); }
341 
343  bool Naive() const { return raSearch->Naive(); }
345  bool& Naive() { return raSearch->Naive(); }
346 
348  double Tau() const { return raSearch->Tau(); }
350  double& Tau() { return raSearch->Tau(); }
351 
353  double Alpha() const { return raSearch->Alpha(); }
355  double& Alpha() { return raSearch->Alpha(); }
356 
358  bool SampleAtLeaves() const { return raSearch->SampleAtLeaves(); }
360  bool& SampleAtLeaves() { return raSearch->SampleAtLeaves(); }
361 
363  bool FirstLeafExact() const { return raSearch->FirstLeafExact(); }
365  bool& FirstLeafExact() { return raSearch->FirstLeafExact(); }
366 
368  size_t SingleSampleLimit() const { return raSearch->SingleSampleLimit(); }
370  size_t& SingleSampleLimit() { return raSearch->SingleSampleLimit(); }
371 
373  size_t LeafSize() const { return leafSize; }
375  size_t& LeafSize() { return leafSize; }
376 
378  TreeTypes TreeType() const { return treeType; }
380  TreeTypes& TreeType() { return treeType; }
381 
383  bool RandomBasis() const { return randomBasis; }
386  bool& RandomBasis() { return randomBasis; }
387 
389  void InitializeModel(const bool naive, const bool singleMode);
390 
392  void BuildModel(arma::mat&& referenceSet,
393  const size_t leafSize,
394  const bool naive,
395  const bool singleMode);
396 
399  void Search(arma::mat&& querySet,
400  const size_t k,
401  arma::Mat<size_t>& neighbors,
402  arma::mat& distances);
403 
408  void Search(const size_t k,
409  arma::Mat<size_t>& neighbors,
410  arma::mat& distances);
411 
413  std::string TreeName() const;
414 };
415 
416 } // namespace neighbor
417 } // namespace mlpack
418 
419 #include "ra_model_impl.hpp"
420 
421 #endif
bool & SampleAtLeaves()
Modify whether or not sampling is done at the leaves.
Definition: ra_model.hpp:360
double & Alpha()
Modify the value of alpha.
Definition: ra_model.hpp:148
double Alpha() const
Get the desired success probability.
Definition: ra_model.hpp:353
bool & SampleAtLeaves()
Modify whether to do sampling at leaves.
Definition: ra_model.hpp:143
void serialize(Archive &ar, const uint32_t)
Serialize the RASearch model.
Definition: ra_model.hpp:185
bool SingleMode() const
Get whether or not single-tree search is being used.
Definition: ra_model.hpp:338
bool & RandomBasis()
Modify whether or not a random basis is being used.
Definition: ra_model.hpp:386
bool Naive() const
Get whether or not naive search is being used.
Definition: ra_model.hpp:343
virtual RAWrapperBase * Clone() const =0
Create a new RAWrapperBase that is the same as this one.
double Tau() const
Get the value of tau.
Definition: ra_model.hpp:151
virtual const arma::mat & Dataset() const =0
Return a reference to the dataset.
RAWrapper(const bool singleMode, const bool naive)
Construct the RAWrapper object, initializing the internally-held RASearch object. ...
Definition: ra_model.hpp:114
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
virtual bool Naive() const =0
Get whether naive search is being used.
RAType ra
The instantiated RASearch object that we are wrapping.
Definition: ra_model.hpp:197
virtual bool SingleMode() const =0
Get whether single-tree search is being used.
virtual double Tau() const =0
Get the value of tau.
size_t LeafSize() const
Get the leaf size (only relevant when the kd-tree is used).
Definition: ra_model.hpp:373
bool RandomBasis() const
Get whether or not a random basis is being used.
Definition: ra_model.hpp:383
bool SampleAtLeaves() const
Get whether to do sampling at leaves.
Definition: ra_model.hpp:141
bool & FirstLeafExact()
Modify whether or not we traverse to the first leaf without approximation.
Definition: ra_model.hpp:365
bool SingleMode() const
Get whether single-tree search is being used.
Definition: ra_model.hpp:156
bool & Naive()
Modify whether or not naive search is being used.
Definition: ra_model.hpp:345
size_t & SingleSampleLimit()
Modify the single sample limit.
Definition: ra_model.hpp:133
LeafSizeRAWrapper(const bool singleMode, const bool naive)
Construct the LeafSizeRAWrapper by delegating to the RAWrapper constructor.
Definition: ra_model.hpp:213
size_t & LeafSize()
Modify the leaf size (only relevant when the kd-tree is used).
Definition: ra_model.hpp:375
virtual bool SampleAtLeaves() const =0
Get whether to do sampling at leaves.
virtual bool FirstLeafExact() const =0
Get whether to do exact search at the first leaf.
double Tau() const
Get the rank-approximation in percentile of the data.
Definition: ra_model.hpp:348
double Alpha() const
Get the value of alpha.
Definition: ra_model.hpp:146
virtual RAWrapper * Clone() const
Create a copy of this RAWrapper object.
Definition: ra_model.hpp:125
virtual double Alpha() const =0
Get the value of alpha.
This class implements the necessary methods for the SortPolicy template parameter of the NeighborSear...
Definition: nearest_neighbor_sort.hpp:31
virtual void Search(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.
virtual ~RAWrapperBase()
Destruct the RAWrapperBase (nothing to do).
Definition: ra_model.hpp:44
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
Definition: ra_model.hpp:358
virtual LeafSizeRAWrapper * Clone() const
Return a copy of the LeafSizeRAWrapper.
Definition: ra_model.hpp:223
bool FirstLeafExact() const
Get whether or not we traverse to the first leaf without approximation.
Definition: ra_model.hpp:363
virtual void Train(arma::mat &&referenceSet, const size_t leafSize)=0
Train the RASearch model with the given parameters.
The L_p metric for arbitrary integer p, with an option to take the root.
Definition: lmetric.hpp:63
LeafSizeRAWrapper wraps any RASearch type that needs to be able to take the leaf size into account wh...
Definition: ra_model.hpp:208
TreeTypes
The list of tree types we can use with RASearch.
Definition: ra_model.hpp:264
bool Naive() const
Get whether naive search is being used.
Definition: ra_model.hpp:161
void serialize(Archive &ar, const uint32_t)
Serialize the RASearch model.
Definition: ra_model.hpp:242
const arma::mat & Dataset() const
Get a reference to the reference set.
Definition: ra_model.hpp:128
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
Definition: ra_model.hpp:368
bool & SingleMode()
Modify whether or not single-tree search is being used.
Definition: ra_model.hpp:340
The RASearch class: This class provides a generic manner to perform rank-approximate search via rando...
Definition: ra_search.hpp:77
bool FirstLeafExact() const
Get whether to do exact search at the first leaf.
Definition: ra_model.hpp:136
RAWrapper is a wrapper class for most RASearch types.
Definition: ra_model.hpp:109
virtual ~LeafSizeRAWrapper()
Delete the LeafSizeRAWrapper.
Definition: ra_model.hpp:220
virtual size_t SingleSampleLimit() const =0
Get the single sample limit.
double & Tau()
Modify the rank-approximation in percentile of the data.
Definition: ra_model.hpp:350
const arma::mat & Dataset() const
Expose the dataset.
Definition: ra_model.hpp:335
bool & Naive()
Modify whether naive search is being used.
Definition: ra_model.hpp:163
size_t SingleSampleLimit() const
Get the single sample limit.
Definition: ra_model.hpp:131
RAWrapperBase is a base wrapper class for holding all RASearch types supported by RAModel...
Definition: ra_model.hpp:32
virtual ~RAWrapper()
Delete the RAWrapper object.
Definition: ra_model.hpp:121
size_t & SingleSampleLimit()
Modify the limit on the size of a node that can be approximation.
Definition: ra_model.hpp:370
RAWrapperBase()
Create the RAWrapperBase object.
Definition: ra_model.hpp:37
double & Alpha()
Modify the desired success probability.
Definition: ra_model.hpp:355
The RAModel class provides an abstraction for the RASearch class, abstracting away the TreeType param...
Definition: ra_model.hpp:257
bool & SingleMode()
Modify whether single-tree search is being used.
Definition: ra_model.hpp:158
TreeTypes & TreeType()
Modify the type of tree being used.
Definition: ra_model.hpp:380
TreeTypes TreeType() const
Get the type of tree being used.
Definition: ra_model.hpp:378
double & Tau()
Modify the value of tau.
Definition: ra_model.hpp:153
bool & FirstLeafExact()
Modify whether to do exact search at the first leaf.
Definition: ra_model.hpp:138