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(util::Timers& timers,
63  arma::mat&& referenceSet,
64  const size_t leafSize) = 0;
65 
68  virtual void Search(util::Timers& timers,
69  arma::mat&& querySet,
70  const math::Range& range,
71  std::vector<std::vector<size_t>>& neighbors,
72  std::vector<std::vector<double>>& distances,
73  const size_t leafSize) = 0;
74 
77  virtual void Search(util::Timers& timers,
78  const math::Range& range,
79  std::vector<std::vector<size_t>>& neighbors,
80  std::vector<std::vector<double>>& distances) = 0;
81 };
82 
86 template<template<typename TreeMetricType,
87  typename TreeStatType,
88  typename TreeMatType> class TreeType>
89 class RSWrapper : public RSWrapperBase
90 {
91  public:
93  RSWrapper(const bool singleMode, const bool naive) :
94  rs(singleMode, naive)
95  {
96  // Nothing else to do.
97  }
98 
101  virtual RSWrapper* Clone() const { return new RSWrapper(*this); }
102 
104  virtual ~RSWrapper() { }
105 
107  const arma::mat& Dataset() const { return rs.ReferenceSet(); }
108 
110  bool SingleMode() const { return rs.SingleMode(); }
112  bool& SingleMode() { return rs.SingleMode(); }
113 
115  bool Naive() const { return rs.Naive(); }
117  bool& Naive() { return rs.Naive(); }
118 
121  virtual void Train(util::Timers& timers,
122  arma::mat&& referenceSet,
123  const size_t /* leafSize */);
124 
127  virtual void Search(util::Timers& timers,
128  arma::mat&& querySet,
129  const math::Range& range,
130  std::vector<std::vector<size_t>>& neighbors,
131  std::vector<std::vector<double>>& distances,
132  const size_t /* leafSize */);
133 
136  virtual void Search(util::Timers& timers,
137  const math::Range& range,
138  std::vector<std::vector<size_t>>& neighbors,
139  std::vector<std::vector<double>>& distances);
140 
142  template<typename Archive>
143  void serialize(Archive& ar, const uint32_t /* version */)
144  {
145  ar(CEREAL_NVP(rs));
146  }
147 
148  protected:
150 
152  RSType rs;
153 };
154 
160 template<template<typename TreeMetricType,
161  typename TreeStatType,
162  typename TreeMatType> class TreeType>
163 class LeafSizeRSWrapper : public RSWrapper<TreeType>
164 {
165  public:
168  LeafSizeRSWrapper(const bool singleMode, const bool naive) :
169  RSWrapper<TreeType>(singleMode, naive)
170  {
171  // Nothing else to do.
172  }
173 
175  virtual ~LeafSizeRSWrapper() { }
176 
178  virtual LeafSizeRSWrapper* Clone() const
179  {
180  return new LeafSizeRSWrapper(*this);
181  }
182 
184  virtual void Train(util::Timers& timers,
185  arma::mat&& referenceSet,
186  const size_t leafSize);
187 
190  virtual void Search(util::Timers& timers,
191  arma::mat&& querySet,
192  const math::Range& range,
193  std::vector<std::vector<size_t>>& neighbors,
194  std::vector<std::vector<double>>& distances,
195  const size_t leafSize);
196 
198  template<typename Archive>
199  void serialize(Archive& ar, const uint32_t /* version */)
200  {
201  ar(CEREAL_NVP(rs));
202  }
203 
204  protected:
206 };
207 
214 class RSModel
215 {
216  public:
218  {
232  OCTREE
233  };
234 
242  RSModel(const TreeTypes treeType = TreeTypes::KD_TREE,
243  const bool randomBasis = false);
244 
250  RSModel(const RSModel& other);
251 
257  RSModel(RSModel&& other);
258 
264  RSModel& operator=(const RSModel& other);
265 
271  RSModel& operator=(RSModel&& other);
272 
276  ~RSModel();
277 
279  template<typename Archive>
280  void serialize(Archive& ar, const uint32_t /* version */);
281 
283  const arma::mat& Dataset() const { return rSearch->Dataset(); }
284 
286  bool SingleMode() const { return rSearch->SingleMode(); }
288  bool& SingleMode() { return rSearch->SingleMode(); }
289 
291  bool Naive() const { return rSearch->Naive(); }
293  bool& Naive() { return rSearch->Naive(); }
294 
296  size_t LeafSize() const { return leafSize; }
298  size_t& LeafSize() { return leafSize; }
299 
301  TreeTypes TreeType() const { return treeType; }
303  TreeTypes& TreeType() { return treeType; }
304 
306  bool RandomBasis() const { return randomBasis; }
309  bool& RandomBasis() { return randomBasis; }
310 
314  void InitializeModel(const bool naive, const bool singleMode);
315 
325  void BuildModel(util::Timers& timers,
326  arma::mat&& referenceSet,
327  const size_t leafSize,
328  const bool naive,
329  const bool singleMode);
330 
341  void Search(util::Timers& timers,
342  arma::mat&& querySet,
343  const math::Range& range,
344  std::vector<std::vector<size_t>>& neighbors,
345  std::vector<std::vector<double>>& distances);
346 
356  void Search(util::Timers& timers,
357  const math::Range& range,
358  std::vector<std::vector<size_t>>& neighbors,
359  std::vector<std::vector<double>>& distances);
360 
361  private:
363  TreeTypes treeType;
366  size_t leafSize;
367 
369  bool randomBasis;
371  arma::mat q;
372 
377  RSWrapperBase* rSearch;
378 
383  std::string TreeName() const;
384 
388  void CleanMemory();
389 };
390 
391 } // namespace range
392 } // namespace mlpack
393 
394 // Include implementation (of serialize() and templated wrapper classes).
395 #include "rs_model_impl.hpp"
396 
397 #endif
RSWrapper is a wrapper class for most RangeSearch types.
Definition: rs_model.hpp:89
bool & Naive()
Modify whether the model is in naive search mode.
Definition: rs_model.hpp:293
TreeTypes TreeType() const
Get the type of tree.
Definition: rs_model.hpp:301
virtual void Train(util::Timers &timers, arma::mat &&referenceSet, const size_t leafSize)=0
Train the model (build the reference tree if needed).
bool Naive() const
Get whether naive search is being used.
Definition: rs_model.hpp:115
const arma::mat & Dataset() const
Get the dataset.
Definition: rs_model.hpp:107
virtual ~LeafSizeRSWrapper()
Delete the LeafSizeRSWrapper.
Definition: rs_model.hpp:175
Linear algebra utility functions, generally performed on matrices or vectors.
bool SingleMode() const
Get whether single-tree search is being used.
Definition: rs_model.hpp:110
virtual RSWrapperBase * Clone() const =0
Create a new RSWrapperBase that is the same as this one.
void serialize(Archive &ar, const uint32_t)
Serialize the RangeSearch model.
Definition: rs_model.hpp:199
virtual const arma::mat & Dataset() const =0
Get the dataset.
size_t LeafSize() const
Get the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:296
bool & SingleMode()
Modify whether single-tree search is being used.
Definition: rs_model.hpp:112
bool & Naive()
Modify whether naive search is being used.
Definition: rs_model.hpp:117
RSWrapperBase()
Create the RSWrapperBase object.
Definition: rs_model.hpp:39
RSWrapper(const bool singleMode, const bool naive)
Create the RSWrapper object.
Definition: rs_model.hpp:93
bool SingleMode() const
Get whether the model is in single-tree search mode.
Definition: rs_model.hpp:286
virtual bool SingleMode() const =0
Get whether single-tree search is being used.
The RSModel class provides an abstraction for the RangeSearch class, abstracting away the TreeType pa...
Definition: rs_model.hpp:214
void serialize(Archive &ar, const uint32_t)
Serialize the RangeSearch model.
Definition: rs_model.hpp:143
virtual LeafSizeRSWrapper * Clone() const
Return a copy of the LeafSizeRSWrapper.
Definition: rs_model.hpp:178
TreeTypes & TreeType()
Modify the type of tree (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:303
size_t & LeafSize()
Modify the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:298
RSType rs
The instantiated RangeSearch object that we are wrapping.
Definition: rs_model.hpp:152
virtual RSWrapper * Clone() const
Create a new RSWrapper that is the same as this one.
Definition: rs_model.hpp:101
virtual void Search(util::Timers &timers, 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.
void CleanMemory(util::Params &params)
Delete any unique pointers that are held by the IO object.
virtual bool Naive() const =0
Get whether naive search is being used.
LeafSizeRSWrapper(const bool singleMode, const bool naive)
Construct the LeafSizeRSWrapper by delegating to the RSWrapper constructor.
Definition: rs_model.hpp:168
bool Naive() const
Get whether the model is in naive search mode.
Definition: rs_model.hpp:291
virtual ~RSWrapper()
Destruct the RSWrapper (nothing to do).
Definition: rs_model.hpp:104
bool & RandomBasis()
Modify whether a random basis is used (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:309
bool RandomBasis() const
Get whether a random basis is used.
Definition: rs_model.hpp:306
RSWrapperBase is a base wrapper class for holding all RangeSearch types supported by RSModel...
Definition: rs_model.hpp:34
bool & SingleMode()
Modify whether the model is in single-tree search mode.
Definition: rs_model.hpp:288
virtual ~RSWrapperBase()
Destruct the RSWrapperBase (nothing to do).
Definition: rs_model.hpp:46
const arma::mat & Dataset() const
Expose the dataset.
Definition: rs_model.hpp:283
RangeSearch< metric::EuclideanDistance, arma::mat, TreeType > RSType
Definition: rs_model.hpp:149