-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlmbm_example.cpp
54 lines (50 loc) · 1.2 KB
/
lmbm_example.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
#include "lmbm.h"
#include <cstdio>
static int fcalls = -1;
double costFunc(void *instance,
const double *x,
double *g,
const int n)
{
double fx1 = -x[0] - x[1];
double fx2 = x[0] * x[0] + x[1] * x[1] -
x[0] - x[1] - 1.0;
double fx = fx1 > fx2 ? fx1 : fx2;
if (g != nullptr)
{
if (fx1 > fx2)
{
g[0] = -1.0;
g[1] = -1.0;
}
else
{
g[0] = 2.0 * x[0] - 1.0;
g[1] = 2.0 * x[1] - 1.0;
}
}
fcalls++;
return fx;
}
int main(int argc, char **argv)
{
int n = 2;
double x[2] = {1.0, 1.0};
double f;
costFunc(nullptr, x, nullptr, 2);
lmbm::lmbm_parameter_t param;
lmbm::lmbm_optimize(n,
x,
&f,
costFunc,
nullptr,
¶m);
{
printf("-----------\n");
printf("| Output: |\n");
printf("-----------\n");
printf("%-16s %f\n", "Final value:", f);
printf("%-16s %f, %f\n", "Solution:", x[0], x[1]);
printf("%-16s %d\n", "Func calls:", fcalls);
}
}