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

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

S
stevenj 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#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

/*************************************************************************/
33

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

S
stevenj 已提交
41 42 43 44 45
/*************************************************************************/

static const char nlopt_algorithm_names[NLOPT_NUM_ALGORITHMS][128] = {
     "DIRECT (global)",
     "DIRECT-L (global)",
46
     "Randomized DIRECT-L (global)",
47 48
     "Original DIRECT version (global)",
     "Original DIRECT-L version (global)",
S
stevenj 已提交
49 50
     "Subplex (local)",
     "StoGO (global)",
51
     "StoGO with randomized search (global)",
S
stevenj 已提交
52 53 54
     "Low-storage BFGS (LBFGS) (local)"
};

55 56 57 58 59 60
const char *nlopt_algorithm_name(nlopt_algorithm a)
{
     if (a < 0 || a >= NLOPT_NUM_ALGORITHMS) return "UNKNOWN";
     return nlopt_algorithm_names[a];
}

S
stevenj 已提交
61 62
/*************************************************************************/

63 64 65 66 67 68 69 70 71 72
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 已提交
73
/*************************************************************************/
S
stevenj 已提交
74 75 76 77

typedef struct {
     nlopt_func f;
     void *f_data;
78 79
     const double *lb, *ub, *x0;
     double *xtmp;
S
stevenj 已提交
80 81
} nlopt_data;

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#define RECENTER 1 /* 0 to disable recentering */

/* for global-search algorithms that ignore the starting guess,
   but always check the center of the search box, we perform a
   coordinate transformation to put the initial guess x0 at the
   center of the box, and store the transformed x in xtmp. */
static void recenter_x(int n, const double *x,
		       const double *lb, const double *ub,
		       const double *x0, double *xtmp)
{
     int i;
     for (i = 0; i < n; ++i) {
#if RECENTER
	  /* Lagrange interpolating polynomial */
	  double xm = 0.5 * (lb[i] + ub[i]);
	  double dlu = 1. / (lb[i] - ub[i]);
	  double dlm = 1. / (lb[i] - xm);
	  double dum = 1. / (ub[i] - xm);
	  double dxu = x[i] - ub[i];
	  double dxl = x[i] - lb[i];
	  double dxm = x[i] - xm;
	  xtmp[i] = (lb[i] * (dxu * dlu) * (dxm * dlm)
		     - ub[i] * (dxl * dlu) * (dxm * dum)
		     + x0[i] * (dxl * dlm) * (dxu * dum));
#else
	  xtmp[i] = x[i];
#endif
     }
}

/* transform grad from df/dxtmp to df/dx */
static void recenter_grad(int n, const double *x,
			  const double *lb, const double *ub,
			  const double *x0,
			  double *grad)
{
#if RECENTER
     if (grad) {
	  int i;
	  for (i = 0; i < n; ++i) {
	       double xm = 0.5 * (lb[i] + ub[i]);
	       double dlu = 1. / (lb[i] - ub[i]);
	       double dlm = 1. / (lb[i] - xm);
	       double dum = 1. / (ub[i] - xm);
	       double dxu = x[i] - ub[i];
	       double dxl = x[i] - lb[i];
	       double dxm = x[i] - xm;
	       grad[i] *= (lb[i] * dlu*dlm * (dxm + dxu)
			   - ub[i] * dum*dlu * (dxm + dxl)
			   + x0[i] * dlm*dum * (dxu + dxl));
	  }
     }
#endif
}

static double f_recenter(int n, const double *x, double *grad, void *data_)
{
     nlopt_data *data = (nlopt_data *) data_;
     double f;
     recenter_x(n, x, data->lb, data->ub, data->x0, data->xtmp);
     f = data->f(n, data->xtmp, grad, data->f_data);
     recenter_grad(n, x, data->lb, data->ub, data->x0, grad);
     return f;
}

S
stevenj 已提交
147 148
#include "subplex.h"

