diff --git a/test/t_tutorial.cxx b/test/t_tutorial.cxx index f9a0ccc3..874853bd 100644 --- a/test/t_tutorial.cxx +++ b/test/t_tutorial.cxx @@ -5,6 +5,8 @@ #include #include +int count = 0; // counter for objective-function evaluations + double myvfunc(const std::vector &x, std::vector &grad, void *data) { (void)data; @@ -27,6 +29,7 @@ double myvconstraint(const std::vector &x, std::vector &grad, vo grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b); grad[1] = -1.0; } + ++count; return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]); } @@ -34,12 +37,21 @@ double myvconstraint(const std::vector &x, std::vector &grad, vo int main(int argc, char *argv[]) { nlopt::opt opt(argc < 2 ? nlopt::LD_MMA : (nlopt::algorithm)atoi(argv[1]), 2); const std::vector lb = {-HUGE_VAL, 1e-6}; + const double exactmin = 0.544331053951817355154952; // sqrt(8/27) opt.set_lower_bounds(lb); opt.set_min_objective(myvfunc, NULL); my_constraint_data data[2] = { {2,0}, {-1,1} }; opt.add_inequality_constraint(myvconstraint, &data[0], 1e-8); opt.add_inequality_constraint(myvconstraint, &data[1], 1e-8); - opt.set_xtol_rel(1e-4); + + // require convergence to within 1e-3 for user-specified algorithm + if (argc < 2) + opt.set_xtol_rel(1e-4); + else { + opt.set_lower_bounds(1e-6); + opt.set_upper_bounds(10.0); + opt.set_stopval(exactmin + 1e-3); + } // try setting an algorithm parameter: */ opt.set_param("inner_maxeval", 123); @@ -58,9 +70,10 @@ int main(int argc, char *argv[]) { try{ opt.optimize(x, minf); - std::cout << "found minimum at f(" << x[0] << "," << x[1] << ") = " - << std::setprecision(10) << minf <