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