mlpack  3.4.2
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 
104 
107 
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 
151 
154 
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  {
230  OCTREE
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 
295 
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
mlpack::range::SingleModeVisitor
SingleModeVisitor exposes the SingleMode() method of the given RSType.
Definition: rs_model.hpp:189
rectangle_tree.hpp
mlpack::range::RSModel::TreeType
TreeTypes TreeType() const
Get the type of tree.
Definition: rs_model.hpp:324
mlpack::range::BiSearchVisitor::operator()
void operator()(RSTypeT< tree::KDTree > *rs) const
Bichromatic range search on the given RSType specialized for KDTrees.
mlpack::range::RSModel::RSModel
RSModel(const RSModel &other)
Copy the given RSModel.
mlpack::range::TrainVisitor::operator()
void operator()(RSTypeT< tree::BallTree > *rs) const
Train on the given RSType specialized for BallTrees.
mlpack::range::RSModel::SingleMode
bool SingleMode() const
Get whether the model is in single-tree search mode.
mlpack::range::BiSearchVisitor::operator()
void operator()(RSTypeT< tree::Octree > *rs) const
Bichromatic range search specialized for octrees.
mlpack::range::RSModel::Dataset
const arma::mat & Dataset() const
Expose the dataset.
mlpack::range::ReferenceSetVisitor::operator()
const arma::mat & operator()(RSType *rs) const
Return the reference set.
mlpack::range::ReferenceSetVisitor
ReferenceSetVisitor exposes the referenceSet of the given RSType.
Definition: rs_model.hpp:167
mlpack::range::RSModel::COVER_TREE
@ COVER_TREE
Definition: rs_model.hpp:218
mlpack::range::RSModel::RSModel
RSModel(RSModel &&other)
Take ownership of the given RSModel.
mlpack::range::RSModel::UB_TREE
@ UB_TREE
Definition: rs_model.hpp:229
octree.hpp
mlpack::range::RSModel::SingleMode
bool & SingleMode()
Modify whether the model is in single-tree search mode.
mlpack::range::NaiveVisitor::operator()
bool & operator()(RSType *rs) const
Get a reference to the naive parameter of the given RangeSearch object.
mlpack::range::BiSearchVisitor
BiSearchVisitor executes a bichromatic range search on the given RSType.
Definition: rs_model.hpp:72
mlpack::range::RSModel::Naive
bool & Naive()
Modify whether the model is in naive search mode.
mlpack::range::SingleModeVisitor::operator()
bool & operator()(RSType *rs) const
Get a reference to the singleMode parameter of the given RangeSeach object.
mlpack::range::RSModel::X_TREE
@ X_TREE
Definition: rs_model.hpp:222
mlpack
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_cli11.hpp:21
mlpack::range::RSModel::Naive
bool Naive() const
Get whether the model is in naive search mode.
mlpack::range::TrainVisitor::operator()
void operator()(RSTypeT< tree::Octree > *rs) const
Train specialized for octrees.
range_search.hpp
mlpack::range::DeleteVisitor
DeleteVisitor deletes the given RSType instance.
Definition: rs_model.hpp:178
mlpack::range::TrainVisitor
TrainVisitor sets the reference set to a new reference set on the given RSType.
Definition: rs_model.hpp:126
mlpack::range::RSModel::KD_TREE
@ KD_TREE
Definition: rs_model.hpp:217
cover_tree.hpp
mlpack::range::RSModel::TreeTypes
TreeTypes
Definition: rs_model.hpp:216
mlpack::range::RSModel::R_PLUS_TREE
@ R_PLUS_TREE
Definition: rs_model.hpp:224
mlpack::range::RSModel::VP_TREE
@ VP_TREE
Definition: rs_model.hpp:226
mlpack::range::RSModel::RSModel
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.
mlpack::range::RSModel::BuildModel
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.
mlpack::range::RSModel
Definition: rs_model.hpp:213
mlpack::range::MonoSearchVisitor
MonoSearchVisitor executes a monochromatic range search on the given RSType.
Definition: rs_model.hpp:41
mlpack::range::RSModel::serialize
void serialize(Archive &ar, const unsigned int)
Serialize the range search model.
mlpack::range::RSModel::R_TREE
@ R_TREE
Definition: rs_model.hpp:219
mlpack::range::RSModel::LeafSize
size_t & LeafSize()
Modify the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:321
mlpack::range::BiSearchVisitor::BiSearchVisitor
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.
mlpack::range::NaiveVisitor
NaiveVisitor exposes the Naive() method of the given RSType.
Definition: rs_model.hpp:203
mlpack::range::RSModel::LeafSize
size_t LeafSize() const
Get the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:319
mlpack::range::RSModel::operator=
RSModel & operator=(RSModel other)
Copy the given RSModel.
mlpack::range::TrainVisitor::TrainVisitor
TrainVisitor(arma::mat &&referenceSet, const size_t leafSize)
Construct the TrainVisitor object with the given reference set, leafSize.
mlpack::math::RangeType< double >
mlpack::range::RSModel::~RSModel
~RSModel()
Clean memory, if necessary.
mlpack::range::RSModel::RandomBasis
bool & RandomBasis()
Modify whether a random basis is used (don't do this after the model has been built).
Definition: rs_model.hpp:332
mlpack::range::RSModel::Search
void Search(const math::Range &range, std::vector< std::vector< size_t >> &neighbors, std::vector< std::vector< double >> &distances)
Perform monochromatic range search, with the reference set as the query set.
mlpack::range::RSModel::RandomBasis
bool RandomBasis() const
Get whether a random basis is used.
Definition: rs_model.hpp:329
mlpack::range::RSModel::MAX_RP_TREE
@ MAX_RP_TREE
Definition: rs_model.hpp:228
mlpack::range::RSModel::HILBERT_R_TREE
@ HILBERT_R_TREE
Definition: rs_model.hpp:223
mlpack::range::DeleteVisitor::operator()
void operator()(RSType *rs) const
Delete the RSType object.
mlpack::range::RSModel::RP_TREE
@ RP_TREE
Definition: rs_model.hpp:227
mlpack::range::RSModel::TreeType
TreeTypes & TreeType()
Modify the type of tree (don't do this after the model has been built).
Definition: rs_model.hpp:326
mlpack::range::RangeSearch
The RangeSearch class is a template class for performing range searches.
Definition: range_search.hpp:43
mlpack::range::RSModel::R_STAR_TREE
@ R_STAR_TREE
Definition: rs_model.hpp:220
mlpack::range::TrainVisitor::operator()
void operator()(RSTypeT< tree::KDTree > *rs) const
Train on the given RSType specialized for KDTrees.
mlpack::range::RSModel::BALL_TREE
@ BALL_TREE
Definition: rs_model.hpp:221
binary_space_tree.hpp
mlpack::range::RSModel::R_PLUS_PLUS_TREE
@ R_PLUS_PLUS_TREE
Definition: rs_model.hpp:225
mlpack::range::MonoSearchVisitor::operator()
void operator()(RSType *rs) const
Perform monochromatic search with the given RangeSearch object.
mlpack::range::RSModel::OCTREE
@ OCTREE
Definition: rs_model.hpp:230
mlpack::range::RSModel::Search
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.
mlpack::range::BiSearchVisitor::operator()
void operator()(RSTypeT< tree::BallTree > *rs) const
Bichromatic range search on the given RSType specialized for BallTrees.
mlpack::range::MonoSearchVisitor::MonoSearchVisitor
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