Skip to content

Instantly share code, notes, and snippets.

@mad-s
Created July 9, 2020 14:23
Show Gist options
  • Save mad-s/c097680d0c083c1df3a5c5d6e71c0856 to your computer and use it in GitHub Desktop.
Save mad-s/c097680d0c083c1df3a5c5d6e71c0856 to your computer and use it in GitHub Desktop.
#include <Eigen/Core>
#include <iostream>
#include <LBFGS.h>
using namespace std;
using namespace LBFGSpp;
using Eigen::Vector2d;
using Eigen::VectorXd;
int main() {
LBFGSParam<double> param;
param.max_iterations = 100;
LBFGSSolver<double> solver(param);
auto fun = [] (const VectorXd &x, VectorXd &grad) -> double {
double mu = 0.0001;
double fx = x(1);//sqrt(x(1));
double a1 = 2;
double b1 = 0;
double a2 = -1;
double b2 = 1;
// c_i >= 0
double c1 = x(1) - pow(a1*x(0)+b1, 3);
double c2 = x(1) - pow(a2*x(0)+b2, 3);
double c3 = x(1);
fx += c1 > 0 ? -mu*log(c1) : std::numeric_limits<double>::infinity();
fx += c2 > 0 ? -mu*log(c2) : std::numeric_limits<double>::infinity();
fx += c3 > 0 ? -mu*log(c3) : std::numeric_limits<double>::infinity();
grad = Vector2d(0., 1/* / sqrt(x(1))*/);
grad += -mu/c1 * Vector2d(-3*pow(a1*x(0)+b1, 2), 1);
grad += -mu/c2 * Vector2d(-3*pow(a2*x(0)+b2, 2), 1);
grad += -mu/c3 * Vector2d(0.,1.);
return fx;
};
VectorXd x = Vector2d(0., 100.);
double fx;
solver.minimize(fun, x, fx);
cout << "fx = " << fx << endl;
cout << "x = " << x << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment