mlpack  master
range_search.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_RANGE_SEARCH_RANGE_SEARCH_HPP
14 #define MLPACK_METHODS_RANGE_SEARCH_RANGE_SEARCH_HPP
15 
16 #include <mlpack/prereqs.hpp>
19 #include "range_search_stat.hpp"
20 
21 namespace mlpack {
22 namespace range {
23 
25 class TrainVisitor;
26 
37 template<typename MetricType = metric::EuclideanDistance,
38  typename MatType = arma::mat,
39  template<typename TreeMetricType,
40  typename TreeStatType,
41  typename TreeMatType> class TreeType = tree::KDTree>
43 {
44  public:
46  typedef TreeType<MetricType, RangeSearchStat, MatType> Tree;
47 
64  RangeSearch(const MatType& referenceSet,
65  const bool naive = false,
66  const bool singleMode = false,
67  const MetricType metric = MetricType());
68 
88  RangeSearch(MatType&& referenceSet,
89  const bool naive = false,
90  const bool singleMode = false,
91  const MetricType metric = MetricType());
92 
118  const bool singleMode = false,
119  const MetricType metric = MetricType());
120 
131  RangeSearch(const bool naive = false,
132  const bool singleMode = false,
133  const MetricType metric = MetricType());
134 
141  RangeSearch(const RangeSearch& other);
142 
148  RangeSearch(RangeSearch&& other);
149 
155  RangeSearch& operator=(const RangeSearch& other);
156 
163 
168  ~RangeSearch();
169 
178  void Train(const MatType& referenceSet);
179 
188  void Train(MatType&& referenceSet);
189 
193  void Train(Tree* referenceTree);
194 
222  void Search(const MatType& querySet,
223  const math::Range& range,
224  std::vector<std::vector<size_t>>& neighbors,
225  std::vector<std::vector<double>>& distances);
226 
263  void Search(Tree* queryTree,
264  const math::Range& range,
265  std::vector<std::vector<size_t>>& neighbors,
266  std::vector<std::vector<double>>& distances);
267 
298  void Search(const math::Range& range,
299  std::vector<std::vector<size_t>>& neighbors,
300  std::vector<std::vector<double>>& distances);
301 
303  bool SingleMode() const { return singleMode; }
305  bool& SingleMode() { return singleMode; }
306 
308  bool Naive() const { return naive; }
310  bool& Naive() { return naive; }
311 
313  size_t BaseCases() const { return baseCases; }
315  size_t Scores() const { return scores; }
316 
318  template<typename Archive>
319  void Serialize(Archive& ar, const unsigned int version);
320 
322  const MatType& ReferenceSet() const { return *referenceSet; }
323 
325  Tree* ReferenceTree() { return referenceTree; }
326 
327  private:
329  std::vector<size_t> oldFromNewReferences;
334  const MatType* referenceSet;
335 
337  bool treeOwner;
339  bool setOwner;
340 
342  bool naive;
345 
347  MetricType metric;
348 
350  size_t baseCases;
352  size_t scores;
353 
355  friend class TrainVisitor;
356 };
357 
358 } // namespace range
359 } // namespace mlpack
360 
361 // Include implementation.
362 #include "range_search_impl.hpp"
363 
364 #endif
The RangeSearch class is a template class for performing range searches.
bool & SingleMode()
Modify whether single-tree search is being used.
bool singleMode
If true, single-tree computation is used.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool naive
If true, O(n^2) naive computation is used.
std::vector< size_t > oldFromNewReferences
Mappings to old reference indices (used when this object builds trees).
A binary space partitioning tree, such as a KD-tree or a ball tree.
bool & Naive()
Modify whether naive search is being used.
bool Naive() const
Get whether naive search is being used.
bool SingleMode() const
Get whether single-tree search is being used.
void Train(const MatType &referenceSet)
Set the reference set to a new reference set, and build a tree if necessary.
MetricType metric
Instantiated distance metric.
~RangeSearch()
Destroy the RangeSearch object.
RangeSearch(const MatType &referenceSet, const bool naive=false, const bool singleMode=false, const MetricType metric=MetricType())
Initialize the RangeSearch object with a given reference dataset (this is the dataset which is search...
bool setOwner
If true, we own the reference set.
RangeSearch & operator=(const RangeSearch &other)
Copy the given RangeSearch model.
TrainVisitor sets the reference set to a new reference set on the given RSType.
Definition: rs_model.hpp:130
size_t baseCases
The total number of base cases during the last search.
Tree * ReferenceTree()
Return the reference tree (or NULL if in naive mode).
size_t BaseCases() const
Get the number of base cases during the last search.
Tree * referenceTree
Reference tree.
const MatType & ReferenceSet() const
Return the reference set.
void Serialize(Archive &ar, const unsigned int version)
Serialize the model.
void Search(const MatType &querySet, const math::Range &range, std::vector< std::vector< size_t >> &neighbors, std::vector< std::vector< double >> &distances)
Search for all reference points in the given range for each point in the query set, returning the results in the neighbors and distances objects.
size_t Scores() const
Get the number of scores during the last search.
size_t scores
The total number of scores during the last search.
bool treeOwner
If true, this object is responsible for deleting the trees.
const MatType * referenceSet
Reference set (data should be accessed using this).
TreeType< MetricType, RangeSearchStat, MatType > Tree
Convenience typedef.