15 #include "nlopt-util.h"
16 #include "testfuncs.h"
18 static nlopt_algorithm algorithm = NLOPT_GN_DIRECT_L;
19 static double ftol_rel = 0, ftol_abs = 0, xtol_rel = 0, xtol_abs = 0, fmin_max_delta = -HUGE_VAL;
20 static int maxeval = 1000, iterations = 1, center_start = 0;
21 static double maxtime = 0.0;
22 static double xinit_tol = -1;
24 static void listalgs(FILE *f)
27 fprintf(f, "Available algorithms:\n");
28 for (i = 0; i < NLOPT_NUM_ALGORITHMS; ++i)
29 fprintf(f, " %2d: %s\n", i, nlopt_algorithm_name((nlopt_algorithm) i));
32 static void listfuncs(FILE *f)
35 fprintf(f, "Available objective functions:\n");
36 for (i = 0; i < NTESTFUNCS; ++i)
37 fprintf(f, " %2d: %s (%d dims)\n", i, testfuncs[i].name, testfuncs[i].n);
40 static int test_function(int ifunc)
44 double *x, fmin, fmin_max, f0, *xtabs;
46 double start = nlopt_seconds();
48 if (ifunc < 0 || ifunc >= NTESTFUNCS) {
49 fprintf(stderr, "testopt: invalid function %d\n", ifunc);
53 func = testfuncs[ifunc];
54 x = (double *) malloc(sizeof(double) * func.n * 3);
55 if (!x) { fprintf(stderr, "testopt: Out of memory!\n"); return 0; }
57 xtabs = x + func.n * 2;
58 for (i = 0; i < func.n; ++i) xtabs[i] = xtol_abs;
59 fmin_max = fmin_max_delta > (-HUGE_VAL) ? fmin_max_delta + func.fmin : (-HUGE_VAL);
61 printf("-----------------------------------------------------------\n");
62 printf("Optimizing %s (%d dims) using %s algorithm\n",
63 func.name, func.n, nlopt_algorithm_name(algorithm));
64 printf("lower bounds at lb = [");
65 for (i = 0; i < func.n; ++i) printf(" %g", func.lb[i]);
67 printf("upper bounds at ub = [");
68 for (i = 0; i < func.n; ++i) printf(" %g", func.ub[i]);
72 printf("Starting guess x = [");
73 for (i = 0; i < func.n; ++i) {
75 x[i] = (func.ub[i] + func.lb[i]) * 0.5;
76 else if (xinit_tol < 0) { /* random starting point near center of box */
77 double dx = (func.ub[i] - func.lb[i]) * 0.25;
78 double xm = 0.5 * (func.ub[i] + func.lb[i]);
79 x[i] = nlopt_urand(xm - dx, xm + dx);
82 x[i] = nlopt_urand(-xinit_tol, xinit_tol)
83 + (1 + nlopt_urand(-xinit_tol, xinit_tol)) * func.xmin[i];
84 if (x[i] > func.ub[i]) x[i] = func.ub[i];
85 else if (x[i] < func.lb[i]) x[i] = func.lb[i];
90 f0 = func.f(func.n, x, x + func.n, func.f_data);
91 printf("Starting function value = %g\n", f0);
93 if (testfuncs_verbose && func.has_gradient) {
94 printf("checking gradient:\n");
95 for (i = 0; i < func.n; ++i) {
98 f = func.f(func.n, x, NULL, func.f_data);
100 printf(" grad[%d] = %g vs. numerical derivative %g\n",
101 i, x[i + func.n], (f - f0) / (x[i] * 1e-6));
105 for (iter = 0; iter < iterations; ++iter) {
106 testfuncs_counter = 0;
107 ret = nlopt_minimize(algorithm,
108 func.n, func.f, func.f_data,
111 fmin_max, ftol_rel, ftol_abs, xtol_rel, xtabs,
113 printf("finished after %g seconds.\n", nlopt_seconds() - start);
114 printf("return code %d from nlopt_minimize\n", ret);
116 fprintf(stderr, "testopt: error in nlopt_minimize\n");
119 printf("Found minimum f = %g after %d evaluations.\n",
120 fmin, testfuncs_counter);
121 printf("Minimum at x = [");
122 for (i = 0; i < func.n; ++i) printf(" %g", x[i]);
124 printf("|f - fmin| = %g, |f - fmin| / |fmin| = %e\n",
125 fabs(fmin - func.fmin), fabs(fmin - func.fmin) / fabs(func.fmin));
127 printf("vs. global minimum f = %g at x = [", func.fmin);
128 for (i = 0; i < func.n; ++i) printf(" %g", func.xmin[i]);
135 static void usage(FILE *f)
137 fprintf(f, "Usage: testopt [OPTIONS]\n"
139 " -h : print this help\n"
140 " -L : list available algorithms and objective functions\n"
141 " -v : verbose mode\n"
142 " -a <n> : use optimization algorithm <n>\n"
143 " -o <n> : use objective function <n>\n"
144 " -0 <x> : starting guess within <x> + (1+<x>) * optimum\n"
145 " -c : starting guess at center of cell\n"
146 " -e <n> : use at most <n> evals (default: %d, 0 to disable)\n"
147 " -t <t> : use at most <t> seconds (default: disabled)\n"
148 " -x <t> : relative tolerance <t> on x (default: disabled)\n"
149 " -X <t> : absolute tolerance <t> on x (default: disabled)\n"
150 " -f <t> : relative tolerance <t> on f (default: disabled)\n"
151 " -F <t> : absolute tolerance <t> on f (default: disabled)\n"
152 " -m <m> : stop when fmin+<m> is reached (default: disabled)\n"
153 " -i <n> : iterate optimization <n> times (default: 1)\n"
154 " -r <s> : use random seed <s> for starting guesses\n"
158 int main(int argc, char **argv)
163 testfuncs_verbose = 0;
168 while ((c = getopt(argc, argv, "hLvc0:r:a:o:i:e:t:x:X:f:F:m:")) != -1)
178 testfuncs_verbose = 1;
181 nlopt_srand((unsigned long) atoi(optarg));
185 if (c < 0 || c >= NLOPT_NUM_ALGORITHMS) {
186 fprintf(stderr, "testopt: invalid algorithm %d\n", c);
190 algorithm = (nlopt_algorithm) c;
193 if (!test_function(atoi(optarg)))
197 maxeval = atoi(optarg);
200 iterations = atoi(optarg);
203 maxtime = atof(optarg);
206 xtol_rel = atof(optarg);
209 xtol_abs = atof(optarg);
212 ftol_rel = atof(optarg);
215 ftol_abs = atof(optarg);
218 fmin_max_delta = atof(optarg);
225 xinit_tol = atof(optarg);
228 fprintf(stderr, "harminv: invalid argument -%c\n", c);