RobWorkProject  23.9.11-
bfgsExample.cpp

Example of using the BFGS optimization algorithm for finding a minimum in the Rosenbrock function.

The Rosenbrock function are defined by: \( f(x, y) = (1-x)^2 + 100(y-x^2)^2 \)

#include <iostream>
using namespace rwlibs::algorithms;
//The rosenbrock function
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;
}
//Differential equation of the rosenbrock function
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) {
// Initialise the minimization functions
BFGS::BFGS_function_struct rosenbrockfunc;
rosenbrockfunc.f = &rosenbrock;
rosenbrockfunc.df = &drosenbrock;
rosenbrockfunc.params = NULL; //Pointer to pass external variables into the minimization problem.
//Store the start guess
BFGS::vector* startguess = new BFGS::vector(2);
(*startguess)(0)=5;
(*startguess)(1)=5;
//Calculate the start error for the minimization problem
BFGS::vector* gradient = new BFGS::vector(2);
double fx=rosenbrock(startguess,rosenbrockfunc.params);
drosenbrock(startguess,rosenbrockfunc.params,gradient);
std::cout<<"f(x): "<<fx<<" df(x): "<<(*gradient)(0)<<std::endl;
//Do optimization
int result = BFGS::optimizer(*startguess,rosenbrockfunc,1e-4,1000,0.01,1e-4,0.9,1);
if(result==BFGS::SUCCESS) {
std::cout<<"Optimization return with succes" << std::endl;
}
else {
std::cout<<"Optimization return with warnings" << std::endl;
}
//Print result
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