#include #include "lbfgsb.h" // RosenBrock Function // http://mathworld.wolfram.com/RosenbrockFunction.html double f (double *x) { return 100 * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]) + (1 - x[0]) * (1 - x[0]); } void grad_f (double *x, double *g) { g[0] = -400 * x[0] * (x[1] - x[0] * x[0]) - 2 * (1 - x[0]); g[1] = 200 * (x[1] - x[0] * x[0]); } int main () { double g[2]; double x[2]; double obj; LBFGS lbfgs; lbfgs.init (2, 5); // # parameters, # LBFGS cache (useally 5 ~ 7) // init x[0] = 5; x[1] = 5; double old_obj = 1000; while (true) { obj = f (x); // obj eval. grad_f (x, g); // calc. gradient lbfgs.optimize (x, &obj, g); // call LBFGS std::cout << "obj=" << obj << " x[0]=" << x[0] << " x[1]=" << x[1] << std::endl; if (std::abs (old_obj - obj) < 0.0000001) break; old_obj = obj; } return 0; }