F77_GETSET(xtol_rel, XTOL_REL, double)
F77_GETSETA(xtol_abs, XTOL_ABS, double)
F77_GETSET(maxeval, MAXEVAL, int)
+F77_GET(numevals, NUMEVALS, int)
F77_GETSET(maxtime, MAXTIME, double)
F77_GETSET(force_stop, FORCE_STOP, int)
NLOPT_GETSET(double, xtol_rel)
NLOPT_GETSET_VEC(xtol_abs)
NLOPT_GETSET(int, maxeval)
+
+ int get_numevals() const {
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt");
+ return nlopt_get_numevals(o);
+ }
+
NLOPT_GETSET(double, maxtime)
NLOPT_GETSET(int, force_stop)
double ftol_rel, ftol_abs; /* relative/absolute f tolerances */
double xtol_rel, *xtol_abs; /* rel/abs x tolerances */
int maxeval; /* max # evaluations */
+ int numevals; /* number of evaluations */
double maxtime; /* max time (seconds) */
int force_stop; /* if nonzero, force a halt the next time we
NLOPT_EXTERN(nlopt_result) nlopt_set_maxeval(nlopt_opt opt, int maxeval);
NLOPT_EXTERN(int) nlopt_get_maxeval(const nlopt_opt opt);
+NLOPT_EXTERN(int) nlopt_get_numevals(const nlopt_opt opt);
+
NLOPT_EXTERN(nlopt_result) nlopt_set_maxtime(nlopt_opt opt, double maxtime);
NLOPT_EXTERN(double) nlopt_get_maxtime(const nlopt_opt opt);
stop.ftol_abs = opt->ftol_abs;
stop.xtol_rel = opt->xtol_rel;
stop.xtol_abs = opt->xtol_abs;
- stop.nevals = 0;
+ stop.nevals_p = &(opt->numevals);
stop.maxeval = opt->maxeval;
stop.maxtime = opt->maxtime;
stop.start = nlopt_seconds();
opt->ftol_rel = opt->ftol_abs = 0;
opt->xtol_rel = 0; opt->xtol_abs = NULL;
opt->maxeval = 0;
+ opt->numevals = 0;
opt->maxtime = 0;
opt->force_stop = 0;
opt->force_stop_child = NULL;
GETSET(maxeval, int, maxeval)
+GETSET(numevals, int, numevals)
+
GETSET(maxtime, double, maxtime)
/*************************************************************************/
unsigned j, k;
L = d->f(n, x, grad, d->f_data);
- d->stop->nevals++;
+ ++ *(d->stop->nevals_p);
if (nlopt_stop_forced(d->stop)) return L;
for (ii = i = 0; i < d->p; ++i) {
/* starting rho suggested by B & M */
if (d.p > 0 || d.m > 0) {
double con2 = 0;
- d.stop->nevals++;
+ ++ *(d.stop->nevals_p);
fcur = f(n, xcur, NULL, f_data);
if (nlopt_stop_forced(stop)) {
ret = NLOPT_FORCED_STOP; goto done; }
double prev_ICM = ICM;
ret = nlopt_optimize_limited(sub_opt, xcur, &fcur,
- stop->maxeval - stop->nevals,
+ stop->maxeval - *(stop->nevals_p),
stop->maxtime - (nlopt_seconds()
- stop->start));
if (auglag_verbose)
printf("auglag: subopt return code %d\n", ret);
if (ret < 0) break;
- d.stop->nevals++;
+ ++ *(d.stop->nevals_p);
fcur = f(n, xcur, NULL, f_data);
if (nlopt_stop_forced(stop)) {
ret = NLOPT_FORCED_STOP; goto done; }
/* L290: */
}
- stop->nevals++;
+ ++ *(stop->nevals_p);
f = calfun(*n, &w[1], calfun_data);
fval[kpt] = f;
if (f < fval[*kopt]) {
}
/* L60: */
}
- stop->nevals++;
+ ++ *(stop->nevals_p);
f = calfun(*n, &x[1], calfun_data);
fval[nf] = f;
if (nf == 1) {
rho = *rhobeg;
delta = rho;
- nresc = stop->nevals;
+ nresc = *(stop->nevals_p);
ntrits = 0;
diffa = zero;
diffb = zero;
itest = 0;
- nfsav = stop->nevals;
+ nfsav = *(stop->nevals_p);
/* Update GOPT if necessary before the first iteration and after each */
/* call of RESCUE that makes a call of CALFUN. */
gopt[i__] += hq[ih] * xopt[j];
}
}
- if (stop->nevals > *npt) {
+ if (*(stop->nevals_p) > *npt) {
i__2 = *npt;
for (k = 1; k <= i__2; ++k) {
temp = zero;
/* Computing 2nd power */
d__1 = ten * rho;
distsq = d__1 * d__1;
- if (stop->nevals <= nfsav + 2) {
+ if (*(stop->nevals_p) <= nfsav + 2) {
goto L650;
}
/* useful safeguard, but is not invoked in most applications of BOBYQA. */
L190:
- nfsav = stop->nevals;
+ nfsav = *(stop->nevals_p);
kbase = kopt;
rc2 = rescue_(n, npt, &xl[1], &xu[1],
stop, calfun, calfun_data,
rc = rc2;
goto L720;
}
- nresc = stop->nevals;
- if (nfsav < stop->nevals) {
- nfsav = stop->nevals;
+ nresc = *(stop->nevals_p);
+ if (nfsav < *(stop->nevals_p)) {
+ nfsav = *(stop->nevals_p);
goto L20;
}
if (ntrits > 0) {
/* Computing 2nd power */
d__1 = vlag[knew];
if (denom <= half * (d__1 * d__1)) {
- if (stop->nevals > nresc) {
+ if (*(stop->nevals_p) > nresc) {
goto L190;
}
/* Return from BOBYQA because of much cancellation in a
;
}
if (scaden <= half * biglsq) {
- if (stop->nevals > nresc) {
+ if (*(stop->nevals_p) > nresc) {
goto L190;
}
/* Return from BOBYQA because of much cancellation in a
else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
if (rc != NLOPT_SUCCESS) goto L720;
- stop->nevals++;
+ ++ *(stop->nevals_p);
f = calfun(*n, &x[1], calfun_data);
if (ntrits == -1) {
fsave = f;
diffb = diffa;
diffa = fabs(diff);
if (dnorm > rho) {
- nfsav = stop->nevals;
+ nfsav = *(stop->nevals_p);
}
/* Pick the next value of DELTA after a trust region step. */
}
delta = MAX2(delta,rho);
ntrits = 0;
- nfsav = stop->nevals;
+ nfsav = *(stop->nevals_p);
goto L60;
}
p->minf = f;
memcpy(p->xmin, x, sizeof(double) * p->n);
}
- p->stop->nevals++;
+ ++ *(p->stop->nevals_p);
return f;
}
#define FUNCTION_EVAL(fv,x,p,freeonerr) fv = function_eval(x, p); if (nlopt_stop_forced((p)->stop)) { free(freeonerr); return NLOPT_FORCED_STOP; } else if (p->minf < p->stop->minf_max) { free(freeonerr); return NLOPT_MINF_MAX_REACHED; } else if (nlopt_stop_evals((p)->stop)) { free(freeonerr); return NLOPT_MAXEVAL_REACHED; } else if (nlopt_stop_time((p)->stop)) { free(freeonerr); return NLOPT_MAXTIME_REACHED; }
static double fcount(int n, const double *x, double *grad, void *p_)
{
params *p = (params *) p_;
- p->stop->nevals++;
+ ++ *(p->stop->nevals_p);
return p->f(n, x, grad, p->f_data);
}
nlopt_result ret;
if (stop->maxeval > 0 &&
- stop->nevals >= stop->maxeval) return NLOPT_MAXEVAL_REACHED;
+ *(stop->nevals_p) >= stop->maxeval) return NLOPT_MAXEVAL_REACHED;
if (stop->maxtime > 0 &&
t - stop->start >= stop->maxtime) return NLOPT_MAXTIME_REACHED;
stop->xtol_rel, stop->xtol_abs,
p->local_maxeval > 0 ?
MIN(p->local_maxeval,
- stop->maxeval - stop->nevals)
- : stop->maxeval - stop->nevals,
+ stop->maxeval - *(stop->nevals_p))
+ : stop->maxeval - *(stop->nevals_p),
stop->maxtime - (t - stop->start));
r[1] = -minf;
if (ret > 0) {
double wmax;
nlopt_result ret;
- /* printf("rect:, %d, %g, %g, %g, %g\n", p->stop->nevals, c[0], c[1], w[0], w[1]); */
+ /* printf("rect:, %d, %g, %g, %g, %g\n", p->stop->nevals_p, c[0], c[1], w[0], w[1]); */
/* check xtol */
for (i = 0; i < n; ++i)
* for the main calculation.
*/
- stop->nevals = 0;
+ *(stop->nevals_p) = 0;
if (n == 0)
{
L40:
if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
- else if (stop->nevals > 0) {
+ else if (*(stop->nevals_p) > 0) {
if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
}
if (rc != NLOPT_SUCCESS) goto L600;
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (calcfc(*n, *m, &x[1], &f, &con[1], state))
{
if (*iprint >= 1) {
goto L620; /* not L600 because we want to use current x, f, resmax */
}
- if (stop->nevals == *iprint - 1 || *iprint == 3) {
+ if (*(stop->nevals_p) == *iprint - 1 || *iprint == 3) {
fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
- stop->nevals, f, resmax);
+ *(stop->nevals_p), f, resmax);
i__1 = iptem;
fprintf(stderr, "cobyla: X =");
for (i__ = 1; i__ <= i__1; ++i__) {
for (k = 1; k <= i__1; ++k) {
datmat[k + jdrop * datmat_dim1] = con[k];
}
- if (stop->nevals > np) {
+ if (*(stop->nevals_p) > np) {
goto L130;
}
}
}
}
- if (stop->nevals <= *n) { /* evaluating initial simplex */
- jdrop = stop->nevals;
+ if (*(stop->nevals_p) <= *n) { /* evaluating initial simplex */
+ jdrop = *(stop->nevals_p);
/* SGJ: was += rho, but using sim[jdrop,jdrop] enforces consistency
if we change the stepsize above to stay in [lb,ub]. */
x[jdrop] += sim[jdrop + jdrop * sim_dim1];
}
if (*iprint == 2) {
fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
- stop->nevals, datmat[mp + np * datmat_dim1], datmat[*mpp + np * datmat_dim1]);
+ *(stop->nevals_p), datmat[mp + np * datmat_dim1], datmat[*mpp + np * datmat_dim1]);
fprintf(stderr, "cobyla: X =");
i__1 = iptem;
*minf = f;
if (*iprint >= 1) {
fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
- stop->nevals, f, resmax);
+ *(stop->nevals_p), f, resmax);
i__1 = iptem;
fprintf(stderr, "cobyla: X =");
for (i__ = 1; i__ <= i__1; ++i__) {
model.model.q0 = f(n, x0, NULL, f_data);
memcpy(X + (iM++ * n), x0, n * sizeof(double));
memset(model.model.Q, 0, sizeof(double) * n*n);
- stop->nevals++;
+ *(stop->nevals_p)++;
feasible_cur = 1;
for (i = 0; i < m; ++i) {
modelc[i].model.q0 = fc(n, x0, NULL, fc_data + fc_datum_size*i);
xcur[j] = x0[j] + (2*s - 1) * dx[j];
fmp[s] = fcur = f(n, xcur, NULL, f_data);
memcpy(X + (iM++ * n), xcur, n * sizeof(double));
- stop->nevals++;
+ *(stop->nevals_p)++;
feasible_cur = 1;
for (i = 0; i < m; ++i) {
fcmp[s][i] = fcval_cur[i] =
gcval[i] = cmodel_func(n, xcur, NULL, modelc + i);
fcur = f(n, xcur, NULL, f_data);
- stop->nevals++;
+ *(stop->nevals_p)++;
feasible_cur = 1;
inner_done = gval >= fcur;
for (i = 0; i < m; ++i) {
random_trial(d, d->p + 1, best);
do {
d->p[0] = d->f(n, d->p + 1, NULL, d->f_data);
- d->stop->nevals++;
+ ++ *(d->stop->nevals_p);
if (nlopt_stop_forced(d->stop)) return NLOPT_FORCED_STOP;
if (d->p[0] < worst->k[0]) break;
if (nlopt_stop_evals(d->stop)) return NLOPT_MAXEVAL_REACHED;
/* generate initial points randomly, plus starting guess x */
memcpy(d->ps + 1, x, sizeof(double) * n);
d->ps[0] = f(n, x, NULL, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (!rb_tree_insert(&d->t, d->ps)) return NLOPT_OUT_OF_MEMORY;
if (d->ps[0] < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
k[1 + j] = nlopt_urand(lb[j], ub[j]);
}
k[0] = f(n, k + 1, NULL, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (!rb_tree_insert(&d->t, k)) return NLOPT_OUT_OF_MEMORY;
if (k[0] < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
Stop when the optimization time (in seconds) exceeds `maxtime`.
+```
+int nlopt::opt::get_numevals() const;
+```
+
+
+Request the number of evaluations.
+
### Forced termination
In certain cases, the caller may wish to *force* the optimization to halt, for some reason unknown to NLopt. For example, if the user presses Ctrl-C, or there is an error of some sort in the objective function. You can do this by throwing *any* exception inside your objective/constraint functions: the exception will be caught, the optimization will be halted gracefully, and another exception (possibly not the same one) will be rethrown. See [Exceptions](#Exceptions.md), below. The C++ equivalent of `nlopt_forced_stop` from the [C API](NLopt_Reference#Forced_termination.md) is to throw an `nlopt::forced_stop` exception.
```
+
Stop when the number of function evaluations exceeds the `integer` `maxeval`. (Zero or negative for no limit.)
+```
+call nlo_get_numevals(nevals, opt)
+```
+
+Request the number of evaluations.
+
+
```
call nlo_set_maxtime(ires, opt, maxtime)
call nlo_get_maxtime(maxtime, opt)
Stop when the number of function evaluations exceeds `maxeval`. (0 or negative for no limit.)
+```
+(nlopt-opt-get-nevals opt)
+```
+
+
+Request the number of evaluations.
+
+
```
(nlopt-opt-set-maxtime opt maxtime)
(nlopt-opt-get-maxtime opt)
Stop when the optimization time (in seconds) exceeds `maxtime`. (0 or negative for no limit.)
+
+
+```
+opt.get_numevals()
+```
+
+
+Request the number of evaluations.
+
+
+
### Forced termination
In certain cases, the caller may wish to *force* the optimization to halt, for some reason unknown to NLopt. For example, if the user presses Ctrl-C, or there is an error of some sort in the objective function. You can do this by raise *any* exception inside your objective/constraint functions:the optimization will be halted gracefully, and the same exception will be raised to the caller. See [Exceptions](#Exceptions.md), below. The Python equivalent of `nlopt_forced_stop` from the [C API](NLopt_Reference#Forced_termination.md) is to throw an `nlopt.ForcedStop` exception.
Stop when the optimization time (in seconds) exceeds `maxtime`. (This is not a strict maximum: the time may exceed maxtime slightly, depending upon the algorithm and on how slow your function evaluation is.) Criterion is disabled if `maxtime` is non-positive.
+```
+int nlopt_get_numevals(nlopt_opt opt);
+```
+
+
+Request the number of evaluations.
+
+
### Forced termination
In certain cases, the caller may wish to *force* the optimization to halt, for some reason unknown to NLopt. For example, if the user presses Ctrl-C, or there is an error of some sort in the objective function. (This is used to implement exception handling in the NLopt wrappers for C++ and other languages.) In this case, it is possible to tell NLopt to halt the optimization gracefully, returning the best point found so far, by calling the following function from *within* your objective or constraint functions:
<small>TBA</small>
- Using CMake build system instead of autotools
+- Add int nlopt_get_numevals() function
NLopt 2.4.2
-----------
esparents[id].fitness =
f(nparameters, esparents[id].parameters, NULL, data_f);
estotal[id].fitness = esparents[id].fitness;
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (*minf > esparents[id].fitness) {
*minf = esparents[id].fitness;
memcpy(x, esparents[id].parameters,
/*esoffsprings[id].fitness = (double)fitness(esoffsprings[id].parameters, nparameters,fittype);*/
esoffsprings[id].fitness = f(nparameters, esoffsprings[id].parameters, NULL, data_f);
estotal[id+np].fitness = esoffsprings[id].fitness;
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (*minf > esoffsprings[id].fitness) {
*minf = esoffsprings[id].fitness;
memcpy(x, esoffsprings[id].parameters,
for (k = 0; k < population; ++k) {
int feasible = 1;
double gpenalty;
- stop->nevals++;
+ ++ *(stop->nevals_p);
fval[k] = f(n, xs + k*n, NULL, f_data);
if (nlopt_stop_forced(stop)) {
ret = NLOPT_FORCED_STOP; goto done; }
/* Common Block Declarations */
typedef struct {
int nres, ndec, nin, nit;
- /* int nfv; -- now stored in stop->nevals */
+ /* int nfv; -- now stored in stop->nevals_p */
int nfg, nfh;
} stat_common;
goto L11190;
}
*f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
- ++stop->nevals;
+ ++*(stop->nevals_p);
++stat_1->nfg;
if (nlopt_stop_time(stop)) { *iterm = 100; goto L11190; }
L11120:
luksan_mxudir__(nf, &r__, &s[1], &xo[1], &x[1], &ix[1], &kbf);
luksan_pcbs04__(nf, &x[1], &ix[1], &xl[1], &xu[1], &eps9, &kbf);
*f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
- ++stop->nevals;
+ ++*(stop->nevals_p);
++stat_1->nfg;
p = luksan_mxudot__(nf, &gf[1], &s[1], &ix[1], &kbf);
goto L11170;
goto L11190;
}
*f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
- ++stop->nevals;
+ ++*(stop->nevals_p);
++stat_1->nfg;
if (nlopt_stop_time(stop)) { *iterm = 100; goto L11190; }
L11120:
luksan_mxudir__(nf, &r__, &s[1], &xo[1], &x[1], &ix[1], &kbf);
luksan_pcbs04__(nf, &x[1], &ix[1], &xl[1], &xu[1], &eps9, &kbf);
*f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
- ++stop->nevals;
+ ++*(stop->nevals_p);
++stat_1->nfg;
p = luksan_mxudot__(nf, &gf[1], &s[1], &ix[1], &kbf);
goto L11170;
luksan_pyadc0__(nf, &n, &x[1], &ix[1], &xl[1], &xu[1], &inew);
}
*f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
- ++stop->nevals;
+ ++(*stop->nevals_p);
++stat_1->nfg;
if (nlopt_stop_time(stop)) { *iterm = 100; goto L11080; }
ld = kd;
ld = 0;
luksan_mxudir__(nf, &pp, &xs[1], &xo[1], &x[1], &ix[1], &kbf);
objgrad(*nf, &x[1], &gf[1], objgrad_data);
- ++stop->nevals;
+ ++*(stop->nevals_p);
++stat_1->nfg;
ld = kd;
luksan_mxvdif__(nf, &gf[1], &gn[1], &go[1]);
luksan_mxudir__(nf, &r__, &s[1], &xo[1], &x[1], &ix[1], &kbf);
luksan_pcbs04__(nf, &x[1], &ix[1], &xl[1], &xu[1], &eps9, &kbf);
*f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
- ++stop->nevals;
+ ++*(stop->nevals_p);
++stat_1->nfg;
ld = kd;
p = luksan_mxudot__(nf, &gf[1], &s[1], &ix[1], &kbf);
static double fcount(unsigned n, const double *x, double *grad, void *p_)
{
mlsl_data *p = (mlsl_data *) p_;
- p->stop->nevals++;
+ ++ *(p->stop->nevals_p);
return p->f(n, x, grad, p->f_data);
}
memcpy(p->x, x, n * sizeof(double));
p->f = f(n, x, NULL, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (!rb_tree_insert(&d.pts, (rb_key) p)) {
free(p); ret = NLOPT_OUT_OF_MEMORY;
}
for (j = 0; j < n; ++j) p->x[j] = nlopt_urand(lb[j],ub[j]);
}
p->f = f(n, p->x, NULL, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (!rb_tree_insert(&d.pts, (rb_key) p)) {
free(p); ret = NLOPT_OUT_OF_MEMORY;
}
if (!lm) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
memcpy(lm+1, p->x, sizeof(double) * n);
lret = nlopt_optimize_limited(local_opt, lm+1, lm,
- stop->maxeval - stop->nevals,
+ stop->maxeval - *(stop->nevals_p),
stop->maxtime -
(t - stop->start));
p->minimized = 1;
}
dd.fval = fcur = *minf = f(n, x, dfdx, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
memcpy(xcur, x, sizeof(double) * n);
if (nlopt_stop_forced(stop)) { ret = NLOPT_FORCED_STOP; goto done; }
}
fcur = f(n, xcur, dfdx_cur, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (nlopt_stop_forced(stop)) {
ret = NLOPT_FORCED_STOP; goto done; }
feasible_cur = 1; infeasibility_cur = 0;
}
dd.fval = fcur = *minf = f(n, x, dfdx, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
memcpy(xcur, x, sizeof(double) * n);
if (nlopt_stop_forced(stop)) { ret = NLOPT_FORCED_STOP; goto done; }
}
fcur = f(n, xcur, dfdx_cur, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (nlopt_stop_forced(stop)) {
ret = NLOPT_FORCED_STOP; goto done; }
feasible_cur = 1; infeasibility_cur = 0;
}
#define CHECK_EVAL(xc,fc) \
- stop->nevals++; \
+ ++ *(stop->nevals_p); \
if (nlopt_stop_forced(stop)) { ret=NLOPT_FORCED_STOP; goto done; } \
if ((fc) <= *minf) { \
*minf = (fc); memcpy(x, (xc), n * sizeof(double)); \
double *scratch, fdiff;
*minf = f(n, x, NULL, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
if (*minf < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
double fprev;
*minf = f(n, x, NULL, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
if (*minf < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
double normi = 0;
double normdx = 0;
int ns, nsubs = 0;
- int nevals = stop->nevals;
+ int nevals = *(stop->nevals_p);
double fdiff, fdiff_max = 0;
memcpy(xprev, x, n * sizeof(double));
ubs[k-i] = ub[p[k]];
}
++nsubs;
- nevals = stop->nevals;
+ nevals = *(stop->nevals_p);
ret = nldrmd_minimize_(ns, subspace_func, &sd, lbs,ubs,xs, minf,
xsstep, stop, psi, scratch, &fdiff);
if (fdiff > fdiff_max) fdiff_max = fdiff;
if (sbplx_verbose)
printf("%d NM iterations for (%d,%d) subspace\n",
- stop->nevals - nevals, sd.is, ns);
+ *(stop->nevals_p) - nevals, sd.is, ns);
for (k = i; k < i+ns; ++k) x[p[k]] = xs[k-i];
if (ret == NLOPT_FAILURE) { ret=NLOPT_XTOL_REACHED; goto done; }
if (ret != NLOPT_XTOL_REACHED) goto done;
ubs[i-sd.is] = ub[p[i]];
}
++nsubs;
- nevals = stop->nevals;
+ nevals = *(stop->nevals_p);
ret = nldrmd_minimize_(ns, subspace_func, &sd, lbs,ubs,xs, minf,
xsstep, stop, psi, scratch, &fdiff);
if (fdiff > fdiff_max) fdiff_max = fdiff;
if (sbplx_verbose)
printf("sbplx: %d NM iterations for (%d,%d) subspace\n",
- stop->nevals - nevals, sd.is, ns);
+ *(stop->nevals_p) - nevals, sd.is, ns);
for (i = sd.is; i < n; ++i) x[p[i]] = xs[i-sd.is];
if (ret == NLOPT_FAILURE) { ret=NLOPT_XTOL_REACHED; goto done; }
if (ret != NLOPT_XTOL_REACHED) goto done;
++nf;
L310:
if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
- else if (stop->nevals > 0) {
+ else if (*(stop->nevals_p) > 0) {
if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
}
if (rc != NLOPT_SUCCESS) goto L530;
- stop->nevals++;
+ ++ *(stop->nevals_p);
f = calfun(*n, &x[1], calfun_data);
if (f < stop->minf_max) {
rc = NLOPT_MINF_MAX_REACHED;
prev_fbest = q_1.fbest = global_1.fx = f(n, &x[1], f_data);
memcpy(q_1.xbest, &x[1], n*sizeof(double));
memcpy(prev_xbest, &x[1], n*sizeof(double));
- stop->nevals++;
+ ++ *(stop->nevals_p);
q_1.stop = stop;
q_1.qf1 = global_1.fx;
if (t0 > 0)
L4:
++(*nf);
ret_val = f(n, t, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (ret_val < q_1->fbest) {
q_1->fbest = ret_val;
memcpy(q_1->xbest, t, n * sizeof(double));
}
feasible_cur = 1; infeasibility_cur = 0;
fcur = f(n, xcur, newgrad, f_data);
- stop->nevals++;
+ ++ *(stop->nevals_p);
if (nlopt_stop_forced(stop)) {
fcur = HUGE_VAL; ret = NLOPT_FORCED_STOP; goto done; }
if (nlopt_isfinite(fcur)) {
#include "tools.h"
#ifdef NLOPT_UTIL_H
-# define IF_NLOPT_CHECK_EVALS stop->nevals++; \
+# define IF_NLOPT_CHECK_EVALS ++ *(stop->nevals_p); \
if (nlopt_stop_evalstime(stop)) \
return LS_MaxEvalTime
#else
grad?OBJECTIVE_AND_GRADIENT:OBJECTIVE_ONLY);
if (grad) data->maxgrad = max(data->maxgrad, normInf(gv));
xv.elements = gv.elements = 0; // prevent deallocation
- data->stop->nevals++;
+ ++ *(data->stop->nevals_p);
return f;
}
#endif
stop->minf_max,
stop->ftol_rel, stop->ftol_abs,
stop->xtol_rel, stop->xtol_abs,
- stop->maxeval - stop->nevals,
+ stop->maxeval - *(stop->nevals_p),
stop->maxtime - stop->start);
*mgr = data.maxgrad;
T.xvals=x ; T.objval=f ;
if (usubc_1.irepl > 0) {
isubc_1.new__ = FALSE_;
evalf_((D_fp)f,fdata, ns, &ips[1], &s[s_dim1 + 1], n, &x[1], &fs[1], nfe);
- stop->nevals++;
+ *(stop->nevals_p)++;
} else {
fs[1] = *fx;
}
for (j = 2; j <= i__1; ++j) {
evalf_((D_fp)f, fdata,ns, &ips[1], &s[j * s_dim1 + 1], n, &x[1], &fs[j],
nfe);
- stop->nevals++;
+ *(stop->nevals_p)++;
/* L10: */
}
il = 1;
goto L40;
}
evalf_((D_fp)f,fdata, ns, &ips[1], &s[itemp * s_dim1 + 1], n, &x[1], &fr, nfe);
- stop->nevals++;
+ *(stop->nevals_p)++;
if (fr < fs[il]) {
/* expand */
goto L40;
}
evalf_((D_fp)f,fdata, ns, &ips[1], &s[ih * s_dim1 + 1], n, &x[1], &fe, nfe);
- stop->nevals++;
+ *(stop->nevals_p)++;
if (fe < fr) {
fs[ih] = fe;
} else {
}
evalf_((D_fp)f,fdata, ns, &ips[1], &s[itemp * s_dim1 + 1], n, &x[1], &fc,
nfe);
- stop->nevals++;
+ *(stop->nevals_p)++;
/* Computing MIN */
d__1 = fr, d__2 = fs[ih];
if (fc < MIN2(d__1,d__2)) {
}
evalf_((D_fp)f,fdata, ns, &ips[1], &s[j * s_dim1 + 1], n, &x[1],
&fs[j], nfe);
- stop->nevals++;
+ *(stop->nevals_p)++;
}
/* L30: */
}
isubc_1.new__ = TRUE_;
usubc_1.initx = TRUE_;
evalf_((D_fp)f, fdata, &c__0, &iwork[1], &dum, n, &x[1], &sfx, nfe);
- stop->nevals++;
+ *(stop->nevals_p)++;
usubc_1.initx = FALSE_;
} else {
opt = nlopt.opt(nlopt.LD_MMA, 2)
opt.set_lower_bounds([-float('inf'), 0])
opt.set_min_objective(myfunc)
-opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,2,0), 1e-8)
-opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,-1,1), 1e-8)
+opt.add_inequality_constraint(lambda x, grad: myconstraint(x,grad, 2, 0), 1e-8)
+opt.add_inequality_constraint(lambda x, grad: myconstraint(x,grad, -1, 1), 1e-8)
opt.set_xtol_rel(1e-4)
x = opt.optimize([1.234, 5.678])
minf = opt.last_optimum_value()
-print('optimum at ', x[0],x[1])
+print('optimum at ', x[0], x[1])
print('minimum value = ', minf)
print('result code = ', opt.last_optimize_result())
+print('nevals = ', opt.get_numevals())
double ftol_abs;
double xtol_rel;
const double *xtol_abs;
- int nevals, maxeval;
+ int *nevals_p, maxeval;
double maxtime, start;
int *force_stop;
char **stop_msg; /* pointer to msg string to update */
int nlopt_stop_evals(const nlopt_stopping *s)
{
- return (s->maxeval > 0 && s->nevals >= s->maxeval);
+ return (s->maxeval > 0 && *(s->nevals_p) >= s->maxeval);
}
int nlopt_stop_time_(double start, double maxtime)