From cffc91db9aa2f7959ea16c847fa9cae699822476 Mon Sep 17 00:00:00 2001 From: stevenj Date: Tue, 4 Sep 2007 12:32:36 -0400 Subject: [PATCH] added -C option to force contraints to be active darcs-hash:20070904163236-c8de0-33897b41afe8d8fd4520b02c19100efeb3193d64.gz --- test/testopt.cpp | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/test/testopt.cpp b/test/testopt.cpp index 63e0fe4..a58ca00 100644 --- a/test/testopt.cpp +++ b/test/testopt.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "config.h" @@ -20,6 +21,7 @@ static double ftol_rel = 0, ftol_abs = 0, xtol_rel = 0, xtol_abs = 0, minf_max_d static int maxeval = 1000, iterations = 1, center_start = 0; static double maxtime = 0.0; static double xinit_tol = -1; +static int force_constraints = 0; static void listalgs(FILE *f) { @@ -41,7 +43,7 @@ static int test_function(int ifunc) { testfunc func; int i, iter; - double *x, minf, minf_max, f0, *xtabs; + double *x, minf, minf_max, f0, *xtabs, *lb, *ub; nlopt_result ret; double start = nlopt_seconds(); @@ -51,10 +53,13 @@ static int test_function(int ifunc) return 0; } func = testfuncs[ifunc]; - x = (double *) malloc(sizeof(double) * func.n * 3); + x = (double *) malloc(sizeof(double) * func.n * 5); if (!x) { fprintf(stderr, "testopt: Out of memory!\n"); return 0; } + lb = x + func.n * 3; + ub = lb + func.n; xtabs = x + func.n * 2; + for (i = 0; i < func.n; ++i) xtabs[i] = xtol_abs; minf_max = minf_max_delta > (-HUGE_VAL) ? minf_max_delta + func.minf : (-HUGE_VAL); @@ -67,22 +72,38 @@ static int test_function(int ifunc) printf("upper bounds at ub = ["); for (i = 0; i < func.n; ++i) printf(" %g", func.ub[i]); printf("]\n"); + memcpy(lb, func.lb, func.n * sizeof(double)); + memcpy(ub, func.ub, func.n * sizeof(double)); + if (force_constraints) { + for (i = 0; i < func.n; ++i) { + if (nlopt_iurand(2) == 0) + ub[i] = nlopt_urand(lb[i], func.xmin[i]); + else + lb[i] = nlopt_urand(func.xmin[i], ub[i]); + } + printf("adjusted lower bounds at lb = ["); + for (i = 0; i < func.n; ++i) printf(" %g", lb[i]); + printf("]\n"); + printf("adjusted upper bounds at ub = ["); + for (i = 0; i < func.n; ++i) printf(" %g", ub[i]); + printf("]\n"); + } printf("Starting guess x = ["); for (i = 0; i < func.n; ++i) { if (center_start) - x[i] = (func.ub[i] + func.lb[i]) * 0.5; + x[i] = (ub[i] + lb[i]) * 0.5; else if (xinit_tol < 0) { /* random starting point near center of box */ - double dx = (func.ub[i] - func.lb[i]) * 0.25; - double xm = 0.5 * (func.ub[i] + func.lb[i]); + double dx = (ub[i] - lb[i]) * 0.25; + double xm = 0.5 * (ub[i] + lb[i]); x[i] = nlopt_urand(xm - dx, xm + dx); } else { x[i] = nlopt_urand(-xinit_tol, xinit_tol) + (1 + nlopt_urand(-xinit_tol, xinit_tol)) * func.xmin[i]; - if (x[i] > func.ub[i]) x[i] = func.ub[i]; - else if (x[i] < func.lb[i]) x[i] = func.lb[i]; + if (x[i] > ub[i]) x[i] = ub[i]; + else if (x[i] < lb[i]) x[i] = lb[i]; } printf(" %g", x[i]); } @@ -106,7 +127,7 @@ static int test_function(int ifunc) testfuncs_counter = 0; ret = nlopt_minimize(algorithm, func.n, func.f, func.f_data, - func.lb, func.ub, + lb, ub, x, &minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtabs, maxeval, maxtime); @@ -143,6 +164,7 @@ static void usage(FILE *f) " -o : use objective function \n" " -0 : starting guess within + (1+) * optimum\n" " -c : starting guess at center of cell\n" + " -C : put optimum outside of bound constraints\n" " -e : use at most evals (default: %d, 0 to disable)\n" " -t : use at most seconds (default: disabled)\n" " -x : relative tolerance on x (default: disabled)\n" @@ -165,7 +187,7 @@ int main(int argc, char **argv) if (argc <= 1) usage(stdout); - while ((c = getopt(argc, argv, "hLvc0:r:a:o:i:e:t:x:X:f:F:m:")) != -1) + while ((c = getopt(argc, argv, "hLvCc0:r:a:o:i:e:t:x:X:f:F:m:")) != -1) switch (c) { case 'h': usage(stdout); @@ -177,6 +199,9 @@ int main(int argc, char **argv) case 'v': testfuncs_verbose = 1; break; + case 'C': + force_constraints = 1; + break; case 'r': nlopt_srand((unsigned long) atoi(optarg)); break; -- 2.30.2