From: Steven G. Johnson Date: Thu, 26 Jul 2018 15:03:32 +0000 (-0400) Subject: fix numevals counter in orig_direct X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=45375e316f66ea1734db590f6fae4a42ba5859af;p=nlopt.git fix numevals counter in orig_direct --- diff --git a/src/api/optimize.c b/src/api/optimize.c index 71614d2..88d71ee 100644 --- a/src/api/optimize.c +++ b/src/api/optimize.c @@ -86,6 +86,7 @@ static double f_direct(int n, const double *x, int *undefined, void *data_) double f; unsigned i, j; f = data->f((unsigned) n, x, NULL, data->f_data); + ++data->numevals; *undefined = nlopt_isnan(f) || nlopt_isinf(f); if (nlopt_get_force_stop(data)) return f; for (i = 0; i < data->m && !*undefined; ++i) { diff --git a/test/testopt.c b/test/testopt.c index 5625d85..07f9037 100644 --- a/test/testopt.c +++ b/test/testopt.c @@ -86,10 +86,10 @@ typedef struct { void *f_data; } bounds_wrap_data; -static double bounds_wrap_func(int n, const double *x, double *grad, void *d_) +static double bounds_wrap_func(unsigned n, const double *x, double *grad, void *d_) { bounds_wrap_data *d = (bounds_wrap_data *) d_; - int i; + unsigned i; double b = 0; for (i = 0; i < n; ++i) { if (x[i] < d->lb[i]) { @@ -102,13 +102,14 @@ static double bounds_wrap_func(int n, const double *x, double *grad, void *d_) } } if (i < n) - fprintf(stderr, "WARNING: bounds violated by x[%d] = %g = %g + %g\n", + fprintf(stderr, "WARNING: bounds violated by x[%u] = %g = %g + %g\n", i, x[i], b, x[i] - b); return d->f(n, x, grad, d->f_data); } static int test_function(int ifunc) { + nlopt_opt opt; testfunc func; int i, iter; double *x, minf, minf_max, f0, *xtabs, *lb, *ub; @@ -218,12 +219,18 @@ static int test_function(int ifunc) } testfuncs_counter = 0; - ret = nlopt_minimize(algorithm, - func.n, bounds_wrap_func, &bw, - lb, ub, - x, &minf, - minf_max, ftol_rel, ftol_abs, xtol_rel, xtabs, - maxeval, maxtime); + opt = nlopt_create(algorithm, func.n); + nlopt_set_min_objective(opt, bounds_wrap_func, &bw); + nlopt_set_lower_bounds(opt, lb); + nlopt_set_upper_bounds(opt, ub); + nlopt_set_stopval(opt, minf_max); + nlopt_set_ftol_rel(opt, ftol_rel); + nlopt_set_ftol_abs(opt, ftol_abs); + nlopt_set_xtol_rel(opt, xtol_rel); + nlopt_set_xtol_abs(opt, xtabs); + nlopt_set_maxeval(opt, maxeval); + nlopt_set_maxtime(opt, maxtime); + ret = nlopt_optimize(opt, x, &minf); printf("finished after %g seconds.\n", nlopt_seconds() - start); printf("return code %d from nlopt_minimize\n", ret); if (ret < 0 && ret != NLOPT_ROUNDOFF_LIMITED @@ -232,8 +239,9 @@ static int test_function(int ifunc) free(x); return 0; } - printf("Found minimum f = %g after %d evaluations.\n", - minf, testfuncs_counter); + printf("Found minimum f = %g after %d evaluations (numevals = %d).\n", + minf, testfuncs_counter, nlopt_get_numevals(opt)); + nlopt_destroy(opt); total_count += testfuncs_counter; if (testfuncs_counter > max_count) max_count = testfuncs_counter; if (testfuncs_counter < min_count) min_count = testfuncs_counter;