From: stevenj Date: Wed, 2 Jun 2010 17:23:33 +0000 (-0400) Subject: use memcpy, return inf on exception from objective X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=79c7914c66863262ec917dc77258448acefd3bfa;p=nlopt.git use memcpy, return inf on exception from objective darcs-hash:20100602172333-c8de0-6b496612c4caef9edcf942e4a6397431223173af.gz --- diff --git a/api/nlopt-in.hpp b/api/nlopt-in.hpp index 9eb1825..ae8e25c 100644 --- a/api/nlopt-in.hpp +++ b/api/nlopt-in.hpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include // convenience overloading for below (not in nlopt:: since has nlopt_ prefix) inline nlopt_result nlopt_get_initial_step(const nlopt_opt opt, double *dx) { @@ -96,6 +98,7 @@ namespace nlopt { catch (...) { d->o->stopped_by_exception = true; d->o->force_stop(); // stop gracefully, opt::optimize will re-throw + return HUGE_VAL; } } @@ -106,17 +109,18 @@ namespace nlopt { myfunc_data *d = reinterpret_cast(d_); try { std::vector &xv = d->o->xtmp; - for (unsigned i = 0; i < n; ++i) xv[i] = x[i]; + if (n) std::memcpy(&xv[0], x, n * sizeof(double)); double val=d->vf(xv, grad ? d->o->gradtmp : d->o->gradtmp0, d->f_data); - if (grad) { + if (grad && n) { std::vector &gradv = d->o->gradtmp; - for (unsigned i = 0; i < n; ++i) grad[i] = gradv[i]; + std::memcpy(grad, &gradv[0], n * sizeof(double)); } return val; } catch (...) { d->o->stopped_by_exception = true; d->o->force_stop(); // stop gracefully, opt::optimize will re-throw + return HUGE_VAL; } }