mlpack
rs_model.hpp
Go to the documentation of this file.
1 
15 #ifndef MLPACK_METHODS_RANGE_SEARCH_RS_MODEL_HPP
16 #define MLPACK_METHODS_RANGE_SEARCH_RS_MODEL_HPP
17 
22 
23 #include "range_search.hpp"
24 
25 namespace mlpack {
26 namespace range {
27 
35 {
36  public:
40 
43  virtual RSWrapperBase* Clone() const = 0;
44 
46  virtual ~RSWrapperBase() { }
47 
49  virtual const arma::mat& Dataset() const = 0;
50 
52  virtual bool SingleMode() const = 0;
54  virtual bool& SingleMode() = 0;
55 
57  virtual bool Naive() const = 0;
59  virtual bool& Naive() = 0;
60 
62  virtual void Train(arma::mat&& referenceSet,
63  const size_t leafSize) = 0;
64 
67  virtual void Search(arma::mat&& querySet,
68  const math::Range& range,
69  std::vector<std::vector<size_t>>& neighbors,
70  std::vector<std::vector<double>>& distances,
71  const size_t leafSize) = 0;
72 
75  virtual void Search(const math::Range& range,
76  std::vector<std::vector<size_t>>& neighbors,
77  std::vector<std::vector<double>>& distances) = 0;
78 };
79 
83 template<template<typename TreeMetricType,
84  typename TreeStatType,
85  typename TreeMatType> class TreeType>
86 class RSWrapper : public RSWrapperBase
87 {
88  public:
90  RSWrapper(const bool singleMode, const bool naive) :
91  rs(singleMode, naive)
92  {
93  // Nothing else to do.
94  }
95 
98  virtual RSWrapper* Clone() const { return new RSWrapper(*this); }
99 
101  virtual ~RSWrapper() { }
102 
104  const arma::mat& Dataset() const { return rs.ReferenceSet(); }
105 
107  bool SingleMode() const { return rs.SingleMode(); }
109  bool& SingleMode() { return rs.SingleMode(); }
110 
112  bool Naive() const { return rs.Naive(); }
114  bool& Naive() { return rs.Naive(); }
115 
118  virtual void Train(arma::mat&& referenceSet,
119  const size_t /* leafSize */);
120 
123  virtual void Search(arma::mat&& querySet,
124  const math::Range& range,
125  std::vector<std::vector<size_t>>& neighbors,
126  std::vector<std::vector<double>>& distances,
127  const size_t /* leafSize */);
128 
131  virtual void Search(const math::Range& range,
132  std::vector<std::vector<size_t>>& neighbors,
133  std::vector<std::vector<double>>& distances);
134 
136  template<typename Archive>
137  void serialize(Archive& ar, const uint32_t /* version */)
138  {
139  ar(CEREAL_NVP(rs));
140  }
141 
142  protected:
144 
146  RSType rs;
147 };
148 
154 template<template<typename TreeMetricType,
155  typename TreeStatType,
156  typename TreeMatType> class TreeType>
157 class LeafSizeRSWrapper : public RSWrapper<TreeType>
158 {
159  public:
162  LeafSizeRSWrapper(const bool singleMode, const bool naive) :
163  RSWrapper<TreeType>(singleMode, naive)
164  {
165  // Nothing else to do.
166  }
167 
169  virtual ~LeafSizeRSWrapper() { }
170 
172  virtual LeafSizeRSWrapper* Clone() const
173  {
174  return new LeafSizeRSWrapper(*this);
175  }
176 
178  virtual void Train(arma::mat&& referenceSet,
179  const size_t leafSize);
180 
183  virtual void Search(arma::mat&& querySet,
184  const math::Range& range,
185  std::vector<std::vector<size_t>>& neighbors,
186  std::vector<std::vector<double>>& distances,
187  const size_t leafSize);
188 
190  template<typename Archive>
191  void serialize(Archive& ar, const uint32_t /* version */)
192  {
193  ar(CEREAL_NVP(rs));
194  }
195 
196  protected:
198 };
199 
206 class RSModel
207 {
208  public:
209  enum TreeTypes
210  {
211  KD_TREE,
212  COVER_TREE,
213  R_TREE,
214  R_STAR_TREE,
215  BALL_TREE,
216  X_TREE,
217  HILBERT_R_TREE,
218  R_PLUS_TREE,
219  R_PLUS_PLUS_TREE,
220  VP_TREE,
221  RP_TREE,
222  MAX_RP_TREE,
223  UB_TREE,
224  OCTREE
225  };
226 
234  RSModel(const TreeTypes treeType = TreeTypes::KD_TREE,
235  const bool randomBasis = false);
236 
242  RSModel(const RSModel& other);
243 
249  RSModel(RSModel&& other);
250 
256  RSModel& operator=(const RSModel& other);
257 
263  RSModel& operator=(RSModel&& other);
264 
268  ~RSModel();
269 
271  template<typename Archive>
272  void serialize(Archive& ar, const uint32_t /* version */);
273 
275  const arma::mat& Dataset() const { return rSearch->Dataset(); }
276 
278  bool SingleMode() const { return rSearch->SingleMode(); }
280  bool& SingleMode() { return rSearch->SingleMode(); }
281 
283  bool Naive() const { return rSearch->Naive(); }
285  bool& Naive() { return rSearch->Naive(); }
286 
288  size_t LeafSize() const { return leafSize; }
290  size_t& LeafSize() { return leafSize; }
291 
293  TreeTypes TreeType() const { return treeType; }
295  TreeTypes& TreeType() { return treeType; }
296 
298  bool RandomBasis() const { return randomBasis; }
301  bool& RandomBasis() { return randomBasis; }
302 
306  void InitializeModel(const bool naive, const bool singleMode);
307 
317  void BuildModel(arma::mat&& referenceSet,
318  const size_t leafSize,
319  const bool naive,
320  const bool singleMode);
321 
332  void Search(arma::mat&& querySet,
333  const math::Range& range,
334  std::vector<std::vector<size_t>>& neighbors,
335  std::vector<std::vector<double>>& distances);
336 
346  void Search(const math::Range& range,
347  std::vector<std::vector<size_t>>& neighbors,
348  std::vector<std::vector<double>>& distances);
349 
350  private:
352  TreeTypes treeType;
355  size_t leafSize;
356 
358  bool randomBasis;
360  arma::mat q;
361 
366  RSWrapperBase* rSearch;
367 
372  std::string TreeName() const;
373 
377  void CleanMemory();
378 };
379 
380 } // namespace range
381 } // namespace mlpack
382 
383 // Include implementation (of serialize() and templated wrapper classes).
384 #include "rs_model_impl.hpp"
385 
386 #endif
RSWrapper is a wrapper class for most RangeSearch types.
Definition: rs_model.hpp:86
bool & Naive()
Modify whether naive search is being used.
Definition: rs_model.hpp:114
bool SingleMode() const
Get whether the model is in single-tree search mode.
Definition: rs_model.hpp:278
virtual const arma::mat & Dataset() const =0
Get the dataset.
const arma::mat & Dataset() const
Get the dataset.
Definition: rs_model.hpp:104
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
bool Naive() const
Get whether the model is in naive search mode.
Definition: rs_model.hpp:283
virtual void Train(arma::mat &&referenceSet, const size_t leafSize)=0
Train the model (build the reference tree if needed).
bool & RandomBasis()
Modify whether a random basis is used (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:301
virtual RSWrapper * Clone() const
Create a new RSWrapper that is the same as this one.
Definition: rs_model.hpp:98
bool Naive() const
Get whether naive search is being used.
Definition: rs_model.hpp:112
void serialize(Archive &ar, const uint32_t)
Serialize the RangeSearch model.
Definition: rs_model.hpp:137
TreeTypes TreeType() const
Get the type of tree.
Definition: rs_model.hpp:293
Forward declaration.
Definition: range_search.hpp:28
bool SingleMode() const
Get whether single-tree search is being used.
Definition: rs_model.hpp:107
bool & Naive()
Modify whether the model is in naive search mode.
Definition: rs_model.hpp:285
virtual bool SingleMode() const =0
Get whether single-tree search is being used.
size_t LeafSize() const
Get the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:288
TreeTypes & TreeType()
Modify the type of tree (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:295
RSType rs
The instantiated RangeSearch object that we are wrapping.
Definition: rs_model.hpp:146
virtual ~RSWrapperBase()
Destruct the RSWrapperBase (nothing to do).
Definition: rs_model.hpp:46
virtual void Search(arma::mat &&querySet, const math::Range &range, std::vector< std::vector< size_t >> &neighbors, std::vector< std::vector< double >> &distances, const size_t leafSize)=0
Perform bichromatic range search (i.e.
bool & SingleMode()
Modify whether single-tree search is being used.
Definition: rs_model.hpp:109
bool RandomBasis() const
Get whether a random basis is used.
Definition: rs_model.hpp:298
The RSModel class provides an abstraction for the RangeSearch class, abstracting away the TreeType pa...
Definition: rs_model.hpp:206
void serialize(Archive &ar, const uint32_t)
Serialize the RangeSearch model.
Definition: rs_model.hpp:191
bool & SingleMode()
Modify whether the model is in single-tree search mode.
Definition: rs_model.hpp:280
size_t & LeafSize()
Modify the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:290
virtual bool Naive() const =0
Get whether naive search is being used.
const arma::mat & Dataset() const
Expose the dataset.
Definition: rs_model.hpp:275
LeafSizeRSWrapper(const bool singleMode, const bool naive)
Construct the LeafSizeRSWrapper by delegating to the RSWrapper constructor.
Definition: rs_model.hpp:162
virtual RSWrapperBase * Clone() const =0
Create a new RSWrapperBase that is the same as this one.
virtual ~RSWrapper()
Destruct the RSWrapper (nothing to do).
Definition: rs_model.hpp:101
RSWrapperBase()
Create the RSWrapperBase object.
Definition: rs_model.hpp:39
virtual ~LeafSizeRSWrapper()
Delete the LeafSizeRSWrapper.
Definition: rs_model.hpp:169
RSWrapperBase is a base wrapper class for holding all RangeSearch types supported by RSModel...
Definition: rs_model.hpp:34
RSWrapper(const bool singleMode, const bool naive)
Create the RSWrapper object.
Definition: rs_model.hpp:90
virtual LeafSizeRSWrapper * Clone() const
Return a copy of the LeafSizeRSWrapper.
Definition: rs_model.hpp:172