/* LA: linear algebra C++ interface library Copyright (C) 2021 Jiri Pittner or This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ //various optimization methods #ifndef _OPTIMIZE_H_ #define _OPTIMIZE_H_ #include #include #include "vec.h" #include "mat.h" #include "smat.h" namespace LA { //base classes with purely virtual functions for multiple inheritance use //optimization problem - provides function and optional gradient, in multiply inherited classess will store auxiliary data for the function template class OptProblem { public: virtual T funcgrad(const NRVec &x, NRVec *grad) const =0; //evaluate function and optionally also gradient at point x (if grad!=NULL) virtual ~OptProblem() {}; }; //test case template class testproblem : public OptProblem { public: virtual T funcgrad(const NRVec &x, NRVec *grad) const; virtual ~testproblem() {}; }; //base classes for optimization methods template class optstate; //this is a collection of parameters, which can be augmented by method-specific additions, to be used as a base class //methods take their specific parameters via setfrom(), those not provided by the caller keep the defaults from the constructor //each derived class has to override setfrom(), converged() and setreached() if it adds parameters relevant for them template class hyperparameters { public: int my_maxstep; //maximum number of iterations int my_maxfunc; //maximum number of function evaluations, <=0 means unlimited T my_f_thr; //threshhold on function change in a single step T my_x_thr; //threshold on x norm change in a single step int my_saddle_order; //0=minimum, k=saddle point with k negative Hessian eigenvalues (only for methods supporting it) int my_verbose; //0=silent, 1=per-iteration info, 2=more detailed info hyperparameters() : my_maxstep(200), my_maxfunc(0), my_f_thr(0), my_x_thr(0), my_saddle_order(0), my_verbose(1) {}; virtual void setfrom(const hyperparameters &p) {*this=p;} //copy those parameters from p which exist in both classes virtual bool converged(const optstate &s) const {return s.stepdone && s.df &s) {my_maxstep=s.iter; my_maxfunc=s.e.nfunc; my_f_thr=s.df; my_x_thr=s.dx;} //store the values reached in the minimization virtual ~hyperparameters(){}; }; //parameters common to methods using the gradient template class gradient_hyperparameters : public hyperparameters { public: T my_g_thr; //gradient norm convergence threshold gradient_hyperparameters() : my_g_thr(1e-6) {}; virtual void setfrom(const hyperparameters &p) {const gradient_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else hyperparameters::setfrom(p);} virtual bool converged(const optstate &s) const {return (s.gnorm>=0 && s.gnorm::converged(s);} virtual void setreached(const optstate &s) {hyperparameters::setreached(s); my_g_thr=s.gnorm;} }; //parameters of gradient methods using line search template class linesearch_hyperparameters : public gradient_hyperparameters { public: T my_c1; //sufficient decrease (Armijo) parameter of Wolfe conditions, 0 &p) {const linesearch_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else gradient_hyperparameters::setfrom(p);} }; //result of the minimization, converts implicitly to the function value template class optresult { public: T f; //final function value bool converged; optresult(const T ff, const bool c) : f(ff), converged(c) {}; operator T() const {return f;} }; //evaluation of the optimized function, counts the evaluations and keeps the best point found template class optevaluator { public: const OptProblem *o; int maxfunc; //<=0 means unlimited int nfunc; //no. of evaluations done T fbest; //lowest function value found NRVec xbest; //and its point optevaluator(const OptProblem *oo, const int maxf=0) : o(oo), maxfunc(maxf), nfunc(0), fbest(0) {}; T operator()(const NRVec &x, NRVec *grad=NULL) { T f=o->funcgrad(x,grad); if(nfunc++ ==0 || f0 && nfunc>=maxfunc;} int remaining() const {return maxfunc>0 ? maxfunc-nfunc : std::numeric_limits::max();} }; //bookkeeping of the iterations common to all methods: convergence and limit tests, verbose output, results template class optstate { public: const char *name; //of the method for verbose output const hyperparameters &hp; //parameters used const optevaluator &e; int iter; //no. of iterations done T f; //current function value T gnorm; //current gradient norm, negative if not available T df; //|f change| in the last step T dx; //|x change| in the last step bool stepdone; //df and dx are valid bool converged; optstate(const char *nm, const hyperparameters &h, const optevaluator &ee) : name(nm), hp(h), e(ee), iter(0), f(0), gnorm(-1), df(0), dx(0), stepdone(false), converged(false) {}; bool stop(const T ff, const T gn= -1, const int needed=1); //to be called at the beginning of each iteration, returns true if the iterations should finish; needed = no. of function evaluations of the next iteration void step(const T dff, const T dxx) {df=std::abs(dff); dx=dxx; stepdone=true;} //record a step done optresult finish(hyperparameters *achieved) const; //final report, fills achieved if not NULL }; //base class for optimization methods, virtual functions to allow a pointer to derived class template class Optimizer { public: //par==NULL means default parameters //if achieved!=NULL, the values actually reached are returned there: my_maxstep = no. of iterations done, my_maxfunc = no. of function evaluations done, thresholds = last values of the convergence criteria //(|f change| and |x change| in the last step, 0 if no step was done; |gradient| for gradient methods), the other parameters as they were used virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL) = 0; virtual ~Optimizer() {}; }; //auxiliary routines //line search along direction p satisfying strong Wolfe conditions (cf. Nocedal & Wright, Numerical Optimization, 2nd ed., alg. 3.5 and 3.6) //on input x,f,g at the starting point, on successful output x,f,g at the new point; returns step length or 0 on failure (x,f,g then unchanged) //alpha1 = initial trial step length template T linesearch_wolfe(optevaluator &e, NRVec &x, T &f, NRVec &g, const NRVec &p, const T alpha1, const linesearch_hyperparameters &lp); //derivative-free line minimization of f(x + t*u): bracketing and Brent's method (Brent, Algorithms for Minimization without Derivatives, 1973, chap. 5) //on input x,f at the starting point, on output x,f at the minimum found; returns t, 0 if no decrease was found (x,f then unchanged) //t1 = initial trial step, tol and abstol = relative and absolute tolerance on t, maxiter = max. no. of function evaluations template T linemin_brent(optevaluator &e, NRVec &x, T &f, const NRVec &u, const T t1, const T tol, const T abstol, const int maxiter, const int verbose=0); //search direction of a line-search method, used by the common driver linesearch_minimize() template class linesearch_direction { public: virtual NRVec direction(const NRVec &g) =0; //search direction at the point with gradient g virtual bool update(const NRVec &s, const NRVec &y, const NRVec &p, const NRVec &g) =0; //after the step s=alpha*p with gradient change y and new gradient g; returns false if the update was skipped virtual void reset() =0; //forget the accumulated information virtual bool steepest() const =0; //no information accumulated, direction() gives steepest descent virtual bool indefinite() const {return false;} //a non-descent direction is legitimate (SR1), take steepest descent step without reset virtual bool alpha_from_previous() const {return false;} //initial step length from the previous one (Nocedal & Wright eq. 3.60), otherwise 1 (unit step in x for steepest descent) virtual ~linesearch_direction() {}; }; //common driver of line-search methods template optresult linesearch_minimize(const char *name, linesearch_direction &dir, const OptProblem *o, NRVec &x, const linesearch_hyperparameters &hp, hyperparameters *achieved); //parameters of first-order methods with a learning rate instead of line search, suitable also for noisy (stochastic) functions and gradients template class firstorder_hyperparameters : public gradient_hyperparameters { public: T my_lr; //learning rate T my_lr_decay; //learning rate in iteration k is lr/(1+lr_decay*k)^lr_power T my_lr_power; T my_weight_decay; //decoupled weight decay x -= lr*weight_decay*x (Loshchilov & Hutter, ICLR 2019) bool my_return_best; //return the point with the lowest function value found instead of the last one; only for (nearly) deterministic f, the lowest of noisy values is biased by the noise firstorder_hyperparameters() : my_lr(1e-3), my_lr_decay(0), my_lr_power(1), my_weight_decay(0), my_return_best(false) {this->my_maxstep=1000;}; virtual void setfrom(const hyperparameters &p) {const firstorder_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else gradient_hyperparameters::setfrom(p);} }; //update rule of a first-order method, used by the common driver firstorder_minimize() template class firstorder_step { public: virtual NRVec step(const NRVec &g, const int k) =0; //direction d of the step x -= lr*d in iteration k=1,2,... with gradient g virtual ~firstorder_step() {}; }; //function value and gradient for firstorder_minimize(), the default is the exact evaluation, derived classes estimate them (SPSA) template class gradient_estimator { public: virtual T operator()(optevaluator &e, const NRVec &x, NRVec &g, const int k) {return e(x,&g);} //returns f at x and the gradient g, k=0,1,... is the iteration virtual int cost() const {return 1;} //no. of function evaluations of one call virtual bool exact() const {return true;} //with estimates no convergence test is done, small gradients or changes can occur by chance virtual T calibrate(optevaluator &e, const NRVec &x, const firstorder_hyperparameters &hp) {return hp.my_lr;} //learning rate to be used virtual ~gradient_estimator() {}; }; //common driver of first-order methods; est==NULL means exact gradients //the learning rate actually used is returned in achieved, if it is of firstorder_hyperparameters type template optresult firstorder_minimize(const char *name, firstorder_step &stepper, const OptProblem *o, NRVec &x, const firstorder_hyperparameters &hp, hyperparameters *achieved, gradient_estimator *est=NULL); //specific methods //parameters common to quasi-Newton methods template class quasinewton_hyperparameters : public linesearch_hyperparameters { public: T my_sy_thr; //skip the BFGS-type update if s.y <= sy_thr*|s|*|y| (curvature condition violated numerically) quasinewton_hyperparameters() : my_sy_thr(1e-12) {}; virtual void setfrom(const hyperparameters &p) {const quasinewton_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else linesearch_hyperparameters::setfrom(p);} }; //quasi-Newton methods with line search, differing by the update of the inverse Hessian approximation enum QN_update {QN_BFGS, QN_DFP, QN_SR1}; template class BFGS_hyperparameters : public quasinewton_hyperparameters { public: QN_update my_update; //BFGS, DFP or SR1 (Nocedal & Wright eqs. 6.17, 6.15, 6.25); DFP needs an accurate line search (c2~0.1), otherwise it can stall T my_sr1_thr; //SR1: skip the update if |(s-Hy).y| <= sr1_thr*|s-Hy|*|y| (cf. Nocedal & Wright eq. 6.26) BFGS_hyperparameters() : my_update(QN_BFGS), my_sr1_thr(1e-8) {}; virtual void setfrom(const hyperparameters &p) {const BFGS_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else quasinewton_hyperparameters::setfrom(p);} }; template class BFGS_Optimizer : public Optimizer { public: virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL); virtual ~BFGS_Optimizer() {}; }; //limited-memory BFGS (Nocedal & Wright chap. 7.2), for large problems template class LBFGS_hyperparameters : public quasinewton_hyperparameters { public: int my_memory; //no. of stored pairs of steps and gradient changes LBFGS_hyperparameters() : my_memory(10) {}; virtual void setfrom(const hyperparameters &p) {const LBFGS_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else quasinewton_hyperparameters::setfrom(p);} }; template class LBFGS_Optimizer : public Optimizer { public: virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL); virtual ~LBFGS_Optimizer() {}; }; //nonlinear conjugate gradient methods (Nocedal & Wright chap. 5.2; Hager & Zhang, SIAM J. Optim. 16, 170 (2005)) //differing by the formula for beta in p_{k+1} = -g_{k+1} + beta p_k; PR and HS are used in the nonnegative variants PR+ and HS+ enum CG_beta {CG_FR, CG_PR, CG_HS, CG_DY, CG_HZ}; template class CG_hyperparameters : public linesearch_hyperparameters { public: CG_beta my_beta; //Fletcher-Reeves, Polak-Ribiere+, Hestenes-Stiefel+, Dai-Yuan, Hager-Zhang int my_restart; //restart with steepest descent every my_restart iterations, <=0 means the problem dimension T my_powell_thr; //restart if |g_{k+1}.g_k| >= powell_thr*|g_{k+1}|^2 (Nocedal & Wright eq. 5.52), <=0 disables CG_hyperparameters() : my_beta(CG_PR), my_restart(0), my_powell_thr(0.2) {this->my_c2=0.1;}; //CG needs a more accurate line search virtual void setfrom(const hyperparameters &p) {const CG_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else linesearch_hyperparameters::setfrom(p);} }; template class CG_Optimizer : public Optimizer { public: virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL); virtual ~CG_Optimizer() {}; }; //Nelder-Mead downhill simplex (Lagarias et al., SIAM J. Optim. 9, 112 (1998)), derivative-free //convergence: f_thr is used for the spread of function values in the simplex, x_thr for its diameter, both have to be satisfied template class NelderMead_hyperparameters : public hyperparameters { public: T my_initstep; //edge length of the initial simplex x0 + initstep*e_i bool my_adaptive; //dimension-dependent coefficients (Gao & Han, Comput. Optim. Appl. 51, 259 (2012)), otherwise the following ones T my_alpha; //reflection T my_gamma; //expansion T my_rho; //contraction T my_sigma; //shrink NelderMead_hyperparameters() : my_initstep(0.1), my_adaptive(true), my_alpha(1), my_gamma(2), my_rho(0.5), my_sigma(0.5) {this->my_maxstep=5000; this->my_f_thr=1e-8; this->my_x_thr=1e-6;}; virtual void setfrom(const hyperparameters &p) {const NelderMead_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else hyperparameters::setfrom(p);} }; template class NelderMead_Optimizer : public Optimizer { public: virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL); virtual ~NelderMead_Optimizer() {}; }; //Powell's conjugate direction method (Powell, Comput. J. 7, 155 (1964)), derivative-free //convergence: f_thr and x_thr are used for the change of f and x in one sweep over all directions, both have to be satisfied template class Powell_hyperparameters : public hyperparameters { public: T my_initstep; //initial trial step of the line minimizations T my_linmin_tol; //relative tolerance of the line minimizations, the absolute one is 0.01*x_thr int my_maxlinemin; //max. no. of function evaluations in one line minimization Powell_hyperparameters() : my_initstep(0.1), my_linmin_tol(1e-4), my_maxlinemin(50) {this->my_maxstep=1000; this->my_f_thr=1e-10; this->my_x_thr=1e-6;}; virtual void setfrom(const hyperparameters &p) {const Powell_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else hyperparameters::setfrom(p);} }; template class Powell_Optimizer : public Optimizer { public: virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL); virtual ~Powell_Optimizer() {}; }; //stochastic gradient descent with momentum v = momentum*v + g, step d = v or d = g + momentum*v (Nesterov) (Sutskever et al., ICML 2013) template class SGD_hyperparameters : public firstorder_hyperparameters { public: T my_momentum; bool my_nesterov; SGD_hyperparameters() : my_momentum(0.9), my_nesterov(false) {this->my_lr=1e-2;}; virtual void setfrom(const hyperparameters &p) {const SGD_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else firstorder_hyperparameters::setfrom(p);} }; template class SGD_Optimizer : public Optimizer { public: virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL); virtual ~SGD_Optimizer() {}; }; //Adam (Kingma & Ba, ICLR 2015), with weight_decay>0 AdamW, optionally AMSGrad (Reddi et al., ICLR 2018); beta1=0 gives RMSprop template class Adam_hyperparameters : public firstorder_hyperparameters { public: T my_beta1; //decay rate of the first moment estimate T my_beta2; //decay rate of the second moment estimate T my_eps; //regularization of the division by the second moment bool my_amsgrad; //use the maximum of the past second moments Adam_hyperparameters() : my_beta1(0.9), my_beta2(0.999), my_eps(1e-8), my_amsgrad(false) {}; virtual void setfrom(const hyperparameters &p) {const Adam_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else firstorder_hyperparameters::setfrom(p);} }; template class Adam_Optimizer : public Optimizer { public: virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL); virtual ~Adam_Optimizer() {}; }; //simultaneous perturbation stochastic approximation (Spall, IEEE Trans. Autom. Control 37, 332 (1992); IEEE Trans. Aerosp. Electron. Syst. 34, 817 (1998)) //derivative-free, 2 function evaluations per gradient estimate independently of the dimension, robust to noise (VQE) //gain sequences: a_k = lr/(1+lr_decay*k)^lr_power, i.e. Spall's a/(k+1+A)^alpha with lr=a/(1+A)^alpha, lr_decay=1/(1+A), lr_power=alpha; c_k = c/(k+1)^gamma //the gradient and f are estimates, therefore no convergence test is done and the iterations run until maxstep or maxfunc //the first estimate at the starting point is always done, even if maxfunc is smaller than its 2*resamplings evaluations template class SPSA_hyperparameters : public firstorder_hyperparameters { public: T my_c; //perturbation size, of the order of the noise of f T my_gamma; //decay exponent of the perturbation size int my_resamplings; //no. of gradient estimates averaged in one iteration int my_calibrate; //no. of gradient estimates at the starting point for the calibration of lr, 0 = no calibration T my_target_step; //calibration: lr is set so that the first step has this length on average SPSA_hyperparameters() : my_c(0.1), my_gamma(0.101), my_resamplings(1), my_calibrate(20), my_target_step(0.1) {this->my_lr_decay=0.01; this->my_lr_power=0.602; this->my_g_thr=0;}; virtual void setfrom(const hyperparameters &p) {const SPSA_hyperparameters *q=dynamic_cast(&p); if(q) *this= *q; else firstorder_hyperparameters::setfrom(p);} }; template class SPSA_Optimizer : public Optimizer { public: virtual optresult minimize(const OptProblem *o, NRVec &x, const hyperparameters *par=NULL, hyperparameters *achieved=NULL); virtual ~SPSA_Optimizer() {}; }; }//namespace #endif /* _OPTIMIZE_H_ */