mlpack  3.4.2
ra_model.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_RANN_RA_MODEL_HPP
15 #define MLPACK_METHODS_RANN_RA_MODEL_HPP
16 
21 #include <boost/variant.hpp>
22 #include "ra_search.hpp"
23 
24 namespace mlpack {
25 namespace neighbor {
26 
30 template<typename SortPolicy,
31  template<typename TreeMetricType,
32  typename TreeStatType,
33  typename TreeMatType> class TreeType>
34 using RAType = RASearch<SortPolicy,
36  arma::mat,
37  TreeType>;
38 
43 class MonoSearchVisitor : public boost::static_visitor<void>
44 {
45  private:
47  const size_t k;
49  arma::Mat<size_t>& neighbors;
51  arma::mat& distances;
52 
53  public:
55  template<typename RAType>
56  void operator()(RAType* ra) const;
57 
59  MonoSearchVisitor(const size_t k,
60  arma::Mat<size_t>& neighbors,
61  arma::mat& distances) :
62  k(k),
63  neighbors(neighbors),
64  distances(distances)
65  {};
66 };
67 
74 template<typename SortPolicy>
75 class BiSearchVisitor : public boost::static_visitor<void>
76 {
77  private:
79  const arma::mat& querySet;
81  const size_t k;
83  arma::Mat<size_t>& neighbors;
85  arma::mat& distances;
87  const size_t leafSize;
88 
90  template<typename RAType>
91  void SearchLeaf(RAType* ra) const;
92 
93  public:
95  template<template<typename TreeMetricType,
96  typename TreeStatType,
97  typename TreeMatType> class TreeType>
99 
101  template<template<typename TreeMetricType,
102  typename TreeStatType,
103  typename TreeMatType> class TreeType>
104  void operator()(RATypeT<TreeType>* ra) const;
105 
108 
111 
113  BiSearchVisitor(const arma::mat& querySet,
114  const size_t k,
115  arma::Mat<size_t>& neighbors,
116  arma::mat& distances,
117  const size_t leafSize);
118 };
119 
126 template<typename SortPolicy>
127 class TrainVisitor : public boost::static_visitor<void>
128 {
129  private:
131  arma::mat&& referenceSet;
133  size_t leafSize;
134 
136  template<typename RAType>
137  void TrainLeaf(RAType* ra) const;
138 
139  public:
141  template<template<typename TreeMetricType,
142  typename TreeStatType,
143  typename TreeMatType> class TreeType>
145 
147  template<template<typename TreeMetricType,
148  typename TreeStatType,
149  typename TreeMatType> class TreeType>
150  void operator()(RATypeT<TreeType>* ra) const;
151 
154 
157 
160  TrainVisitor(arma::mat&& referenceSet,
161  const size_t leafSize);
162 };
163 
167 class SingleSampleLimitVisitor : public boost::static_visitor<size_t&>
168 {
169  public:
170  template<typename RAType>
171  size_t& operator()(RAType* ra) const;
172 };
173 
177 class FirstLeafExactVisitor : public boost::static_visitor<bool&>
178 {
179  public:
180  template<typename RAType>
181  bool& operator()(RAType* ra) const;
182 };
183 
187 class SampleAtLeavesVisitor : public boost::static_visitor<bool&>
188 {
189  public:
191  template<typename RAType>
192  bool& operator()(RAType *) const;
193 };
194 
198 class AlphaVisitor : public boost::static_visitor<double&>
199 {
200  public:
202  template<typename RAType>
203  double& operator()(RAType* ra) const;
204 };
205 
209 class TauVisitor : public boost::static_visitor<double&>
210 {
211  public:
213  template<typename RAType>
214  double& operator()(RAType* ra) const;
215 };
216 
220 class SingleModeVisitor : public boost::static_visitor<bool&>
221 {
222  public:
224  template<typename RAType>
225  bool& operator()(RAType* ra) const;
226 };
227 
231 class ReferenceSetVisitor : public boost::static_visitor<const arma::mat&>
232 {
233  public:
235  template<typename RAType>
236  const arma::mat& operator()(RAType* ra) const;
237 };
238 
242 class DeleteVisitor : public boost::static_visitor<void>
243 {
244  public:
246  template<typename RAType> void operator()(RAType* ra) const;
247 };
248 
252 class NaiveVisitor : public boost::static_visitor<bool&>
253 {
254  public:
258  template<typename RAType>
259  bool& operator()(RAType* ra) const;
260 };
261 
270 template<typename SortPolicy>
271 class RAModel
272 {
273  public:
279  {
289  OCTREE
290  };
291 
292  private:
294  TreeTypes treeType;
296  size_t leafSize;
297 
299  bool randomBasis;
301  arma::mat q;
302 
304  boost::variant<RAType<SortPolicy, tree::KDTree>*,
314 
315  public:
320  RAModel(TreeTypes treeType = TreeTypes::KD_TREE, bool randomBasis = false);
321 
327  RAModel(const RAModel& other);
328 
334  RAModel(RAModel&& other);
335 
341  RAModel& operator=(const RAModel& other);
342 
349 
352 
354  template<typename Archive>
355  void serialize(Archive& ar, const unsigned int /* version */);
356 
358  const arma::mat& Dataset() const;
359 
361  bool SingleMode() const;
363  bool& SingleMode();
364 
366  bool Naive() const;
368  bool& Naive();
369 
371  double Tau() const;
373  double& Tau();
374 
376  double Alpha() const;
378  double& Alpha();
379 
381  bool SampleAtLeaves() const;
383  bool& SampleAtLeaves();
384 
386  bool FirstLeafExact() const;
388  bool& FirstLeafExact();
389 
391  size_t SingleSampleLimit() const;
393  size_t& SingleSampleLimit();
394 
396  size_t LeafSize() const;
398  size_t& LeafSize();
399 
404 
406  bool RandomBasis() const;
409  bool& RandomBasis();
410 
412  void BuildModel(arma::mat&& referenceSet,
413  const size_t leafSize,
414  const bool naive,
415  const bool singleMode);
416 
419  void Search(arma::mat&& querySet,
420  const size_t k,
421  arma::Mat<size_t>& neighbors,
422  arma::mat& distances);
423 
428  void Search(const size_t k,
429  arma::Mat<size_t>& neighbors,
430  arma::mat& distances);
431 
433  std::string TreeName() const;
434 };
435 
436 } // namespace neighbor
437 } // namespace mlpack
438 
439 #include "ra_model_impl.hpp"
440 
441 #endif
mlpack::neighbor::RAModel::RandomBasis
bool RandomBasis() const
Get whether or not a random basis is being used.
rectangle_tree.hpp
mlpack::neighbor::BiSearchVisitor::operator()
void operator()(RATypeT< tree::Octree > *ra) const
Bichromatic search on the given RAType specialized for octrees.
mlpack::neighbor::DeleteVisitor::operator()
void operator()(RAType *ra) const
Delete the RAType Object.
mlpack::neighbor::RAModel::SampleAtLeaves
bool & SampleAtLeaves()
Modify whether or not sampling is done at the leaves.
mlpack::neighbor::FirstLeafExactVisitor
Exposes the FirstLeafExact() method of the given RAType.
Definition: ra_model.hpp:178
mlpack::neighbor::RAModel::Tau
double Tau() const
Get the rank-approximation in percentile of the data.
mlpack::neighbor::RAModel::COVER_TREE
@ COVER_TREE
Definition: ra_model.hpp:281
mlpack::neighbor::TrainVisitor
TrainVisitor sets the reference set to a new reference set on the given NSType.
Definition: ra_model.hpp:128
mlpack::neighbor::ReferenceSetVisitor::operator()
const arma::mat & operator()(RAType *ra) const
Return the reference set.
mlpack::neighbor::RAModel::Naive
bool Naive() const
Get whether or not naive search is being used.
mlpack::neighbor::BiSearchVisitor::operator()
void operator()(RATypeT< tree::KDTree > *ra) const
Bichromatic search on the given RAType specialized for KDTrees.
mlpack::neighbor::SingleModeVisitor
Exposes the SingleMode() method of the given RAType.
Definition: ra_model.hpp:221
mlpack::neighbor::TauVisitor
Exposes the Tau() method of the given RAType.
Definition: ra_model.hpp:210
mlpack::neighbor::RAModel::operator=
RAModel & operator=(RAModel &&other)
Take ownership of the given RAModel.
mlpack::neighbor::RAModel::RAModel
RAModel(const RAModel &other)
Copy the given RAModel.
ra_search.hpp
mlpack::neighbor::RAModel::serialize
void serialize(Archive &ar, const unsigned int)
Serialize the model.
octree.hpp
mlpack::neighbor::RAModel::Dataset
const arma::mat & Dataset() const
Expose the dataset.
mlpack::neighbor::RAModel::R_STAR_TREE
@ R_STAR_TREE
Definition: ra_model.hpp:283
mlpack::neighbor::RAModel::RAModel
RAModel(RAModel &&other)
Take ownership of the given RAModel.
mlpack::neighbor::RAModel::Search
void Search(const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
Perform rank-approximate neighbor search, using the reference set as the query set.
mlpack::neighbor::TrainVisitor::operator()
void operator()(RATypeT< tree::Octree > *ra) const
Train on the given RAType specialized for Octrees.
mlpack::neighbor::AlphaVisitor
Exposes the Alpha() method of the given RAType.
Definition: ra_model.hpp:199
mlpack::neighbor::RAModel::SingleSampleLimit
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
mlpack::neighbor::MonoSearchVisitor::MonoSearchVisitor
MonoSearchVisitor(const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
Construct the MonoSearchVisitor object with the given parameters.
Definition: ra_model.hpp:59
mlpack::neighbor::SingleSampleLimitVisitor::operator()
size_t & operator()(RAType *ra) const
mlpack::neighbor::RAModel::BuildModel
void BuildModel(arma::mat &&referenceSet, const size_t leafSize, const bool naive, const bool singleMode)
Build the reference tree.
mlpack::neighbor::RAModel::KD_TREE
@ KD_TREE
Definition: ra_model.hpp:280
mlpack
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_cli11.hpp:21
mlpack::neighbor::TrainVisitor::operator()
void operator()(RATypeT< tree::KDTree > *ra) const
Train on the given RAType specialized for KDTrees.
mlpack::neighbor::TrainVisitor::TrainVisitor
TrainVisitor(arma::mat &&referenceSet, const size_t leafSize)
Construct the TrainVisitor object with the given reference set, leafSize for BinarySpaceTrees.
cover_tree.hpp
mlpack::neighbor::DeleteVisitor
DeleteVisitor deletes the given NSType instance.
Definition: ns_model.hpp:230
mlpack::neighbor::RAModel::operator=
RAModel & operator=(const RAModel &other)
Copy the given RAModel.
mlpack::neighbor::AlphaVisitor::operator()
double & operator()(RAType *ra) const
Return Alpha parameter.
mlpack::neighbor::NaiveVisitor::operator()
bool & operator()(RAType *ra) const
Get a reference to the naive parameter of the given RASearch object.
mlpack::neighbor::RAModel::RandomBasis
bool & RandomBasis()
Modify whether or not a random basis is being used.
mlpack::neighbor::NaiveVisitor
NaiveVisitor exposes the Naive() method of the given RAType.
Definition: ra_model.hpp:253
mlpack::neighbor::RAModel::RAModel
RAModel(TreeTypes treeType=TreeTypes::KD_TREE, bool randomBasis=false)
Initialize the RAModel with the given type and whether or not a random basis should be used.
mlpack::neighbor::RAModel::SingleMode
bool & SingleMode()
Modify whether or not single-tree search is being used.
mlpack::neighbor::FirstLeafExactVisitor::operator()
bool & operator()(RAType *ra) const
mlpack::neighbor::RAModel::Tau
double & Tau()
Modify the rank-approximation in percentile of the data.
mlpack::neighbor::RAModel::R_PLUS_PLUS_TREE
@ R_PLUS_PLUS_TREE
Definition: ra_model.hpp:287
mlpack::neighbor::ReferenceSetVisitor
ReferenceSetVisitor exposes the referenceSet of the given NSType.
Definition: ns_model.hpp:219
mlpack::neighbor::BiSearchVisitor::BiSearchVisitor
BiSearchVisitor(const arma::mat &querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances, const size_t leafSize)
Construct the BiSearchVisitor.
mlpack::neighbor::RAModel::Alpha
double & Alpha()
Modify the desired success probability.
mlpack::metric::EuclideanDistance
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
mlpack::neighbor::RAModel::Alpha
double Alpha() const
Get the desired success probability.
mlpack::neighbor::RAModel::LeafSize
size_t LeafSize() const
Get the leaf size (only relevant when the kd-tree is used).
mlpack::neighbor::RAModel::FirstLeafExact
bool & FirstLeafExact()
Modify whether or not we traverse to the first leaf without approximation.
mlpack::neighbor::RAModel::Search
void Search(arma::mat &&querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances)
Perform rank-approximate neighbor search, taking ownership of the query set.
mlpack::neighbor::RAType
RASearch< SortPolicy, metric::EuclideanDistance, arma::mat, TreeType > RAType
Alias template for RASearch.
Definition: ra_model.hpp:37
mlpack::neighbor::RAModel::UB_TREE
@ UB_TREE
Definition: ra_model.hpp:288
mlpack::neighbor::RAModel::SingleMode
bool SingleMode() const
Get whether or not single-tree search is being used.
mlpack::neighbor::RAModel::R_PLUS_TREE
@ R_PLUS_TREE
Definition: ra_model.hpp:286
mlpack::neighbor::SampleAtLeavesVisitor::operator()
bool & operator()(RAType *) const
Return SampleAtLeaves (whether or not sampling is done at leaves).
mlpack::neighbor::RAModel::TreeType
TreeTypes TreeType() const
Get the type of tree being used.
mlpack::neighbor::RAModel::FirstLeafExact
bool FirstLeafExact() const
Get whether or not we traverse to the first leaf without approximation.
mlpack::neighbor::RAModel::X_TREE
@ X_TREE
Definition: ra_model.hpp:284
mlpack::neighbor::RAModel::LeafSize
size_t & LeafSize()
Modify the leaf size (only relevant when the kd-tree is used).
mlpack::neighbor::RAModel::HILBERT_R_TREE
@ HILBERT_R_TREE
Definition: ra_model.hpp:285
mlpack::neighbor::RAModel
The RAModel class provides an abstraction for the RASearch class, abstracting away the TreeType param...
Definition: ra_model.hpp:272
mlpack::neighbor::TauVisitor::operator()
double & operator()(RAType *ra) const
Get a reference to the Tau parameter.
mlpack::neighbor::RAModel::R_TREE
@ R_TREE
Definition: ra_model.hpp:282
mlpack::neighbor::RASearch
The RASearch class: This class provides a generic manner to perform rank-approximate search via rando...
Definition: ra_search.hpp:76
binary_space_tree.hpp
mlpack::neighbor::SampleAtLeavesVisitor
Exposes the SampleAtLeaves() method of the given RAType.
Definition: ra_model.hpp:188
mlpack::neighbor::MonoSearchVisitor
MonoSearchVisitor executes a monochromatic neighbor search on the given NSType.
Definition: ns_model.hpp:49
mlpack::neighbor::RAModel::TreeTypes
TreeTypes
The list of tree types we can use with RASearch.
Definition: ra_model.hpp:279
mlpack::neighbor::MonoSearchVisitor::operator()
void operator()(RAType *ra) const
Perform monochromatic nearest neighbor search.
mlpack::neighbor::RAModel::TreeType
TreeTypes & TreeType()
Modify the type of tree being used.
mlpack::neighbor::RAModel::TreeName
std::string TreeName() const
Get the name of the tree type.
mlpack::neighbor::RAModel::~RAModel
~RAModel()
Clean memory, if necessary.
mlpack::neighbor::RAModel::SampleAtLeaves
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
mlpack::neighbor::SingleModeVisitor::operator()
bool & operator()(RAType *ra) const
Get a reference to the SingleMode parameter of the given RASearch object.
mlpack::neighbor::RAModel::OCTREE
@ OCTREE
Definition: ra_model.hpp:289
mlpack::neighbor::RAModel::SingleSampleLimit
size_t & SingleSampleLimit()
Modify the limit on the size of a node that can be approximation.
mlpack::neighbor::SingleSampleLimitVisitor
Exposes the SingleSampleLimit() method of the given RAType.
Definition: ra_model.hpp:168
mlpack::neighbor::RAModel::Naive
bool & Naive()
Modify whether or not naive search is being used.