// (Note: it is inefficient to use std::vector<double> for the arguments,
// since that would require a copy to be made of NLopt's double* data.)
+ //////////////////////////////////////////////////////////////////////
+
+ // NLopt-specific exceptions (corresponding to error codes):
+ class roundoff_limited : public std::runtime_error {
+ public:
+ roundoff_limited() : std::runtime_error("nlopt roundoff-limited") {}
+ };
+
+ class forced_stop : public std::runtime_error {
+ public:
+ forced_stop() : std::runtime_error("nlopt forced stop") {}
+ };
+
//////////////////////////////////////////////////////////////////////
class opt {
switch (ret) {
case NLOPT_FAILURE: throw std::runtime_error("nlopt failure");
case NLOPT_OUT_OF_MEMORY: throw std::bad_alloc();
- case NLOPT_INVALID_ARGS: throw std::invalid_argument("nlopt");
- case NLOPT_ROUNDOFF_LIMITED: throw std::runtime_error("nlopt roundoff");
+ case NLOPT_INVALID_ARGS: throw std::invalid_argument("nlopt invalid argument");
+ case NLOPT_ROUNDOFF_LIMITED: throw roundoff_limited();
+ case NLOPT_FORCED_STOP: throw forced_stop();
default: break;
}
}
.B NLOPT_ROUNDOFF_LIMITED
Halted because roundoff errors limited progress.
.TP
-.B NLOPT_FORCE_STOP
+.B NLOPT_FORCED_STOP
Halted because the user called \fBnlopt_force_stop\fR(\fIopt\fR) on
the optimization's \fBnlopt_opt\fR object \fIopt\fR from the user's
objective function.
NLOPT_INVALID_ARGS = -2,
NLOPT_OUT_OF_MEMORY = -3,
NLOPT_ROUNDOFF_LIMITED = -4,
- NLOPT_FORCE_STOP = -5,
+ NLOPT_FORCED_STOP = -5,
NLOPT_SUCCESS = 1, /* generic success code */
NLOPT_STOPVAL_REACHED = 2,
NLOPT_FTOL_REACHED = 3,
if (freedx) { free(opt->dx); opt->dx = NULL; }
switch (iret) {
case -2: return NLOPT_INVALID_ARGS;
- case -20: return NLOPT_FORCE_STOP;
+ case -20: return NLOPT_FORCED_STOP;
case -10: return NLOPT_MAXTIME_REACHED;
case -1: return NLOPT_MAXEVAL_REACHED;
case 0: return NLOPT_XTOL_REACHED;
if (ret != NLOPT_SUCCESS) break;
}
- if (nlopt_stop_forced(stop)) {ret = NLOPT_FORCE_STOP; break;}
+ if (nlopt_stop_forced(stop)) {ret = NLOPT_FORCED_STOP; break;}
if (nlopt_stop_evals(stop)) {ret = NLOPT_MAXEVAL_REACHED; break;}
if (nlopt_stop_time(stop)) {ret = NLOPT_MAXTIME_REACHED; break;}
goto L340;
}
- if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
if (f < fval[*kopt]) {
*kopt = kpt;
}
- if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
else if (f < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
else if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
temp = xpt[nf + ipt * xpt_dim1] * xpt[nf + jpt * xpt_dim1];
hq[ih] = (fbeg - fval[ipt + 1] - fval[jpt + 1] + f) / temp;
}
- if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
else if (f < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
else if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
/* L380: */
}
- if (nlopt_stop_forced(stop)) rc = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
if (rc != NLOPT_SUCCESS) goto L720;
p->stop->nevals++;
return f;
}
-#define FUNCTION_EVAL(fv,x,p,freeonerr) fv = function_eval(x, p); if (nlopt_stop_forced((p)->stop)) { free(freeonerr); return NLOPT_FORCE_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; }
+#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; }
#define THIRD (0.3333333333333333333333)
#*&!%*@ Fortran-66 spaghetti code */
L40:
- if (nlopt_stop_forced(stop)) rc = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
else 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;
memcpy(x, x0, sizeof(double) * n);
feasible = 1;
}
- if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
else if (*minf < stop->minf_max) ret = NLOPT_MINF_MAX_REACHED;
memcpy(x, xcur, sizeof(double) * n);
feasible = 1;
}
- if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop))
ret = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop))
do {
d->p[0] = d->f(n, d->p + 1, NULL, d->f_data);
d->stop->nevals++;
- if (nlopt_stop_forced(d->stop)) return NLOPT_FORCE_STOP;
+ 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;
if (nlopt_stop_time(d->stop)) return NLOPT_MAXTIME_REACHED;
if (ret != NLOPT_SUCCESS) goto done;
}
- if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
if (ret != NLOPT_SUCCESS) goto done;
case 4: return NLOPT_SUCCESS; /* gradient tolerance reached */
case 6: return NLOPT_SUCCESS;
case 12: case 13: return NLOPT_MAXEVAL_REACHED;
- case -999: return NLOPT_FORCE_STOP;
+ case -999: return NLOPT_FORCED_STOP;
default: return NLOPT_FAILURE;
}
}
case 4: return NLOPT_SUCCESS; /* gradient tolerance reached */
case 6: return NLOPT_SUCCESS;
case 12: case 13: return NLOPT_MAXEVAL_REACHED;
- case -999: return NLOPT_FORCE_STOP;
+ case -999: return NLOPT_FORCED_STOP;
default: return NLOPT_FAILURE;
}
}
case 4: return NLOPT_SUCCESS; /* gradient tolerance reached */
case 6: return NLOPT_SUCCESS;
case 12: case 13: return NLOPT_MAXEVAL_REACHED;
- case -999: return NLOPT_FORCE_STOP;
+ case -999: return NLOPT_FORCED_STOP;
default: return NLOPT_FAILURE;
}
}
if (!rb_tree_insert(&d.pts, (rb_key) p)) {
free(p); ret = NLOPT_OUT_OF_MEMORY;
}
- if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
else if (p->f < stop->minf_max) ret = NLOPT_MINF_MAX_REACHED;
if (!rb_tree_insert(&d.pts, (rb_key) p)) {
free(p); ret = NLOPT_OUT_OF_MEMORY;
}
- if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
else if (p->f < stop->minf_max) ret = NLOPT_MINF_MAX_REACHED;
double t = nlopt_seconds();
if (nlopt_stop_forced(stop)) {
- ret = NLOPT_FORCE_STOP; break;
+ ret = NLOPT_FORCED_STOP; break;
}
if (nlopt_stop_evals(stop)) {
ret = NLOPT_MAXEVAL_REACHED; break;
if (!rb_tree_insert(&d.lms, lm)) {
free(lm); ret = NLOPT_OUT_OF_MEMORY;
}
- else if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+ else if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (*lm < stop->minf_max)
ret = NLOPT_MINF_MAX_REACHED;
else if (nlopt_stop_evals(stop))
while (1) { /* outer iterations */
double fprev = fcur;
- if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
else if (feasible && *minf < stop->minf_max)
else if (new_infeasible_constraint) feasible = 0;
}
- if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
else if (feasible && *minf < stop->minf_max)
#define CHECK_EVAL(xc,fc) \
stop->nevals++; \
- if (nlopt_stop_forced(stop)) { ret=NLOPT_FORCE_STOP; goto done; } \
+ if (nlopt_stop_forced(stop)) { ret=NLOPT_FORCED_STOP; goto done; } \
if ((fc) <= *minf) { \
*minf = (fc); memcpy(x, (xc), n * sizeof(double)); \
if (*minf < stop->minf_max) { ret=NLOPT_MINF_MAX_REACHED; goto done; } \
*minf = f(n, x, NULL, f_data);
stop->nevals++;
- if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+ 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;
if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
*minf = f(n, x, NULL, f_data);
stop->nevals++;
- if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+ 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;
if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
}
++nf;
L310:
- if (nlopt_stop_forced(stop)) rc = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
else 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;
q_1->fbest = ret_val;
memcpy(q_1->xbest, t, n * sizeof(double));
}
- if (nlopt_stop_forced(stop)) *ret = NLOPT_FORCE_STOP;
+ if (nlopt_stop_forced(stop)) *ret = NLOPT_FORCED_STOP;
else if (nlopt_stop_evals(stop)) *ret = NLOPT_MAXEVAL_REACHED;
else if (nlopt_stop_time(stop)) *ret = NLOPT_MAXTIME_REACHED;
else if (ret_val <= stop->minf_max) *ret = NLOPT_MINF_MAX_REACHED;