libcmaes 0.10.2
A C++11 library for stochastic optimization with CMA-ES
Loading...
Searching...
No Matches
parameters.h
1
22#ifndef PARAMETERS_H
23#define PARAMETERS_H
24
25#include <libcmaes/eo_matrix.h>
26#include <libcmaes/genopheno.h>
27#include <libcmaes/llogging.h>
28#include <string>
29#include <cmath>
30#include <limits>
31#include <unordered_map>
32#include <map>
33#include <chrono>
34
35namespace libcmaes
36{
40 template <class TGenoPheno=GenoPheno<NoBoundStrategy> >
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
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 {
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 {
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 {
500 }
501
506 bool get_maximize() const
507 {
508 return _maximize;
509 }
510
515 inline void set_initial_fvalue(const bool &b)
516 {
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;
572 int _algo = 0;
574 bool _with_gradient=false;
575 bool _with_edm=false;
577 std::unordered_map<int,double> _fixed_p;
581 bool _mt_feval = false;
582 int _max_hist = -1;
584 bool _maximize = false;
585 static std::map<std::string,int> _algos;
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
ACM Surrogate strategy for CMA-ES, follows: 'Surrogate-Assisted Evolutionary Algorithms',...
Definition surrogatestrategy.h:295
Active Covariance Matrix update. This implementation closely follows N. Hansen, R....
Definition acovarianceupdate.h:39
Implementation of the BIPOP flavor of CMA-ES, with restarts that control the population of offsprings...
Definition bipopcmastrategy.h:38
Holder of the set of evolving solutions from running an instance of CMA-ES.
Definition cmasolutions.h:42
CMA-ES termination criteria, see reference paper in cmastrategy.h.
Definition cmastopcriteria.h:76
This is an implementation of CMA-ES. It uses the reference algorithm and termination criteria of the ...
Definition cmastrategy.h:46
candidate solution point, in function parameter space.
Definition candidate.h:34
Covariance Matrix update. This is an implementation closely follows: Hansen, N. (2009)....
Definition covarianceupdate.h:38
Main class describing an evolutionary optimization strategy. Every algorithm in libcmaes descends fro...
Definition esostrategy.h:52
an optimizer main class.
Definition esoptimizer.h:72
Implementation of the IPOP flavor of CMA-ES, with restarts that linearly increase the population of o...
Definition ipopcmastrategy.h:36
Generic class for Evolution Strategy parameters.
Definition parameters.h:42
bool _full_fplot
Definition parameters.h:564
bool get_edm() const
returns whether edm is activated.
Definition parameters.h:461
bool get_gradient() const
returns whether the gradient injection scheme is activated.
Definition parameters.h:443
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
double get_ftarget() const
returns objective function target value.
Definition parameters.h:268
void set_max_hist(const int &m)
sets maximum history size, allows to keep memory requirements fixed.
Definition parameters.h:488
int get_max_iter() const
returns maximum number of iterations
Definition parameters.h:224
bool get_maximize() const
returns whether the maximization mode is enabled
Definition parameters.h:506
TGenoPheno get_gp() const
returns the current genotype/phenotype transform object.
Definition parameters.h:395
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_fixed_p(const int &index, const double &value)
freezes a parameter to a given value during optimization.
Definition parameters.h:195
double _rlambda
Definition parameters.h:591
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
int get_max_fevals() const
returns maximum budget of objective function calls
Definition parameters.h:242
void set_uh(const bool &b)
activates / deactivates uncertainty handling scheme.
Definition parameters.h:524
bool _initial_fvalue
Definition parameters.h:587
bool quiet() const
returns whether the quiet mode is on.
Definition parameters.h:359
void set_full_fplot(const bool &b)
activates / deactivates the full output (for legacy plotting).
Definition parameters.h:413
bool _uh
Definition parameters.h:590
void set_x0(const double *x0)
sets initial objective function parameter values to array x0
Definition parameters.h:102
void unset_fixed_p(const int &index)
unfreezes a parameter.
Definition parameters.h:204
bool get_mt_feval() const
returns whether the parallel evaluation of objective function is activated
Definition parameters.h:479
static std::map< std::string, int > _algos
Definition parameters.h:585
int _max_iter
Definition parameters.h:559
void set_x0(const double &x0)
sets initial objective function parameter values to x0 across all dimensions
Definition parameters.h:93
int lambda() const
returns lambda, number of offsprings per generation
Definition parameters.h:332
void set_maximize(const bool &maximize)
active internal maximization scheme (simply returns -f instead of f)
Definition parameters.h:497
Parameters()
empty constructor.
Definition parameters.h:63
dVec get_x0max() const
returns upper bound on x0 vector
Definition parameters.h:185
void set_ftarget(const double &val)
sets the objective function target value when known.
Definition parameters.h:251
bool get_uh() const
get uncertainty handling status.
Definition parameters.h:533
int _max_hist
Definition parameters.h:582
bool _quiet
Definition parameters.h:562
void set_mt_feval(const bool &mt)
activate / deactivate the parallel evaluation of objective function
Definition parameters.h:470
dVec get_x0min() const
returns lower bound on x0 vector
Definition parameters.h:176
void set_x0(const dVec &x0)
sets initial objective function parameter values from Eigen vector
Definition parameters.h:113
bool _mt_feval
Definition parameters.h:581
int _algo
Definition parameters.h:572
double get_ftolerance() const
returns function tolerance
Definition parameters.h:305
double _ftarget
Definition parameters.h:567
std::unordered_map< int, double > _fixed_p
Definition parameters.h:577
bool _maximize
Definition parameters.h:584
int get_seed() const
returns random generator's seed.
Definition parameters.h:288
std::string get_fplot() const
returns the current output filename.
Definition parameters.h:422
int _tpa
Definition parameters.h:598
int dim() const
returns the problem's dimension
Definition parameters.h:341
void set_xtolerance(const double &v)
sets parameter tolerance as stopping criteria for TolX.
Definition parameters.h:314
dVec _x0min
Definition parameters.h:565
std::string _fplot
Definition parameters.h:563
void set_max_fevals(const int &fevals)
sets the maximum budget of objective function calls allowed for the optimization.
Definition parameters.h:233
double _xtol
Definition parameters.h:569
double get_xtolerance() const
returns parameter tolerance
Definition parameters.h:323
double _ftolerance
Definition parameters.h:568
double _epsuh
Definition parameters.h:592
TGenoPheno _gp
Definition parameters.h:579
void set_fplot(const std::string &fplot)
sets the output filename (activates the output to file).
Definition parameters.h:404
void set_tpa(const int &b)
activates / deactivates two-point adaptation step-size mechanism
Definition parameters.h:542
void set_seed(const int &seed)
sets random generator's seed, 0 is special value to generate random seed.
Definition parameters.h:277
void set_edm(const bool &edm)
activates computation of expected distance to minimum when optimization has completed
Definition parameters.h:452
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
uint64_t _seed
Definition parameters.h:571
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
double _csuh
Definition parameters.h:594
void set_max_iter(const int &maxiter)
sets the maximum number of iterations allowed for the optimization.
Definition parameters.h:215
void set_ftolerance(const double &v)
sets function tolerance as stopping criteria for TolHistFun: monitors the difference in function valu...
Definition parameters.h:299
int get_tpa() const
get two-point adapation step-size mechanism status.
Definition parameters.h:551
int _max_fevals
Definition parameters.h:560
bool _with_edm
Definition parameters.h:575
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 get_algo() const
returns which algorithm is set for the optimization at hand.
Definition parameters.h:377
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_algo(const int &algo)
sets the optimization algorithm.
Definition parameters.h:368
void reset_ftarget()
resets the objective function target value to its inactive state.
Definition parameters.h:259
dVec _x0max
Definition parameters.h:566
void set_gp(const TGenoPheno &gp)
sets the genotype/phenotype transform object.
Definition parameters.h:386
double _thetauh
Definition parameters.h:593
int _lambda
Definition parameters.h:558
void set_initial_fvalue(const bool &b)
whether to compute initial objective function value (i.e. at x0)
Definition parameters.h:515
double _alphathuh
Definition parameters.h:595
bool _with_gradient
Definition parameters.h:574
int _dim
Definition parameters.h:557
Simple surrogate strategy: trains every n steps, and exploits in between, mostly as an example and fo...
Definition surrogatestrategy.h:216
VD-CMA update that is a linear time/space variant of CMA-ES This is an implementation that closely fo...
Definition vdcmaupdate.h:39
Definition errstats.h:34
linear scaling of the parameter space to achieve similar sensitivity across all components.
Definition acovarianceupdate.h:30