multiple_random_dimension_select.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_DECISION_TREE_MULTIPLE_RANDOM_DIMENSION_SPLIT_HPP
13 #define MLPACK_METHODS_DECISION_TREE_MULTIPLE_RANDOM_DIMENSION_SPLIT_HPP
14 
15 namespace mlpack {
16 namespace tree {
17 
24 {
25  public:
31  MultipleRandomDimensionSelect(const size_t numDimensions = 0) :
32  numDimensions(numDimensions),
33  i(0),
34  dimensions(0)
35  { }
36 
40  size_t Begin()
41  {
42  // Reset if possible.
43  if (numDimensions == 0 || numDimensions > dimensions)
44  numDimensions = (size_t) std::sqrt(dimensions);
45 
46  values.set_size(numDimensions + 1);
47 
48  // Try setting new values.
49  for (size_t i = 0; i < numDimensions; ++i)
50  {
51  // Generate random different numbers.
52  bool unique = false;
53  size_t value;
54  while (!unique)
55  {
56  value = math::RandInt(dimensions);
57 
58  // Check if we already have the value.
59  unique = true;
60  for (size_t j = 0; j < i; ++j)
61  {
62  if (values[j] == value)
63  {
64  unique = false;
65  break;
66  }
67  }
68  }
69 
70  values[i] = value;
71  }
72 
73  values[numDimensions] = std::numeric_limits<size_t>::max();
74 
75  i = 0;
76  return values[0];
77  }
78 
82  size_t End() const { return size_t(-1); }
83 
87  size_t Next()
88  {
89  return values[++i];
90  }
91 
93  size_t Dimensions() const { return dimensions; }
95  size_t& Dimensions() { return dimensions; }
96 
97  private:
99  size_t numDimensions;
101  arma::Col<size_t> values;
103  size_t i;
105  size_t dimensions;
106 };
107 
108 } // namespace tree
109 } // namespace mlpack
110 
111 #endif
.hpp
Definition: add_to_po.hpp:21
This dimension selection policy allows the selection from a few random dimensions.
size_t & Dimensions()
Set the number of dimensions.
size_t Dimensions() const
Get the number of dimensions.
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:105
MultipleRandomDimensionSelect(const size_t numDimensions=0)
Instantiate the MultipleRandomDimensionSelect object.