mlpack  2.0.1
ns_model.hpp
Go to the documentation of this file.
1 
17 #ifndef __MLPACK_METHODS_NEIGHBOR_SEARCH_NS_MODEL_HPP
18 #define __MLPACK_METHODS_NEIGHBOR_SEARCH_NS_MODEL_HPP
19 
23 
24 #include "neighbor_search.hpp"
25 
26 namespace mlpack {
27 namespace neighbor {
28 
29 template<typename SortPolicy>
31 {
32  static const std::string Name() { return "neighbor_search_model"; }
33 };
34 
35 template<>
37 {
38  static const std::string Name() { return "nearest_neighbor_search_model"; }
39 };
40 
41 template<>
43 {
44  static const std::string Name() { return "furthest_neighbor_search_model"; }
45 };
46 
47 template<typename SortPolicy>
48 class NSModel
49 {
50  public:
51  enum TreeTypes
52  {
57  BALL_TREE
58  };
59 
60  private:
61  int treeType;
62  size_t leafSize;
63 
64  // For random projections.
66  arma::mat q;
67 
68  template<template<typename TreeMetricType,
69  typename TreeStatType,
70  typename TreeMatType> class TreeType>
71  using NSType = NeighborSearch<SortPolicy,
73  arma::mat,
74  TreeType,
77  arma::mat>::template DualTreeTraverser>;
78 
79  // Only one of these pointers will be non-NULL.
85 
86  public:
91  NSModel(int treeType = TreeTypes::KD_TREE, bool randomBasis = false);
92 
94  ~NSModel();
95 
97  template<typename Archive>
98  void Serialize(Archive& ar, const unsigned int /* version */);
99 
101  const arma::mat& Dataset() const;
102 
104  bool SingleMode() const;
105  bool& SingleMode();
106 
107  bool Naive() const;
108  bool& Naive();
109 
110  size_t LeafSize() const { return leafSize; }
111  size_t& LeafSize() { return leafSize; }
112 
113  int TreeType() const { return treeType; }
114  int& TreeType() { return treeType; }
115 
116  bool RandomBasis() const { return randomBasis; }
117  bool& RandomBasis() { return randomBasis; }
118 
120  void BuildModel(arma::mat&& referenceSet,
121  const size_t leafSize,
122  const bool naive,
123  const bool singleMode);
124 
126  void Search(arma::mat&& querySet,
127  const size_t k,
128  arma::Mat<size_t>& neighbors,
129  arma::mat& distances);
130 
132  void Search(const size_t k,
133  arma::Mat<size_t>& neighbors,
134  arma::mat& distances);
135 
136  std::string TreeName() const;
137 };
138 
139 } // namespace neighbor
140 } // namespace mlpack
141 
142 // Include implementation.
143 #include "ns_model_impl.hpp"
144 
145 #endif
static const std::string Name()
Definition: ns_model.hpp:32
Linear algebra utility functions, generally performed on matrices or vectors.
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:113
Extra data for each node in the tree.
The NeighborSearch class is a template class for performing distance-based neighbor searches...
NSType< tree::RStarTree > * rStarTreeNS
Definition: ns_model.hpp:83
This class implements the necessary methods for the SortPolicy template parameter of the NeighborSear...
This class implements the necessary methods for the SortPolicy template parameter of the NeighborSear...
NSType< tree::KDTree > * kdTreeNS
Definition: ns_model.hpp:80
bool RandomBasis() const
Definition: ns_model.hpp:116
NSType< tree::StandardCoverTree > * coverTreeNS
Definition: ns_model.hpp:81
size_t LeafSize() const
Definition: ns_model.hpp:110
NSType< tree::RTree > * rTreeNS
Definition: ns_model.hpp:82
NSType< tree::BallTree > * ballTreeNS
Definition: ns_model.hpp:84