-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
73 lines (66 loc) · 1.72 KB
/
main.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include "lbfgs.h"
using namespace std;
float evaluate(
void *instance,
const float *x,
float *g,
const int n,
const float step
) {
int i;
float fx = 0.0;
for (i = 0; i < n; i += 2) {
float t1 = 1.0 - x[i];
float t2 = 10.0 * (x[i + 1] - x[i] * x[i]);
g[i + 1] = 20.0 * t2;
g[i] = -2.0 * (x[i] * g[i + 1] + t1);
fx += t1 * t1 + t2 * t2;
}
return fx;
}
int progress(
void *instance,
const float *x,
const float *g,
const float fx,
const float xnorm,
const float gnorm,
const float step,
int n,
int k,
int ls
)
{
printf("Iteration %d:\n", k);
printf(" fx = %f, x[0] = %f, x[1] = %f\n", fx, x[0], x[1]);
printf(" xnorm = %f, gnorm = %f, step = %f\n", xnorm, gnorm, step);
printf("\n");
return 0;
}
int main() {
cout << "Hello, World!" << endl;
int i, ret = 0;
float fx;
int N=100;
float *x = new float[N];
lbfgs_parameter_t param;
/* Initialize the variables. */
for (i = 0;i < N;i += 2) {
x[i] = -1.2;
x[i+1] = 1.0;
}
/* Initialize the parameters for the L-BFGS optimization. */
lbfgs_parameter_init(¶m);
/*param.linesearch = LBFGS_LINESEARCH_BACKTRACKING;*/
/*
Start the L-BFGS optimization; this will invoke the callback functions
evaluate() and progress() when necessary.
*/
ret = lbfgs(N, x, &fx, evaluate, progress, NULL, ¶m);
/* Report the result. */
printf("L-BFGS optimization terminated with status code = %d\n", ret);
printf(" fx = %f, x[0] = %f, x[1] = %f\n", fx, x[0], x[1]);
delete [] x;
return 0;
}