random_selection.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_OPTIMIZERS_CMAES_RANDOM_SELECTION_HPP
13 #define MLPACK_CORE_OPTIMIZERS_CMAES_RANDOM_SELECTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace optimization {
19 
20 /*
21  * Randomly select dataset points for use in the Evaluation step.
22  */
24 {
25  public:
31  RandomSelection(const double fraction = 0.3) : fraction(fraction)
32  {
33  // Nothing to do here.
34  }
35 
37  double Fraction() const { return fraction; }
39  double& Fraction() { return fraction; }
40 
49  template<typename DecomposableFunctionType>
50  double Select(DecomposableFunctionType& function,
51  const size_t batchSize,
52  const arma::mat& iterate)
53  {
54  // Find the number of functions to use.
55  const size_t numFunctions = function.NumFunctions();
56 
57  double objective = 0;
58  for (size_t f = 0; f < std::floor(numFunctions * fraction); f += batchSize)
59  {
60  const size_t selection = math::RandInt(0, numFunctions);
61  const size_t effectiveBatchSize = std::min(batchSize,
62  numFunctions - selection);
63 
64  objective += function.Evaluate(iterate, selection, effectiveBatchSize);
65  }
66 
67  return objective;
68  }
69 
70  private:
72  double fraction;
73 };
74 
75 } // namespace optimization
76 } // namespace mlpack
77 
78 #endif
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
double & Fraction()
Modify the dataset fraction.
int RandInt(const int hiExclusive)
Generates a uniform random integer.
Definition: random.hpp:87
double Select(DecomposableFunctionType &function, const size_t batchSize, const arma::mat &iterate)
Randomly select dataset points to calculate the objective function.
double Fraction() const
Get the dataset fraction.
RandomSelection(const double fraction=0.3)
Constructor for the random selection strategy.