mlpack  2.2.5
 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 
37 {
38  static const std::string Name() { return "range_search_model"; }
39 };
40 
45 class MonoSearchVisitor : public boost::static_visitor<void>
46 {
47  private:
49  const math::Range& range;
51  std::vector<std::vector<size_t>>& neighbors;
53  std::vector<std::vector<double>>& distances;
54 
55  public:
57  template<typename RSType>
58  void operator()(RSType* rs) const;
59 
62  std::vector<std::vector<size_t>>& neighbors,
63  std::vector<std::vector<double>>& distances):
64  range(range),
65  neighbors(neighbors),
66  distances(distances)
67  {};
68 };
69 
76 class BiSearchVisitor : public boost::static_visitor<void>
77 {
78  private:
80  const arma::mat& querySet;
82  const math::Range& range;
84  std::vector<std::vector<size_t>>& neighbors;
86  std::vector<std::vector<double>>& distances;
88  const size_t leafSize;
89 
91  template<typename RSType>
92  void SearchLeaf(RSType* rs) const;
93 
94  public:
96  template<template<typename TreeMetricType,
97  typename TreeStatType,
98  typename TreeMatType> class TreeType>
100 
102  template<template<typename TreeMetricType,
103  typename TreeStatType,
104  typename TreeMatType> class TreeType>
105  void operator()(RSTypeT<TreeType>* rs) const;
106 
108  void operator()(RSTypeT<tree::KDTree>* rs) const;
109 
111  void operator()(RSTypeT<tree::BallTree>* rs) const;
112 
114  void operator()(RSTypeT<tree::Octree>* rs) const;
115 
117  BiSearchVisitor(const arma::mat& querySet,
118  const math::Range& range,
119  std::vector<std::vector<size_t>>& neighbors,
120  std::vector<std::vector<double>>& distances,
121  const size_t leafSize);
122 };
123 
130 class TrainVisitor : public boost::static_visitor<void>
131 {
132  private:
134  arma::mat&& referenceSet;
136  size_t leafSize;
138  template<typename RSType>
139  void TrainLeaf(RSType* rs) const;
140 
141  public:
143  template<template<typename TreeMetricType,
144  typename TreeStatType,
145  typename TreeMatType> class TreeType>
147 
149  template<template<typename TreeMetricType,
150  typename TreeStatType,
151  typename TreeMatType> class TreeType>
152  void operator()(RSTypeT<TreeType>* rs) const;
153 
155  void operator()(RSTypeT<tree::KDTree>* rs) const;
156 
158  void operator()(RSTypeT<tree::BallTree>* rs) const;
159 
161  void operator()(RSTypeT<tree::Octree>* rs) const;
162 
164  TrainVisitor(arma::mat&& referenceSet,
165  const size_t leafSize);
166 };
167 
171 class ReferenceSetVisitor : public boost::static_visitor<const arma::mat&>
172 {
173  public:
175  template<typename RSType>
176  const arma::mat& operator()(RSType* rs) const;
177 };
178 
182 class DeleteVisitor : public boost::static_visitor<void>
183 {
184  public:
186  template<typename RSType>
187  void operator()(RSType* rs) const;
188 };
189 
193 template<typename Archive>
194 class SerializeVisitor : public boost::static_visitor<void>
195 {
196  private:
198  Archive& ar;
200  const std::string& name;
201 
202  public:
204  template<typename RSType>
205  void operator()(RSType* rs) const;
206 
208  SerializeVisitor(Archive& ar, const std::string& name);
209 };
210 
214 class SingleModeVisitor : public boost::static_visitor<bool&>
215 {
216  public:
221  template<typename RSType>
222  bool& operator()(RSType* rs) const;
223 };
224 
228 class NaiveVisitor : public boost::static_visitor<bool&>
229 {
230  public:
234  template<typename RSType>
235  bool& operator()(RSType* rs) const;
236 };
237 
238 class RSModel
239 {
240  public:
242  {
257  };
258 
259  private:
260  TreeTypes treeType;
261  size_t leafSize;
262 
264  bool randomBasis;
266  arma::mat q;
267 
273  boost::variant<RSType<tree::KDTree>*,
286  RSType<tree::Octree>*> rSearch;
287 
288  public:
296  RSModel(const TreeTypes treeType = TreeTypes::KD_TREE,
297  const bool randomBasis = false);
298 
300  RSModel(const RSModel& other);
302  RSModel(RSModel&& other);
304  RSModel& operator=(const RSModel& other);
306  RSModel& operator=(RSModel&& other);
307 
311  ~RSModel();
312 
314  template<typename Archive>
315  void Serialize(Archive& ar, const unsigned int /* version */);
316 
318  const arma::mat& Dataset() const;
319 
321  bool SingleMode() const;
323  bool& SingleMode();
324 
326  bool Naive() const;
328  bool& Naive();
329 
331  size_t LeafSize() const { return leafSize; }
333  size_t& LeafSize() { return leafSize; }
334 
336  TreeTypes TreeType() const { return treeType; }
338  TreeTypes& TreeType() { return treeType; }
339 
341  bool RandomBasis() const { return randomBasis; }
344  bool& RandomBasis() { return randomBasis; }
345 
355  void BuildModel(arma::mat&& referenceSet,
356  const size_t leafSize,
357  const bool naive,
358  const bool singleMode);
359 
370  void Search(arma::mat&& querySet,
371  const math::Range& range,
372  std::vector<std::vector<size_t>>& neighbors,
373  std::vector<std::vector<double>>& distances);
374 
384  void Search(const math::Range& range,
385  std::vector<std::vector<size_t>>& neighbors,
386  std::vector<std::vector<double>>& distances);
387 
388  private:
393  std::string TreeName() const;
394 
398  void CleanMemory();
399 };
400 
401 } // namespace range
402 } // namespace mlpack
403 
404 // Include implementation (of Serialize() and inline functions).
405 #include "rs_model_impl.hpp"
406 
407 #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:341
bool & operator()(RSType *rs) const
Get a reference to the naive parameter of the given RangeSearch object.
Exposes the seralize method of the given RSType.
Definition: rs_model.hpp:194
~RSModel()
Clean memory, if necessary.
SerializeVisitor(Archive &ar, const std::string &name)
Construct the SerializeVisitor with the given archive and name.
ReferenceSetVisitor exposes the referenceSet of the given RSType.
Definition: rs_model.hpp:171
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.
static const std::string Name()
Definition: rs_model.hpp:38
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:336
size_t LeafSize() const
Get the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:331
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:61
void operator()(RSTypeT< TreeType > *rs) const
Default Bichromatic range search on the given RSType instance.
void operator()(RSType *rs) const
Serialize the given model.
NaiveVisitor exposes the Naive() method of the given RSType.
Definition: rs_model.hpp:228
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:130
TreeTypes & TreeType()
Modify the type of tree (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:338
size_t & LeafSize()
Modify the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:333
const arma::mat & Dataset() const
Expose the dataset.
MonoSearchVisitor executes a monochromatic range search on the given RSType.
Definition: rs_model.hpp:45
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:76
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:344
SingleModeVisitor exposes the SingleMode() method of the given RSType.
Definition: rs_model.hpp:214
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:182
void Serialize(Archive &ar, const unsigned int)
Serialize the range search model.
RSModel & operator=(const RSModel &other)
Copy operator.