libcmaes
A C++11 library for stochastic optimization with CMA-ES
 All Classes Namespaces Functions Variables Typedefs
esostrategy.h
1 
22 #ifndef ESOSTRATEGY_H
23 #define ESOSTRATEGY_H
24 
25 #include "eo_matrix.h" // to include Eigen everywhere.
26 #include "candidate.h"
27 #include "eigenmvn.h"
28 #include <random>
29 
30 namespace libcmaes
31 {
32  typedef std::function<double (const double*, const int &n)> FitFunc;
33  typedef std::function<dVec (const double*, const int &n)> GradFunc;
34 
35  typedef std::function<void(const dMat&, const dMat&)> EvalFunc;
36  typedef std::function<dMat(void)> AskFunc;
37  typedef std::function<void(void)> TellFunc;
38 
39  template<class TParameters,class TSolutions>
40  using ProgressFunc = std::function<int (const TParameters&, const TSolutions&)>; // template aliasing.
41 
42  template<class TParameters,class TSolutions>
43  using PlotFunc = std::function<int (const TParameters&, const TSolutions&, std::ofstream &fplotstream)>;
44 
50  template<class TParameters,class TSolutions,class TStopCriteria>
52  {
53  public:
58  {
59  }
60 
66  ESOStrategy(FitFunc &func,
67  TParameters &parameters);
68 
75  ESOStrategy(FitFunc &func,
76  TParameters &parameters,
77  const TSolutions &solutions);
78 
79  protected:
80  ~ESOStrategy();
81 
82  public:
87  dMat ask();
88 
98  void eval(const dMat &candidates,
99  const dMat &phenocandidates=dMat(0,0));
100 
105  void tell();
106 
111  bool stop();
112 
122  int optimize(const EvalFunc &evalf, const AskFunc &askf, const TellFunc &tellf);
123 
127  void inc_iter();
128 
133  void update_fevals(const int &evals);
134 
139  void set_gradient_func(GradFunc &gfunc) { _gfunc = gfunc; }
140 
148  void set_progress_func(ProgressFunc<TParameters,TSolutions> &pfunc) { _pfunc = pfunc; }
149 
154  void start_from_solution(const TSolutions &sol)
155  {
156  _parameters.set_x0(sol.best_candidate().get_x_dvec());
157  _solutions = sol;
158  _solutions.reset();
159  }
160 
167  void set_plot_func(PlotFunc<TParameters,TSolutions> &pffunc) { if (!_parameters._full_fplot) _pffunc = pffunc; }
168 
174  dVec gradf(const dVec &x);
175 
181  dVec gradgp(const dVec &x) const;
182 
187  double edm();
188 
193  TSolutions& get_solutions() { return _solutions; }
194 
199  TParameters& get_parameters() { return _parameters; }
200 
207  double fitfunc(const double *x, const int N) { return _func(x,N); }
208 
213  void uncertainty_handling();
214 
219  void tpa_update();
220 
221  // deprecated.
222  Candidate best_solution() const;
223 
224  void set_initial_elitist(const bool &e) { _initial_elitist = e; }
225 
226  protected:
227  FitFunc _func;
228  int _nevals;
229  int _niter;
230  TSolutions _solutions;
231  TParameters _parameters;
232  ProgressFunc<TParameters,TSolutions> _pfunc;
233  GradFunc _gfunc = nullptr;
234  PlotFunc<TParameters,TSolutions> _pffunc;
235  FitFunc _funcaux;
236  bool _initial_elitist = false;
238  private:
239  std::mt19937 _uhgen;
240  std::uniform_real_distribution<> _uhunif;
242  };
243 
244 }
245 
246 #endif
void update_fevals(const int &evals)
updates the consumed budget of objective function evaluations.
Definition: esostrategy.cc:192
bool _initial_elitist
Definition: esostrategy.h:236
TParameters & get_parameters()
returns reference to current optimization parameters object
Definition: esostrategy.h:199
double fitfunc(const double *x, const int N)
execute objective function
Definition: esostrategy.h:207
void set_plot_func(PlotFunc< TParameters, TSolutions > &pffunc)
Sets the possibly custom plot to file function, that is useful for storing into file various possibly...
Definition: esostrategy.h:167
FitFunc _func
Definition: esostrategy.h:227
dVec gradf(const dVec &x)
returns numerical gradient of objective function at x.
Definition: esostrategy.cc:199
void set_progress_func(ProgressFunc< TParameters, TSolutions > &pfunc)
Sets the possibly custom progress function, that is called in between every search step...
Definition: esostrategy.h:148
GradFunc _gfunc
Definition: esostrategy.h:233
int _niter
Definition: esostrategy.h:229
Main class describing an evolutionary optimization strategy. Every algorithm in libcmaes descends fro...
Definition: esostrategy.h:51
TSolutions & get_solutions()
returns reference to current solution object
Definition: esostrategy.h:193
void uncertainty_handling()
uncertainty handling scheme that computes and uncertainty level based on a dual candidate ranking...
Definition: esostrategy.cc:245
int _nevals
Definition: esostrategy.h:228
void eval(const dMat &candidates, const dMat &phenocandidates=dMat(0, 0))
Evaluates a set of candidates against the objective function. The procedure is multithreaded and stor...
Definition: esostrategy.cc:84
ProgressFunc< TParameters, TSolutions > _pfunc
Definition: esostrategy.h:232
linear scaling of the parameter space to achieve similar sensitivity across all components.
Definition: acovarianceupdate.cc:25
void start_from_solution(const TSolutions &sol)
starts optimization from a given solution object.
Definition: esostrategy.h:154
int optimize(const EvalFunc &evalf, const AskFunc &askf, const TellFunc &tellf)
Finds the minimum of the objective function. It makes alternative calls to ask(), tell() and stop() u...
ESOStrategy()
dummy constructor.
Definition: esostrategy.h:57
candidate solution point, in function parameter space.
Definition: candidate.h:33
void inc_iter()
increment iteration count.
Definition: esostrategy.cc:185
PlotFunc< TParameters, TSolutions > _pffunc
Definition: esostrategy.h:234
void set_gradient_func(GradFunc &gfunc)
sets the gradient function, if available.
Definition: esostrategy.h:139
void tpa_update()
updates the two-point adaptation average rank difference for the step-size adaptation mechanism ...
Definition: esostrategy.cc:359
TSolutions _solutions
Definition: esostrategy.h:230
dVec gradgp(const dVec &x) const
returns the numerical gradient of the objective function in phenotype space
Definition: esostrategy.cc:220
void tell()
Updates the state of the stochastic search, and prepares for the next iteration.
double edm()
computes expected distance to minimum (EDM).
Definition: esostrategy.cc:227
TParameters _parameters
Definition: esostrategy.h:231
bool stop()
Decides whether to stop the search for solutions.
dMat ask()
Generates a set of candidate points.