chiark / gitweb /
in Matlab/Octave interface, make returning NaN from the objective equivalent to an...
authorstevenj <stevenj@alum.mit.edu>
Mon, 16 Jan 2012 14:49:19 +0000 (09:49 -0500)
committerstevenj <stevenj@alum.mit.edu>
Mon, 16 Jan 2012 14:49:19 +0000 (09:49 -0500)
Ignore-this: 7aacd84c708a986dce9f793c08c8ec65

darcs-hash:20120116144919-c8de0-a067fa3d0dab251b263603c122f1038b636c34ac.gz

octave/nlopt_optimize-mex.c
octave/nlopt_optimize-oct.cc

index b275f9c4568dda5d9421f1eacec1ee33bc014b12..85819fe003fbffd4f994e8c4709596b7b77e6016 100644 (file)
@@ -84,6 +84,7 @@ typedef struct user_function_data_s {
      int xrhs, nrhs;
      int verbose, neval;
      struct user_function_data_s *dpre;
+     nlopt_opt opt;
 } user_function_data;
 
 static double user_function(unsigned n, const double *x,
@@ -115,6 +116,7 @@ static double user_function(unsigned n, const double *x,
   }
   d->neval++;
   if (d->verbose) mexPrintf("nlopt_optimize eval #%d: %g\n", d->neval, f);
+  if (mxIsNaN(f)) nlopt_force_stop(d->opt);
   return f;
 }
 
@@ -222,6 +224,7 @@ void mexFunction(int nlhs, mxArray *plhs[],
 
      d.neval = 0;
      d.verbose = (int) struct_val_default(prhs[0], "verbose", 0);
+     d.opt = opt;
 
      /* function f = prhs[1] */
      mx = struct_funcval(prhs[0], "min_objective");
@@ -257,6 +260,7 @@ void mexFunction(int nlhs, mxArray *plhs[],
               dpre.xrhs = 1;
          }
          dpre.verbose = d.verbose > 2;
+         dpre.opt = opt;
          dpre.neval = 0;
          dpre.prhs[dpre.xrhs] = d.prhs[d.xrhs];
          dpre.prhs[d.xrhs+1] = mxCreateDoubleMatrix(1, n, mxREAL);
@@ -301,6 +305,7 @@ void mexFunction(int nlhs, mxArray *plhs[],
                    dfc[j].xrhs = 1;
               }
               dfc[j].verbose = d.verbose > 1;
+              dfc[j].opt = opt;
               dfc[j].neval = 0;
               dfc[j].prhs[dfc[j].xrhs] = d.prhs[d.xrhs];
               CHECK(nlopt_add_inequality_constraint(opt, user_function,
@@ -337,6 +342,7 @@ void mexFunction(int nlhs, mxArray *plhs[],
                    dh[j].xrhs = 1;
               }
               dh[j].verbose = d.verbose > 1;
+              dh[j].opt = opt;
               dh[j].neval = 0;
               dh[j].prhs[dh[j].xrhs] = d.prhs[d.xrhs];
               CHECK(nlopt_add_equality_constraint(opt, user_function,
index f40763c4001af385cb102e1e4edd34ec01ef4f7b..d30264e494bdd3fd5e164aa134b40ca09b041ca0 100644 (file)
@@ -66,6 +66,7 @@ static Matrix struct_val_default(Octave_map &m, const std::string& k,
 typedef struct {
   octave_function *f;
   int neval, verbose;
+  nlopt_opt opt;
 } user_function_data;
 
 static double user_function(unsigned n, const double *x,
@@ -98,7 +99,9 @@ static double user_function(unsigned n, const double *x,
     data->neval++;
     if (data->verbose) printf("nlopt_optimize eval #%d: %g\n", 
                              data->neval, res(0).double_value());
-    return res(0).double_value();
+    double f = res(0).double_value();
+    if (f != f /* isnan(f) */) nlopt_force_stop(data->opt);
+    return f;
   }
   return 0;
 }                               
@@ -226,6 +229,7 @@ DEFUN_DLD(nlopt_optimize, args, nargout, NLOPT_OPTIMIZE_USAGE)
   user_function_data d;
   d.neval = 0;
   d.verbose = struct_val_default(opts, "verbose", 0);
+  d.opt = opt;
   if (opts.contains("min_objective")) {
     CHECK(opts.contents("min_objective").length() == 1 
          && (opts.contents("min_objective"))(0).is_function_handle(),