S
stevenj 已提交
149
static double f_subplex(int n, const double *x, void *data_)
S
stevenj 已提交
150
{
151
     int i;
S
stevenj 已提交
152
     nlopt_data *data = (nlopt_data *) data_;
S
stevenj 已提交
153
     double f;
154 155 156 157 158

     /* 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 已提交
159
	       return MY_INF;
160

S
stevenj 已提交
161 162
     f = data->f(n, x, NULL, data->f_data);
     return (isnan(f) ? MY_INF : f);
S
stevenj 已提交
163 164 165 166
}

#include "direct.h"

S
stevenj 已提交
167
static double f_direct(int n, const double *x, int *undefined, void *data_)
S
stevenj 已提交
168 169
{
     nlopt_data *data = (nlopt_data *) data_;
170 171 172
     double f;
     recenter_x(n, x, data->lb, data->ub, data->x0, data->xtmp);
     f = data->f(n, data->xtmp, NULL, data->f_data);
S
stevenj 已提交
173
     *undefined = isnan(f) || my_isinf(f);
S
stevenj 已提交
174 175 176
     return f;
}

S
stevenj 已提交
177
#include "stogo.h"
178

S
stevenj 已提交
179 180
#include "l-bfgs-b.h"

181 182
#include "cdirect.h"

S
stevenj 已提交
183
/*************************************************************************/
S
stevenj 已提交
184

185 186
/* same as nlopt_minimize, but xtol_abs is required to be non-NULL */
static nlopt_result nlopt_minimize_(
187
     nlopt_algorithm algorithm,
S
stevenj 已提交
188 189 190 191
     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 已提交
192
     double fmin_max, double ftol_rel, double ftol_abs,
S
stevenj 已提交
193 194 195
     double xtol_rel, const double *xtol_abs,
     int maxeval, double maxtime)
{
196
     int i;
S
stevenj 已提交
197
     nlopt_data d;
198 199
     nlopt_stopping stop;

200 201 202 203
     /* some basic argument checks */
     if (n <= 0 || !f || !lb || !ub || !x || !fmin)
	  return NLOPT_INVALID_ARGS;

S
stevenj 已提交
204 205
     d.f = f;
     d.f_data = f_data;
206 207
     d.lb = lb;
     d.ub = ub;
208
     d.x0 = d.xtmp = NULL;
209

210 211 212 213
     /* make sure rand generator is inited */
     if (!nlopt_srand_called)
	  nlopt_srand_time(); /* default is non-deterministic */

214 215 216 217
     /* 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 已提交
218

219 220 221 222 223 224 225 226 227 228 229 230
     stop.n = n;
     stop.fmin_max = (isnan(fmin_max) || (my_isinf(fmin_max) && fmin_max < 0))
	  ? -MY_INF : fmin_max;
     stop.ftol_rel = ftol_rel;
     stop.ftol_abs = ftol_abs;
     stop.xtol_rel = xtol_rel;
     stop.xtol_abs = xtol_abs;
     stop.nevals = 0;
     stop.maxeval = maxeval;
     stop.maxtime = maxtime;
     stop.start = nlopt_seconds();

231
     switch (algorithm) {
S
stevenj 已提交
232
	 case NLOPT_GLOBAL_DIRECT:
233
	 case NLOPT_GLOBAL_DIRECT_L: 
234 235 236 237
	 case NLOPT_GLOBAL_DIRECT_L_RANDOMIZED: 
	      return cdirect(n, f, f_data, lb, ub, x, fmin, &stop, 0.0, 
			     (algorithm == NLOPT_GLOBAL_DIRECT ? 0 : 1)
			     + 10 * (algorithm == NLOPT_GLOBAL_DIRECT_L_RANDOMIZED ? 2 : 0));
238 239 240

	 case NLOPT_GLOBAL_ORIG_DIRECT:
	 case NLOPT_GLOBAL_ORIG_DIRECT_L: 
241
	 {
242 243 244 245 246
	      int iret;
	      d.xtmp = (double *) malloc(sizeof(double) * n*2);
	      if (!d.xtmp) return NLOPT_OUT_OF_MEMORY;
	      memcpy(d.xtmp + n, x, sizeof(double) * n); d.x0 = d.xtmp + n;
	      iret = direct_optimize(f_direct, &d, n, lb, ub, x, fmin,
247
				     maxeval, -1, 0.0, 0.0,
248 249
				     pow(xtol_rel, (double) n), -1.0,
				     stop.fmin_max, 0.0,
250
				     NULL, 
251
				     algorithm == NLOPT_GLOBAL_ORIG_DIRECT
252 253 254 255 256
				     ? DIRECT_ORIGINAL
				     : DIRECT_GABLONSKY);
	      recenter_x(n, x, lb, ub, d.x0, x);
	      free(d.xtmp);
	      switch (iret) {
S
stevenj 已提交
257 258 259 260 261 262 263 264 265 266 267 268
		  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:
269
		       return NLOPT_FMIN_MAX_REACHED;
S
stevenj 已提交
270 271 272 273 274
		  case DIRECT_VOLTOL:
		  case DIRECT_SIGMATOL:
		       return NLOPT_XTOL_REACHED;
		  case DIRECT_OUT_OF_MEMORY:
		       return NLOPT_OUT_OF_MEMORY;
S
stevenj 已提交
275 276
	      }
	      break;
277
	 }
S
stevenj 已提交
278 279

	 case NLOPT_GLOBAL_STOGO:
280 281 282 283 284
	 case NLOPT_GLOBAL_STOGO_RANDOMIZED: {
	      int iret;
	      d.xtmp = (double *) malloc(sizeof(double) * n*2);
	      if (!d.xtmp) return NLOPT_OUT_OF_MEMORY;
	      memcpy(d.xtmp + n, x, sizeof(double) * n); d.x0 = d.xtmp + n;
285
	      iret = stogo_minimize(n, f_recenter, &d, x, fmin, lb, ub, &stop,
286 287 288 289 290
				    algorithm == NLOPT_GLOBAL_STOGO
				    ? 0 : 2*n);
	      recenter_x(n, x, lb, ub, d.x0, x);
	      free(d.xtmp);
	      if (!iret) return NLOPT_FAILURE;
S
stevenj 已提交
291
	      break;
292
	 }
S
stevenj 已提交
293 294

	 case NLOPT_LOCAL_SUBPLEX: {
295
	      int iret;
S
stevenj 已提交
296 297
	      double *scale = (double *) malloc(sizeof(double) * n);
	      if (!scale) return NLOPT_OUT_OF_MEMORY;
298 299 300 301 302 303 304 305 306 307
	      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;
	      }
308
	      iret = subplex(f_subplex, fmin, x, n, &d, &stop, scale);
S
stevenj 已提交
309 310
	      free(scale);
	      switch (iret) {
S
stevenj 已提交
311
		  case -2: return NLOPT_INVALID_ARGS;
312
		  case -10: return NLOPT_MAXTIME_REACHED;
S
stevenj 已提交
313 314 315 316
		  case -1: return NLOPT_MAXEVAL_REACHED;
		  case 0: return NLOPT_XTOL_REACHED;
		  case 1: return NLOPT_SUCCESS;
		  case 2: return NLOPT_FMIN_MAX_REACHED;
317 318 319
		  case 20: return NLOPT_FTOL_REACHED;
		  case -200: return NLOPT_OUT_OF_MEMORY;
		  default: return NLOPT_FAILURE; /* unknown return code */
S
stevenj 已提交
320 321 322 323 324
	      }
	      break;
	 }

	 case NLOPT_LOCAL_LBFGS: {
325
	      int iret, *nbd = (int *) malloc(sizeof(int) * n);
S
stevenj 已提交
326 327
	      if (!nbd) return NLOPT_OUT_OF_MEMORY;
	      for (i = 0; i < n; ++i) {
S
stevenj 已提交
328 329
		   int linf = my_isinf(lb[i]) && lb[i] < 0;
		   int uinf = my_isinf(ub[i]) && ub[i] > 0;
S
stevenj 已提交
330 331 332 333
		   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 已提交
334
				     xtol_abs ? *xtol_abs : xtol_rel,
S
stevenj 已提交
335 336 337 338
				     maxeval);
	      free(nbd);
	      if (iret <= 0) {
		   switch (iret) {
S
stevenj 已提交
339 340
		       case -1: return NLOPT_INVALID_ARGS;
		       case -2: default: return NLOPT_FAILURE;
S
stevenj 已提交
341 342 343 344 345
		   }
	      }
	      else {
		   *fmin = f(n, x, NULL, f_data);
		   switch (iret) {
S
stevenj 已提交
346 347 348
		       case 5: return NLOPT_MAXEVAL_REACHED;
		       case 2: return NLOPT_XTOL_REACHED;
		       case 1: return NLOPT_FTOL_REACHED;
S
stevenj 已提交
349 350 351 352 353 354 355 356 357 358 359 360
		       default: return NLOPT_SUCCESS;
		   }
	      }
	      break;
	 }

	 default:
	      return NLOPT_INVALID_ARGS;
     }

     return NLOPT_SUCCESS;
}
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

nlopt_result nlopt_minimize(
     nlopt_algorithm algorithm,
     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 */
     double fmin_max, double ftol_rel, double ftol_abs,
     double xtol_rel, const double *xtol_abs,
     int maxeval, double maxtime)
{
     nlopt_result ret;
     if (xtol_abs)
	  ret = nlopt_minimize_(algorithm, n, f, f_data, lb, ub,
				x, fmin, fmin_max, ftol_rel, ftol_abs,
				xtol_rel, xtol_abs, maxeval, maxtime);
     else {
	  int i;
	  double *xtol = (double *) malloc(sizeof(double) * n);
	  if (!xtol) return NLOPT_OUT_OF_MEMORY;
	  for (i = 0; i < n; ++i) xtol[i] = -1;
	  ret = nlopt_minimize_(algorithm, n, f, f_data, lb, ub,
				x, fmin, fmin_max, ftol_rel, ftol_abs,
				xtol_rel, xtol, maxeval, maxtime);
	  free(xtol);
     }
     return ret;
}