chiark / gitweb /
use memcpy, return inf on exception from objective
authorstevenj <stevenj@alum.mit.edu>
Wed, 2 Jun 2010 17:23:33 +0000 (13:23 -0400)
committerstevenj <stevenj@alum.mit.edu>
Wed, 2 Jun 2010 17:23:33 +0000 (13:23 -0400)
darcs-hash:20100602172333-c8de0-6b496612c4caef9edcf942e4a6397431223173af.gz

api/nlopt-in.hpp

index 9eb1825c45cc6e6cd661eb7caaa5feb2ad4c40a5..ae8e25ce4d3ad60b18c5a85bb7d541db3bcf2e64 100644 (file)
@@ -29,6 +29,8 @@
 #include <stdexcept>
 #include <new>
 #include <cstdlib>
+#include <cstring>
+#include <cmath>
 
 // 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<myfunc_data*>(d_);
       try {
        std::vector<double> &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<double> &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;
       }
     }