Example of using the BFGS optimization algorithm for finding a minimum in the Rosenbrock function.
#include <iostream>
 
static double rosenbrock(const Eigen::VectorXd * x, void * params)
{
    double temp = 1-(*x)(0);
    double temp2 = (*x)(1)-(*x)(0)*(*x)(0);
    return temp*temp+100*temp2*temp2;
}
 
static void drosenbrock(const Eigen::VectorXd * x, void * params, Eigen::VectorXd * g)
{
    (*g)(0) = -2*(1-(*x)(0))-400*(*x)(0)*(-(*x)(0)*(*x)(0)+(*x)(1));
    (*g)(1) = 200*(-(*x)(0)*(*x)(0)+(*x)(1));
}
 
int main(int argc, char** argv) {
    
    BFGS::BFGS_function_struct rosenbrockfunc;
    rosenbrockfunc.f = &rosenbrock;
    rosenbrockfunc.df = &drosenbrock;
    rosenbrockfunc.params = NULL; 
 
    
    (*startguess)(0)=5;
    (*startguess)(1)=5;
 
    
    double fx=rosenbrock(startguess,rosenbrockfunc.params);
    drosenbrock(startguess,rosenbrockfunc.params,gradient);
    std::cout<<"f(x): "<<fx<<" df(x): "<<(*gradient)(0)<<std::endl;
 
    
    int result = 
BFGS::optimizer(*startguess,rosenbrockfunc,1e-4,1000,0.01,1e-4,0.9,1);
 
         std::cout<<"Optimization return with succes" << std::endl;
    }
    else {
        std::cout<<"Optimization return with warnings" << std::endl;
    }
    
    
    fx=rosenbrock(startguess,rosenbrockfunc.params);
    drosenbrock(startguess,rosenbrockfunc.params,gradient);
    std::cout<<"Optimization result i (" << (*startguess)(0) <<", " << (*startguess)(0) << ")" << std::endl;
    std::cout <<"f(x): "<<fx<<" df(x): "<<(*gradient)(0)<<std::endl;
 
}
static int optimizer(vector &startguess, BFGS_function_struct function, double tolerance, unsigned int iterationLimit, double initialStepsize=1.0, double c1=1e-4, double c2=0.9, double alphamax=1.0)
Minimize a function using the BFGS algorithm.
@ SUCCESS
Definition: BFGS.hpp:50
Eigen::VectorXd vector
Vector type used in the minimazation algorithm.
Definition: BFGS.hpp:26
Various algorithms.
Definition: BFGS.hpp:10