nlopt.c 6.2 KB
Newer Older
S
stevenj 已提交
1 2 3 4
#include <stdlib.h>
#include <math.h>

#include "nlopt.h"
5
#include "nlopt-util.h"
S
stevenj 已提交
6 7
#include "config.h"

S
stevenj 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#define MIN(a,b) ((a) < (b) ? (a) : (b))

/*************************************************************************/

#ifdef INFINITY
#  define MY_INF INFINITY
#else
#  define MY_INF HUGE_VAL
#endif

static int my_isinf(double x) {
     return fabs(x) >= HUGE_VAL * 0.99
#ifdef HAVE_ISINF
	  || isinf(x)
#endif
	  ;
}

#ifndef HAVE_ISNAN
static int my_isnan(double x) { return x != x; }
#  define isnan my_isnan
#endif

/*************************************************************************/
32

S
stevenj 已提交
33 34 35 36 37 38 39
void nlopt_version(int *major, int *minor, int *bugfix)
{
     *major = MAJOR_VERSION;
     *minor = MINOR_VERSION;
     *bugfix = BUGFIX_VERSION;
}

S
stevenj 已提交
40 41 42 43 44 45 46 47 48 49
/*************************************************************************/

static const char nlopt_algorithm_names[NLOPT_NUM_ALGORITHMS][128] = {
     "DIRECT (global)",
     "DIRECT-L (global)",
     "Subplex (local)",
     "StoGO (global)",
     "Low-storage BFGS (LBFGS) (local)"
};

50 51 52 53 54 55
const char *nlopt_algorithm_name(nlopt_algorithm a)
{
     if (a < 0 || a >= NLOPT_NUM_ALGORITHMS) return "UNKNOWN";
     return nlopt_algorithm_names[a];
}

S
stevenj 已提交
56 57
/*************************************************************************/

58 59 60 61 62 63 64 65 66 67
static int nlopt_srand_called = 0;
void nlopt_srand(unsigned long seed) {
     nlopt_srand_called = 1;
     nlopt_init_genrand(seed);
}

void nlopt_srand_time() {
     nlopt_srand(nlopt_time_seed());
}

S
stevenj 已提交
68
/*************************************************************************/
S
stevenj 已提交
69 70 71 72

typedef struct {
     nlopt_func f;
     void *f_data;
73
     const double *lb, *ub;
S
stevenj 已提交
74 75 76 77
} nlopt_data;

#include "subplex.h"

S
stevenj 已提交
78
static double f_subplex(int n, const double *x, void *data_)
S
stevenj 已提交
79
{
80
     int i;
S
stevenj 已提交
81
     nlopt_data *data = (nlopt_data *) data_;
S
stevenj 已提交
82
     double f;
83 84 85 86 87

     /* subplex does not support bound constraints, but it supports
	discontinuous objectives so we can just return Inf for invalid x */
     for (i = 0; i < n; ++i)
	  if (x[i] < data->lb[i] || x[i] > data->ub[i])
S
stevenj 已提交
88
	       return MY_INF;
89

S
stevenj 已提交
90 91
     f = data->f(n, x, NULL, data->f_data);
     return (isnan(f) ? MY_INF : f);
S
stevenj 已提交
92 93 94 95
}

#include "direct.h"

S
stevenj 已提交
96
static double f_direct(int n, const double *x, int *undefined, void *data_)
S
stevenj 已提交
97 98
{
     nlopt_data *data = (nlopt_data *) data_;
S
stevenj 已提交
99 100
     double f = data->f(n, x, NULL, data->f_data);
     *undefined = isnan(f) || my_isinf(f);
S
stevenj 已提交
101 102 103
     return f;
}

S
stevenj 已提交
104 105 106
#include "stogo.h"
#include "l-bfgs-b.h"

S
stevenj 已提交
107
/*************************************************************************/
S
stevenj 已提交
108 109

nlopt_result nlopt_minimize(
110
     nlopt_algorithm algorithm,
S
stevenj 已提交
111 112 113 114
     int n, nlopt_func f, void *f_data,
     const double *lb, const double *ub, /* bounds */
     double *x, /* in: initial guess, out: minimizer */
     double *fmin, /* out: minimum */
S
stevenj 已提交
115
     double fmin_max, double ftol_rel, double ftol_abs,
S
stevenj 已提交
116 117 118
     double xtol_rel, const double *xtol_abs,
     int maxeval, double maxtime)
{
119
     int i;
S
stevenj 已提交
120 121 122
     nlopt_data d;
     d.f = f;
     d.f_data = f_data;
123 124 125
     d.lb = lb;
     d.ub = ub;

126 127 128 129
     /* make sure rand generator is inited */
     if (!nlopt_srand_called)
	  nlopt_srand_time(); /* default is non-deterministic */

130 131 132 133
     /* check bound constraints */
     for (i = 0; i < n; ++i)
	  if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i])
	       return NLOPT_INVALID_ARGS;
S
stevenj 已提交
134

