1 /* Copyright (c) 2007-2010 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.
37 #define USE_FEENABLEEXCEPT 0
38 #if USE_FEENABLEEXCEPT
40 extern "C" int feenableexcept (int EXCEPTS);
45 #include "nlopt-util.h"
46 #include "testfuncs.h"
48 static nlopt_algorithm algorithm = NLOPT_GN_DIRECT_L;
49 static double ftol_rel = 0, ftol_abs = 0, xtol_rel = 0, xtol_abs = 0, minf_max_delta = -HUGE_VAL;
50 static int maxeval = 1000, iterations = 1, center_start = 0;
51 static double maxtime = 0.0;
52 static double xinit_tol = -1;
53 static int force_constraints = 0;
55 static void listalgs(FILE *f)
58 fprintf(f, "Available algorithms:\n");
59 for (i = 0; i < NLOPT_NUM_ALGORITHMS; ++i)
60 fprintf(f, " %2d: %s\n", i, nlopt_algorithm_name((nlopt_algorithm) i));
63 static void listfuncs(FILE *f)
66 fprintf(f, "Available objective functions:\n");
67 for (i = 0; i < NTESTFUNCS; ++i)
68 fprintf(f, " %2d: %s (%d dims)\n", i, testfuncs[i].name, testfuncs[i].n);
72 const double *lb, *ub;
77 static double bounds_wrap_func(int n, const double *x, double *grad, void *d_)
79 bounds_wrap_data *d = (bounds_wrap_data *) d_;
82 for (i = 0; i < n; ++i) {
83 if (x[i] < d->lb[i]) {
87 else if (x[i] > d->ub[i]) {
93 fprintf(stderr, "WARNING: bounds violated by x[%d] = %g = %g + %g\n",
94 i, x[i], b, x[i] - b);
95 return d->f(n, x, grad, d->f_data);
98 static int test_function(int ifunc)
102 double *x, minf, minf_max, f0, *xtabs, *lb, *ub;
104 double start = nlopt_seconds();
105 int total_count = 0, max_count = 0, min_count = 1<<30;
106 double total_err = 0, max_err = 0;
109 if (ifunc < 0 || ifunc >= NTESTFUNCS) {
110 fprintf(stderr, "testopt: invalid function %d\n", ifunc);
114 func = testfuncs[ifunc];
115 x = (double *) malloc(sizeof(double) * func.n * 5);
116 if (!x) { fprintf(stderr, "testopt: Out of memory!\n"); return 0; }
120 xtabs = x + func.n * 2;
124 bw.f_data = func.f_data;
126 for (i = 0; i < func.n; ++i) xtabs[i] = xtol_abs;
127 minf_max = minf_max_delta > (-HUGE_VAL) ? minf_max_delta + func.minf : (-HUGE_VAL);
129 printf("-----------------------------------------------------------\n");
130 printf("Optimizing %s (%d dims) using %s algorithm\n",
131 func.name, func.n, nlopt_algorithm_name(algorithm));
132 printf("lower bounds at lb = [");
133 for (i = 0; i < func.n; ++i) printf(" %g", func.lb[i]);
135 printf("upper bounds at ub = [");
136 for (i = 0; i < func.n; ++i) printf(" %g", func.ub[i]);
138 memcpy(lb, func.lb, func.n * sizeof(double));
139 memcpy(ub, func.ub, func.n * sizeof(double));
140 if (force_constraints) {
141 for (i = 0; i < func.n; ++i) {
142 if (nlopt_iurand(2) == 0)
143 ub[i] = nlopt_urand(lb[i], func.xmin[i]);
145 lb[i] = nlopt_urand(func.xmin[i], ub[i]);
147 printf("adjusted lower bounds at lb = [");
148 for (i = 0; i < func.n; ++i) printf(" %g", lb[i]);
150 printf("adjusted upper bounds at ub = [");
151 for (i = 0; i < func.n; ++i) printf(" %g", ub[i]);
155 if (fabs(func.f(func.n, func.xmin, 0, func.f_data) - func.minf) > 1e-8) {
156 fprintf(stderr, "BUG: function does not achieve given lower bound!\n");
157 fprintf(stderr, "f(%g", func.xmin[0]);
158 for (i = 1; i < func.n; ++i) fprintf(stderr, ", %g", func.xmin[i]);
159 fprintf(stderr, ") = %0.16g instead of %0.16g, |diff| = %g\n",
160 func.f(func.n, func.xmin, 0, func.f_data), func.minf,
161 fabs(func.f(func.n, func.xmin, 0, func.f_data) - func.minf));
165 for (iter = 0; iter < iterations; ++iter) {
167 testfuncs_counter = 0;
169 printf("Starting guess x = [");
170 for (i = 0; i < func.n; ++i) {
172 x[i] = (ub[i] + lb[i]) * 0.5;
173 else if (xinit_tol < 0) { /* random starting point near center of box */
174 double dx = (ub[i] - lb[i]) * 0.25;
175 double xm = 0.5 * (ub[i] + lb[i]);
176 x[i] = nlopt_urand(xm - dx, xm + dx);
179 x[i] = nlopt_urand(-xinit_tol, xinit_tol)
180 + (1 + nlopt_urand(-xinit_tol, xinit_tol)) * func.xmin[i];
181 if (x[i] > ub[i]) x[i] = ub[i];
182 else if (x[i] < lb[i]) x[i] = lb[i];
187 f0 = func.f(func.n, x, x + func.n, func.f_data);
188 printf("Starting function value = %g\n", f0);
190 if (iter == 0 && testfuncs_verbose && func.has_gradient) {
191 printf("checking gradient:\n");
192 for (i = 0; i < func.n; ++i) {
195 f = func.f(func.n, x, NULL, func.f_data);
197 printf(" grad[%d] = %g vs. numerical derivative %g\n",
198 i, x[i + func.n], (f - f0) / (x[i] * 1e-6));
202 ret = nlopt_minimize(algorithm,
203 func.n, bounds_wrap_func, &bw,
206 minf_max, ftol_rel, ftol_abs, xtol_rel, xtabs,
208 printf("finished after %g seconds.\n", nlopt_seconds() - start);
209 printf("return code %d from nlopt_minimize\n", ret);
210 if (ret < 0 && ret != NLOPT_ROUNDOFF_LIMITED) {
211 fprintf(stderr, "testopt: error in nlopt_minimize\n");
214 printf("Found minimum f = %g after %d evaluations.\n",
215 minf, testfuncs_counter);
216 total_count += testfuncs_counter;
217 if (testfuncs_counter > max_count) max_count = testfuncs_counter;
218 if (testfuncs_counter < min_count) min_count = testfuncs_counter;
219 printf("Minimum at x = [");
220 for (i = 0; i < func.n; ++i) printf(" %g", x[i]);
223 printf("|f - minf| = %g\n", fabs(minf - func.minf));
225 printf("|f - minf| = %g, |f - minf| / |minf| = %e\n",
226 fabs(minf - func.minf), fabs(minf - func.minf) / fabs(func.minf));
227 total_err += fabs(minf - func.minf);
228 if (fabs(minf - func.minf) > max_err)
229 max_err = fabs(minf - func.minf);
230 printf("vs. global minimum f = %g at x = [", func.minf);
231 for (i = 0; i < func.n; ++i) printf(" %g", func.xmin[i]);
234 val = func.f(func.n, x, NULL, func.f_data);
236 fprintf(stderr, "Mismatch %g between returned minf=%g and f(x) = %g\n",
237 minf - val, minf, val);
242 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);
248 static void usage(FILE *f)
250 fprintf(f, "Usage: testopt [OPTIONS]\n"
252 " -h : print this help\n"
253 " -L : list available algorithms and objective functions\n"
254 " -v : verbose mode\n"
255 " -a <n> : use optimization algorithm <n>\n"
256 " -o <n> : use objective function <n>\n"
257 " -0 <x> : starting guess within <x> + (1+<x>) * optimum\n"
258 " -c : starting guess at center of cell\n"
259 " -C : put optimum outside of bound constraints\n"
260 " -e <n> : use at most <n> evals (default: %d, 0 to disable)\n"
261 " -t <t> : use at most <t> seconds (default: disabled)\n"
262 " -x <t> : relative tolerance <t> on x (default: disabled)\n"
263 " -X <t> : absolute tolerance <t> on x (default: disabled)\n"
264 " -f <t> : relative tolerance <t> on f (default: disabled)\n"
265 " -F <t> : absolute tolerance <t> on f (default: disabled)\n"
266 " -m <m> : stop when minf+<m> is reached (default: disabled)\n"
267 " -i <n> : iterate optimization <n> times (default: 1)\n"
268 " -r <s> : use random seed <s> for starting guesses\n"
272 int main(int argc, char **argv)
277 testfuncs_verbose = 0;
282 #if USE_FEENABLEEXCEPT
283 feenableexcept(FE_INVALID);
286 while ((c = getopt(argc, argv, "hLvCc0:r:a:o:i:e:t:x:X:f:F:m:")) != -1)
296 testfuncs_verbose = 1;
299 force_constraints = 1;
302 nlopt_srand((unsigned long) atoi(optarg));
306 if (c < 0 || c >= NLOPT_NUM_ALGORITHMS) {
307 fprintf(stderr, "testopt: invalid algorithm %d\n", c);
311 algorithm = (nlopt_algorithm) c;
314 if (!test_function(atoi(optarg)))
318 maxeval = atoi(optarg);
321 iterations = atoi(optarg);
324 maxtime = atof(optarg);
327 xtol_rel = atof(optarg);
330 xtol_abs = atof(optarg);
333 ftol_rel = atof(optarg);
336 ftol_abs = atof(optarg);
339 minf_max_delta = atof(optarg);
346 xinit_tol = atof(optarg);
349 fprintf(stderr, "harminv: invalid argument -%c\n", c);