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);
42 static int test_function(int ifunc)
46 double *x, minf, minf_max, f0, *xtabs, *lb, *ub;
48 double start = nlopt_seconds();
50 if (ifunc < 0 || ifunc >= NTESTFUNCS) {
51 fprintf(stderr, "testopt: invalid function %d\n", ifunc);
55 func = testfuncs[ifunc];
56 x = (double *) malloc(sizeof(double) * func.n * 5);
57 if (!x) { fprintf(stderr, "testopt: Out of memory!\n"); return 0; }
61 xtabs = x + func.n * 2;
63 for (i = 0; i < func.n; ++i) xtabs[i] = xtol_abs;
64 minf_max = minf_max_delta > (-HUGE_VAL) ? minf_max_delta + func.minf : (-HUGE_VAL);
66 printf("-----------------------------------------------------------\n");
67 printf("Optimizing %s (%d dims) using %s algorithm\n",
68 func.name, func.n, nlopt_algorithm_name(algorithm));
69 printf("lower bounds at lb = [");
70 for (i = 0; i < func.n; ++i) printf(" %g", func.lb[i]);
72 printf("upper bounds at ub = [");
73 for (i = 0; i < func.n; ++i) printf(" %g", func.ub[i]);
75 memcpy(lb, func.lb, func.n * sizeof(double));
76 memcpy(ub, func.ub, func.n * sizeof(double));
77 if (force_constraints) {
78 for (i = 0; i < func.n; ++i) {
79 if (nlopt_iurand(2) == 0)
80 ub[i] = nlopt_urand(lb[i], func.xmin[i]);
82 lb[i] = nlopt_urand(func.xmin[i], ub[i]);
84 printf("adjusted lower bounds at lb = [");
85 for (i = 0; i < func.n; ++i) printf(" %g", lb[i]);
87 printf("adjusted upper bounds at ub = [");
88 for (i = 0; i < func.n; ++i) printf(" %g", ub[i]);
93 printf("Starting guess x = [");
94 for (i = 0; i < func.n; ++i) {
96 x[i] = (ub[i] + lb[i]) * 0.5;
97 else if (xinit_tol < 0) { /* random starting point near center of box */
98 double dx = (ub[i] - lb[i]) * 0.25;
99 double xm = 0.5 * (ub[i] + lb[i]);
100 x[i] = nlopt_urand(xm - dx, xm + dx);
103 x[i] = nlopt_urand(-xinit_tol, xinit_tol)
104 + (1 + nlopt_urand(-xinit_tol, xinit_tol)) * func.xmin[i];
105 if (x[i] > ub[i]) x[i] = ub[i];
106 else if (x[i] < lb[i]) x[i] = lb[i];
111 f0 = func.f(func.n, x, x + func.n, func.f_data);
112 printf("Starting function value = %g\n", f0);
114 if (testfuncs_verbose && func.has_gradient) {
115 printf("checking gradient:\n");
116 for (i = 0; i < func.n; ++i) {
119 f = func.f(func.n, x, NULL, func.f_data);
121 printf(" grad[%d] = %g vs. numerical derivative %g\n",
122 i, x[i + func.n], (f - f0) / (x[i] * 1e-6));
126 for (iter = 0; iter < iterations; ++iter) {
127 testfuncs_counter = 0;
128 ret = nlopt_minimize(algorithm,
129 func.n, func.f, func.f_data,
132 minf_max, ftol_rel, ftol_abs, xtol_rel, xtabs,
134 printf("finished after %g seconds.\n", nlopt_seconds() - start);
135 printf("return code %d from nlopt_minimize\n", ret);
137 fprintf(stderr, "testopt: error in nlopt_minimize\n");
140 printf("Found minimum f = %g after %d evaluations.\n",
141 minf, testfuncs_counter);
142 printf("Minimum at x = [");
143 for (i = 0; i < func.n; ++i) printf(" %g", x[i]);
145 printf("|f - minf| = %g, |f - minf| / |minf| = %e\n",
146 fabs(minf - func.minf), fabs(minf - func.minf) / fabs(func.minf));
148 printf("vs. global minimum f = %g at x = [", func.minf);
149 for (i = 0; i < func.n; ++i) printf(" %g", func.xmin[i]);
156 static void usage(FILE *f)
158 fprintf(f, "Usage: testopt [OPTIONS]\n"
160 " -h : print this help\n"
161 " -L : list available algorithms and objective functions\n"
162 " -v : verbose mode\n"
163 " -a <n> : use optimization algorithm <n>\n"
164 " -o <n> : use objective function <n>\n"
165 " -0 <x> : starting guess within <x> + (1+<x>) * optimum\n"
166 " -c : starting guess at center of cell\n"
167 " -C : put optimum outside of bound constraints\n"
168 " -e <n> : use at most <n> evals (default: %d, 0 to disable)\n"
169 " -t <t> : use at most <t> seconds (default: disabled)\n"
170 " -x <t> : relative tolerance <t> on x (default: disabled)\n"
171 " -X <t> : absolute tolerance <t> on x (default: disabled)\n"
172 " -f <t> : relative tolerance <t> on f (default: disabled)\n"
173 " -F <t> : absolute tolerance <t> on f (default: disabled)\n"
174 " -m <m> : stop when minf+<m> is reached (default: disabled)\n"
175 " -i <n> : iterate optimization <n> times (default: 1)\n"
176 " -r <s> : use random seed <s> for starting guesses\n"
180 int main(int argc, char **argv)
185 testfuncs_verbose = 0;
190 while ((c = getopt(argc, argv, "hLvCc0:r:a:o:i:e:t:x:X:f:F:m:")) != -1)
200 testfuncs_verbose = 1;
203 force_constraints = 1;
206 nlopt_srand((unsigned long) atoi(optarg));
210 if (c < 0 || c >= NLOPT_NUM_ALGORITHMS) {
211 fprintf(stderr, "testopt: invalid algorithm %d\n", c);
215 algorithm = (nlopt_algorithm) c;
218 if (!test_function(atoi(optarg)))
222 maxeval = atoi(optarg);
225 iterations = atoi(optarg);
228 maxtime = atof(optarg);
231 xtol_rel = atof(optarg);
234 xtol_abs = atof(optarg);
237 ftol_rel = atof(optarg);
240 ftol_abs = atof(optarg);
243 minf_max_delta = atof(optarg);
250 xinit_tol = atof(optarg);
253 fprintf(stderr, "harminv: invalid argument -%c\n", c);