chiark / gitweb /
Import nlopt_2.4.2+dfsg.orig.tar.gz
[nlopt.git] / util / stop.c
1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
2  *
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:
10  * 
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  * 
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. 
21  */
22
23 #include <math.h>
24 #include "nlopt-util.h"
25
26 /* utility routines to implement the various stopping criteria */
27
28 static int relstop(double vold, double vnew, double reltol, double abstol)
29 {
30      if (nlopt_isinf(vold)) return 0;
31      return(fabs(vnew - vold) < abstol 
32             || fabs(vnew - vold) < reltol * (fabs(vnew) + fabs(vold)) * 0.5
33             || (reltol > 0 && vnew == vold)); /* catch vnew == vold == 0 */
34 }
35
36 int nlopt_stop_ftol(const nlopt_stopping *s, double f, double oldf)
37 {
38      return (relstop(oldf, f, s->ftol_rel, s->ftol_abs));
39 }
40
41 int nlopt_stop_f(const nlopt_stopping *s, double f, double oldf)
42 {
43      return (f <= s->minf_max || nlopt_stop_ftol(s, f, oldf));
44 }
45
46 int nlopt_stop_x(const nlopt_stopping *s, const double *x, const double *oldx)
47 {
48      unsigned i;
49      for (i = 0; i < s->n; ++i)
50           if (!relstop(oldx[i], x[i], s->xtol_rel, s->xtol_abs[i]))
51                return 0;
52      return 1;
53 }
54
55 int nlopt_stop_dx(const nlopt_stopping *s, const double *x, const double *dx)
56 {
57      unsigned i;
58      for (i = 0; i < s->n; ++i)
59           if (!relstop(x[i] - dx[i], x[i], s->xtol_rel, s->xtol_abs[i]))
60                return 0;
61      return 1;
62 }
63
64 static double sc(double x, double smin, double smax)
65 {
66      return smin + x * (smax - smin);
67 }
68
69 /* some of the algorithms rescale x to a unit hypercube, so we need to
70    scale back before we can compare to the tolerances */
71 int nlopt_stop_xs(const nlopt_stopping *s,
72                   const double *xs, const double *oldxs,
73                   const double *scale_min, const double *scale_max)
74 {
75      unsigned i;
76      for (i = 0; i < s->n; ++i)
77           if (relstop(sc(oldxs[i], scale_min[i], scale_max[i]), 
78                       sc(xs[i], scale_min[i], scale_max[i]),
79                       s->xtol_rel, s->xtol_abs[i]))
80                return 1;
81      return 0;
82 }
83
84 int nlopt_stop_evals(const nlopt_stopping *s)
85 {
86      return (s->maxeval > 0 && s->nevals >= s->maxeval);
87 }
88
89 int nlopt_stop_time_(double start, double maxtime)
90 {
91      return (maxtime > 0 && nlopt_seconds() - start >= maxtime);
92 }
93
94 int nlopt_stop_time(const nlopt_stopping *s)
95 {
96      return nlopt_stop_time_(s->start, s->maxtime);
97 }
98
99 int nlopt_stop_evalstime(const nlopt_stopping *stop)
100 {
101      return nlopt_stop_evals(stop) || nlopt_stop_time(stop);
102 }
103
104 int nlopt_stop_forced(const nlopt_stopping *stop)
105 {
106      return stop->force_stop && *(stop->force_stop);
107 }
108
109 unsigned nlopt_count_constraints(unsigned p, const nlopt_constraint *c)
110 {
111      unsigned i, count = 0;
112      for (i = 0; i < p; ++i)
113           count += c[i].m;
114      return count;
115 }
116
117 unsigned nlopt_max_constraint_dim(unsigned p, const nlopt_constraint *c)
118 {
119      unsigned i, max_dim = 0;
120      for (i = 0; i < p; ++i)
121           if (c[i].m > max_dim)
122                max_dim = c[i].m;
123      return max_dim;
124 }
125
126 void nlopt_eval_constraint(double *result, double *grad,
127                            const nlopt_constraint *c,
128                            unsigned n, const double *x)
129 {
130      if (c->f)
131           result[0] = c->f(n, x, grad, c->f_data);
132      else
133           c->mf(c->m, result, n, x, grad, c->f_data);
134 }