15 #include "nlopt-util.h"
16 #include "testfuncs.h"
18 static nlopt_algorithm algorithm = NLOPT_GLOBAL_DIRECT;
19 static double ftol_rel = 0, ftol_abs = 0, xtol_rel = 0, xtol_abs = 0, fmin_max = -HUGE_VAL;
20 static int maxeval = 1000;
21 static double maxtime = 0.0;
23 static void listalgs(FILE *f)
26 fprintf(f, "Available algorithms:\n");
27 for (i = 0; i < NLOPT_NUM_ALGORITHMS; ++i)
28 fprintf(f, " %2d: %s\n", i, nlopt_algorithm_name((nlopt_algorithm) i));
31 static void listfuncs(FILE *f)
34 fprintf(f, "Available objective functions:\n");
35 for (i = 0; i < NTESTFUNCS; ++i)
36 fprintf(f, " %2d: %s (%d dims)\n", i, testfuncs[i].name, testfuncs[i].n);
39 static int test_function(int ifunc)
43 double *x, fmin, f0, *xtabs;
45 double start = nlopt_seconds();
47 if (ifunc < 0 || ifunc >= NTESTFUNCS) {
48 fprintf(stderr, "testopt: invalid function %d\n", ifunc);
52 func = testfuncs[ifunc];
53 if (!func.has_gradient && algorithm >= NLOPT_GLOBAL_STOGO) {
55 "testopt: A function with gradients is required for %s\n",
56 nlopt_algorithm_name(algorithm));
59 x = (double *) malloc(sizeof(double) * func.n * 3);
60 if (!x) { fprintf(stderr, "testopt: Out of memory!\n"); return 0; }
62 xtabs = x + func.n * 2;
63 for (i = 0; i < func.n; ++i) xtabs[i] = xtol_abs;
66 printf("-----------------------------------------------------------\n");
67 printf("Optimizing %s (%d dims) using %s algorithm\n",
68 func.name, func.n, nlopt_algorithm_name(algorithm));
69 printf("Starting guess x = [");
70 for (i = 0; i < func.n; ++i)
71 printf(" %g", x[i] = nlopt_urand(func.lb[i], func.ub[i]));
73 f0 = func.f(func.n, x, x + func.n, func.f_data);
74 printf("Starting function value = %g\n", f0);
76 if (testfuncs_verbose && func.has_gradient) {
77 printf("checking gradient:\n");
78 for (i = 0; i < func.n; ++i) {
81 f = func.f(func.n, x, NULL, func.f_data);
83 printf(" grad[%d] = %g vs. numerical derivative %g\n",
84 i, x[i + func.n], (f - f0) / (x[i] * 1e-6));
88 testfuncs_counter = 0;
89 ret = nlopt_minimize(algorithm,
90 func.n, func.f, func.f_data,
93 fmin_max, ftol_rel, ftol_abs, xtol_rel, xtabs,
95 printf("finished after %g seconds.\n", nlopt_seconds() - start);
96 printf("return code %d from nlopt_minimize\n", ret);
98 fprintf(stderr, "testopt: error in nlopt_minimize\n");
101 printf("Found minimum f = %g after %d evaluations.\n",
102 fmin, testfuncs_counter);
103 printf("Minimum at x = [");
104 for (i = 0; i < func.n; ++i) printf(" %g", x[i]);
106 printf("vs. global minimum f = %g at x = [", func.fmin);
107 for (i = 0; i < func.n; ++i) printf(" %g", func.xmin[i]);
109 printf("|f - fmin| = %g, |f - fmin| / |fmin| = %e\n",
110 fabs(fmin - func.fmin), fabs(fmin - func.fmin) / fabs(func.fmin));
116 static void usage(FILE *f)
118 fprintf(f, "Usage: testopt [OPTIONS]\n"
120 " -h : print this help\n"
121 " -L : list available algorithms and objective functions\n"
122 " -v : verbose mode\n"
123 " -r <s> : use random seed <s> for starting guesses\n"
124 " -a <n> : use optimization algorithm <n>\n"
125 " -o <n> : use objective function <n>\n"
126 " -e <n> : use at most <n> evals (default: %d, 0 to disable)\n"
127 " -t <t> : use at most <t> seconds (default: disabled)\n"
128 " -x <t> : relative tolerance <t> on x (default: disabled)\n"
129 " -X <t> : absolute tolerance <t> on x (default: disabled)\n"
130 " -f <t> : relative tolerance <t> on f (default: disabled)\n"
131 " -F <t> : absolute tolerance <t> on f (default: disabled)\n"
132 " -m <m> : minimize f until <m> is reached (default: disabled)\n"
136 int main(int argc, char **argv)
141 testfuncs_verbose = 0;
146 while ((c = getopt(argc, argv, "hLvr:a:o:e:t:x:X:f:F:m:")) != -1)
156 testfuncs_verbose = 1;
159 nlopt_srand((unsigned long) atoi(optarg));
163 if (c < 0 || c >= NLOPT_NUM_ALGORITHMS) {
164 fprintf(stderr, "testopt: invalid algorithm %d\n", c);
168 algorithm = (nlopt_algorithm) c;
171 if (!test_function(atoi(optarg)))
175 maxeval = atoi(optarg);
178 maxtime = atof(optarg);
181 xtol_rel = atof(optarg);
184 xtol_abs = atof(optarg);
187 ftol_rel = atof(optarg);
190 ftol_abs = atof(optarg);
193 fmin_max = atof(optarg);
196 fprintf(stderr, "harminv: invalid argument -%c\n", c);