libcmaes
A C++11 library for stochastic optimization with CMA-ES
 All Classes Namespaces Functions Variables Typedefs
parameters.h
1 
22 #ifndef PARAMETERS_H
23 #define PARAMETERS_H
24 
25 #include "eo_matrix.h"
26 #include "genopheno.h"
27 #include "llogging.h"
28 #include <string>
29 #include <cmath>
30 #include <limits>
31 #include <unordered_map>
32 #include <map>
33 #include <chrono>
34 
35 namespace libcmaes
36 {
40  template <class TGenoPheno=GenoPheno<NoBoundStrategy> >
41  class Parameters
42  {
43  friend class CMASolutions;
44  template <class U, class V> friend class CMAStrategy;
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  friend class VDCMAUpdate;
53  friend class Candidate;
54 #ifdef HAVE_SURROG
55  template <template <class X,class Y> class U, class V, class W> friend class SimpleSurrogateStrategy;
56  template <template <class X,class Y> class U, class V, class W> friend class ACMSurrogateStrategy;
57 #endif
58 
59  public:
64  {}
65 
74  Parameters(const int &dim, const double *x0, const int &lambda=-1,
75  const uint64_t &seed=0, const TGenoPheno &gp=GenoPheno<NoBoundStrategy>())
76  :_dim(dim),_lambda(lambda),_seed(seed),_gp(gp)
77  {
78  if (_lambda == -1 || _lambda < 2) // lambda is unspecified
79  _lambda = 4 + floor(3.0*log(_dim));
80  if (_seed == 0) // seed is not forced.
81  _seed = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
82  set_x0(x0);
83  }
84 
85  ~Parameters()
86  {
87  }
88 
93  void set_x0(const double &x0)
94  {
95  _x0min = _x0max = dVec::Constant(_dim,x0);
96  }
97 
102  void set_x0(const double *x0)
103  {
104  _x0min = _x0max = dVec(_dim);
105  for (int i=0;i<_dim;i++)
106  _x0min(i) = _x0max(i) = x0[i];
107  }
108 
113  void set_x0(const dVec &x0)
114  {
115  _x0min = x0;
116  _x0max = x0;
117  }
118 
126  void set_x0(const double &x0min, const double &x0max)
127  {
128  _x0min = dVec::Constant(_dim,x0min);
129  _x0max = dVec::Constant(_dim,x0max);
130  }
131 
138  void set_x0(const double *x0min, const double *x0max)
139  {
140  _x0min = dVec(_dim);
141  _x0max = dVec(_dim);
142  for (int i=0;i<_dim;i++)
143  {
144  _x0min(i) = x0min[i];
145  _x0max(i) = x0max[i];
146  }
147  }
148 
155  void set_x0(const std::vector<double> &x0min, const std::vector<double> &x0max)
156  {
157  set_x0(&x0min[0],&x0max[0]);
158  }
159 
166  void set_x0(const dVec &x0min, const dVec &x0max)
167  {
168  _x0min = x0min;
169  _x0max = x0max;
170  }
171 
176  inline dVec get_x0min() const
177  {
178  return _x0min;
179  }
180 
185  inline dVec get_x0max() const
186  {
187  return _x0max;
188  }
189 
195  void set_fixed_p(const int &index, const double &value)
196  {
197  _fixed_p.insert(std::pair<int,double>(index,value));
198  }
199 
204  void unset_fixed_p(const int &index)
205  {
206  std::unordered_map<int,double>::iterator mit;
207  if ((mit=_fixed_p.find(index))!=_fixed_p.end())
208  _fixed_p.erase(mit);
209  }
210 
215  void set_max_iter(const int &maxiter)
216  {
217  _max_iter = maxiter;
218  }
219 
224  inline int get_max_iter() const
225  {
226  return _max_iter;
227  }
228 
233  void set_max_fevals(const int &fevals)
234  {
235  _max_fevals = fevals;
236  }
237 
242  inline int get_max_fevals() const
243  {
244  return _max_fevals;
245  }
246 
251  void set_ftarget(const double &val)
252  {
253  _ftarget = val;
254  }
255 
260  {
261  _ftarget = -std::numeric_limits<double>::infinity();
262  }
263 
268  inline double get_ftarget() const
269  {
270  return _ftarget;
271  }
272 
277  void set_seed(const int &seed)
278  {
279  if (_seed == 0)
280  _seed = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
281  else _seed = seed;
282  }
283 
288  inline int get_seed() const
289  {
290  return _seed;
291  }
292 
299  void set_ftolerance(const double &v) { _ftolerance = v; }
300 
305  inline double get_ftolerance() const
306  {
307  return _ftolerance;
308  }
309 
314  void set_xtolerance(const double &v)
315  {
316  _xtol = v;
317  }
318 
323  double get_xtolerance() const
324  {
325  return _xtol;
326  }
327 
332  inline int lambda() const
333  {
334  return _lambda;
335  }
336 
341  inline int dim() const
342  {
343  return _dim;
344  }
345 
350  void set_quiet(const bool &quiet)
351  {
352  _quiet = quiet;
353  }
354 
359  inline bool quiet() const
360  {
361  return _quiet;
362  }
363 
368  void set_algo(const int &algo)
369  {
370  _algo = algo;
371  }
372 
377  inline int get_algo() const
378  {
379  return _algo;
380  }
381 
386  void set_gp(const TGenoPheno &gp)
387  {
388  _gp = gp;
389  }
390 
395  inline TGenoPheno get_gp() const
396  {
397  return _gp;
398  }
399 
404  void set_fplot(const std::string &fplot)
405  {
406  _fplot = fplot;
407  }
408 
413  void set_full_fplot(const bool &b)
414  {
415  _full_fplot = b;
416  }
417 
422  inline std::string get_fplot() const
423  {
424  return _fplot;
425  }
426 
432  void set_gradient(const bool &gradient)
433  {
434  _with_gradient = gradient;
435  /*if (this->_tpa != 0)
436  set_tpa(2); */ // TPA default when gradient is activated. XXX: deactivated until flaw is fixed.
437  }
438 
443  inline bool get_gradient() const
444  {
445  return _with_gradient;
446  }
447 
452  void set_edm(const bool &edm)
453  {
454  _with_edm = edm;
455  }
456 
461  inline bool get_edm() const
462  {
463  return _with_edm;
464  }
465 
470  void set_mt_feval(const bool &mt)
471  {
472  _mt_feval = mt;
473  }
474 
479  inline bool get_mt_feval() const
480  {
481  return _mt_feval;
482  }
483 
488  void set_max_hist(const int &m)
489  {
490  _max_hist = m;
491  }
492 
497  void set_maximize(const bool &maximize)
498  {
499  _maximize = maximize;
500  }
501 
506  bool get_maximize() const
507  {
508  return _maximize;
509  }
510 
515  inline void set_initial_fvalue(const bool &b)
516  {
517  _initial_fvalue = b;
518  }
519 
524  inline void set_uh(const bool &b)
525  {
526  _uh = b;
527  }
528 
533  inline bool get_uh() const
534  {
535  return _uh;
536  }
537 
542  inline void set_tpa(const int &b)
543  {
544  _tpa = b;
545  }
546 
551  inline int get_tpa() const
552  {
553  return _tpa;
554  }
555 
556  protected:
557  int _dim;
558  int _lambda = -1;
559  int _max_iter = -1;
560  int _max_fevals = -1;
562  bool _quiet = true;
563  std::string _fplot = "";
564  bool _full_fplot = false;
565  dVec _x0min;
566  dVec _x0max;
567  double _ftarget = -std::numeric_limits<double>::infinity();
568  double _ftolerance = 1e-12;
569  double _xtol = 1e-12;
571  uint64_t _seed = 0;
572  int _algo = 0;
574  bool _with_gradient=false;
575  bool _with_edm=false;
577  std::unordered_map<int,double> _fixed_p;
579  TGenoPheno _gp;
581  bool _mt_feval = false;
582  int _max_hist = -1;
584  bool _maximize = false;
585  static std::map<std::string,int> _algos; ;
586 
587  bool _initial_fvalue = false;
589  // uncertainty handling
590  bool _uh = false;
591  double _rlambda;
592  double _epsuh = 1e-7;
593  double _thetauh = 0.2;
594  double _csuh = 1.0;
595  double _alphathuh = 1.0;
597  // two-point adaptation
598  int _tpa = 1;
599  double _tpa_csigma = 0.3;
600  };
601 }
602 
603 #endif
int _dim
Definition: parameters.h:557
std::unordered_map< int, double > _fixed_p
Definition: parameters.h:577
Implementation of the IPOP flavor of CMA-ES, with restarts that linearly increase the population of o...
Definition: ipopcmastrategy.h:35
void set_quiet(const bool &quiet)
sets the quiet mode (no output from the library) for the optimization at hand
Definition: parameters.h:350
void set_ftolerance(const double &v)
sets function tolerance as stopping criteria for TolHistFun: monitors the difference in function valu...
Definition: parameters.h:299
std::string _fplot
Definition: parameters.h:563
int _tpa
Definition: parameters.h:598
int _max_hist
Definition: parameters.h:582
void set_max_iter(const int &maxiter)
sets the maximum number of iterations allowed for the optimization.
Definition: parameters.h:215
int lambda() const
returns lambda, number of offsprings per generation
Definition: parameters.h:332
void set_edm(const bool &edm)
activates computation of expected distance to minimum when optimization has completed ...
Definition: parameters.h:452
int _lambda
Definition: parameters.h:558
Simple surrogate strategy: trains every n steps, and exploits in between, mostly as an example and fo...
Definition: surrogatestrategy.h:215
Holder of the set of evolving solutions from running an instance of CMA-ES.
Definition: cmasolutions.h:41
Parameters(const int &dim, const double *x0, const int &lambda=-1, const uint64_t &seed=0, const TGenoPheno &gp=GenoPheno< NoBoundStrategy >())
constructor
Definition: parameters.h:74
TGenoPheno _gp
Definition: parameters.h:579
Generic class for Evolution Strategy parameters.
Definition: parameters.h:41
void set_ftarget(const double &val)
sets the objective function target value when known.
Definition: parameters.h:251
dVec get_x0max() const
returns upper bound on x0 vector
Definition: parameters.h:185
void set_uh(const bool &b)
activates / deactivates uncertainty handling scheme.
Definition: parameters.h:524
int _algo
Definition: parameters.h:572
int get_max_iter() const
returns maximum number of iterations
Definition: parameters.h:224
void set_maximize(const bool &maximize)
active internal maximization scheme (simply returns -f instead of f)
Definition: parameters.h:497
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
void reset_ftarget()
resets the objective function target value to its inactive state.
Definition: parameters.h:259
void set_x0(const double *x0)
sets initial objective function parameter values to array x0
Definition: parameters.h:102
void set_full_fplot(const bool &b)
activates / deactivates the full output (for legacy plotting).
Definition: parameters.h:413
bool quiet() const
returns whether the quiet mode is on.
Definition: parameters.h:359
double _epsuh
Definition: parameters.h:592
dVec get_x0min() const
returns lower bound on x0 vector
Definition: parameters.h:176
void set_xtolerance(const double &v)
sets parameter tolerance as stopping criteria for TolX.
Definition: parameters.h:314
void set_tpa(const int &b)
activates / deactivates two-point adaptation step-size mechanism
Definition: parameters.h:542
void set_x0(const std::vector< double > &x0min, const std::vector< double > &x0max)
sets bounds on initial objective function parameter values. Initial value is sampled uniformly within...
Definition: parameters.h:155
void set_seed(const int &seed)
sets random generator's seed, 0 is special value to generate random seed.
Definition: parameters.h:277
Main class describing an evolutionary optimization strategy. Every algorithm in libcmaes descends fro...
Definition: esostrategy.h:51
int get_max_fevals() const
returns maximum budget of objective function calls
Definition: parameters.h:242
int get_algo() const
returns which algorithm is set for the optimization at hand.
Definition: parameters.h:377
std::string get_fplot() const
returns the current output filename.
Definition: parameters.h:422
double _rlambda
Definition: parameters.h:591
void set_x0(const double &x0min, const double &x0max)
sets bounds on initial objective function parameter values. Bounds are the same across all dimensions...
Definition: parameters.h:126
void set_fplot(const std::string &fplot)
sets the output filename (activates the output to file).
Definition: parameters.h:404
Definition: errstats.h:33
bool get_uh() const
get uncertainty handling status.
Definition: parameters.h:533
double get_ftarget() const
returns objective function target value.
Definition: parameters.h:268
linear scaling of the parameter space to achieve similar sensitivity across all components.
Definition: acovarianceupdate.cc:25
double _alphathuh
Definition: parameters.h:595
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
dVec _x0max
Definition: parameters.h:566
dVec _x0min
Definition: parameters.h:565
void set_fixed_p(const int &index, const double &value)
freezes a parameter to a given value during optimization.
Definition: parameters.h:195
bool _maximize
Definition: parameters.h:584
Parameters()
empty constructor.
Definition: parameters.h:63
void set_max_hist(const int &m)
sets maximum history size, allows to keep memory requirements fixed.
Definition: parameters.h:488
bool get_edm() const
returns whether edm is activated.
Definition: parameters.h:461
int get_seed() const
returns random generator's seed.
Definition: parameters.h:288
Implementation of the BIPOP flavor of CMA-ES, with restarts that control the population of offsprings...
Definition: bipopcmastrategy.h:37
bool _mt_feval
Definition: parameters.h:581
double _ftolerance
Definition: parameters.h:568
void set_mt_feval(const bool &mt)
activate / deactivate the parallel evaluation of objective function
Definition: parameters.h:470
void set_x0(const dVec &x0)
sets initial objective function parameter values from Eigen vector
Definition: parameters.h:113
void set_x0(const dVec &x0min, const dVec &x0max)
sets bounds on initial objective function parameter values. Initial value is sampled uniformly within...
Definition: parameters.h:166
int get_tpa() const
get two-point adapation step-size mechanism status.
Definition: parameters.h:551
void set_x0(const double &x0)
sets initial objective function parameter values to x0 across all dimensions
Definition: parameters.h:93
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
bool _uh
Definition: parameters.h:590
double _csuh
Definition: parameters.h:594
void unset_fixed_p(const int &index)
unfreezes a parameter.
Definition: parameters.h:204
bool _full_fplot
Definition: parameters.h:564
bool _initial_fvalue
Definition: parameters.h:587
void set_gp(const TGenoPheno &gp)
sets the genotype/phenotype transform object.
Definition: parameters.h:386
candidate solution point, in function parameter space.
Definition: candidate.h:33
bool _with_edm
Definition: parameters.h:575
bool _quiet
Definition: parameters.h:562
VD-CMA update that is a linear time/space variant of CMA-ES This is an implementation that closely fo...
Definition: vdcmaupdate.h:38
int _max_iter
Definition: parameters.h:559
This is an implementation of CMA-ES. It uses the reference algorithm and termination criteria of the ...
Definition: cmastrategy.h:45
double _thetauh
Definition: parameters.h:593
TGenoPheno get_gp() const
returns the current genotype/phenotype transform object.
Definition: parameters.h:395
bool get_gradient() const
returns whether the gradient injection scheme is activated.
Definition: parameters.h:443
int dim() const
returns the problem's dimension
Definition: parameters.h:341
Definition: genopheno.h:35
void set_x0(const double *x0min, const double *x0max)
sets bounds on initial objective function parameter values. Initial value is sampled uniformly within...
Definition: parameters.h:138
int _max_fevals
Definition: parameters.h:560
void set_max_fevals(const int &fevals)
sets the maximum budget of objective function calls allowed for the optimization. ...
Definition: parameters.h:233
void set_algo(const int &algo)
sets the optimization algorithm.
Definition: parameters.h:368
bool get_mt_feval() const
returns whether the parallel evaluation of objective function is activated
Definition: parameters.h:479
void set_gradient(const bool &gradient)
activates the gradient injection scheme. If no gradient function is defined, injects a numerical grad...
Definition: parameters.h:432
static std::map< std::string, int > _algos
Definition: parameters.h:585
double _xtol
Definition: parameters.h:569
void set_initial_fvalue(const bool &b)
whether to compute initial objective function value (i.e. at x0)
Definition: parameters.h:515
bool get_maximize() const
returns whether the maximization mode is enabled
Definition: parameters.h:506
double _ftarget
Definition: parameters.h:567
double get_xtolerance() const
returns parameter tolerance
Definition: parameters.h:323
double get_ftolerance() const
returns function tolerance
Definition: parameters.h:305
uint64_t _seed
Definition: parameters.h:571
bool _with_gradient
Definition: parameters.h:574