-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlbfgs_simple.cpp
More file actions
40 lines (32 loc) · 774 Bytes
/
lbfgs_simple.cpp
File metadata and controls
40 lines (32 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <Eigen/Core>
#include "LBFGS.h"
#include <iostream>
using Eigen::VectorXd;
using namespace LBFGSpp;
using namespace std;
double quadratic(const VectorXd& x, VectorXd& grad)
{
//compute f(x) and grad(x)
const int n = x.size();
VectorXd d(n);
for (int i=0; i<n; ++i)
{
d[i] = i;
}
double f = (x-d).squaredNorm();
grad = 2.0 * (x-d);
return f;
}
int main()
{
const int n = 10; //dimension
LBFGSParam<double> param;
LBFGSSolver<double> solver(param);
VectorXd x = VectorXd::Zero(n);
double fx; // f(x)
int niter = solver.minimize(quadratic, x, fx);
cout << niter << " iterations" << endl;
cout << "x = \n" << x.transpose() << endl;
cout << "f(x) = " << fx << endl;
return 0;
}