提交 b71abf6b 编写于 作者: S stevenj

in Matlab/Octave interface, make returning NaN from the objective equivalent...

in Matlab/Octave interface, make returning NaN from the objective equivalent to an nlopt_force_stop; thanks to Norman Violet for the suggestion

Ignore-this: 7aacd84c708a986dce9f793c08c8ec65

darcs-hash:20120116144919-c8de0-a067fa3d0dab251b263603c122f1038b636c34ac.gz
上级 a062f036
......@@ -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,
......
......@@ -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(),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册