135
     switch (algorithm) {
S
stevenj 已提交
136
	 case NLOPT_GLOBAL_DIRECT:
137
	 case NLOPT_GLOBAL_DIRECT_L:
S
stevenj 已提交
138 139 140 141
	      switch (direct_optimize(f_direct, &d, n, lb, ub, x, fmin,
				      maxeval, 500, ftol_rel, ftol_abs,
				      xtol_rel, xtol_rel,
				      DIRECT_UNKNOWN_FGLOBAL, -1.0,
142 143 144 145
				      NULL, 
				      algorithm == NLOPT_GLOBAL_DIRECT
				      ? DIRECT_ORIGINAL
				      : DIRECT_GABLONSKY)) {
S
stevenj 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		  case DIRECT_INVALID_BOUNDS:
		  case DIRECT_MAXFEVAL_TOOBIG:
		  case DIRECT_INVALID_ARGS:
		       return NLOPT_INVALID_ARGS;
		  case DIRECT_INIT_FAILED:
		  case DIRECT_SAMPLEPOINTS_FAILED:
		  case DIRECT_SAMPLE_FAILED:
		       return NLOPT_FAILURE;
		  case DIRECT_MAXFEVAL_EXCEEDED:
		  case DIRECT_MAXITER_EXCEEDED:
		       return NLOPT_MAXEVAL_REACHED;
		  case DIRECT_GLOBAL_FOUND:
		       return NLOPT_SUCCESS;
		  case DIRECT_VOLTOL:
		  case DIRECT_SIGMATOL:
		       return NLOPT_XTOL_REACHED;
		  case DIRECT_OUT_OF_MEMORY:
		       return NLOPT_OUT_OF_MEMORY;
S
stevenj 已提交
164 165 166 167 168 169 170 171 172 173
	      }
	      break;

	 case NLOPT_GLOBAL_STOGO:
	      if (!stogo_minimize(n, f, f_data, x, fmin, lb, ub,
				  maxeval, maxtime))
		   return NLOPT_FAILURE;
	      break;

	 case NLOPT_LOCAL_SUBPLEX: {
174
	      int iret;
S
stevenj 已提交
175 176
	      double *scale = (double *) malloc(sizeof(double) * n);
	      if (!scale) return NLOPT_OUT_OF_MEMORY;
177 178 179 180 181 182 183 184 185 186
	      for (i = 0; i < n; ++i) {
		   if (!my_isinf(ub[i]) && !my_isinf(lb[i]))
			scale[i] = (ub[i] - lb[i]) * 0.01;
		   else if (!my_isinf(lb[i]) && x[i] > lb[i])
			scale[i] = (x[i] - lb[i]) * 0.01;
		   else if (!my_isinf(ub[i]) && x[i] < ub[i])
			scale[i] = (ub[i] - x[i]) * 0.01;
		   else
			scale[i] = 0.01 * x[i] + 0.0001;
	      }
S
stevenj 已提交
187
	      iret = subplex(f_subplex, fmin, x, n, &d, xtol_rel, maxeval,
S
stevenj 已提交
188
			     fmin_max, !my_isinf(fmin_max), scale);
S
stevenj 已提交
189 190
	      free(scale);
	      switch (iret) {
S
stevenj 已提交
191 192 193 194 195
		  case -2: return NLOPT_INVALID_ARGS;
		  case -1: return NLOPT_MAXEVAL_REACHED;
		  case 0: return NLOPT_XTOL_REACHED;
		  case 1: return NLOPT_SUCCESS;
		  case 2: return NLOPT_FMIN_MAX_REACHED;
S
stevenj 已提交
196 197 198 199 200
	      }
	      break;
	 }

	 case NLOPT_LOCAL_LBFGS: {
201
	      int iret, *nbd = (int *) malloc(sizeof(int) * n);
S
stevenj 已提交
202 203
	      if (!nbd) return NLOPT_OUT_OF_MEMORY;
	      for (i = 0; i < n; ++i) {
S
stevenj 已提交
204 205
		   int linf = my_isinf(lb[i]) && lb[i] < 0;
		   int uinf = my_isinf(ub[i]) && ub[i] > 0;
S
stevenj 已提交
206 207 208 209
		   nbd[i] = linf && uinf ? 0 : (uinf ? 1 : (linf ? 3 : 2));
	      }
	      iret = lbfgsb_minimize(n, f, f_data, x, nbd, lb, ub,
				     MIN(n, 5), 0.0, ftol_rel, 
S
whoops  
stevenj 已提交
210
				     xtol_abs ? *xtol_abs : xtol_rel,
S
stevenj 已提交
211 212 213 214
				     maxeval);
	      free(nbd);
	      if (iret <= 0) {
		   switch (iret) {
S
stevenj 已提交
215 216
		       case -1: return NLOPT_INVALID_ARGS;
		       case -2: default: return NLOPT_FAILURE;
S
stevenj 已提交
217 218 219 220 221
		   }
	      }
	      else {
		   *fmin = f(n, x, NULL, f_data);
		   switch (iret) {
S
stevenj 已提交
222 223 224
		       case 5: return NLOPT_MAXEVAL_REACHED;
		       case 2: return NLOPT_XTOL_REACHED;
		       case 1: return NLOPT_FTOL_REACHED;
S
stevenj 已提交
225 226 227 228 229 230 231 232 233 234 235 236
		       default: return NLOPT_SUCCESS;
		   }
	      }
	      break;
	 }

	 default:
	      return NLOPT_INVALID_ARGS;
     }

     return NLOPT_SUCCESS;
}