13 #if TIME_WITH_SYS_TIME
14 # include <sys/time.h>
18 # include <sys/time.h>
25 #include "testfuncs.h"
27 static double urand(double a, double b)
29 return a + (rand() * (b - a) / RAND_MAX);
32 static nlopt_algorithm algorithm = NLOPT_GLOBAL_DIRECT;
33 static double ftol_rel = 0, ftol_abs = 0, xtol_rel = 0;
34 static int maxeval = 1000;
35 static double maxtime = 0.0;
37 static int test_function(int ifunc)
44 if (ifunc < 0 || ifunc >= NTESTFUNCS) {
45 fprintf(stderr, "testopt: invalid function %d\n", ifunc);
48 func = testfuncs[ifunc];
49 if (!func.has_gradient && algorithm >= NLOPT_GLOBAL_STOGO) {
51 "testopt: A function with gradients is required for %s\n",
52 nlopt_algorithm_name(algorithm));
55 x = (double *) malloc(sizeof(double) * func.n * 2);
56 if (!x) { fprintf(stderr, "testopt: Out of memory!\n"); return 0; }
59 printf("-----------------------------------------------------------\n");
60 printf("Optimizing %s (%d dims) using %s algorithm\n",
61 func.name, func.n, nlopt_algorithm_name(algorithm));
62 printf("Starting guess x = [");
63 for (i = 0; i < func.n; ++i)
64 printf(" %g", x[i] = urand(func.lb[i], func.ub[i]));
66 f0 = func.f(func.n, x, x + func.n, func.f_data);
67 printf("Starting function value = %g\n", f0);
69 if (testfuncs_verbose && func.has_gradient) {
70 printf("checking gradient:\n");
71 for (i = 0; i < func.n; ++i) {
74 f = func.f(func.n, x, NULL, func.f_data);
76 printf(" grad[%d] = %g vs. numerical derivative %g\n",
77 i, x[i + func.n], (f - f0) / (x[i] * 1e-6));
81 testfuncs_counter = 0;
82 ret = nlopt_minimize(algorithm,
83 func.n, func.f, func.f_data,
86 HUGE_VAL, ftol_rel, ftol_abs, xtol_rel, NULL,
88 printf("return code %d from nlopt_minimize\n", ret);
90 fprintf(stderr, "testopt: error in nlopt_minimize\n");
93 printf("Found minimum f = %g after %d evaluations.\n",
94 fmin, testfuncs_counter);
95 printf("Minimum at x = [");
96 for (i = 0; i < func.n; ++i) printf(" %g", x[i]);
98 printf("vs. global minimum f = %g at x = [", func.fmin);
99 for (i = 0; i < func.n; ++i) printf(" %g", func.xmin[i]);
101 printf("|f - fmin| = %g, |f - fmin| / |fmin| = %e\n",
102 fabs(fmin - func.fmin), fabs(fmin - func.fmin) / fabs(func.fmin));
108 static void usage(FILE *f)
110 fprintf(f, "Usage: testopt [OPTIONS]\n"
112 " -h : print this help\n"
113 " -v : verbose mode\n"
114 " -r <s> : use random seed <s> for starting guesses\n"
115 " -a <n> : use optimization algorithm <n>\n"
116 " -o <n> : use objective function <n>\n"
117 " -e <n> : use at most <n> evals (default: %d)\n",
121 int main(int argc, char **argv)
125 srand((unsigned) time(NULL));
126 testfuncs_verbose = 0;
131 while ((c = getopt(argc, argv, "hvra:o:e:")) != -1)
137 testfuncs_verbose = 1;
140 srand((unsigned) atoi(optarg));
144 if (c < 0 || c >= NLOPT_NUM_ALGORITHMS) {
145 fprintf(stderr, "testopt: invalid algorithm %d\n", c);
148 algorithm = (nlopt_algorithm) c;
151 if (!test_function(atoi(optarg)))
155 maxeval = atoi(optarg);
158 fprintf(stderr, "harminv: invalid argument -%c\n", c);