Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LBFGS Breaks on this trivial case #21

Closed
mrlooi opened this issue Sep 28, 2021 · 2 comments · Fixed by #25
Closed

LBFGS Breaks on this trivial case #21

mrlooi opened this issue Sep 28, 2021 · 2 comments · Fixed by #25

Comments

@mrlooi
Copy link

mrlooi commented Sep 28, 2021

Dear oh dear....

OUTPUT

terminate called after throwing an instance of 'std::runtime_error'
  what():  the line search routine reached the maximum number of iterations
Aborted (core dumped)

CODE

#include <Eigen/Core>
#include <iostream>
#include <LBFGS.h>

using Eigen::VectorXf;
using Eigen::MatrixXf;
using namespace LBFGSpp;

class Rosenbrock
{
private:
    int n;
public:
    Rosenbrock(int n_) : n(n_) {}
    float operator()(const VectorXf& x, VectorXf& grad)
    {
        float out = 0;
        for (int i = 0; i < n-1; ++i) {
            out += 100 * pow(x[i + 1] - pow(x[i], 2), 2) + pow(1 - x[i], 2);
        }

        grad[0] = -400 * x[0] * (x[1] - pow(x[0], 2)) - 2 * (1 - x[0]);
        grad[n - 1] = 200 * (x[n - 1] - pow(x[n - 2], 2));
        for (int i = 1; i < n - 1; ++i) {
            float xm = x[i];
            grad[i] = 200 * (xm - pow(x[i - 1], 2)) - 400 * (x[i + 1] - pow(xm, 2)) * xm - 2 * (1 - xm);
        }
        return out;
    }
};

int main()
{
    const int n = 5;
    LBFGSParam<float> param;
    LBFGSSolver<float> solver(param);
    Rosenbrock fun(n);

    VectorXf x = VectorXf::Zero(n);
    x << 1.3, 0.7, 0.8, 1.9, 1.2;//, 0.5, 1.3, 1.5, 1.2, 0.9;

    float fx;
    int niter = solver.minimize(fun, x, fx);

    std::cout << niter << " iterations" << std::endl;
    std::cout << "x = \n" << x.transpose() << std::endl;
    std::cout << "f(x) = " << fx << std::endl;

    return 0;
}
@mrlooi mrlooi closed this as completed Sep 28, 2021
@mrlooi mrlooi reopened this Sep 28, 2021
@mrlooi
Copy link
Author

mrlooi commented Sep 28, 2021

It seems to work fine with LBFGS-B with infinity/-infinity bounds
But fails with the standard LBFGS

@mrlooi
Copy link
Author

mrlooi commented Oct 7, 2021

any luck?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant