chiark / gitweb /
ca33df4b9fb3831f9aa17f52936e86c772422728
[nlopt.git] / util / nlopt-util.h
1 /* Copyright (c) 2007-2011 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 #ifndef NLOPT_UTIL_H
24 #define NLOPT_UTIL_H
25
26 #include <stdlib.h>
27 #include <math.h>
28 #include "config.h"
29
30 #include "nlopt.h"
31
32 /* workaround for Solaris + gcc 3.4.x bug (see configure.ac) */
33 #if defined(__GNUC__) && defined(REPLACEMENT_HUGE_VAL)
34 #  undef HUGE_VAL
35 #  define HUGE_VAL REPLACEMENT_HUGE_VAL
36 #endif
37
38 #ifndef HAVE_COPYSIGN
39    /* not quite right for y == -0, but good enough for us */
40 #  define copysign(x, y) ((y) < 0 ? -fabs(x) : fabs(x))
41 #endif
42
43 #ifdef __cplusplus
44 extern "C"
45 {
46 #endif /* __cplusplus */
47
48 int nlopt_isinf(double x);
49
50 /* re-entrant qsort */
51 extern void nlopt_qsort_r(void *base_, size_t nmemb, size_t size, void *thunk,
52                           int (*compar)(void *, const void *, const void *));
53
54 /* seconds timer */
55 extern double nlopt_seconds(void);
56 extern unsigned long nlopt_time_seed(void);
57
58 /* pseudorandom number generation by Mersenne twister algorithm */
59 extern void nlopt_init_genrand(unsigned long s);
60 extern double nlopt_urand(double a, double b);
61 extern int nlopt_iurand(int n);
62 extern double nlopt_nrand(double mean, double stddev);
63
64 /* Sobol' low-discrepancy-sequence generation */
65 typedef struct nlopt_soboldata_s *nlopt_sobol;
66 extern nlopt_sobol nlopt_sobol_create(unsigned sdim);
67 extern void nlopt_sobol_destroy(nlopt_sobol s);
68 extern void nlopt_sobol_next01(nlopt_sobol s, double *x);
69 extern void nlopt_sobol_next(nlopt_sobol s, double *x,
70                             const double *lb, const double *ub);
71 extern void nlopt_sobol_skip(nlopt_sobol s, unsigned n, double *x);
72
73 /* stopping criteria */
74 typedef struct {
75      unsigned n;
76      double minf_max;
77      double ftol_rel;
78      double ftol_abs;
79      double xtol_rel;
80      const double *xtol_abs;
81      int nevals, maxeval;
82      double maxtime, start;
83      int *force_stop;
84 } nlopt_stopping;
85 extern int nlopt_stop_f(const nlopt_stopping *stop, double f, double oldf);
86 extern int nlopt_stop_ftol(const nlopt_stopping *stop, double f, double oldf);
87 extern int nlopt_stop_x(const nlopt_stopping *stop, 
88                         const double *x, const double *oldx);
89 extern int nlopt_stop_dx(const nlopt_stopping *stop, 
90                          const double *x, const double *dx);
91 extern int nlopt_stop_xs(const nlopt_stopping *stop, 
92                          const double *xs, const double *oldxs,
93                          const double *scale_min, const double *scale_max);
94 extern int nlopt_stop_evals(const nlopt_stopping *stop);
95 extern int nlopt_stop_time_(double start, double maxtime);
96 extern int nlopt_stop_time(const nlopt_stopping *stop);
97 extern int nlopt_stop_evalstime(const nlopt_stopping *stop);
98 extern int nlopt_stop_forced(const nlopt_stopping *stop);
99
100 /* for local optimizations, temporarily setting eval/time limits */
101 extern nlopt_result nlopt_optimize_limited(nlopt_opt opt, 
102                                            double *x, double *minf,
103                                            int maxevals, double maxtime);
104
105 /* data structure for nonlinear inequality or equality constraint
106    (f <= 0 or f = 0, respectively).  tol (>= 0) is a tolerance
107    that is used for stopping criteria -- the point is considered
108    "feasible" for purposes of stopping if the constraint is violated
109    by at most tol. */
110 typedef struct {
111      unsigned m; /* dimensional of constraint: mf maps R^n -> R^m */
112      nlopt_func f; /* one-dimensional constraint, requires m == 1 */
113      nlopt_mfunc mf;
114      nlopt_precond pre; /* preconditioner for f (NULL if none or if mf) */
115      void *f_data;
116      double *tol;
117 } nlopt_constraint;
118
119 extern unsigned nlopt_count_constraints(unsigned p, const nlopt_constraint *c);
120 extern unsigned nlopt_max_constraint_dim(unsigned p, const nlopt_constraint *c);
121 extern void nlopt_eval_constraint(double *result, double *grad,
122                                   const nlopt_constraint *c,
123                                   unsigned n, const double *x);
124
125 /* rescale.c: */
126 double *nlopt_compute_rescaling(unsigned n, const double *dx);
127 double *nlopt_new_rescaled(unsigned n, const double *s, const double *x);
128 void nlopt_rescale(unsigned n, const double *s, const double *x, double *xs);
129 void nlopt_unscale(unsigned n, const double *s, const double *x, double *xs);
130
131 #ifdef __cplusplus
132 }  /* extern "C" */
133 #endif /* __cplusplus */
134
135 #endif