23 #ifndef O2SCL_GSL_MMIN_BFGS2_H
24 #define O2SCL_GSL_MMIN_BFGS2_H
29 #include <gsl/gsl_blas.h>
30 #include <gsl/gsl_poly.h>
31 #include <gsl/gsl_multimin.h>
32 #include <o2scl/mmin.h>
33 #include <o2scl/cblas.h>
35 #ifndef DOXYGEN_NO_O2NS
50 virtual double wrap_f(
double alpha)=0;
52 virtual double wrap_df(
double alpha)=0;
54 virtual void wrap_fdf(
double alpha,
double *f,
double *df)=0;
69 #ifndef DOXYGEN_INTERNAL
112 if (alpha == x_cache_key) {
119 for(
size_t i=0;i<
dim;i++) {
134 if (alpha==f_cache_key) {
148 if (alpha==df_cache_key)
return df_alpha;
151 if (alpha!=g_cache_key) {
166 virtual void wrap_fdf(
double alpha,
double *f,
double *df) {
169 if (alpha == f_cache_key && alpha == df_cache_key) {
174 if (alpha == f_cache_key || alpha == df_cache_key) {
214 double f, vec_t &t_g, vec_t &t_p, auto_grad_t *ag) {
231 for(
size_t i=0;i<
dim;i++) {
247 double t_f_alpha, t_df_alpha;
248 wrap_fdf(alpha,&t_f_alpha,&t_df_alpha);
252 for(
size_t i=0;i<
dim;i++) {
269 for(
size_t i=0;i<
dim;i++) {
292 #ifndef DOXYGEN_INTERNAL
302 double interp_quad(
double f0,
double fp0,
double f1,
double zl,
316 double cubic(
double c0,
double c1,
double c2,
double c3,
double z);
319 void check_extremum(
double c0,
double c1,
double c2,
double c3,
double z,
320 double *zmin,
double *fmin);
324 double fp1,
double zl,
double zh);
327 double interpolate(
double a,
double fa,
double fpa,
double b,
328 double fb,
double fpb,
double xmin,
double xmax,
341 double tau1,
double tau2,
double tau3,
342 int order,
double alpha1,
double *alpha_new);
379 class def_auto_grad_t=
383 #ifndef DOXYGEN_INTERNAL
447 double alpha=0.0, alpha1;
454 if (pnorm == 0.0 || g0norm == 0.0 || fp0 == 0) {
455 for(
size_t i=0;i<this->
dim;i++) st_dx[i]=0.0;
456 O2SCL_CONV2(
"Either pnorm, g0norm, or fp0 vanished in ",
457 "mmin_bfgs2::iterate().",
465 double dbl_eps=std::numeric_limits<double>::epsilon();
469 if (-delta_f>10.0*dbl_eps*fabs(f0)) del=-delta_f;
470 else del=10.0*dbl_eps*fabs(f0);
471 if (2.0*del/(-fp0)<1.0) alpha1=2.0*del/(-fp0);
479 status=
lm.
minimize(wrap,rho,sigma,tau1,tau2,tau3,order,
484 "mmin_bfgs2::iterate().",
492 wrap.update_position(alpha,*st_x,&st_f,st_grad);
504 double dxg, dgg, dxdg, dgnorm, A, B;
507 for(
size_t i=0;i<
dim;i++) dx0[i]=(*st_x)[i];
511 for(
size_t i=0;i<
dim;i++) st_dx[i]=dx0[i];
514 for(
size_t i=0;i<
dim;i++) dg0[i]=st_grad[i];
525 A=-(1.0 + dgnorm * dgnorm / dxdg) * B + dgg / dxdg;
531 for(
size_t i=0;i<
dim;i++) p[i]=st_grad[i];
536 for(
size_t i=0;i<
dim;i++) {
547 dir=(pg >= 0.0) ? -1.0 : +1.0;
552 wrap.change_direction();
558 virtual const char *
type() {
return "mmin_bfgs2";}
569 for(
size_t i=0;i<n;i++) {
580 wrap.av_x_alpha.resize(n);
581 wrap.av_g_alpha.resize(n);
590 wrap.av_x_alpha.clear();
591 wrap.av_g_alpha.clear();
611 virtual int set(vec_t &x,
double u_step_size,
double tol_u,
621 agrad->set_function(ufunc);
622 (*agrad)(
dim,x,st_grad);
626 for(
size_t i=0;i<
dim;i++) {
631 for(
size_t i=0;i<
dim;i++) {
642 wrap.prepare_wrapper(ufunc,0,x0,st_f,g0,p,
agrad);
659 virtual int set_de(vec_t &x,
double u_step_size,
double tol_u,
660 func_t &ufunc, dfunc_t &udfunc) {
669 udfunc(
dim,x,st_grad);
673 for(
size_t i=0;i<
dim;i++) {
678 for(
size_t i=0;i<
dim;i++) {
689 wrap.prepare_wrapper(ufunc,&udfunc,x0,st_f,g0,p,
agrad);
717 virtual int mmin(
size_t nn, vec_t &xx,
double &fmin,
721 O2SCL_ERR2(
"Tried to min over zero variables ",
748 norm,this->
tol_rel,
"mmin_bfgs2");
759 for(
size_t i=0;i<nn;i++) xx[i]=(*st_x)[i];
775 virtual int mmin_de(
size_t nn, vec_t &xx,
double &fmin,
776 func_t &ufunc, dfunc_t &udfunc) {
779 O2SCL_ERR2(
"Tried to min over zero variables ",
805 norm,this->
tol_rel,
"mmin_bfgs2");
813 for(
size_t i=0;i<nn;i++) xx[i]=(*st_x)[i];
827 #ifndef DOXYGEN_INTERNAL
840 #ifndef DOXYGEN_NO_O2NS
Class for automatically computing gradients [abstract base].
void update_position(double alpha, vec_t &t_x, double *t_f, vec_t &t_g)
Update position.
double step_size
The size of the first trial step (default 0.01)
dfunc_t * dfunc
Derivative.
double lmin_tol
The tolerance for the 1-dimensional minimizer.
#define O2SCL_CONV_RET(d, n, b)
Set a "convergence" error and return the error value.
void moveto(double alpha)
Move to a new point, using the cached value if possible.
virtual int mmin_de(size_t nn, vec_t &xx, double &fmin, func_t &ufunc, dfunc_t &udfunc)
Calculate the minimum min of func w.r.t the array x of size nn.
The line minimizer for mmin_bfgs2.
double interp_quad(double f0, double fp0, double f1, double zl, double zh)
Minimize the interpolating quadratic.
vec_t av_x_alpha
Temporary storage.
double interp_cubic(double f0, double fp0, double f1, double fp1, double zl, double zh)
Interpolate using a cubic.
invalid argument supplied by user
virtual void wrap_fdf(double alpha, double *f, double *df)=0
Function and derivative.
virtual int iterate()
Perform an iteration.
bool grad_given
True if the gradient was given by the user.
virtual int set(vec_t &x, double u_step_size, double tol_u, func_t &ufunc)
Set the function and initial guess.
exceeded max number of iterations
int minimize(mmin_wrap_gsl &wrap, double rho, double sigma, double tau1, double tau2, double tau3, int order, double alpha1, double *alpha_new)
The line minimization.
iteration has not converged
int verbose
Output control.
void change_direction()
Convert cache values to the new minimizer direction.
virtual int mmin(size_t nn, vec_t &xx, double &fmin, func_t &ufunc)
Calculate the minimum min of func w.r.t the array x of size nn.
void check_extremum(double c0, double c1, double c2, double c3, double z, double *zmin, double *fmin)
Test to see curvature is positive.
virtual int allocate(size_t n)
Allocate the memory.
virtual int set_de(vec_t &x, double u_step_size, double tol_u, func_t &ufunc, dfunc_t &udfunc)
Set the function, the gradient, and the initial guess.
virtual int free()
Free the allocated memory.
double dnrm2(const size_t N, const vec_t &X)
Compute the norm of the vector X.
bool err_nonconv
If true, call the error handler if the routine does not "converge".
double cubic(double c0, double c1, double c2, double c3, double z)
Minimize the interpolating cubic.
iteration is not making progress toward solution
virtual double wrap_df(double alpha)
Evaluate the derivative.
Multidimensional minimization [abstract base].
int print_iter(size_t nv, vec2_t &x, double y, int iter, double value, double limit, std::string comment)
Print out iteration information.
vec_t av_g_alpha
Temporary storage.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
def_auto_grad_t def_grad
Default automatic gradient object.
double interpolate(double a, double fa, double fpa, double b, double fb, double fpb, double xmin, double xmax, int order)
Perform the interpolation.
double ddot(const size_t N, const vec_t &X, const vec2_t &Y)
Compute .
int restart()
Reset the minimizer to use the current point as a new starting point.
Simple automatic computation of gradient by finite differencing.
virtual double wrap_df(double alpha)=0
Derivative.
virtual double wrap_f(double alpha)
Evaluate the function.
#define O2SCL_CONV2(d, d2, n, b)
Set a "convergence" error, two-string version.
int last_ntrial
The number of iterations for in the most recent minimization.
virtual void wrap_fdf(double alpha, double *f, double *df)
Evaluate the function and the derivative.
void dscal(const double alpha, const size_t N, vec_t &X)
Compute .
auto_grad_t * agrad
Automatic gradient object.
std::function< int(size_t, boost::numeric::ublas::vector< double > &, boost::numeric::ublas::vector< double > &)> grad_funct11
Array of multi-dimensional functions typedef.
double slope()
Compute the slope.
void daxpy(const double alpha, const size_t N, const vec_t &X, vec2_t &Y)
Compute .
double tol_rel
Function value tolerance.
void prepare_wrapper(func_t &ufunc, dfunc_t *udfunc, vec_t &t_x, double f, vec_t &t_g, vec_t &t_p, auto_grad_t *ag)
Initialize wrapper.
Virtual base for the mmin_bfgs2 wrapper.
Wrapper class for the mmin_bfgs2 minimizer.
Multidimensional minimization by the BFGS algorithm (GSL)
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct11
Multi-dimensional function typedef.
virtual const char * type()
Return string denoting type("mmin_bfgs2")
mmin_linmin_gsl lm
The line minimizer.
auto_grad_t * agrad
The automatic gradient object.
virtual double wrap_f(double alpha)=0
Function.
size_t dim
Number of minimization dimensions.
int ntrial
Maximum number of iterations.