chiark / gitweb /
put source code into src subdirectory
[nlopt.git] / src / algs / stogo / tstc.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "stogo.h"
5
6 /* has two global minima at (0.09,-0.71) and (-0.09,0.71), plus
7    4 additional local minima */
8 static int cnt=0;
9 double tst_obj(int n, const double *xy, double *g, void *unused)
10 {
11   double x, y, f;
12   x = xy[0];
13   y = xy[1];
14   f = ((x*x)*(4-2.1*(x*x)+((x*x)*(x*x))/3) + x*y + (y*y)*(-4+4*(y*y)));
15   printf("feval:, %d, %g, %g, %g\n", ++cnt, x,y, f);
16   if (g) {
17        g[0] = /* df/dx */
18             ((2*x)*(4-2.1*(x*x)+((x*x)*(x*x))/3)
19              + (x*x)*(-4.2*x+4*(x*(x*x))/3)
20              + y);
21        g[1] = /* df/dy */
22             (x + (2*y)*(-4+4*(y*y)) + (y*y)*(8*(y)));
23   }
24   return f;
25 }
26
27 int main(int argc, char **argv)
28 {
29   int n = 2;
30   double x[2], l[2], u[2];
31   long int maxits = 0, maxtim = 0;
32   int info;
33   double minf;
34
35   maxits = argc < 2 ? 100 : atoi(argv[1]);
36
37   l[0] = -3; l[1] = -3;
38   u[0] = 3; u[1] = 3;
39
40   info = stogo_minimize(n, tst_obj, NULL, x, &minf, l, u, maxits, maxtim);
41
42   printf("min f = %g at (%g,%g) after %d evals, return value %d\n",
43          minf, x[0], x[1], cnt, info);
44
45   return EXIT_SUCCESS;
46 }