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
24namespace mlpack {
25namespace neighbor {
26
30template<typename SortPolicy,
31 template<typename TreeMetricType,
32 typename TreeStatType,
33 typename TreeMatType> class TreeType>
34using RAType = RASearch<SortPolicy,
36 arma::mat,
37 TreeType>;
38
43class 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
74template<typename SortPolicy>
75class 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
126template<typename SortPolicy>
127class 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
167class SingleSampleLimitVisitor : public boost::static_visitor<size_t&>
168{
169 public:
170 template<typename RAType>
171 size_t& operator()(RAType* ra) const;
172};
173
177class FirstLeafExactVisitor : public boost::static_visitor<bool&>
178{
179 public:
180 template<typename RAType>
181 bool& operator()(RAType* ra) const;
182};
183
187class SampleAtLeavesVisitor : public boost::static_visitor<bool&>
188{
189 public:
191 template<typename RAType>
192 bool& operator()(RAType *) const;
193};
194
198class AlphaVisitor : public boost::static_visitor<double&>
199{
200 public:
202 template<typename RAType>
203 double& operator()(RAType* ra) const;
204};
205
209class TauVisitor : public boost::static_visitor<double&>
210{
211 public:
213 template<typename RAType>
214 double& operator()(RAType* ra) const;
215};
216
220class SingleModeVisitor : public boost::static_visitor<bool&>
221{
222 public:
224 template<typename RAType>
225 bool& operator()(RAType* ra) const;
226};
227
231class 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
242class DeleteVisitor : public boost::static_visitor<void>
243{
244 public:
246 template<typename RAType> void operator()(RAType* ra) const;
247};
248
252class NaiveVisitor : public boost::static_visitor<bool&>
253{
254 public:
258 template<typename RAType>
259 bool& operator()(RAType* ra) const;
260};
261
270template<typename SortPolicy>
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;
384
386 bool FirstLeafExact() const;
389
391 size_t SingleSampleLimit() const;
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
Exposes the Alpha() method of the given RAType.
Definition: ra_model.hpp:199
double & operator()(RAType *ra) const
Return Alpha parameter.
void operator()(RATypeT< tree::Octree > *ra) const
Bichromatic search on the given RAType specialized for octrees.
BiSearchVisitor(const arma::mat &querySet, const size_t k, arma::Mat< size_t > &neighbors, arma::mat &distances, const size_t leafSize)
Construct the BiSearchVisitor.
void operator()(RATypeT< tree::KDTree > *ra) const
Bichromatic search on the given RAType specialized for KDTrees.
DeleteVisitor deletes the given NSType instance.
Definition: ns_model.hpp:230
void operator()(RAType *ra) const
Delete the RAType Object.
Exposes the FirstLeafExact() method of the given RAType.
Definition: ra_model.hpp:178
bool & operator()(RAType *ra) const
MonoSearchVisitor executes a monochromatic neighbor search on the given NSType.
Definition: ns_model.hpp:49
void operator()(RAType *ra) const
Perform monochromatic nearest neighbor search.
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
NaiveVisitor exposes the Naive() method of the given RAType.
Definition: ra_model.hpp:253
bool & operator()(RAType *ra) const
Get a reference to the naive parameter of the given RASearch object.
The RAModel class provides an abstraction for the RASearch class, abstracting away the TreeType param...
Definition: ra_model.hpp:272
bool FirstLeafExact() const
Get whether or not we traverse to the first leaf without approximation.
const arma::mat & Dataset() const
Expose the dataset.
bool & RandomBasis()
Modify whether or not a random basis is being used.
size_t & LeafSize()
Modify the leaf size (only relevant when the kd-tree is used).
RAModel(RAModel &&other)
Take ownership of the given RAModel.
bool & SampleAtLeaves()
Modify whether or not sampling is done at the leaves.
bool Naive() const
Get whether or not naive search is being used.
void BuildModel(arma::mat &&referenceSet, const size_t leafSize, const bool naive, const bool singleMode)
Build the reference tree.
double & Alpha()
Modify the desired success probability.
size_t SingleSampleLimit() const
Get the limit on the size of a node that can be approximated.
double Tau() const
Get the rank-approximation in percentile of the data.
double Alpha() const
Get the desired success probability.
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.
TreeTypes & TreeType()
Modify the type of tree being used.
TreeTypes
The list of tree types we can use with RASearch.
Definition: ra_model.hpp:279
double & Tau()
Modify the rank-approximation in percentile of the data.
bool SingleMode() const
Get whether or not single-tree search is being used.
~RAModel()
Clean memory, if necessary.
size_t & SingleSampleLimit()
Modify the limit on the size of a node that can be approximation.
RAModel(const RAModel &other)
Copy the given RAModel.
bool & Naive()
Modify whether or not naive search is being used.
std::string TreeName() const
Get the name of the tree type.
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.
bool SampleAtLeaves() const
Get whether or not sampling is done at the leaves.
size_t LeafSize() const
Get the leaf size (only relevant when the kd-tree is used).
bool & SingleMode()
Modify whether or not single-tree search is being used.
TreeTypes TreeType() const
Get the type of tree being used.
bool RandomBasis() const
Get whether or not a random basis is being used.
RAModel & operator=(const RAModel &other)
Copy the given RAModel.
void serialize(Archive &ar, const unsigned int)
Serialize the model.
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.
RAModel & operator=(RAModel &&other)
Take ownership of the given RAModel.
bool & FirstLeafExact()
Modify whether or not we traverse to the first leaf without approximation.
The RASearch class: This class provides a generic manner to perform rank-approximate search via rando...
Definition: ra_search.hpp:76
ReferenceSetVisitor exposes the referenceSet of the given NSType.
Definition: ns_model.hpp:219
const arma::mat & operator()(RAType *ra) const
Return the reference set.
Exposes the SampleAtLeaves() method of the given RAType.
Definition: ra_model.hpp:188
bool & operator()(RAType *) const
Return SampleAtLeaves (whether or not sampling is done at leaves).
Exposes the SingleMode() method of the given RAType.
Definition: ra_model.hpp:221
bool & operator()(RAType *ra) const
Get a reference to the SingleMode parameter of the given RASearch object.
Exposes the SingleSampleLimit() method of the given RAType.
Definition: ra_model.hpp:168
size_t & operator()(RAType *ra) const
Exposes the Tau() method of the given RAType.
Definition: ra_model.hpp:210
double & operator()(RAType *ra) const
Get a reference to the Tau parameter.
TrainVisitor sets the reference set to a new reference set on the given NSType.
Definition: ra_model.hpp:128
void operator()(RATypeT< tree::Octree > *ra) const
Train on the given RAType specialized for Octrees.
TrainVisitor(arma::mat &&referenceSet, const size_t leafSize)
Construct the TrainVisitor object with the given reference set, leafSize for BinarySpaceTrees.
void operator()(RATypeT< tree::KDTree > *ra) const
Train on the given RAType specialized for KDTrees.
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
RASearch< SortPolicy, metric::EuclideanDistance, arma::mat, TreeType > RAType
Alias template for RASearch.
Definition: ra_model.hpp:37
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1