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

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

7 8
static const char nlopt_algorithm_names[NLOPT_NUM_ALGORITHMS][128] = {
     "DIRECT (global)",
9
     "DIRECT-L (global)",
10 11 12 13 14 15 16 17 18 19 20
     "Subplex (local)",
     "StoGO (global)",
     "Low-storage BFGS (LBFGS) (local)"
};

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

S
stevenj 已提交
21 22 23 24 25 26 27 28 29 30 31 32
static int my_isinf(double x) {
     return x == HUGE_VAL
#ifdef HAVE_ISINF
	  || isinf(x)
#endif
	  ;
}

#ifndef HAVE_ISNAN
static int my_isnan(double x) { return x != x; }
#  define isnan my_isnan
#endif
S
stevenj 已提交
33 34 35 36 37 38 39 40

typedef struct {
     nlopt_func f;
     void *f_data;
} nlopt_data;

#include "subplex.h"

S
stevenj 已提交
41
static double f_subplex(int n, const double *x, void *data_)
S
stevenj 已提交
42 43
{
     nlopt_data *data = (nlopt_data *) data_;
S
stevenj 已提交
44
     return data->f(n, x, NULL, data->f_data);
S
stevenj 已提交
45 46 47 48
}

#include "direct.h"

S
stevenj 已提交
49
static double f_direct(int n, const double *x, int *undefined, void *data_)
S
stevenj 已提交
50 51
{
     nlopt_data *data = (nlopt_data *) data_;
S
stevenj 已提交
52 53
     double f = data->f(n, x, NULL, data->f_data);
     *undefined = isnan(f) || my_isinf(f);
S
stevenj 已提交
54 55 56
     return f;
}

S
stevenj 已提交
57 58 59 60
#include "stogo.h"
#include "l-bfgs-b.h"

#define MIN(a,b) ((a) < (b) ? (a) : (b))
S
stevenj 已提交
61 62

nlopt_result nlopt_minimize(
63
     nlopt_algorithm algorithm,
S
stevenj 已提交
64 65 66 67
     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 已提交
68
     double fmin_max, double ftol_rel, double ftol_abs,
S
stevenj 已提交
69 70 71 72 73 74 75
     double xtol_rel, const double *xtol_abs,
     int maxeval, double maxtime)
{
     nlopt_data d;
     d.f = f;
     d.f_data = f_data;

76
     switch (algorithm) {
S
stevenj 已提交
77
	 case NLOPT_GLOBAL_DIRECT:
78
	 case NLOPT_GLOBAL_DIRECT_L:
S
stevenj 已提交
79 80 81 82
	      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,
83 84 85 86
				      NULL, 
				      algorithm == NLOPT_GLOBAL_DIRECT
				      ? DIRECT_ORIGINAL
				      : DIRECT_GABLONSKY)) {
S
stevenj 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		  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 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	      }
	      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: {
	      int iret, i;
	      double *scale = (double *) malloc(sizeof(double) * n);
	      if (!scale) return NLOPT_OUT_OF_MEMORY;
	      for (i = 0; i < n; ++i)
		   scale[i] = fabs(ub[i] - lb[i]);
	      iret = subplex(f_subplex, fmin, x, n, &d, xtol_rel, maxeval,
S
stevenj 已提交
121
			     fmin_max, !my_isinf(fmin_max), scale);
S
stevenj 已提交
122 123
	      free(scale);
	      switch (iret) {
S
stevenj 已提交
124 125 126 127 128
		  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 已提交
129 130 131 132 133 134 135 136
	      }
	      break;
	 }

	 case NLOPT_LOCAL_LBFGS: {
	      int iret, i, *nbd = (int *) malloc(sizeof(int) * n);
	      if (!nbd) return NLOPT_OUT_OF_MEMORY;
	      for (i = 0; i < n; ++i) {
S
stevenj 已提交
137 138
		   int linf = my_isinf(lb[i]) && lb[i] < 0;
		   int uinf = my_isinf(ub[i]) && ub[i] > 0;
S
stevenj 已提交
139 140 141 142
		   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 已提交
143
				     xtol_abs ? *xtol_abs : xtol_rel,
S
stevenj 已提交
144 145 146 147
				     maxeval);
	      free(nbd);
	      if (iret <= 0) {
		   switch (iret) {
S
stevenj 已提交
148 149
		       case -1: return NLOPT_INVALID_ARGS;
		       case -2: default: return NLOPT_FAILURE;
S
stevenj 已提交
150 151 152 153 154
		   }
	      }
	      else {
		   *fmin = f(n, x, NULL, f_data);
		   switch (iret) {
S
stevenj 已提交
155 156 157
		       case 5: return NLOPT_MAXEVAL_REACHED;
		       case 2: return NLOPT_XTOL_REACHED;
		       case 1: return NLOPT_FTOL_REACHED;
S
stevenj 已提交
158 159 160 161 162 163 164 165 166 167 168 169
		       default: return NLOPT_SUCCESS;
		   }
	      }
	      break;
	 }

	 default:
	      return NLOPT_INVALID_ARGS;
     }

     return NLOPT_SUCCESS;
}