mlpack  master
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:
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;
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;
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  {
256  OCTREE
257  };
258 
259  private:
261  size_t leafSize;
262 
266  arma::mat q;
267 
273  boost::variant<RSType<tree::KDTree>*,
287 
288  public:
296  RSModel(const TreeTypes treeType = TreeTypes::KD_TREE,
297  const bool randomBasis = false);
298 
304  RSModel(const RSModel& other);
305 
311  RSModel(RSModel&& other);
312 
318  RSModel& operator=(const RSModel& other);
319 
325  RSModel& operator=(RSModel&& other);
326 
330  ~RSModel();
331 
333  template<typename Archive>
334  void Serialize(Archive& ar, const unsigned int /* version */);
335 
337  const arma::mat& Dataset() const;
338 
340  bool SingleMode() const;
342  bool& SingleMode();
343 
345  bool Naive() const;
347  bool& Naive();
348 
350  size_t LeafSize() const { return leafSize; }
352  size_t& LeafSize() { return leafSize; }
353 
355  TreeTypes TreeType() const { return treeType; }
357  TreeTypes& TreeType() { return treeType; }
358 
360  bool RandomBasis() const { return randomBasis; }
363  bool& RandomBasis() { return randomBasis; }
364 
374  void BuildModel(arma::mat&& referenceSet,
375  const size_t leafSize,
376  const bool naive,
377  const bool singleMode);
378 
389  void Search(arma::mat&& querySet,
390  const math::Range& range,
391  std::vector<std::vector<size_t>>& neighbors,
392  std::vector<std::vector<double>>& distances);
393 
403  void Search(const math::Range& range,
404  std::vector<std::vector<size_t>>& neighbors,
405  std::vector<std::vector<double>>& distances);
406 
407  private:
412  std::string TreeName() const;
413 
417  void CleanMemory();
418 };
419 
420 } // namespace range
421 } // namespace mlpack
422 
423 // Include implementation (of Serialize() and inline functions).
424 #include "rs_model_impl.hpp"
425 
426 #endif
The RangeSearch class is a template class for performing range searches.
std::vector< std::vector< size_t > > & neighbors
The result vector for neighbors.
Definition: rs_model.hpp:84
const math::Range & range
Range to search neighbours for.
Definition: rs_model.hpp:82
Exposes the seralize method of the given RSType.
Definition: rs_model.hpp:194
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
arma::mat q
Random projection matrix.
Definition: rs_model.hpp:266
size_t leafSize
The leaf size, used only by BinarySpaceTree.
Definition: rs_model.hpp:136
ReferenceSetVisitor exposes the referenceSet of the given RSType.
Definition: rs_model.hpp:171
std::vector< std::vector< double > > & distances
Output distances.
Definition: rs_model.hpp:53
std::vector< std::vector< size_t > > & neighbors
Output neighbors.
Definition: rs_model.hpp:51
const math::Range & range
The range to search for.
Definition: rs_model.hpp:49
bool & RandomBasis()
Modify whether a random basis is used (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:363
TreeTypes TreeType() const
Get the type of tree.
Definition: rs_model.hpp:355
boost::variant< RSType< tree::KDTree > *, RSType< tree::StandardCoverTree > *, RSType< tree::RTree > *, RSType< tree::RStarTree > *, RSType< tree::BallTree > *, RSType< tree::XTree > *, RSType< tree::HilbertRTree > *, RSType< tree::RPlusTree > *, RSType< tree::RPlusPlusTree > *, RSType< tree::VPTree > *, RSType< tree::RPTree > *, RSType< tree::MaxRPTree > *, RSType< tree::UBTree > *, RSType< tree::Octree > * > rSearch
rSearch holds an instance of the RangeSearch class for the current treeType.
Definition: rs_model.hpp:286
const size_t leafSize
The number of points in a leaf (for BinarySpaceTrees).
Definition: rs_model.hpp:88
static const std::string Name()
Definition: rs_model.hpp:38
const std::string & name
Name of the model to serialize.
Definition: rs_model.hpp:200
size_t LeafSize() const
Get the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:350
TreeTypes & TreeType()
Modify the type of tree (don&#39;t do this after the model has been built).
Definition: rs_model.hpp:357
Archive & ar
Archive to serialize to.
Definition: rs_model.hpp:198
bool randomBasis
If true, we randomly project the data into a new basis before search.
Definition: rs_model.hpp:264
NaiveVisitor exposes the Naive() method of the given RSType.
Definition: rs_model.hpp:228
TrainVisitor sets the reference set to a new reference set on the given RSType.
Definition: rs_model.hpp:130
bool RandomBasis() const
Get whether a random basis is used.
Definition: rs_model.hpp:360
size_t & LeafSize()
Modify the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:352
MonoSearchVisitor executes a monochromatic range search on the given RSType.
Definition: rs_model.hpp:45
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
arma::mat && referenceSet
The reference set to use for training.
Definition: rs_model.hpp:134
const arma::mat & querySet
The query set for the bichromatic search.
Definition: rs_model.hpp:80
BiSearchVisitor executes a bichromatic range search on the given RSType.
Definition: rs_model.hpp:76
test cpp RESULT_VARIABLE MEX_RESULT_TRASH OUTPUT_VARIABLE MEX_OUTPUT ERROR_VARIABLE MEX_ERROR_TRASH string(REGEX MATCH"Warning: You are using"MEX_WARNING"${MEX_OUTPUT}") if(MEX_WARNING) string(REGEX REPLACE".*using [a-zA-Z]* version \"([0-9.]*)[^\"]*\".*""\\1"OTHER_COMPILER_VERSION"$
Definition: CMakeLists.txt:18
SingleModeVisitor exposes the SingleMode() method of the given RSType.
Definition: rs_model.hpp:214
std::vector< std::vector< double > > & distances
The result vector for distances.
Definition: rs_model.hpp:86
DeleteVisitor deletes the given RSType instance.
Definition: rs_model.hpp:182