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
25namespace mlpack {
26namespace range {
27
31template<template<typename TreeMetricType,
32 typename TreeStatType,
33 typename TreeMatType> class TreeType>
35
40class 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
71class 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
125class 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
166class 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
177class DeleteVisitor : public boost::static_visitor<void>
178{
179 public:
181 template<typename RSType>
182 void operator()(RSType* rs) const;
183};
184
188class SingleModeVisitor : public boost::static_visitor<bool&>
189{
190 public:
195 template<typename RSType>
196 bool& operator()(RSType* rs) const;
197};
198
202class NaiveVisitor : public boost::static_visitor<bool&>
203{
204 public:
208 template<typename RSType>
209 bool& operator()(RSType* rs) const;
210};
211
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
Simple real-valued range.
Definition: range.hpp:35
BiSearchVisitor executes a bichromatic range search on the given RSType.
Definition: rs_model.hpp:72
void operator()(RSTypeT< tree::BallTree > *rs) const
Bichromatic range search on the given RSType specialized for BallTrees.
void operator()(RSTypeT< tree::Octree > *rs) const
Bichromatic range search specialized for octrees.
void operator()(RSTypeT< tree::KDTree > *rs) const
Bichromatic range search on the given RSType specialized for KDTrees.
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.
DeleteVisitor deletes the given RSType instance.
Definition: rs_model.hpp:178
void operator()(RSType *rs) const
Delete the RSType object.
MonoSearchVisitor executes a monochromatic range search on the given RSType.
Definition: rs_model.hpp:41
void operator()(RSType *rs) const
Perform monochromatic search with the given RangeSearch object.
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
NaiveVisitor exposes the Naive() method of the given RSType.
Definition: rs_model.hpp:203
bool & operator()(RSType *rs) const
Get a reference to the naive parameter of the given RangeSearch object.
RSModel(RSModel &&other)
Take ownership of the given RSModel.
const arma::mat & Dataset() const
Expose the dataset.
bool & RandomBasis()
Modify whether a random basis is used (don't do this after the model has been built).
Definition: rs_model.hpp:332
size_t & LeafSize()
Modify the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:321
bool Naive() const
Get whether the model is in naive search mode.
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.
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.
RSModel & operator=(RSModel other)
Copy the given RSModel.
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.
~RSModel()
Clean memory, if necessary.
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.
TreeTypes & TreeType()
Modify the type of tree (don't do this after the model has been built).
Definition: rs_model.hpp:326
bool SingleMode() const
Get whether the model is in single-tree search mode.
RSModel(const RSModel &other)
Copy the given RSModel.
bool & Naive()
Modify whether the model is in naive search mode.
size_t LeafSize() const
Get the leaf size (applicable to everything but the cover tree).
Definition: rs_model.hpp:319
bool & SingleMode()
Modify whether the model is in single-tree search mode.
TreeTypes TreeType() const
Get the type of tree.
Definition: rs_model.hpp:324
bool RandomBasis() const
Get whether a random basis is used.
Definition: rs_model.hpp:329
void serialize(Archive &ar, const unsigned int)
Serialize the range search model.
The RangeSearch class is a template class for performing range searches.
ReferenceSetVisitor exposes the referenceSet of the given RSType.
Definition: rs_model.hpp:167
const arma::mat & operator()(RSType *rs) const
Return the reference set.
SingleModeVisitor exposes the SingleMode() method of the given RSType.
Definition: rs_model.hpp:189
bool & operator()(RSType *rs) const
Get a reference to the singleMode parameter of the given RangeSeach object.
TrainVisitor sets the reference set to a new reference set on the given RSType.
Definition: rs_model.hpp:126
void operator()(RSTypeT< tree::BallTree > *rs) const
Train on the given RSType specialized for BallTrees.
TrainVisitor(arma::mat &&referenceSet, const size_t leafSize)
Construct the TrainVisitor object with the given reference set, leafSize.
void operator()(RSTypeT< tree::Octree > *rs) const
Train specialized for octrees.
void operator()(RSTypeT< tree::KDTree > *rs) const
Train on the given RSType specialized for KDTrees.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1