16 #include "nlopt-util.h"
17 #include "testfuncs.h"
19 static nlopt_algorithm algorithm = NLOPT_GN_DIRECT_L;
20 static double ftol_rel = 0, ftol_abs = 0, xtol_rel = 0, xtol_abs = 0, minf_max_delta = -HUGE_VAL;
21 static int maxeval = 1000, iterations = 1, center_start = 0;
22 static double maxtime = 0.0;
23 static double xinit_tol = -1;
24 static int force_constraints = 0;
26 static void listalgs(FILE *f)
29 fprintf(f, "Available algorithms:\n");
30 for (i = 0; i < NLOPT_NUM_ALGORITHMS; ++i)
31 fprintf(f, " %2d: %s\n", i, nlopt_algorithm_name((nlopt_algorithm) i));
34 static void listfuncs(FILE *f)
37 fprintf(f, "Available objective functions:\n");
38 for (i = 0; i < NTESTFUNCS; ++i)
39 fprintf(f, " %2d: %s (%d dims)\n", i, testfuncs[i].name, testfuncs[i].n);
43 const double *lb, *ub;
48 static double bounds_wrap_func(int n, const double *x, double *grad, void *d_)
50 bounds_wrap_data *d = (bounds_wrap_data *) d_;
52 for (i = 0; i < n; ++i)
53 if (x[i] < d->lb[i] || x[i] > d->ub[i])
56 fprintf(stderr, "WARNING: bounds violated by x[%d] = %g\n", i, x[i]);
57 return d->f(n, x, grad, d->f_data);
60 static int test_function(int ifunc)
64 double *x, minf, minf_max, f0, *xtabs, *lb, *ub;
66 double start = nlopt_seconds();
68 double total_err = 0, max_err = 0;
71 if (ifunc < 0 || ifunc >= NTESTFUNCS) {
72 fprintf(stderr, "testopt: invalid function %d\n", ifunc);
76 func = testfuncs[ifunc];
77 x = (double *) malloc(sizeof(double) * func.n * 5);
78 if (!x) { fprintf(stderr, "testopt: Out of memory!\n"); return 0; }
82 xtabs = x + func.n * 2;
86 bw.f_data = func.f_data;
88 for (i = 0; i < func.n; ++i) xtabs[i] = xtol_abs;
89 minf_max = minf_max_delta > (-HUGE_VAL) ? minf_max_delta + func.minf : (-HUGE_VAL);
91 printf("-----------------------------------------------------------\n");
92 printf("Optimizing %s (%d dims) using %s algorithm\n",
93 func.name, func.n, nlopt_algorithm_name(algorithm));
94 printf("lower bounds at lb = [");
95 for (i = 0; i < func.n; ++i) printf(" %g", func.lb[i]);
97 printf("upper bounds at ub = [");
98 for (i = 0; i < func.n; ++i) printf(" %g", func.ub[i]);
100 memcpy(lb, func.lb, func.n * sizeof(double));
101 memcpy(ub, func.ub, func.n * sizeof(double));
102 if (force_constraints) {
103 for (i = 0; i < func.n; ++i) {
104 if (nlopt_iurand(2) == 0)
105 ub[i] = nlopt_urand(lb[i], func.xmin[i]);
107 lb[i] = nlopt_urand(func.xmin[i], ub[i]);
109 printf("adjusted lower bounds at lb = [");
110 for (i = 0; i < func.n; ++i) printf(" %g", lb[i]);
112 printf("adjusted upper bounds at ub = [");
113 for (i = 0; i < func.n; ++i) printf(" %g", ub[i]);
117 if (fabs(func.f(func.n, func.xmin, 0, func.f_data) - func.minf) > 1e-8) {
118 fprintf(stderr, "BUG: function does not achieve given lower bound!\n");
119 fprintf(stderr, "f(%g", func.xmin[0]);
120 for (i = 1; i < func.n; ++i) fprintf(stderr, ", %g", func.xmin[i]);
121 fprintf(stderr, ") = %0.16g instead of %0.16g, |diff| = %g\n",
122 func.f(func.n, func.xmin, 0, func.f_data), func.minf,
123 fabs(func.f(func.n, func.xmin, 0, func.f_data) - func.minf));
127 for (iter = 0; iter < iterations; ++iter) {
129 testfuncs_counter = 0;
131 printf("Starting guess x = [");
132 for (i = 0; i < func.n; ++i) {
134 x[i] = (ub[i] + lb[i]) * 0.5;
135 else if (xinit_tol < 0) { /* random starting point near center of box */
136 double dx = (ub[i] - lb[i]) * 0.25;
137 double xm = 0.5 * (ub[i] + lb[i]);
138 x[i] = nlopt_urand(xm - dx, xm + dx);
141 x[i] = nlopt_urand(-xinit_tol, xinit_tol)
142 + (1 + nlopt_urand(-xinit_tol, xinit_tol)) * func.xmin[i];
143 if (x[i] > ub[i]) x[i] = ub[i];
144 else if (x[i] < lb[i]) x[i] = lb[i];
149 f0 = func.f(func.n, x, x + func.n, func.f_data);
150 printf("Starting function value = %g\n", f0);
152 if (iter > 0 && testfuncs_verbose && func.has_gradient) {
153 printf("checking gradient:\n");
154 for (i = 0; i < func.n; ++i) {
157 f = func.f(func.n, x, NULL, func.f_data);
159 printf(" grad[%d] = %g vs. numerical derivative %g\n",
160 i, x[i + func.n], (f - f0) / (x[i] * 1e-6));
164 ret = nlopt_minimize(algorithm,
165 func.n, bounds_wrap_func, &bw,
168 minf_max, ftol_rel, ftol_abs, xtol_rel, xtabs,
170 printf("finished after %g seconds.\n", nlopt_seconds() - start);
171 printf("return code %d from nlopt_minimize\n", ret);
173 fprintf(stderr, "testopt: error in nlopt_minimize\n");
176 printf("Found minimum f = %g after %d evaluations.\n",
177 minf, testfuncs_counter);
178 total_count += testfuncs_counter;
179 printf("Minimum at x = [");
180 for (i = 0; i < func.n; ++i) printf(" %g", x[i]);
182 printf("|f - minf| = %g, |f - minf| / |minf| = %e\n",
183 fabs(minf - func.minf), fabs(minf - func.minf) / fabs(func.minf));
184 total_err += fabs(minf - func.minf);
185 if (fabs(minf - func.minf) > max_err)
186 max_err = fabs(minf - func.minf);
187 printf("vs. global minimum f = %g at x = [", func.minf);
188 for (i = 0; i < func.n; ++i) printf(" %g", func.xmin[i]);
191 val = func.f(func.n, x, NULL, func.f_data);
193 fprintf(stderr, "Mismatch %g between returned minf=%g and f(x) = %g\n",
194 minf - val, minf, val);
198 printf("average #evaluations = %g\naverage |f-minf| = %g, max |f-minf| = %g\n", total_count * 1.0 / iterations, total_err / iterations, max_err);
204 static void usage(FILE *f)
206 fprintf(f, "Usage: testopt [OPTIONS]\n"
208 " -h : print this help\n"
209 " -L : list available algorithms and objective functions\n"
210 " -v : verbose mode\n"
211 " -a <n> : use optimization algorithm <n>\n"
212 " -o <n> : use objective function <n>\n"
213 " -0 <x> : starting guess within <x> + (1+<x>) * optimum\n"
214 " -c : starting guess at center of cell\n"
215 " -C : put optimum outside of bound constraints\n"
216 " -e <n> : use at most <n> evals (default: %d, 0 to disable)\n"
217 " -t <t> : use at most <t> seconds (default: disabled)\n"
218 " -x <t> : relative tolerance <t> on x (default: disabled)\n"
219 " -X <t> : absolute tolerance <t> on x (default: disabled)\n"
220 " -f <t> : relative tolerance <t> on f (default: disabled)\n"
221 " -F <t> : absolute tolerance <t> on f (default: disabled)\n"
222 " -m <m> : stop when minf+<m> is reached (default: disabled)\n"
223 " -i <n> : iterate optimization <n> times (default: 1)\n"
224 " -r <s> : use random seed <s> for starting guesses\n"
228 int main(int argc, char **argv)
233 testfuncs_verbose = 0;
238 while ((c = getopt(argc, argv, "hLvCc0:r:a:o:i:e:t:x:X:f:F:m:")) != -1)
248 testfuncs_verbose = 1;
251 force_constraints = 1;
254 nlopt_srand((unsigned long) atoi(optarg));
258 if (c < 0 || c >= NLOPT_NUM_ALGORITHMS) {
259 fprintf(stderr, "testopt: invalid algorithm %d\n", c);
263 algorithm = (nlopt_algorithm) c;
266 if (!test_function(atoi(optarg)))
270 maxeval = atoi(optarg);
273 iterations = atoi(optarg);
276 maxtime = atof(optarg);
279 xtol_rel = atof(optarg);
282 xtol_abs = atof(optarg);
285 ftol_rel = atof(optarg);
288 ftol_abs = atof(optarg);
291 minf_max_delta = atof(optarg);
298 xinit_tol = atof(optarg);
301 fprintf(stderr, "harminv: invalid argument -%c\n", c);