libcmaes
A C++11 library for stochastic optimization with CMA-ES
 All Classes Namespaces Functions Variables Typedefs
cmasolutions.h
1 
22 #ifndef CMASOLUTIONS_H
23 #define CMASOLUTIONS_H
24 
25 #include "libcmaes_config.h"
26 #include "candidate.h"
27 #include "eo_matrix.h"
28 #include "cmaparameters.h"
29 #include "cmastopcriteria.h"
30 #include "pli.h"
31 #include <vector>
32 #include <algorithm>
33 
34 namespace libcmaes
35 {
36 
42  {
43  template <class U, class V> friend class CMAStrategy;
44  template <class U, class V, class W> friend class ESOptimizer;
45  template <class U, class V, class W> friend class ESOStrategy;
46  template <class U> friend class CMAStopCriteria;
47  template <class U, class V> friend class IPOPCMAStrategy;
48  template <class U, class V> friend class BIPOPCMAStrategy;
49  friend class CovarianceUpdate;
50  friend class ACovarianceUpdate;
51  template <class U> friend class errstats;
52 #ifdef HAVE_SURROG
53  template <template <class X,class Y> class U, class V, class W> friend class SimpleSurrogateStrategy;
54  template <template <class X,class Y> class U, class V, class W> friend class ACMSurrogateStrategy;
55 #endif
56  friend class VDCMAUpdate;
57 
58  public:
63 
68  template<class TGenoPheno=GenoPheno<NoBoundStrategy>>
70 
71  ~CMASolutions();
72 
77  {
78  std::stable_sort(_candidates.begin(),_candidates.end(),
79  [](Candidate const &c1, Candidate const &c2){return c1.get_fvalue() < c2.get_fvalue();});
80  }
81 
88 
94  void update_eigenv(const dVec &eigenvalues,
95  const dMat &eigenvectors);
96 
103  inline Candidate best_candidate() const
104  {
105  if (_best_candidates_hist.empty()) // iter = 0
106  {
107  if (_initial_candidate.get_x_size())
108  return _initial_candidate;
109  else return Candidate(std::numeric_limits<double>::quiet_NaN(),_xmean);
110  }
111  return _best_candidates_hist.back();
112  }
113 
119  {
120  return _best_seen_candidate;
121  }
122 
128  {
129  return _worst_seen_candidate;
130  }
131 
136  inline Candidate& get_candidate(const int &r)
137  {
138  return _candidates.at(r);
139  }
140 
141  inline Candidate get_candidate(const int &r) const
142  {
143  return _candidates.at(r);
144  }
145 
149  inline std::vector<Candidate>& candidates()
150  {
151  return _candidates;
152  }
153 
158  inline int size() const
159  {
160  return _candidates.size();
161  }
162 
168  void reset();
169 
174  void reset_as_fixed(const int &k);
175 
179  bool get_pli(const int &k, pli &p) const
180  {
181  std::map<int,pli>::const_iterator mit;
182  if ((mit=_pls.find(k))!=_pls.end())
183  {
184  p = (*mit).second;
185  return true;
186  }
187  return false;
188  }
189 
194  inline int dim() const
195  {
196  return _xmean.size();
197  }
198 
203  inline double edm() const
204  {
205  return _edm;
206  }
207 
212  inline dMat cov() const
213  {
214  return _cov;
215  }
216 
221  inline const dMat& cov_ref() const
222  {
223  return _cov;
224  }
225 
230  inline const double* cov_data() const
231  {
232  return _cov.data();
233  }
234 
240  dMat full_cov() const;
241 
246  inline dMat sepcov() const
247  {
248  return _sepcov;
249  }
250 
255  inline const dMat& sepcov_ref() const
256  {
257  return _sepcov;
258  }
259 
264  inline const double* sepcov_data() const
265  {
266  return _sepcov.data();
267  }
268 
273  inline dMat csqinv() const
274  {
275  return _csqinv;
276  }
277 
282  inline dMat sepcsqinv() const
283  {
284  return _sepcsqinv;
285  }
286 
294  template<class TGenoPheno=GenoPheno<NoBoundStrategy>>
295  inline dVec stds(const CMAParameters<TGenoPheno> &cmaparams) const
296  {
297  if (!cmaparams.is_sep() && !cmaparams.is_vd())
298  return cmaparams.get_gp().pheno(static_cast<dVec>(_cov.diagonal().cwiseSqrt()));
299  else if (cmaparams.is_sep())
300  return cmaparams.get_gp().pheno(static_cast<dVec>(_sepcov.cwiseSqrt()));
301  else if (cmaparams.is_vd())
302  return cmaparams.get_gp().pheno(static_cast<dVec>((dVec::Constant(cmaparams.dim(),1.0)+_v.cwiseProduct(_v)).cwiseSqrt().cwiseProduct(_sepcov)));
303  return dVec(); // should never reach here.
304  }
305 
311  template<class TGenoPheno=GenoPheno<NoBoundStrategy>>
312  inline dVec errors(const CMAParameters<TGenoPheno> &cmaparams) const
313  {
314  if (!cmaparams.is_sep() && !cmaparams.is_vd())
315  return cmaparams.get_gp().pheno(static_cast<dVec>(std::sqrt(_sigma)*_cov.diagonal().cwiseSqrt()));
316  else if (cmaparams.is_sep())
317  return cmaparams.get_gp().pheno(static_cast<dVec>(std::sqrt(_sigma)*_sepcov.cwiseSqrt()));
318  else if (cmaparams.is_vd())
319  return cmaparams.get_gp().pheno(static_cast<dVec>(std::sqrt(_sigma)*(dVec::Constant(cmaparams.dim(),1.0)+_v.cwiseProduct(_v)).cwiseSqrt().cwiseProduct(_sepcov)));
320  return dVec(); // should never reach here.
321  }
322 
327  dMat corr() const;
328 
333  double corr(const int &i, const int &j) const;
334 
339  inline double sigma() const
340  {
341  return _sigma;
342  }
343 
348  inline dVec xmean() const
349  {
350  return _xmean;
351  }
352 
357  inline void set_xmean(const dVec &xmean)
358  {
359  _xmean = xmean;
360  }
361 
366  inline int run_status() const
367  {
368  return _run_status;
369  }
370 
375  inline std::string status_msg() const
376  {
377  return CMAStopCriteria<>::_scriterias[_run_status];
378  }
379 
384  inline int elapsed_time() const
385  {
386  return _elapsed_time;
387  }
388 
393  inline int elapsed_last_iter() const
394  {
395  return _elapsed_last_iter;
396  }
397 
402  inline int niter() const
403  {
404  return _niter;
405  }
406 
411  inline int nevals() const
412  {
413  return _nevals;
414  }
415 
420  inline double min_eigenv() const
421  {
422  return _min_eigenv;
423  }
424 
429  inline double max_eigenv() const
430  {
431  return _max_eigenv;
432  }
433 
438  inline bool updated_eigen() const
439  {
440  return _updated_eigen;
441  }
442 
447  inline int fevals() const
448  {
449  return _nevals;
450  }
451 
456  inline dVec eigenvalues() const
457  {
458  return _leigenvalues;
459  }
460 
465  inline dMat eigenvectors() const
466  {
467  return _leigenvectors;
468  }
469 
475  template <class TGenoPheno=GenoPheno<NoBoundStrategy>>
476  std::ostream& print(std::ostream &out,
477  const int &verb_level=0,
478  const TGenoPheno &gp=TGenoPheno()) const;
479 
480  private:
481  dMat _cov;
482  dMat _csqinv;
483  dMat _sepcov;
484  dMat _sepcsqinv;
485  dVec _xmean;
486  dVec _psigma;
487  dVec _pc;
488  short _hsig = 1;
489  double _sigma;
490  std::vector<Candidate> _candidates;
491  std::vector<Candidate> _best_candidates_hist;
492  int _max_hist = -1;
494  double _max_eigenv = 0.0;
495  double _min_eigenv = 0.0;
496  dVec _leigenvalues;
497  dMat _leigenvectors;
498  int _niter = 0;
499  int _nevals = 0;
500  int _kcand = 1;
501  std::vector<Candidate> _k_best_candidates_hist;
502  std::vector<double> _bfvalues;
503  std::vector<double> _median_fvalues;
505  int _eigeniter = 0;
506  bool _updated_eigen = true;
508  // status of the run.
509  int _run_status = 0;
510  int _elapsed_time = 0;
511  int _elapsed_last_iter = 0;
512 #ifdef HAVE_DEBUG
513  int _elapsed_eval = 0;
514  int _elapsed_ask = 0;
515  int _elapsed_tell = 0;
516  int _elapsed_stop = 0;
517 #endif
518 
519  std::map<int,pli> _pls;
520  double _edm = 0.0;
522  Candidate _best_seen_candidate;
523  int _best_seen_iter;
524  Candidate _worst_seen_candidate;
525  Candidate _initial_candidate;
526 
527  dVec _v;
529  std::vector<RankedCandidate> _candidates_uh;
530  int _lambda_reev;
531  double _suh;
533  double _tpa_s = 0.0;
534  int _tpa_p1 = 0;
535  int _tpa_p2 = 1;
536  dVec _tpa_x1;
537  dVec _tpa_x2;
538  dVec _xmean_prev;
539  };
540 
541  std::ostream& operator<<(std::ostream &out,const CMASolutions &cmas);
542 
543 }
544 
545 #endif
double sigma() const
returns current value of step-size sigma
Definition: cmasolutions.h:339
Implementation of the IPOP flavor of CMA-ES, with restarts that linearly increase the population of o...
Definition: ipopcmastrategy.h:35
Simple surrogate strategy: trains every n steps, and exploits in between, mostly as an example and fo...
Definition: surrogatestrategy.h:215
CMASolutions()
dummy constructor.
Definition: cmasolutions.h:62
bool updated_eigen() const
returns whether the last update is lazy
Definition: cmasolutions.h:438
Holder of the set of evolving solutions from running an instance of CMA-ES.
Definition: cmasolutions.h:41
const dMat & cov_ref() const
returns reference to error covariance matrix
Definition: cmasolutions.h:221
profile likelihood object holder as a set of points and values.
Definition: pli.h:34
Generic class for Evolution Strategy parameters.
Definition: parameters.h:41
const dMat & sepcov_ref() const
returns reference to separable covariance diagonal vector, only applicable to sep-CMA-ES algorithms...
Definition: cmasolutions.h:255
dVec xmean() const
returns current distribution's mean in parameter space
Definition: cmasolutions.h:348
const double * sepcov_data() const
returns pointer to covariance diagnoal vector
Definition: cmasolutions.h:264
std::string status_msg() const
returns current optimization status' message.
Definition: cmasolutions.h:375
unsigned int get_x_size() const
get x vector size
Definition: candidate.h:106
Covariance Matrix update. This is an implementation closely follows: Hansen, N. (2009). Benchmarking a BI-Population CMA-ES on the BBOB-2009 Function Testbed. Workshop Proceedings of the GECCO Genetic and Evolutionary Computation Conference, ACM, pp. 2389-2395.
Definition: covarianceupdate.h:37
std::ostream & print(std::ostream &out, const int &verb_level=0, const TGenoPheno &gp=TGenoPheno()) const
print the solution object out.
Definition: cmasolutions.cc:257
Main class describing an evolutionary optimization strategy. Every algorithm in libcmaes descends fro...
Definition: esostrategy.h:51
int run_status() const
returns current optimization status.
Definition: cmasolutions.h:366
double edm() const
returns expected distance to minimum.
Definition: cmasolutions.h:203
double max_eigenv() const
returns current maximal eigen value
Definition: cmasolutions.h:429
bool get_pli(const int &k, pli &p) const
get profile likelihood if previously computed.
Definition: cmasolutions.h:179
void reset_as_fixed(const int &k)
re-arrange solution object such that parameter 'k' is fixed (i.e. removed).
Definition: cmasolutions.cc:229
void reset()
resets the solution object in order to restart from the current solution with fresh covariance matrix...
Definition: cmasolutions.cc:205
Candidate best_candidate() const
returns current best solution candidate. NOTE: candidates MUST be sorted
Definition: cmasolutions.h:103
void update_eigenv(const dVec &eigenvalues, const dMat &eigenvectors)
updates reference eigenvalue and eigenvectors, for use in termination criteria.
Definition: cmasolutions.cc:196
Definition: errstats.h:33
void sort_candidates()
sorts the current internal set of solution candidates.
Definition: cmasolutions.h:76
linear scaling of the parameter space to achieve similar sensitivity across all components.
Definition: acovarianceupdate.cc:25
Parameters for various flavors of the CMA-ES algorithm.
Definition: cmaparameters.h:35
dMat corr() const
returns correlation matrix
Definition: cmasolutions.cc:138
dMat cov() const
returns error covariance matrix
Definition: cmasolutions.h:212
ACM Surrogate strategy for CMA-ES, follows: 'Surrogate-Assisted Evolutionary Algorithms', Ilya Loshchilov, PhD Thesis, Universite Paris-Sud 11, 2013. http://www.loshchilov.com/phd.html see Chapter 4.
Definition: surrogatestrategy.h:294
dMat sepcsqinv() const
returns inverse root square of separable covariance diagonal matrix, only applicable to sep-CMA-ES al...
Definition: cmasolutions.h:282
dVec stds(const CMAParameters< TGenoPheno > &cmaparams) const
Definition: cmasolutions.h:295
dMat csqinv() const
returns inverse root square of covariance matrix
Definition: cmasolutions.h:273
int nevals() const
returns current budget (number of objective function calls)
Definition: cmasolutions.h:411
int niter() const
returns current number of iterations
Definition: cmasolutions.h:402
Implementation of the BIPOP flavor of CMA-ES, with restarts that control the population of offsprings...
Definition: bipopcmastrategy.h:37
double get_fvalue() const
get function value of this candidate.
Definition: candidate.h:64
const double * cov_data() const
returns pointer to covariance matrix array
Definition: cmasolutions.h:230
void set_xmean(const dVec &xmean)
sets the current distributions' mean in parameter space
Definition: cmasolutions.h:357
Active Covariance Matrix update. This implementation closely follows N. Hansen, R. Ros, "Benchmarking a Weighted Negative Covariance Matrix Update on the BBOB-2010 Noiseless Testbed", GECCO'10, 2010.
Definition: acovarianceupdate.h:38
CMA-ES termination criteria, see reference paper in cmastrategy.h.
Definition: cmastopcriteria.h:75
an optimizer main class.
Definition: esoptimizer.h:71
Candidate get_best_seen_candidate() const
returns the best seen candidate.
Definition: cmasolutions.h:118
std::vector< Candidate > & candidates()
get a reference to the full candidate set
Definition: cmasolutions.h:149
candidate solution point, in function parameter space.
Definition: candidate.h:33
dMat full_cov() const
returns full covariance matrix. Similar to cov() but in case of linear-sized algorithms like sep and ...
Definition: cmasolutions.cc:128
int elapsed_last_iter() const
returns time spent on last iteration
Definition: cmasolutions.h:393
VD-CMA update that is a linear time/space variant of CMA-ES This is an implementation that closely fo...
Definition: vdcmaupdate.h:38
double min_eigenv() const
returns current minimal eigen value
Definition: cmasolutions.h:420
dVec errors(const CMAParameters< TGenoPheno > &cmaparams) const
Definition: cmasolutions.h:312
bool is_sep() const
whether algorithm leverages separability.
Definition: cmaparameters.h:182
dMat sepcov() const
returns separable covariance diagonal matrix, only applicable to sep-CMA-ES algorithms.
Definition: cmasolutions.h:246
This is an implementation of CMA-ES. It uses the reference algorithm and termination criteria of the ...
Definition: cmastrategy.h:45
TGenoPheno get_gp() const
returns the current genotype/phenotype transform object.
Definition: parameters.h:395
void update_best_candidates()
updates the history of best candidates, as well as other meaningful values, typically used in termina...
Definition: cmasolutions.cc:90
bool is_vd() const
whether algorithm uses vd update.
Definition: cmaparameters.h:193
int dim() const
returns the problem's dimension
Definition: parameters.h:341
int fevals() const
returns current number of objective function evaluations
Definition: cmasolutions.h:447
int elapsed_time() const
returns currently elapsed time spent on optimization
Definition: cmasolutions.h:384
dVec eigenvalues() const
returns last computed eigenvalues
Definition: cmasolutions.h:456
int size() const
number of candidate solutions.
Definition: cmasolutions.h:158
Candidate & get_candidate(const int &r)
get a reference to the r-th candidate in current set
Definition: cmasolutions.h:136
int dim() const
return problem dimension.
Definition: cmasolutions.h:194
dMat eigenvectors() const
returns last computed eigenvectors
Definition: cmasolutions.h:465
Candidate get_worst_seen_candidate() const
returns the worst seen candidate.
Definition: cmasolutions.h:127