libcmaes
A C++11 library for stochastic optimization with CMA-ES
 All Classes Namespaces Functions Variables Typedefs
cmaparameters.h
1 
22 #ifndef CMAPARAMETERS_H
23 #define CMAPARAMETERS_H
24 
25 #include "parameters.h"
26 #include "eo_matrix.h"
27 #include <float.h>
28 
29 namespace libcmaes
30 {
34  template <class TGenoPheno=GenoPheno<NoBoundStrategy> >
35  class CMAParameters : public Parameters<TGenoPheno>
36  {
37  friend class CMASolutions;
38  template <class U, class V> friend class CMAStrategy;
39  template <class U, class V, class W> friend class ESOStrategy;
40  template <class U> friend class CMAStopCriteria;
41  template <class U, class V> friend class IPOPCMAStrategy;
42  template <class U, class V> friend class BIPOPCMAStrategy;
43  friend class CovarianceUpdate;
44  friend class ACovarianceUpdate;
45  template <class U> friend class errstats;
46  friend class VDCMAUpdate;
47 
48  public:
49  CMAParameters() {} //TODO: var init even if this constructor is not supposed to be used for now.
50 
61  CMAParameters(const int &dim,
62  const double *x0,
63  const double &sigma,
64  const int &lambda=-1,
65  const uint64_t &seed=0,
66  const TGenoPheno &gp=TGenoPheno());
67 
77  CMAParameters(const std::vector<double> &x0,
78  const double &sigma,
79  const int &lambda=-1,
80  const uint64_t &seed=0,
81  const TGenoPheno &gp=TGenoPheno());
82 
92  CMAParameters(const std::vector<double> &x0,
93  const std::vector<double> &sigma,
94  const int &lambda=-1,
95  const std::vector<double> &lbounds=std::vector<double>(),
96  const std::vector<double> &ubounds=std::vector<double>(),
97  const uint64_t &seed=0);
98 
99  ~CMAParameters();
100 
104  void initialize_parameters();
105 
106 
107  void reset_as_fixed(const int &k);
108 
112  void set_noisy();
113 
119  void set_algo(const int &algo)
120  {
121  this->_algo = algo;
122  /*if (this->_tpa != 0
123  && (this->_algo == 6 // sepCMAES
124  || this->_algo == 7 //sepIPOP_CMAES
125  || this->_algo == 8 //sepBIPOP_CMAES
126  || this->_algo == 9 //sepaCMAES
127  || this->_algo == 10 //sepaIPOP_CMAES
128  || this->_algo == 11 //sepBIPOP_CMAES
129  || this->_algo == 12 //VD_CMAES
130  || this->_algo == 13 //VD_IPOP_CMAES
131  || this->_algo == 14)) //VD_BIPOP_CMAES
132  set_tpa(2); */ // XXX: deactivated until flaw is fixed
133  }
134 
139  void set_str_algo(const std::string &algo)
140  {
141  std::map<std::string,int>::const_iterator mit;
143  Parameters<TGenoPheno>::_algo = (*mit).second;
144  else LOG(ERROR) << "unknown algorithm " << algo << std::endl;
145  if (algo.find("sep")!=std::string::npos)
146  set_sep();
147  if (algo.find("vd")!=std::string::npos)
148  set_vd();
149  }
150 
155  double get_sigma_init() const
156  {
157  return _sigma_init;
158  }
159 
166  void set_gradient(const bool &gradient)
167  {
168  this->_with_gradient = gradient;
169  /*if (this->_tpa != 0)
170  set_tpa(2);*/ // TPA default when gradient is activated.
171  }
172 
176  void set_sep();
177 
182  bool is_sep() const { return _sep; }
183 
187  void set_vd();
188 
193  bool is_vd() const { return _vd; }
194 
201  void set_fixed_p(const int &index, const double &value);
202 
207  void unset_fixed_p(const int &index);
208 
213  inline void set_restarts(const int &nrestarts) { _nrestarts = nrestarts; }
214 
219  inline int get_restarts() const { return _nrestarts; }
220 
225  inline void set_lazy_update(const bool &lz) { _lazy_update = lz; }
226 
231  inline bool get_lazy_update() { return _lazy_update; }
232 
242  inline void set_elitism(const int &e)
243  {
244  if (e == 0)
245  _elitist = _initial_elitist = _initial_elitist_on_restart;
246  else if (e == 1)
247  {
248  _elitist = true;
249  _initial_elitist = _initial_elitist_on_restart = false;
250  }
251  else if (e == 2)
252  {
253  _initial_elitist = true;
254  _elitist = _initial_elitist_on_restart = false;
255  }
256  else if (e == 3)
257  {
258  _initial_elitist_on_restart = true;
259  _elitist = _initial_elitist = false;
260  }
261  }
262 
269  inline void set_stopping_criteria(const int &criteria,
270  const bool &active)
271  {
272  _stoppingcrit.insert(std::pair<int,bool>(criteria,active));
273  }
274 
280  void set_tpa(const int &b); // overrides def in parameters.h in order to reset dsigma
281 
286  void set_tpa_dsigma(const double &d) { _dsigma = d; }
287 
288  private:
289  int _mu;
290  dVec _weights;
291  double _csigma;
292  double _c1;
293  double _cmu;
294  double _cc;
295  double _muw;
296  double _dsigma;
298  // computed once at init for speeding up operations.
299  double _fact_ps;
300  double _fact_pc;
301  double _chi;
303  double _sigma_init;
305  int _nrestarts = 9;
306  bool _lazy_update;
307  double _lazy_value;
309  // active cma.
310  double _cm;
311  double _alphacov;
312  double _alphaminusold;
313  double _deltamaxsigma;
314  double _lambdamintarget;
315  double _alphaminusmin;
317  // sep cma (diagonal cov).
318  bool _sep = false;
319  bool _vd = false;
320 
321  bool _elitist = false;
322  bool _initial_elitist = false;
323  bool _initial_elitist_on_restart = false;
325  // stopping criteria
326  std::map<int,bool> _stoppingcrit;
327  };
328 
329  template<class TGenoPheno>
330  std::map<std::string,int> Parameters<TGenoPheno>::_algos = {{"cmaes",0},{"ipop",1},{"bipop",2},{"acmaes",3},{"aipop",4},{"abipop",5},{"sepcmaes",6},{"sepipop",7},{"sepbipop",8},{"sepacmaes",9},{"sepipop",10},{"sepbipop",11},{"vdcma",12},{"vdipopcma",13},{"vdbipopcma",14}};
331 }
332 
333 #endif
Implementation of the IPOP flavor of CMA-ES, with restarts that linearly increase the population of o...
Definition: ipopcmastrategy.h:35
void set_fixed_p(const int &index, const double &value)
freezes a parameter to a given value in genotype during optimization. Adapts some generic parameters ...
Definition: cmaparameters.cc:179
int lambda() const
returns lambda, number of offsprings per generation
Definition: parameters.h:332
Holder of the set of evolving solutions from running an instance of CMA-ES.
Definition: cmasolutions.h:41
Generic class for Evolution Strategy parameters.
Definition: parameters.h:41
void set_lazy_update(const bool &lz)
sets the lazy update (i.e. updates the eigenvalues every few steps).
Definition: cmaparameters.h:225
int _algo
Definition: parameters.h:572
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 set_str_algo(const std::string &algo)
sets the optimization algorithm.
Definition: cmaparameters.h:139
void set_algo(const int &algo)
sets the optimization algorithm. Note: overrides Parameters::set_algo
Definition: cmaparameters.h:119
void set_vd()
activates VD decomposition.
Definition: cmaparameters.cc:162
int get_restarts() const
get the number of restarts (applies to IPOP and BIPOP).
Definition: cmaparameters.h:219
Main class describing an evolutionary optimization strategy. Every algorithm in libcmaes descends fro...
Definition: esostrategy.h:51
void set_elitism(const int &e)
sets elitism: 0 -> no elitism 1 -> elitism: reinjects the best-ever seen solution 2 -> initial elitis...
Definition: cmaparameters.h:242
Definition: errstats.h:33
void set_restarts(const int &nrestarts)
sets the maximum number of restarts (applies to IPOP and BIPOP).
Definition: cmaparameters.h:213
void set_noisy()
adapt parameters for noisy objective function.
Definition: cmaparameters.cc:121
bool get_lazy_update()
get lazy update status.
Definition: cmaparameters.h:231
void set_gradient(const bool &gradient)
activates the gradient injection scheme. If no gradient function is defined, injects a numerical grad...
Definition: cmaparameters.h:166
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
void set_tpa_dsigma(const double &d)
sets dsigma value, use with care.
Definition: cmaparameters.h:286
Implementation of the BIPOP flavor of CMA-ES, with restarts that control the population of offsprings...
Definition: bipopcmastrategy.h:37
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
double get_sigma_init() const
returns initial sigma value
Definition: cmaparameters.h:155
void initialize_parameters()
initialize required parameters based on dim, lambda, x0 and sigma.
Definition: cmaparameters.cc:74
VD-CMA update that is a linear time/space variant of CMA-ES This is an implementation that closely fo...
Definition: vdcmaupdate.h:38
bool is_sep() const
whether algorithm leverages separability.
Definition: cmaparameters.h:182
void set_tpa(const int &b)
activates / deactivates two-point adaptation step-size mechanism. Overrides parameters::set_tpa by au...
Definition: cmaparameters.cc:132
This is an implementation of CMA-ES. It uses the reference algorithm and termination criteria of the ...
Definition: cmastrategy.h:45
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
void set_sep()
fix parameters for sep-CMA-ES, using only the diagonal of covariance matrix.
Definition: cmaparameters.cc:152
void set_stopping_criteria(const int &criteria, const bool &active)
all stopping criteria are active by default, this allows to control them
Definition: cmaparameters.h:269
void unset_fixed_p(const int &index)
unfreezes a parameter.
Definition: cmaparameters.cc:188
bool _with_gradient
Definition: parameters.h:574