chiark / gitweb /
recommend building in a subdir
[nlopt.git] / src / algs / direct / tstc.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "direct.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, int *undefined_flag, 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   return f;
17 }
18
19 int main(int argc, char **argv)
20 {
21   int n = 2;
22   double x[2], l[2], u[2];
23   long int maxits = 0;
24   int info;
25   double minf;
26   int force_stop = 0;
27
28   maxits = argc < 2 ? 100 : atoi(argv[1]);
29
30   l[0] = -3; l[1] = -3;
31   u[0] = 3; u[1] = 3;
32
33   info = direct_optimize(tst_obj, NULL, n, l, u, x, &minf,
34                          maxits, 500,
35                          0, 0, 0, 0, 
36                          0.0, -1.0,
37                          &force_stop, 
38                          DIRECT_UNKNOWN_FGLOBAL, 0,
39                          stdout, DIRECT_GABLONSKY);
40
41   printf("min f = %g at (%g,%g) after %d evals, return value %d\n",
42          minf, x[0], x[1], cnt, info);
43
44   return EXIT_SUCCESS;
45 }