random_point_selection.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_DBSCAN_RANDOM_POINT_SELECTION_HPP
13 #define MLPACK_METHODS_DBSCAN_RANDOM_POINT_SELECTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace dbscan {
19 
24 {
25  public:
32  template<typename MatType>
33  size_t Select(const size_t /* point */,
34  const MatType& data)
35  {
36  // Initialize the length of the unvisited bitset.
37  size_t size = data.n_cols; // Get the size of points.
38  if (unvisited.size() != size)
39  unvisited.resize(size, true); // Resize & Set bitset to one.
40 
41  // Count the unvisited points and generate nth index randomly.
42  const size_t max = std::count(unvisited.begin(), unvisited.end(), true);
43  const size_t index = math::RandInt(max);
44 
45  // Select the index'th unvisited point.
46  size_t found = 0;
47  for (size_t i = 0; i < unvisited.size(); ++i)
48  {
49  if (unvisited[i])
50  ++found;
51 
52  if (found > index)
53  {
54  unvisited[i].flip(); // Set unvisited point to visited point.
55  return i;
56  }
57  }
58  return 0; // Not sure if it is possible to get here.
59  }
60 
61  private:
62  // Bitset for unvisited points. If true, mean unvisited.
63  std::vector<bool> unvisited;
64 };
65 
66 } // namespace dbscan
67 } // namespace mlpack
68 
69 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t Select(const size_t, const MatType &data)
Select the next point to use, randomly.
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:110
This class can be used to randomly select the next point to use for DBSCAN.