mlpack  3.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #include <boost/variant.hpp>
23 #include "range_search.hpp"
24 
25 namespace mlpack {
26 namespace range {
27 
31 template<template<typename TreeMetricType,
32  typename TreeStatType,
33  typename TreeMatType> class TreeType>
35 
40 class MonoSearchVisitor : public boost::static_visitor<void>
41 {
42  private:
44  const math::Range& range;
46  std::vector<std::vector<size_t>>& neighbors;
48  std::vector<std::vector<double>>& distances;
49 
50  public:
52  template<typename RSType>
53  void operator()(RSType* rs) const;
54 
57  std::vector<std::vector<size_t>>& neighbors,
58  std::vector<std::vector<double>>& distances):
59  range(range),
60  neighbors(neighbors),
61  distances(distances)
62  {};
63 };
64 
71 class BiSearchVisitor : public boost::static_visitor<void>
72 {
73  private:
75  const arma::mat& querySet;
77  const math::Range& range;
79  std::vector<std::vector<size_t>>& neighbors;
81  std::vector<std::vector<double>>& distances;
83  const size_t leafSize;
84 
86  template<typename RSType>
87  void SearchLeaf(RSType* rs) const;
88 
89  public:
91  template<template<typename TreeMetricType,
92  typename TreeStatType,
93  typename TreeMatType> class TreeType>
95 
97  template<template<typename TreeMetricType,
98  typename TreeStatType,
99  typename TreeMatType> class TreeType>
100  void operator()(RSTypeT<TreeType>* rs) const;
101 
103  void operator()(RSTypeT<tree::KDTree>* rs) const;
104 
106  void operator()(RSTypeT<tree::BallTree>* rs) const;
107 
109  void operator()(RSTypeT<tree::Octree>* rs) const;
110 
112  BiSearchVisitor(const arma::mat& querySet,
113  const math::Range& range,
114  std::vector<std::vector<size_t>>& neighbors,
115  std::vector<std::vector<double>>& distances,
116  const size_t leafSize);
117 };
118 
125 class TrainVisitor : public boost::static_visitor<void>
126 {
127  private:
129  arma::mat&& referenceSet;
131  size_t leafSize;
133  template<typename RSType>
134  void TrainLeaf(RSType* rs) const;
135 
136  public:
138  template<template<typename TreeMetricType,
139  typename TreeStatType,
140  typename TreeMatType> class TreeType>
142 
144  template<template<typename TreeMetricType,
145  typename TreeStatType,
146  typename TreeMatType> class TreeType>
147  void operator()(RSTypeT<TreeType>* rs) const;
148 
150  void operator()(RSTypeT<tree::KDTree>* rs) const;
151 
153  void operator()(RSTypeT<tree::BallTree>* rs) const;
154 
156  void operator()(RSTypeT<tree::Octree>* rs) const;
157 
159  TrainVisitor(arma::mat&& referenceSet,
160  const size_t leafSize);
161 };
162 
166 class ReferenceSetVisitor : public boost::static_visitor<const arma::mat&>
167 {
168  public:
170  template<typename RSType>
171  const arma::mat& operator()(RSType* rs) const;
172 };
173 
177 class DeleteVisitor : public boost::static_visitor<void>
178 {
179  public:
181  template<typename RSType>
182  void operator()(RSType* rs) const;
183 };
184 
188 class SingleModeVisitor : public boost::static_visitor<bool&>
189 {
190  public:
195  template<typename RSType>
196  bool& operator()(RSType* rs) const;
197 };
198 
202 class NaiveVisitor : public boost::static_visitor<bool&>
203 {
204  public:
208  template<typename RSType>
209  bool& operator()(RSType* rs) const;
210 };
211 
212 class RSModel
213 {
214  public:
216  {
231  };
232 
233  private:
234  TreeTypes treeType;
235  size_t leafSize;
236 
238  bool randomBasis;
240  arma::mat q;
241 
247  boost::variant<RSType<tree::KDTree>*,
260  RSType<tree::Octree>*> rSearch;
261 
262  public:
270  RSModel(const TreeTypes treeType = TreeTypes::KD_TREE,
271  const bool randomBasis = false);
272 
278  RSModel(const RSModel& other);
279 
285  RSModel(RSModel&& other);
286 
294  RSModel& operator=(RSModel other);
295 
299  ~RSModel();
300 
302  template<typename Archive>
303  void serialize(Archive& ar, const unsigned int /* version */);
304 
306  const arma::mat& Dataset() const;
307 
309  bool SingleMode() const;
311  bool& SingleMode();
312 
314  bool Naive() const;
316  bool& Naive();
317 
319  size_t LeafSize() const { return leafSize; }
321  size_t& LeafSize() { return leafSize; }
322 
324  TreeTypes TreeType() const { return treeType; }
326  TreeTypes& TreeType() { return treeType; }
327 
329  bool RandomBasis() const { return randomBasis; }
332  bool& RandomBasis() { return randomBasis; }
333 
343  void BuildModel(arma::mat&& referenceSet,
344  const size_t leafSize,
345  const bool naive,
346  const bool singleMode);
347 
358  void Search(arma::mat&& querySet,
359  const math::Range& range,
360  std::vector<std::vector<size_t>>& neighbors,
361  std::vector<std::vector<double>>& distances);
362 
372  void Search(const math::Range& range,
373  std::vector<std::vector<size_t>>& neighbors,
374  std::vector<std::vector<double>>& distances);
375 
376  private:
381  std::string TreeName() const;
382 
386  void CleanMemory();
387 };
388 
389 } // namespace range
390 } // namespace mlpack
391 
392 // Include implementation (of serialize() and inline functions).
393 #include "rs_model_impl.hpp"
394 
395 #endif
The RangeSearch class is a template class for performing range searches.
bool SingleMode() const
Get whether the model is in single-tree search mode.
void Search(arma::mat &&querySet, const math::Range &range, std::vector< std::vector< size_t >> &neighbors, std::vector< std::vector< double >> &distances)
Perform range search.
bool RandomBasis() const
Get whether a random basis is used.
Definition: rs_model.hpp:329
bool & operator()(RSType *rs) const
Get a reference to the naive parameter of the given RangeSearch object.
~RSModel()
Clean memory, if necessary.
ReferenceSetVisitor exposes the referenceSet of the given RSType.
Definition: rs_model.hpp:166
RSModel & operator=(RSModel other)
Copy the given RSModel.
void serialize(Archive &ar, const unsigned int)
Serialize the range search model.
const arma::mat & operator()(RSType *rs) const
Return the reference set.
void BuildModel(arma::mat &&referenceSet, const size_t leafSize, const bool naive, const bool singleMode)
Build the reference tree on the given dataset with the given parameters.
void operator()(RSType *rs) const
Perform monochromatic search with the given RangeSearch object.
TrainVisitor(arma::mat &&referenceSet, const size_t leafSize)
Construct the TrainVisitor object with the given reference set, leafSize.
TreeTypes TreeType() const
Get the type of tree.
Definition: rs_model.hpp:324
size_t LeafSize() const
Get the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:319
MonoSearchVisitor(const math::Range &range, std::vector< std::vector< size_t >> &neighbors, std::vector< std::vector< double >> &distances)
Construct the MonoSearchVisitor with the given parameters.
Definition: rs_model.hpp:56
void operator()(RSTypeT< TreeType > *rs) const
Default Bichromatic range search on the given RSType instance.
NaiveVisitor exposes the Naive() method of the given RSType.
Definition: rs_model.hpp:202
bool Naive() const
Get whether the model is in naive search mode.
TrainVisitor sets the reference set to a new reference set on the given RSType.
Definition: rs_model.hpp:125
TreeTypes & TreeType()
Modify the type of tree (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:326
size_t & LeafSize()
Modify the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:321
const arma::mat & Dataset() const
Expose the dataset.
MonoSearchVisitor executes a monochromatic range search on the given RSType.
Definition: rs_model.hpp:40
BiSearchVisitor(const arma::mat &querySet, const math::Range &range, std::vector< std::vector< size_t >> &neighbors, std::vector< std::vector< double >> &distances, const size_t leafSize)
Construct the BiSearchVisitor.
void operator()(RSTypeT< TreeType > *rs) const
Default Train on the given RSType instance.
BiSearchVisitor executes a bichromatic range search on the given RSType.
Definition: rs_model.hpp:71
void operator()(RSType *rs) const
Delete the RSType object.
bool & RandomBasis()
Modify whether a random basis is used (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:332
SingleModeVisitor exposes the SingleMode() method of the given RSType.
Definition: rs_model.hpp:188
RSModel(const TreeTypes treeType=TreeTypes::KD_TREE, const bool randomBasis=false)
Initialize the RSModel with the given type and whether or not a random basis should be used...
bool & operator()(RSType *rs) const
Get a reference to the singleMode parameter of the given RangeSeach object.
DeleteVisitor deletes the given RSType instance.
Definition: rs_model.hpp:177
src mlpack core util version hpp VERSION_HPP_CONTENTS string(REGEX REPLACE".*#define MLPACK_VERSION_MAJOR ([0-9]+).*""\\1"MLPACK_VERSION_MAJOR"${VERSION_HPP_CONTENTS}") string(REGEX REPLACE".* MLPACK_VERSION_MINOR "$
Definition: CMakeLists.txt:79