chiark / gitweb /
copyright year bump
[nlopt.git] / api / optimize.c
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 #include <stdlib.h>
24 #include <math.h>
25 #include <float.h>
26
27 #include "nlopt-internal.h"
28
29 /*********************************************************************/
30
31 #ifndef HAVE_ISNAN
32 static int my_isnan(double x) { return x != x; }
33 #  define isnan my_isnan
34 #endif
35
36 /*********************************************************************/
37
38 #include "praxis.h"
39 #include "direct.h"
40
41 #ifdef WITH_CXX
42 #  include "stogo.h"
43 #endif
44
45 #include "cdirect.h"
46
47 #ifdef WITH_NOCEDAL
48 #  include "l-bfgs-b.h"
49 #endif
50
51 #include "luksan.h"
52
53 #include "crs.h"
54
55 #include "mlsl.h"
56 #include "mma.h"
57 #include "cobyla.h"
58 #include "newuoa.h"
59 #include "neldermead.h"
60 #include "auglag.h"
61 #include "bobyqa.h"
62 #include "isres.h"
63 #include "slsqp.h"
64
65 /*********************************************************************/
66
67 static double f_bound(int n, const double *x, void *data_)
68 {
69      int i;
70      nlopt_opt data = (nlopt_opt) data_;
71      double f;
72
73      /* some methods do not support bound constraints, but support
74         discontinuous objectives so we can just return Inf for invalid x */
75      for (i = 0; i < n; ++i)
76           if (x[i] < data->lb[i] || x[i] > data->ub[i])
77                return HUGE_VAL;
78
79      f = data->f((unsigned) n, x, NULL, data->f_data);
80      return (isnan(f) || nlopt_isinf(f) ? HUGE_VAL : f);
81 }
82
83 static double f_noderiv(int n, const double *x, void *data_)
84 {
85      nlopt_opt data = (nlopt_opt) data_;
86      return data->f((unsigned) n, x, NULL, data->f_data);
87 }
88
89 static double f_direct(int n, const double *x, int *undefined, void *data_)
90 {
91      nlopt_opt data = (nlopt_opt) data_;
92      double f;
93      unsigned i, j;
94      f = data->f((unsigned) n, x, NULL, data->f_data);
95      *undefined = isnan(f) || nlopt_isinf(f);
96      if (nlopt_get_force_stop(data)) return f;
97      for (i = 0; i < data->m && !*undefined; ++i) {
98           nlopt_eval_constraint(data->work, NULL, data->fc+i, (unsigned) n, x);
99           if (nlopt_get_force_stop(data)) return f;
100           for (j = 0; j < data->fc[i].m; ++j)
101                if (data->work[j] > 0)
102                     *undefined = 1;
103      }
104      return f;
105 }
106
107 /*********************************************************************/
108
109 /* get min(dx) for algorithms requiring a scalar initial step size */
110 static nlopt_result initial_step(nlopt_opt opt, const double *x, double *step)
111 {
112      unsigned freedx = 0, i;
113
114      if (!opt->dx) {
115           freedx = 1;
116           if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
117                return NLOPT_OUT_OF_MEMORY;
118      }
119
120      *step = HUGE_VAL;
121      for (i = 0; i < opt->n; ++i)
122           if (*step > fabs(opt->dx[i]))
123                *step = fabs(opt->dx[i]);
124
125      if (freedx) { free(opt->dx); opt->dx = NULL; }
126      return NLOPT_SUCCESS;
127 }
128
129 /*********************************************************************/
130
131 /* return true if [lb,ub] is finite in every dimension (n dimensions) */
132 static int finite_domain(unsigned n, const double *lb, const double *ub)
133 {
134      unsigned i;
135      for (i = 0; i < n; ++i)
136           if (nlopt_isinf(ub[i] - lb[i])) return 0;
137      return 1;
138 }
139
140 /*********************************************************************/
141 /* wrapper functions, only for derivative-free methods, that
142    eliminate dimensions with lb == ub.   (The gradient-based methods
143    should handle this case directly, since they operate on much
144    larger vectors where I am loathe to make copies unnecessarily.) */
145
146 typedef struct {
147      nlopt_func f;
148      nlopt_mfunc mf;
149      void *f_data;
150      unsigned n; /* true dimension */
151      double *x; /* scratch vector of length n */
152      double *grad; /* optional scratch vector of length n */
153      const double *lb, *ub; /* bounds, of length n */
154 } elimdim_data;
155
156 static void *elimdim_makedata(nlopt_func f, nlopt_mfunc mf, void *f_data,
157                               unsigned n, double *x, const double *lb,
158                               const double *ub, double *grad)
159 {
160      elimdim_data *d = (elimdim_data *) malloc(sizeof(elimdim_data));
161      if (!d) return NULL;
162      d->f = f; d->mf = mf; d->f_data = f_data; d->n = n; d->x = x;
163      d->lb = lb; d->ub = ub;
164      d->grad = grad;
165      return d;
166 }
167
168 static double elimdim_func(unsigned n0, const double *x0, double *grad, void *d_)
169 {
170      elimdim_data *d = (elimdim_data *) d_;
171      double *x = d->x;
172      const double *lb = d->lb, *ub = d->ub;
173      double val;
174      unsigned n = d->n, i, j;
175
176      (void) n0; /* unused */
177      for (i = j = 0; i < n; ++i) {
178           if (lb[i] == ub[i])
179                x[i] = lb[i];
180           else /* assert: j < n0 */
181                x[i] = x0[j++];
182      }
183      val = d->f(n, x, grad ? d->grad : NULL, d->f_data);
184      if (grad) {
185           /* assert: d->grad != NULL */
186           for (i = j = 0; i < n; ++i)
187                if (lb[i] != ub[i])
188                     grad[j++] = d->grad[i];
189      }
190      return val;
191 }
192
193
194 static void elimdim_mfunc(unsigned m, double *result,
195                           unsigned n0, const double *x0, double *grad, void *d_)
196 {
197      elimdim_data *d = (elimdim_data *) d_;
198      double *x = d->x;
199      const double *lb = d->lb, *ub = d->ub;
200      unsigned n = d->n, i, j;
201
202      (void) n0; /* unused */
203      (void) grad; /* assert: grad == NULL */
204      for (i = j = 0; i < n; ++i) {
205           if (lb[i] == ub[i])
206                x[i] = lb[i];
207           else /* assert: j < n0 */
208                x[i] = x0[j++];
209      }
210      d->mf(m, result, n, x, NULL, d->f_data);
211 }
212
213 /* compute the eliminated dimension: number of dims with lb[i] != ub[i] */
214 static unsigned elimdim_dimension(unsigned n, const double *lb, const double *ub)
215 {
216      unsigned n0 = 0, i;
217      for (i = 0; i < n; ++i) n0 += lb[i] != ub[i] ? 1U : 0;
218      return n0;
219 }
220
221 /* modify v to "shrunk" version, with dimensions for lb[i] == ub[i] elim'ed */
222 static void elimdim_shrink(unsigned n, double *v,
223                            const double *lb, const double *ub)
224 {
225      unsigned i, j;
226      if (v)
227           for (i = j = 0; i < n; ++i)
228                if (lb[i] != ub[i])
229                     v[j++] = v[i];
230 }
231
232 /* inverse of elimdim_shrink */
233 static void elimdim_expand(unsigned n, double *v,
234                            const double *lb, const double *ub)
235 {
236      unsigned i, j;
237      if (v && n > 0) {
238           j = elimdim_dimension(n, lb, ub) - 1;
239           for (i = n - 1; i > 0; --i) {
240                if (lb[i] != ub[i])
241                     v[i] = v[j--];
242                else
243                     v[i] = lb[i];
244           }
245           if (lb[0] == ub[0])
246                v[0] = lb[0];
247      }
248 }
249
250 /* given opt, create a new opt with equal-constraint dimensions eliminated */
251 static nlopt_opt elimdim_create(nlopt_opt opt)
252 {
253      nlopt_opt opt0 = nlopt_copy(opt);
254      double *x, *grad = NULL;
255      unsigned i;
256      
257      if (!opt0) return NULL;
258      x = (double *) malloc(sizeof(double) * opt->n);
259      if (opt->n && !x) { nlopt_destroy(opt0); return NULL; }
260
261      if (opt->algorithm == NLOPT_GD_STOGO
262          || opt->algorithm == NLOPT_GD_STOGO_RAND) {
263           grad = (double *) malloc(sizeof(double) * opt->n);
264           if (opt->n && !grad) goto bad;
265      }
266
267      opt0->n = elimdim_dimension(opt->n, opt->lb, opt->ub);
268      elimdim_shrink(opt->n, opt0->lb, opt->lb, opt->ub);
269      elimdim_shrink(opt->n, opt0->ub, opt->lb, opt->ub);
270      elimdim_shrink(opt->n, opt0->xtol_abs, opt->lb, opt->ub);
271      elimdim_shrink(opt->n, opt0->dx, opt->lb, opt->ub);
272
273      opt0->munge_on_destroy = opt0->munge_on_copy = NULL;
274
275      opt0->f = elimdim_func;
276      opt0->f_data = elimdim_makedata(opt->f, NULL, opt->f_data,
277                                      opt->n, x, opt->lb, opt->ub, grad);
278      if (!opt0->f_data) goto bad;
279
280      for (i = 0; i < opt->m; ++i) {
281           opt0->fc[i].f = elimdim_func;
282           opt0->fc[i].mf = elimdim_mfunc;
283           opt0->fc[i].f_data = elimdim_makedata(opt->fc[i].f, opt->fc[i].mf,
284                                                 opt->fc[i].f_data,
285                                                 opt->n, x, opt->lb, opt->ub,
286                                                 NULL);
287           if (!opt0->fc[i].f_data) goto bad;
288      }
289
290      for (i = 0; i < opt->p; ++i) {
291           opt0->h[i].f = elimdim_func;
292           opt0->h[i].mf = elimdim_mfunc;
293           opt0->h[i].f_data = elimdim_makedata(opt->h[i].f, opt->h[i].mf,
294                                                opt->h[i].f_data,
295                                                opt->n, x, opt->lb, opt->ub,
296                                                NULL);
297           if (!opt0->h[i].f_data) goto bad;
298      }
299
300      return opt0;
301 bad:
302      free(grad);
303      free(x);
304      nlopt_destroy(opt0);
305      return NULL;
306 }
307
308 /* like nlopt_destroy, but also frees elimdim_data */
309 static void elimdim_destroy(nlopt_opt opt)
310 {
311      unsigned i;
312      if (!opt) return;
313
314      free(((elimdim_data*) opt->f_data)->x);
315      free(((elimdim_data*) opt->f_data)->grad);
316      free(opt->f_data); opt->f_data = NULL;
317
318      for (i = 0; i < opt->m; ++i) {
319           free(opt->fc[i].f_data);
320           opt->fc[i].f_data = NULL;
321      }
322      for (i = 0; i < opt->p; ++i) {
323           free(opt->h[i].f_data);
324           opt->h[i].f_data = NULL;
325      }
326
327      nlopt_destroy(opt);
328 }
329
330 /* return whether to use elimdim wrapping. */
331 static int elimdim_wrapcheck(nlopt_opt opt)
332 {
333      if (!opt) return 0;
334      if (elimdim_dimension(opt->n, opt->lb, opt->ub) == opt->n) return 0;
335      switch (opt->algorithm) {
336          case NLOPT_GN_DIRECT:
337          case NLOPT_GN_DIRECT_L: 
338          case NLOPT_GN_DIRECT_L_RAND: 
339          case NLOPT_GN_DIRECT_NOSCAL:
340          case NLOPT_GN_DIRECT_L_NOSCAL: 
341          case NLOPT_GN_DIRECT_L_RAND_NOSCAL: 
342          case NLOPT_GN_ORIG_DIRECT:
343          case NLOPT_GN_ORIG_DIRECT_L:
344          case NLOPT_LN_PRAXIS:
345          case NLOPT_LN_COBYLA:
346          case NLOPT_LN_NEWUOA:
347          case NLOPT_LN_NEWUOA_BOUND:
348          case NLOPT_LN_BOBYQA:
349          case NLOPT_LN_NELDERMEAD:
350          case NLOPT_LN_SBPLX:
351          case NLOPT_GN_ISRES:
352          case NLOPT_GD_STOGO:
353          case NLOPT_GD_STOGO_RAND:
354               return 1;
355
356          default: return 0;
357      }
358 }
359
360 /*********************************************************************/
361
362 #define POP(defaultpop) (opt->stochastic_population > 0 ?               \
363                          opt->stochastic_population :                   \
364                          (nlopt_stochastic_population > 0 ?             \
365                           nlopt_stochastic_population : (defaultpop)))
366
367 /* unlike nlopt_optimize() below, only handles minimization case */
368 static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
369 {
370      const double *lb, *ub;
371      nlopt_algorithm algorithm;
372      nlopt_func f; void *f_data;
373      unsigned n, i;
374      int ni;
375      nlopt_stopping stop;
376
377      if (!opt || !x || !minf || !opt->f
378          || opt->maximize) return NLOPT_INVALID_ARGS;
379
380      /* reset stopping flag */
381      nlopt_set_force_stop(opt, 0);
382      opt->force_stop_child = NULL;
383      
384      /* copy a few params to local vars for convenience */
385      n = opt->n;
386      ni = (int) n; /* most of the subroutines take "int" arg */
387      lb = opt->lb; ub = opt->ub;
388      algorithm = opt->algorithm;
389      f = opt->f; f_data = opt->f_data;
390
391      if (n == 0) { /* trivial case: no degrees of freedom */
392           *minf = opt->f(n, x, NULL, opt->f_data);
393           return NLOPT_SUCCESS;
394      }
395
396      *minf = HUGE_VAL;
397      
398      /* make sure rand generator is inited */
399      nlopt_srand_time_default(); /* default is non-deterministic */
400
401      /* check bound constraints */
402      for (i = 0; i < n; ++i)
403           if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i])
404                return NLOPT_INVALID_ARGS;
405
406      stop.n = n;
407      stop.minf_max = opt->stopval;
408      stop.ftol_rel = opt->ftol_rel;
409      stop.ftol_abs = opt->ftol_abs;
410      stop.xtol_rel = opt->xtol_rel;
411      stop.xtol_abs = opt->xtol_abs;
412      stop.nevals = 0;
413      stop.maxeval = opt->maxeval;
414      stop.maxtime = opt->maxtime;
415      stop.start = nlopt_seconds();
416      stop.force_stop = &(opt->force_stop);
417
418      switch (algorithm) {
419          case NLOPT_GN_DIRECT:
420          case NLOPT_GN_DIRECT_L: 
421          case NLOPT_GN_DIRECT_L_RAND: 
422               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
423               return cdirect(ni, f, f_data, 
424                              lb, ub, x, minf, &stop, 0.0, 
425                              (algorithm != NLOPT_GN_DIRECT)
426                              + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND 
427                                     ? 2 : (algorithm != NLOPT_GN_DIRECT))
428                              + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND 
429                                     ? 1 : (algorithm != NLOPT_GN_DIRECT)));
430               
431          case NLOPT_GN_DIRECT_NOSCAL:
432          case NLOPT_GN_DIRECT_L_NOSCAL: 
433          case NLOPT_GN_DIRECT_L_RAND_NOSCAL: 
434               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
435               return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf, 
436                                       &stop, 0.0, 
437                                       (algorithm != NLOPT_GN_DIRECT)
438                                       + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
439                                       + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
440               
441          case NLOPT_GN_ORIG_DIRECT:
442          case NLOPT_GN_ORIG_DIRECT_L: {
443               direct_return_code dret;
444               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
445               opt->work = (double*) malloc(sizeof(double) *
446                                            nlopt_max_constraint_dim(opt->m,
447                                                                     opt->fc));
448               if (!opt->work) return NLOPT_OUT_OF_MEMORY;
449               dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
450                                      stop.maxeval, -1,
451                                      stop.start, stop.maxtime,
452                                      0.0, 0.0,
453                                      pow(stop.xtol_rel, (double) n), -1.0,
454                                      stop.force_stop,
455                                      stop.minf_max, 0.0,
456                                      NULL, 
457                                      algorithm == NLOPT_GN_ORIG_DIRECT
458                                      ? DIRECT_ORIGINAL
459                                      : DIRECT_GABLONSKY);
460               free(opt->work); opt->work = NULL;
461               switch (dret) {
462                   case DIRECT_INVALID_BOUNDS:
463                   case DIRECT_MAXFEVAL_TOOBIG:
464                   case DIRECT_INVALID_ARGS:
465                        return NLOPT_INVALID_ARGS;
466                   case DIRECT_INIT_FAILED:
467                   case DIRECT_SAMPLEPOINTS_FAILED:
468                   case DIRECT_SAMPLE_FAILED:
469                        return NLOPT_FAILURE;
470                   case DIRECT_MAXFEVAL_EXCEEDED:
471                   case DIRECT_MAXITER_EXCEEDED:
472                        return NLOPT_MAXEVAL_REACHED;
473                   case DIRECT_MAXTIME_EXCEEDED:
474                        return NLOPT_MAXTIME_REACHED;
475                   case DIRECT_GLOBAL_FOUND:
476                        return NLOPT_MINF_MAX_REACHED;
477                   case DIRECT_VOLTOL:
478                   case DIRECT_SIGMATOL:
479                        return NLOPT_XTOL_REACHED;
480                   case DIRECT_OUT_OF_MEMORY:
481                        return NLOPT_OUT_OF_MEMORY;
482                   case DIRECT_FORCED_STOP:
483                        return NLOPT_FORCED_STOP;
484               }
485               break;
486          }
487
488          case NLOPT_GD_STOGO:
489          case NLOPT_GD_STOGO_RAND:
490 #ifdef WITH_CXX
491               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
492               if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop,
493                                   algorithm == NLOPT_GD_STOGO
494                                   ? 0 : (int) POP(2*n)))
495                    return NLOPT_FAILURE;
496               break;
497 #else
498               return NLOPT_INVALID_ARGS;
499 #endif
500
501 #if 0
502               /* lacking a free/open-source license, we no longer use
503                  Rowan's code, and instead use by "sbplx" re-implementation */
504          case NLOPT_LN_SUBPLEX: {
505               int iret, freedx = 0;
506               if (!opt->dx) {
507                    freedx = 1;
508                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
509                         return NLOPT_OUT_OF_MEMORY;
510               }                
511               iret = nlopt_subplex(f_bound, minf, x, n, opt, &stop, opt->dx);
512               if (freedx) { free(opt->dx); opt->dx = NULL; }
513               switch (iret) {
514                   case -2: return NLOPT_INVALID_ARGS;
515                   case -20: return NLOPT_FORCED_STOP;
516                   case -10: return NLOPT_MAXTIME_REACHED;
517                   case -1: return NLOPT_MAXEVAL_REACHED;
518                   case 0: return NLOPT_XTOL_REACHED;
519                   case 1: return NLOPT_SUCCESS;
520                   case 2: return NLOPT_MINF_MAX_REACHED;
521                   case 20: return NLOPT_FTOL_REACHED;
522                   case -200: return NLOPT_OUT_OF_MEMORY;
523                   default: return NLOPT_FAILURE; /* unknown return code */
524               }
525               break;
526          }
527 #endif
528
529          case NLOPT_LN_PRAXIS: {
530               double step;
531               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
532                    return NLOPT_OUT_OF_MEMORY;
533               return praxis_(0.0, DBL_EPSILON, 
534                              step, ni, x, f_bound, opt, &stop, minf);
535          }
536
537 #ifdef WITH_NOCEDAL
538          case NLOPT_LD_LBFGS_NOCEDAL: {
539               int iret, *nbd = (int *) malloc(sizeof(int) * n);
540               if (!nbd) return NLOPT_OUT_OF_MEMORY;
541               for (i = 0; i < n; ++i) {
542                    int linf = nlopt_isinf(lb[i]) && lb[i] < 0;
543                    int uinf = nlopt_isinf(ub[i]) && ub[i] > 0;
544                    nbd[i] = linf && uinf ? 0 : (uinf ? 1 : (linf ? 3 : 2));
545               }
546               iret = lbfgsb_minimize(ni, f, f_data, x, nbd, lb, ub,
547                                      ni < 5 ? ni : 5, 0.0, stop.ftol_rel, 
548                                      stop.xtol_abs[0] > 0 ? stop.xtol_abs[0]
549                                      : stop.xtol_rel,
550                                      stop.maxeval);
551               free(nbd);
552               if (iret <= 0) {
553                    switch (iret) {
554                        case -1: return NLOPT_INVALID_ARGS;
555                        case -2: default: return NLOPT_FAILURE;
556                    }
557               }
558               else {
559                    *minf = f(n, x, NULL, f_data);
560                    switch (iret) {
561                        case 5: return NLOPT_MAXEVAL_REACHED;
562                        case 2: return NLOPT_XTOL_REACHED;
563                        case 1: return NLOPT_FTOL_REACHED;
564                        default: return NLOPT_SUCCESS;
565                    }
566               }
567               break;
568          }
569 #endif
570
571          case NLOPT_LD_LBFGS: 
572               return luksan_plis(ni, f, f_data, lb, ub, x, minf, 
573                                  &stop, opt->vector_storage);
574
575          case NLOPT_LD_VAR1: 
576          case NLOPT_LD_VAR2: 
577               return luksan_plip(ni, f, f_data, lb, ub, x, minf, 
578                                  &stop, opt->vector_storage,
579                                  algorithm == NLOPT_LD_VAR1 ? 1 : 2);
580
581          case NLOPT_LD_TNEWTON: 
582          case NLOPT_LD_TNEWTON_RESTART: 
583          case NLOPT_LD_TNEWTON_PRECOND: 
584          case NLOPT_LD_TNEWTON_PRECOND_RESTART: 
585               return luksan_pnet(ni, f, f_data, lb, ub, x, minf,
586                                  &stop, opt->vector_storage,
587                                  1 + (algorithm - NLOPT_LD_TNEWTON) % 2,
588                                  1 + (algorithm - NLOPT_LD_TNEWTON) / 2);
589
590          case NLOPT_GN_CRS2_LM:
591               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
592               return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop, 
593                                   (int) POP(0), 0);
594
595          case NLOPT_G_MLSL:
596          case NLOPT_G_MLSL_LDS:
597          case NLOPT_GN_MLSL:
598          case NLOPT_GD_MLSL:
599          case NLOPT_GN_MLSL_LDS:
600          case NLOPT_GD_MLSL_LDS: {
601               nlopt_opt local_opt = opt->local_opt;
602               nlopt_result ret;
603               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
604               if (!local_opt && (algorithm == NLOPT_G_MLSL 
605                                  || algorithm == NLOPT_G_MLSL_LDS))
606                    return NLOPT_INVALID_ARGS;
607               if (!local_opt) { /* default */
608                    nlopt_algorithm local_alg = (algorithm == NLOPT_GN_MLSL ||
609                                                 algorithm == NLOPT_GN_MLSL_LDS)
610                         ? nlopt_local_search_alg_nonderiv
611                         : nlopt_local_search_alg_deriv;
612                    /* don't call MLSL recursively! */
613                    if (local_alg >= NLOPT_GN_MLSL
614                        && local_alg <= NLOPT_GD_MLSL_LDS)
615                         local_alg = (algorithm == NLOPT_GN_MLSL ||
616                                      algorithm == NLOPT_GN_MLSL_LDS)
617                              ? NLOPT_LN_COBYLA : NLOPT_LD_MMA;
618                    local_opt = nlopt_create(local_alg, n);
619                    if (!local_opt) return NLOPT_FAILURE;
620                    nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
621                    nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
622                    nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
623                    nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
624                    nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
625               }
626               if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
627               for (i = 0; i < n && stop.xtol_abs[i] > 0; ++i) ;
628               if (local_opt->ftol_rel <= 0 && local_opt->ftol_abs <= 0 &&
629                   local_opt->xtol_rel <= 0 && i < n) {
630                    /* it is not sensible to call MLSL without *some*
631                       nonzero tolerance for the local search */
632                    nlopt_set_ftol_rel(local_opt, 1e-15);
633                    nlopt_set_xtol_rel(local_opt, 1e-7);
634               }
635               opt->force_stop_child = local_opt;
636               ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
637                                   local_opt, (int) POP(0),
638                                   algorithm >= NLOPT_GN_MLSL_LDS &&
639                                   algorithm != NLOPT_G_MLSL);
640               opt->force_stop_child = NULL;
641               if (!opt->local_opt) nlopt_destroy(local_opt);
642               return ret;
643          }
644
645          case NLOPT_LD_MMA: {
646               nlopt_opt dual_opt;
647               nlopt_result ret;
648 #define LO(param, def) (opt->local_opt ? opt->local_opt->param : (def))
649               dual_opt = nlopt_create(LO(algorithm,
650                                          nlopt_local_search_alg_deriv),
651                                       nlopt_count_constraints(opt->m,
652                                                               opt->fc));
653               if (!dual_opt) return NLOPT_FAILURE;
654               nlopt_set_ftol_rel(dual_opt, LO(ftol_rel, 1e-12));
655               nlopt_set_ftol_abs(dual_opt, LO(ftol_abs, 0.0));
656               nlopt_set_maxeval(dual_opt, LO(maxeval, 100000));
657 #undef LO
658
659               ret = mma_minimize(n, f, f_data, opt->m, opt->fc,
660                                  lb, ub, x, minf, &stop, dual_opt);
661               nlopt_destroy(dual_opt);
662               return ret;
663          }
664
665          case NLOPT_LN_COBYLA: {
666               nlopt_result ret;
667               int freedx = 0;
668               if (!opt->dx) {
669                    freedx = 1;
670                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
671                         return NLOPT_OUT_OF_MEMORY;
672               }
673               return cobyla_minimize(n, f, f_data, 
674                                      opt->m, opt->fc,
675                                      opt->p, opt->h,
676                                      lb, ub, x, minf, &stop,
677                                      opt->dx);
678               if (freedx) { free(opt->dx); opt->dx = NULL; }
679               return ret;
680          }
681                                      
682          case NLOPT_LN_NEWUOA: {
683               double step;
684               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
685                    return NLOPT_OUT_OF_MEMORY;
686               return newuoa(ni, 2*n+1, x, 0, 0, step,
687                             &stop, minf, f_noderiv, opt);
688          }
689                                      
690          case NLOPT_LN_NEWUOA_BOUND: {
691               double step;
692               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
693                    return NLOPT_OUT_OF_MEMORY;
694               return newuoa(ni, 2*n+1, x, lb, ub, step,
695                             &stop, minf, f_noderiv, opt);
696          }
697
698          case NLOPT_LN_BOBYQA: {
699               nlopt_result ret;
700               int freedx = 0;
701               if (!opt->dx) {
702                    freedx = 1;
703                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
704                         return NLOPT_OUT_OF_MEMORY;
705               }
706               ret = bobyqa(ni, 2*n+1, x, lb, ub, opt->dx,
707                            &stop, minf, opt->f, opt->f_data);
708               if (freedx) { free(opt->dx); opt->dx = NULL; }
709               return ret;
710          }
711
712          case NLOPT_LN_NELDERMEAD: 
713          case NLOPT_LN_SBPLX: 
714          {
715               nlopt_result ret;
716               int freedx = 0;
717               if (!opt->dx) {
718                    freedx = 1;
719                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
720                         return NLOPT_OUT_OF_MEMORY;
721               }
722               if (algorithm == NLOPT_LN_NELDERMEAD)
723                    ret= nldrmd_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
724               else
725                    ret= sbplx_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
726               if (freedx) { free(opt->dx); opt->dx = NULL; }
727               return ret;
728          }
729
730          case NLOPT_AUGLAG:
731          case NLOPT_AUGLAG_EQ:
732          case NLOPT_LN_AUGLAG:
733          case NLOPT_LN_AUGLAG_EQ:
734          case NLOPT_LD_AUGLAG:
735          case NLOPT_LD_AUGLAG_EQ: {
736               nlopt_opt local_opt = opt->local_opt;
737               nlopt_result ret;
738               if ((algorithm == NLOPT_AUGLAG || algorithm == NLOPT_AUGLAG_EQ)
739                   && !local_opt)
740                    return NLOPT_INVALID_ARGS;
741               if (!local_opt) { /* default */
742                    local_opt = nlopt_create(
743                         algorithm == NLOPT_LN_AUGLAG || 
744                         algorithm == NLOPT_LN_AUGLAG_EQ
745                         ? nlopt_local_search_alg_nonderiv
746                         : nlopt_local_search_alg_deriv, n);
747                    if (!local_opt) return NLOPT_FAILURE;
748                    nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
749                    nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
750                    nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
751                    nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
752                    nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
753               }
754               if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
755               opt->force_stop_child = local_opt;
756               ret = auglag_minimize(ni, f, f_data, 
757                                     opt->m, opt->fc, 
758                                     opt->p, opt->h,
759                                     lb, ub, x, minf, &stop,
760                                     local_opt,
761                                     algorithm == NLOPT_AUGLAG_EQ
762                                     || algorithm == NLOPT_LN_AUGLAG_EQ
763                                     || algorithm == NLOPT_LD_AUGLAG_EQ);
764               opt->force_stop_child = NULL;
765               if (!opt->local_opt) nlopt_destroy(local_opt);
766               return ret;
767          }
768
769          case NLOPT_GN_ISRES:
770               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
771               return isres_minimize(ni, f, f_data, 
772                                     (int) (opt->m), opt->fc,
773                                     (int) (opt->p), opt->h,
774                                     lb, ub, x, minf, &stop,
775                                     (int) POP(0));
776
777          case NLOPT_LD_SLSQP:
778               return nlopt_slsqp(n, f, f_data,
779                                  opt->m, opt->fc,
780                                  opt->p, opt->h,
781                                  lb, ub, x, minf, &stop);
782                                      
783          default:
784               return NLOPT_INVALID_ARGS;
785      }
786
787      return NLOPT_SUCCESS; /* never reached */
788 }
789
790 /*********************************************************************/
791
792 typedef struct {
793      nlopt_func f;
794      void *f_data;
795 } f_max_data;
796
797 /* wrapper for maximizing: just flip the sign of f and grad */
798 static double f_max(unsigned n, const double *x, double *grad, void *data)
799 {
800      f_max_data *d = (f_max_data *) data;
801      double val = d->f(n, x, grad, d->f_data);
802      if (grad) {
803           unsigned i;
804           for (i = 0; i < n; ++i)
805                grad[i] = -grad[i];
806      }
807      return -val;
808 }
809
810 nlopt_result 
811 NLOPT_STDCALL nlopt_optimize(nlopt_opt opt, double *x, double *opt_f)
812 {
813      nlopt_func f; void *f_data;
814      f_max_data fmd;
815      int maximize;
816      nlopt_result ret;
817
818      if (!opt || !opt_f || !opt->f) return NLOPT_INVALID_ARGS;
819      f = opt->f; f_data = opt->f_data;
820
821      /* for maximizing, just minimize the f_max wrapper, which 
822         flips the sign of everything */
823      if ((maximize = opt->maximize)) {
824           fmd.f = f; fmd.f_data = f_data;
825           opt->f = f_max; opt->f_data = &fmd;
826           opt->stopval = -opt->stopval;
827           opt->maximize = 0;
828      }
829
830      { /* possibly eliminate lb == ub dimensions for some algorithms */
831           nlopt_opt elim_opt = opt;
832           if (elimdim_wrapcheck(opt)) {
833                elim_opt = elimdim_create(opt);
834                if (!elim_opt) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
835                elimdim_shrink(opt->n, x, opt->lb, opt->ub);
836           }
837
838           ret = nlopt_optimize_(elim_opt, x, opt_f);
839
840           if (elim_opt != opt) {
841                elimdim_destroy(elim_opt);
842                elimdim_expand(opt->n, x, opt->lb, opt->ub);
843           }
844      }
845
846 done:
847      if (maximize) { /* restore original signs */
848           opt->maximize = maximize;
849           opt->stopval = -opt->stopval;
850           opt->f = f; opt->f_data = f_data;
851           *opt_f = -*opt_f;
852      }
853
854      return ret;
855 }
856
857 /*********************************************************************/
858
859 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf,
860                                     int maxeval, double maxtime)
861 {
862      int save_maxeval;
863      double save_maxtime;
864      nlopt_result ret;
865
866      if (!opt) return NLOPT_INVALID_ARGS;
867
868      save_maxeval = nlopt_get_maxeval(opt);
869      save_maxtime = nlopt_get_maxtime(opt);
870      
871      /* override opt limits if maxeval and/or maxtime are more stringent */
872      if (save_maxeval <= 0 || (maxeval > 0 && maxeval < save_maxeval))
873           nlopt_set_maxeval(opt, maxeval);
874      if (save_maxtime <= 0 || (maxtime > 0 && maxtime < save_maxtime))
875           nlopt_set_maxtime(opt, maxtime);
876
877      ret = nlopt_optimize(opt, x, minf);
878
879      nlopt_set_maxeval(opt, save_maxeval);
880      nlopt_set_maxtime(opt, save_maxtime);
881
882      return ret;
883 }
884
885 /*********************************************************************/