2 #include "nlopt-util.h"
4 /* utility routines to implement the various stopping criteria */
6 static int relstop(double old, double new, double reltol, double abstol)
8 return(fabs(new - old) < abstol
9 || fabs(new - old) < reltol * (fabs(new) + fabs(old)) * 0.5);
12 int nlopt_stop_ftol(const nlopt_stopping *s, const double f, double oldf)
14 return (relstop(oldf, f, s->ftol_rel, s->ftol_abs));
17 int nlopt_stop_f(const nlopt_stopping *s, const double f, double oldf)
19 return (f <= s->fmin_max || nlopt_stop_ftol(s, f, oldf));
22 int nlopt_stop_x(const nlopt_stopping *s, const double *x, const double *oldx)
25 for (i = 0; i < s->n; ++i)
26 if (relstop(oldx[i], x[i], s->xtol_rel, s->xtol_abs[i]))
31 static double sc(double x, double smin, double smax)
33 return smin + x * (smax - smin);
36 /* some of the algorithms rescale x to a unit hypercube, so we need to
37 scale back before we can compare to the tolerances */
38 int nlopt_stop_xs(const nlopt_stopping *s,
39 const double *xs, const double *oldxs,
40 const double *scale_min, const double *scale_max)
43 for (i = 0; i < s->n; ++i)
44 if (relstop(sc(oldxs[i], scale_min[i], scale_max[i]),
45 sc(xs[i], scale_min[i], scale_max[i]),
46 s->xtol_rel, s->xtol_abs[i]))
51 int nlopt_stop_evals(const nlopt_stopping *s)
53 return (s->maxeval > 0 && s->nevals >= s->maxeval);
56 int nlopt_stop_time(const nlopt_stopping *s)
58 return (s->maxtime > 0 && nlopt_seconds() - s->start >= s->maxtime);