1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "nlopt-util.h"
29 /* utility routines to implement the various stopping criteria */
31 static int relstop(double vold, double vnew, double reltol, double abstol)
33 if (nlopt_isinf(vold)) return 0;
34 return(fabs(vnew - vold) < abstol
35 || fabs(vnew - vold) < reltol * (fabs(vnew) + fabs(vold)) * 0.5
36 || (reltol > 0 && vnew == vold)); /* catch vnew == vold == 0 */
39 int nlopt_stop_ftol(const nlopt_stopping *s, double f, double oldf)
41 return (relstop(oldf, f, s->ftol_rel, s->ftol_abs));
44 int nlopt_stop_f(const nlopt_stopping *s, double f, double oldf)
46 return (f <= s->minf_max || nlopt_stop_ftol(s, f, oldf));
49 int nlopt_stop_x(const nlopt_stopping *s, const double *x, const double *oldx)
52 for (i = 0; i < s->n; ++i)
53 if (!relstop(oldx[i], x[i], s->xtol_rel, s->xtol_abs[i]))
58 int nlopt_stop_dx(const nlopt_stopping *s, const double *x, const double *dx)
61 for (i = 0; i < s->n; ++i)
62 if (!relstop(x[i] - dx[i], x[i], s->xtol_rel, s->xtol_abs[i]))
67 static double sc(double x, double smin, double smax)
69 return smin + x * (smax - smin);
72 /* some of the algorithms rescale x to a unit hypercube, so we need to
73 scale back before we can compare to the tolerances */
74 int nlopt_stop_xs(const nlopt_stopping *s,
75 const double *xs, const double *oldxs,
76 const double *scale_min, const double *scale_max)
79 for (i = 0; i < s->n; ++i)
80 if (relstop(sc(oldxs[i], scale_min[i], scale_max[i]),
81 sc(xs[i], scale_min[i], scale_max[i]),
82 s->xtol_rel, s->xtol_abs[i]))
87 int nlopt_stop_evals(const nlopt_stopping *s)
89 return (s->maxeval > 0 && s->nevals >= s->maxeval);
92 int nlopt_stop_time_(double start, double maxtime)
94 return (maxtime > 0 && nlopt_seconds() - start >= maxtime);
97 int nlopt_stop_time(const nlopt_stopping *s)
99 return nlopt_stop_time_(s->start, s->maxtime);
102 int nlopt_stop_evalstime(const nlopt_stopping *stop)
104 return nlopt_stop_evals(stop) || nlopt_stop_time(stop);
107 int nlopt_stop_forced(const nlopt_stopping *stop)
109 return stop->force_stop && *(stop->force_stop);
112 unsigned nlopt_count_constraints(unsigned p, const nlopt_constraint *c)
114 unsigned i, count = 0;
115 for (i = 0; i < p; ++i)
120 unsigned nlopt_max_constraint_dim(unsigned p, const nlopt_constraint *c)
122 unsigned i, max_dim = 0;
123 for (i = 0; i < p; ++i)
124 if (c[i].m > max_dim)
129 void nlopt_eval_constraint(double *result, double *grad,
130 const nlopt_constraint *c,
131 unsigned n, const double *x)
134 result[0] = c->f(n, x, grad, c->f_data);
136 c->mf(c->m, result, n, x, grad, c->f_data);
139 char *nlopt_vsprintf(char *p, const char *format, va_list ap)
141 size_t len = strlen(format) + 128;
144 p = (char *) realloc(p, len);
147 while ((ret = vsnprintf(p, len, format, ap)) < 0 || (size_t)ret >= len) {
148 /* C99 vsnprintf returns the required number of bytes (excluding \0)
149 if the buffer is too small; older versions (e.g. MS) return -1 */
150 len = ret >= 0 ? (size_t)(ret + 1) : (len*3)>>1;
151 p = (char *) realloc(p, len);
157 void nlopt_stop_msg(const nlopt_stopping *s, const char *format, ...)
161 va_start(ap, format);
162 *(s->stop_msg) = nlopt_vsprintf(*(s->stop_msg), format, ap);