1 /* Copyright (c) 2007-2011 Massachusetts Institute of Technology
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "nlopt_config.h"
36 # include "nlopt-getopt.h"
39 #define USE_FEENABLEEXCEPT 0
40 #if USE_FEENABLEEXCEPT
42 extern "C" int feenableexcept (int EXCEPTS);
47 #include "nlopt-util.h"
48 #include "testfuncs.h"
50 static nlopt_algorithm algorithm = NLOPT_GN_DIRECT_L;
51 static double ftol_rel = 0, ftol_abs = 0, xtol_rel = 0, xtol_abs = 0, minf_max_delta;
52 static int maxeval = 1000, iterations = 1, center_start = 0;
53 static double maxtime = 0.0;
54 static double xinit_tol = -1;
55 static int force_constraints = 0;
56 static int fix_bounds[100] = {0,0,0,0,0,0,0,0,0,0,
57 0,0,0,0,0,0,0,0,0,0,
58 0,0,0,0,0,0,0,0,0,0,
59 0,0,0,0,0,0,0,0,0,0,
60 0,0,0,0,0,0,0,0,0,0,
61 0,0,0,0,0,0,0,0,0,0,
62 0,0,0,0,0,0,0,0,0,0,
63 0,0,0,0,0,0,0,0,0,0,
64 0,0,0,0,0,0,0,0,0,0,
67 static void listalgs(FILE *f)
70 fprintf(f, "Available algorithms:\n");
71 for (i = 0; i < NLOPT_NUM_ALGORITHMS; ++i)
72 fprintf(f, " %2d: %s\n", i, nlopt_algorithm_name((nlopt_algorithm) i));
75 static void listfuncs(FILE *f)
78 fprintf(f, "Available objective functions:\n");
79 for (i = 0; i < NTESTFUNCS; ++i)
80 fprintf(f, " %2d: %s (%d dims)\n", i, testfuncs[i].name, testfuncs[i].n);
84 const double *lb, *ub;
89 static double bounds_wrap_func(int n, const double *x, double *grad, void *d_)
91 bounds_wrap_data *d = (bounds_wrap_data *) d_;
94 for (i = 0; i < n; ++i) {
95 if (x[i] < d->lb[i]) {
99 else if (x[i] > d->ub[i]) {
105 fprintf(stderr, "WARNING: bounds violated by x[%d] = %g = %g + %g\n",
106 i, x[i], b, x[i] - b);
107 return d->f(n, x, grad, d->f_data);
110 static int test_function(int ifunc)
114 double *x, minf, minf_max, f0, *xtabs, *lb, *ub;
116 double start = nlopt_seconds();
117 int total_count = 0, max_count = 0, min_count = 1<<30;
118 double total_err = 0, max_err = 0;
121 if (ifunc < 0 || ifunc >= NTESTFUNCS) {
122 fprintf(stderr, "testopt: invalid function %d\n", ifunc);
126 func = testfuncs[ifunc];
127 x = (double *) malloc(sizeof(double) * func.n * 5);
128 if (!x) { fprintf(stderr, "testopt: Out of memory!\n"); return 0; }
132 xtabs = x + func.n * 2;
136 bw.f_data = func.f_data;
138 for (i = 0; i < func.n; ++i) xtabs[i] = xtol_abs;
139 minf_max = minf_max_delta > (-HUGE_VAL) ? minf_max_delta + func.minf : (-HUGE_VAL);
141 printf("-----------------------------------------------------------\n");
142 printf("Optimizing %s (%d dims) using %s algorithm\n",
143 func.name, func.n, nlopt_algorithm_name(algorithm));
144 printf("lower bounds at lb = [");
145 for (i = 0; i < func.n; ++i) printf(" %g", func.lb[i]);
147 printf("upper bounds at ub = [");
148 for (i = 0; i < func.n; ++i) printf(" %g", func.ub[i]);
150 memcpy(lb, func.lb, func.n * sizeof(double));
151 memcpy(ub, func.ub, func.n * sizeof(double));
152 for (i = 0; i < func.n; ++i) if (fix_bounds[i]) {
153 printf("fixing bounds for dim[%d] to xmin[%d]=%g\n",
155 lb[i] = ub[i] = func.xmin[i];
157 if (force_constraints) {
158 for (i = 0; i < func.n; ++i) {
159 if (nlopt_iurand(2) == 0)
160 ub[i] = nlopt_urand(lb[i], func.xmin[i]);
162 lb[i] = nlopt_urand(func.xmin[i], ub[i]);
164 printf("adjusted lower bounds at lb = [");
165 for (i = 0; i < func.n; ++i) printf(" %g", lb[i]);
167 printf("adjusted upper bounds at ub = [");
168 for (i = 0; i < func.n; ++i) printf(" %g", ub[i]);
172 if (fabs(func.f(func.n, func.xmin, 0, func.f_data) - func.minf) > 1e-8) {
173 fprintf(stderr, "BUG: function does not achieve given lower bound!\n");
174 fprintf(stderr, "f(%g", func.xmin[0]);
175 for (i = 1; i < func.n; ++i) fprintf(stderr, ", %g", func.xmin[i]);
176 fprintf(stderr, ") = %0.16g instead of %0.16g, |diff| = %g\n",
177 func.f(func.n, func.xmin, 0, func.f_data), func.minf,
178 fabs(func.f(func.n, func.xmin, 0, func.f_data) - func.minf));
183 for (iter = 0; iter < iterations; ++iter) {
185 testfuncs_counter = 0;
187 printf("Starting guess x = [");
188 for (i = 0; i < func.n; ++i) {
190 x[i] = (ub[i] + lb[i]) * 0.5;
191 else if (xinit_tol < 0) { /* random starting point near center of box */
192 double dx = (ub[i] - lb[i]) * 0.25;
193 double xm = 0.5 * (ub[i] + lb[i]);
194 x[i] = nlopt_urand(xm - dx, xm + dx);
197 x[i] = nlopt_urand(-xinit_tol, xinit_tol)
198 + (1 + nlopt_urand(-xinit_tol, xinit_tol)) * func.xmin[i];
199 if (x[i] > ub[i]) x[i] = ub[i];
200 else if (x[i] < lb[i]) x[i] = lb[i];
205 f0 = func.f(func.n, x, x + func.n, func.f_data);
206 printf("Starting function value = %g\n", f0);
208 if (iter == 0 && testfuncs_verbose && func.has_gradient) {
209 printf("checking gradient:\n");
210 for (i = 0; i < func.n; ++i) {
213 f = func.f(func.n, x, NULL, func.f_data);
215 printf(" grad[%d] = %g vs. numerical derivative %g\n",
216 i, x[i + func.n], (f - f0) / (x[i] * 1e-6));
220 testfuncs_counter = 0;
221 ret = nlopt_minimize(algorithm,
222 func.n, bounds_wrap_func, &bw,
225 minf_max, ftol_rel, ftol_abs, xtol_rel, xtabs,
227 printf("finished after %g seconds.\n", nlopt_seconds() - start);
228 printf("return code %d from nlopt_minimize\n", ret);
229 if (ret < 0 && ret != NLOPT_ROUNDOFF_LIMITED
230 && ret != NLOPT_FORCED_STOP) {
231 fprintf(stderr, "testopt: error in nlopt_minimize\n");
235 printf("Found minimum f = %g after %d evaluations.\n",
236 minf, testfuncs_counter);
237 total_count += testfuncs_counter;
238 if (testfuncs_counter > max_count) max_count = testfuncs_counter;
239 if (testfuncs_counter < min_count) min_count = testfuncs_counter;
240 printf("Minimum at x = [");
241 for (i = 0; i < func.n; ++i) printf(" %g", x[i]);
244 printf("|f - minf| = %g\n", fabs(minf - func.minf));
246 printf("|f - minf| = %g, |f - minf| / |minf| = %e\n",
247 fabs(minf - func.minf), fabs(minf - func.minf) / fabs(func.minf));
248 total_err += fabs(minf - func.minf);
249 if (fabs(minf - func.minf) > max_err)
250 max_err = fabs(minf - func.minf);
251 printf("vs. global minimum f = %g at x = [", func.minf);
252 for (i = 0; i < func.n; ++i) printf(" %g", func.xmin[i]);
255 val = func.f(func.n, x, NULL, func.f_data);
256 if (fabs(val - minf) > 1e-12) {
257 fprintf(stderr, "Mismatch %g between returned minf=%g and f(x) = %g\n",
258 minf - val, minf, val);
264 printf("average #evaluations = %g (%d-%d)\naverage |f-minf| = %g, max |f-minf| = %g\n", total_count * 1.0 / iterations, min_count, max_count, total_err / iterations, max_err);
270 static void usage(FILE *f)
272 fprintf(f, "Usage: testopt [OPTIONS]\n"
274 " -h : print this help\n"
275 " -L : list available algorithms and objective functions\n"
276 " -v : verbose mode\n"
277 " -a <n> : use optimization algorithm <n>\n"
278 " -o <n> : use objective function <n>\n"
279 " -0 <x> : starting guess within <x> + (1+<x>) * optimum\n"
280 " -b <dim0,dim1,...>: eliminate given dims by equating bounds\n"
281 " -c : starting guess at center of cell\n"
282 " -C : put optimum outside of bound constraints\n"
283 " -e <n> : use at most <n> evals (default: %d, 0 to disable)\n"
284 " -t <t> : use at most <t> seconds (default: disabled)\n"
285 " -x <t> : relative tolerance <t> on x (default: disabled)\n"
286 " -X <t> : absolute tolerance <t> on x (default: disabled)\n"
287 " -f <t> : relative tolerance <t> on f (default: disabled)\n"
288 " -F <t> : absolute tolerance <t> on f (default: disabled)\n"
289 " -m <m> : stop when minf+<m> is reached (default: disabled)\n"
290 " -i <n> : iterate optimization <n> times (default: 1)\n"
291 " -r <s> : use random seed <s> for starting guesses\n"
295 int main(int argc, char **argv)
300 testfuncs_verbose = 0;
301 minf_max_delta = -HUGE_VAL;
306 #if USE_FEENABLEEXCEPT
307 feenableexcept(FE_INVALID);
310 while ((c = getopt(argc, argv, "hLvCc0:r:a:o:i:e:t:x:X:f:F:m:b:")) != -1)
320 testfuncs_verbose = 1;
323 force_constraints = 1;
326 nlopt_srand((unsigned long) atoi(optarg));
330 if (c < 0 || c >= NLOPT_NUM_ALGORITHMS) {
331 fprintf(stderr, "testopt: invalid algorithm %d\n", c);
335 algorithm = (nlopt_algorithm) c;
338 if (!test_function(atoi(optarg)))
342 maxeval = atoi(optarg);
345 iterations = atoi(optarg);
348 maxtime = atof(optarg);
351 xtol_rel = atof(optarg);
354 xtol_abs = atof(optarg);
357 ftol_rel = atof(optarg);
360 ftol_abs = atof(optarg);
363 minf_max_delta = atof(optarg);
370 xinit_tol = atof(optarg);
373 const char *s = optarg;
376 if (b < 0 || b >= 100) {
377 fprintf(stderr, "invalid -b argument");
381 s = strchr(s, ','); if (s) ++s;
386 fprintf(stderr, "harminv: invalid argument -%c\n", c);