chiark / gitweb /
ef9402ab18da8049f62b93a42cbf5511fc5c475c
[nlopt.git] / api / optimize.c
1 /* Copyright (c) 2007-2010 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
142 #define POP(defaultpop) (opt->stochastic_population > 0 ?               \
143                          opt->stochastic_population :                   \
144                          (nlopt_stochastic_population > 0 ?             \
145                           nlopt_stochastic_population : (defaultpop)))
146
147 /* unlike nlopt_optimize() below, only handles minimization case */
148 static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
149 {
150      const double *lb, *ub;
151      nlopt_algorithm algorithm;
152      nlopt_func f; void *f_data;
153      unsigned n, i;
154      int ni;
155      nlopt_stopping stop;
156
157      if (!opt || !x || !minf || !opt->f
158          || opt->maximize) return NLOPT_INVALID_ARGS;
159
160      /* reset stopping flag */
161      nlopt_set_force_stop(opt, 0);
162      opt->force_stop_child = NULL;
163      
164      /* copy a few params to local vars for convenience */
165      n = opt->n;
166      ni = (int) n; /* most of the subroutines take "int" arg */
167      lb = opt->lb; ub = opt->ub;
168      algorithm = opt->algorithm;
169      f = opt->f; f_data = opt->f_data;
170
171      if (n == 0) { /* trivial case: no degrees of freedom */
172           *minf = opt->f(n, x, NULL, opt->f_data);
173           return NLOPT_SUCCESS;
174      }
175
176      *minf = HUGE_VAL;
177      
178      /* make sure rand generator is inited */
179      nlopt_srand_time_default(); /* default is non-deterministic */
180
181      /* check bound constraints */
182      for (i = 0; i < n; ++i)
183           if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i])
184                return NLOPT_INVALID_ARGS;
185
186      stop.n = n;
187      stop.minf_max = opt->stopval;
188      stop.ftol_rel = opt->ftol_rel;
189      stop.ftol_abs = opt->ftol_abs;
190      stop.xtol_rel = opt->xtol_rel;
191      stop.xtol_abs = opt->xtol_abs;
192      stop.nevals = 0;
193      stop.maxeval = opt->maxeval;
194      stop.maxtime = opt->maxtime;
195      stop.start = nlopt_seconds();
196      stop.force_stop = &(opt->force_stop);
197
198      switch (algorithm) {
199          case NLOPT_GN_DIRECT:
200          case NLOPT_GN_DIRECT_L: 
201          case NLOPT_GN_DIRECT_L_RAND: 
202               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
203               return cdirect(ni, f, f_data, 
204                              lb, ub, x, minf, &stop, 0.0, 
205                              (algorithm != NLOPT_GN_DIRECT)
206                              + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND 
207                                     ? 2 : (algorithm != NLOPT_GN_DIRECT))
208                              + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND 
209                                     ? 1 : (algorithm != NLOPT_GN_DIRECT)));
210               
211          case NLOPT_GN_DIRECT_NOSCAL:
212          case NLOPT_GN_DIRECT_L_NOSCAL: 
213          case NLOPT_GN_DIRECT_L_RAND_NOSCAL: 
214               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
215               return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf, 
216                                       &stop, 0.0, 
217                                       (algorithm != NLOPT_GN_DIRECT)
218                                       + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
219                                       + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
220               
221          case NLOPT_GN_ORIG_DIRECT:
222          case NLOPT_GN_ORIG_DIRECT_L: {
223               direct_return_code dret;
224               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
225               opt->work = (double*) malloc(sizeof(double) *
226                                            nlopt_max_constraint_dim(opt->m,
227                                                                     opt->fc));
228               if (!opt->work) return NLOPT_OUT_OF_MEMORY;
229               dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
230                                      stop.maxeval, -1, 0.0, 0.0,
231                                      pow(stop.xtol_rel, (double) n), -1.0,
232                                      stop.minf_max, 0.0,
233                                      NULL, 
234                                      algorithm == NLOPT_GN_ORIG_DIRECT
235                                      ? DIRECT_ORIGINAL
236                                      : DIRECT_GABLONSKY);
237               free(opt->work); opt->work = NULL;
238               switch (dret) {
239                   case DIRECT_INVALID_BOUNDS:
240                   case DIRECT_MAXFEVAL_TOOBIG:
241                   case DIRECT_INVALID_ARGS:
242                        return NLOPT_INVALID_ARGS;
243                   case DIRECT_INIT_FAILED:
244                   case DIRECT_SAMPLEPOINTS_FAILED:
245                   case DIRECT_SAMPLE_FAILED:
246                        return NLOPT_FAILURE;
247                   case DIRECT_MAXFEVAL_EXCEEDED:
248                   case DIRECT_MAXITER_EXCEEDED:
249                        return NLOPT_MAXEVAL_REACHED;
250                   case DIRECT_GLOBAL_FOUND:
251                        return NLOPT_MINF_MAX_REACHED;
252                   case DIRECT_VOLTOL:
253                   case DIRECT_SIGMATOL:
254                        return NLOPT_XTOL_REACHED;
255                   case DIRECT_OUT_OF_MEMORY:
256                        return NLOPT_OUT_OF_MEMORY;
257               }
258               break;
259          }
260
261          case NLOPT_GD_STOGO:
262          case NLOPT_GD_STOGO_RAND:
263 #ifdef WITH_CXX
264               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
265               if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop,
266                                   algorithm == NLOPT_GD_STOGO
267                                   ? 0 : (int) POP(2*n)))
268                    return NLOPT_FAILURE;
269               break;
270 #else
271               return NLOPT_FAILURE;
272 #endif
273
274 #if 0
275               /* lacking a free/open-source license, we no longer use
276                  Rowan's code, and instead use by "sbplx" re-implementation */
277          case NLOPT_LN_SUBPLEX: {
278               int iret, freedx = 0;
279               if (!opt->dx) {
280                    freedx = 1;
281                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
282                         return NLOPT_OUT_OF_MEMORY;
283               }                
284               iret = nlopt_subplex(f_bound, minf, x, n, opt, &stop, opt->dx);
285               if (freedx) { free(opt->dx); opt->dx = NULL; }
286               switch (iret) {
287                   case -2: return NLOPT_INVALID_ARGS;
288                   case -20: return NLOPT_FORCED_STOP;
289                   case -10: return NLOPT_MAXTIME_REACHED;
290                   case -1: return NLOPT_MAXEVAL_REACHED;
291                   case 0: return NLOPT_XTOL_REACHED;
292                   case 1: return NLOPT_SUCCESS;
293                   case 2: return NLOPT_MINF_MAX_REACHED;
294                   case 20: return NLOPT_FTOL_REACHED;
295                   case -200: return NLOPT_OUT_OF_MEMORY;
296                   default: return NLOPT_FAILURE; /* unknown return code */
297               }
298               break;
299          }
300 #endif
301
302          case NLOPT_LN_PRAXIS: {
303               double step;
304               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
305                    return NLOPT_OUT_OF_MEMORY;
306               return praxis_(0.0, DBL_EPSILON, 
307                              step, ni, x, f_bound, opt, &stop, minf);
308          }
309
310 #ifdef WITH_NOCEDAL
311          case NLOPT_LD_LBFGS_NOCEDAL: {
312               int iret, *nbd = (int *) malloc(sizeof(int) * n);
313               if (!nbd) return NLOPT_OUT_OF_MEMORY;
314               for (i = 0; i < n; ++i) {
315                    int linf = nlopt_isinf(lb[i]) && lb[i] < 0;
316                    int uinf = nlopt_isinf(ub[i]) && ub[i] > 0;
317                    nbd[i] = linf && uinf ? 0 : (uinf ? 1 : (linf ? 3 : 2));
318               }
319               iret = lbfgsb_minimize(ni, f, f_data, x, nbd, lb, ub,
320                                      ni < 5 ? ni : 5, 0.0, stop.ftol_rel, 
321                                      stop.xtol_abs[0] > 0 ? stop.xtol_abs[0]
322                                      : stop.xtol_rel,
323                                      stop.maxeval);
324               free(nbd);
325               if (iret <= 0) {
326                    switch (iret) {
327                        case -1: return NLOPT_INVALID_ARGS;
328                        case -2: default: return NLOPT_FAILURE;
329                    }
330               }
331               else {
332                    *minf = f(n, x, NULL, f_data);
333                    switch (iret) {
334                        case 5: return NLOPT_MAXEVAL_REACHED;
335                        case 2: return NLOPT_XTOL_REACHED;
336                        case 1: return NLOPT_FTOL_REACHED;
337                        default: return NLOPT_SUCCESS;
338                    }
339               }
340               break;
341          }
342 #endif
343
344          case NLOPT_LD_LBFGS: 
345               return luksan_plis(ni, f, f_data, lb, ub, x, minf, &stop);
346
347          case NLOPT_LD_VAR1: 
348          case NLOPT_LD_VAR2: 
349               return luksan_plip(ni, f, f_data, lb, ub, x, minf, &stop,
350                    algorithm == NLOPT_LD_VAR1 ? 1 : 2);
351
352          case NLOPT_LD_TNEWTON: 
353          case NLOPT_LD_TNEWTON_RESTART: 
354          case NLOPT_LD_TNEWTON_PRECOND: 
355          case NLOPT_LD_TNEWTON_PRECOND_RESTART: 
356               return luksan_pnet(ni, f, f_data, lb, ub, x, minf, &stop,
357                                  1 + (algorithm - NLOPT_LD_TNEWTON) % 2,
358                                  1 + (algorithm - NLOPT_LD_TNEWTON) / 2);
359
360          case NLOPT_GN_CRS2_LM:
361               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
362               return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop, 
363                                   (int) POP(0), 0);
364
365          case NLOPT_G_MLSL:
366          case NLOPT_G_MLSL_LDS:
367          case NLOPT_GN_MLSL:
368          case NLOPT_GD_MLSL:
369          case NLOPT_GN_MLSL_LDS:
370          case NLOPT_GD_MLSL_LDS: {
371               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
372               nlopt_opt local_opt = opt->local_opt;
373               nlopt_result ret;
374               if (!local_opt && (algorithm == NLOPT_G_MLSL 
375                                  || algorithm == NLOPT_G_MLSL_LDS))
376                    return NLOPT_INVALID_ARGS;
377               if (!local_opt) { /* default */
378                    nlopt_algorithm local_alg = (algorithm == NLOPT_GN_MLSL ||
379                                                 algorithm == NLOPT_GN_MLSL_LDS)
380                         ? nlopt_local_search_alg_nonderiv
381                         : nlopt_local_search_alg_deriv;
382                    /* don't call MLSL recursively! */
383                    if (local_alg >= NLOPT_GN_MLSL
384                        && local_alg <= NLOPT_GD_MLSL_LDS)
385                         local_alg = (algorithm == NLOPT_GN_MLSL ||
386                                      algorithm == NLOPT_GN_MLSL_LDS)
387                              ? NLOPT_LN_COBYLA : NLOPT_LD_MMA;
388                    local_opt = nlopt_create(local_alg, n);
389                    if (!local_opt) return NLOPT_FAILURE;
390                    nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
391                    nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
392                    nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
393                    nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
394                    nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
395               }
396               if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
397               for (i = 0; i < n && stop.xtol_abs[i] > 0; ++i) ;
398               if (local_opt->ftol_rel <= 0 && local_opt->ftol_abs <= 0 &&
399                   local_opt->xtol_rel <= 0 && i < n) {
400                    /* it is not sensible to call MLSL without *some*
401                       nonzero tolerance for the local search */
402                    nlopt_set_ftol_rel(local_opt, 1e-15);
403                    nlopt_set_xtol_rel(local_opt, 1e-7);
404               }
405               opt->force_stop_child = local_opt;
406               ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
407                                   local_opt, (int) POP(0),
408                                   algorithm >= NLOPT_GN_MLSL_LDS &&
409                                   algorithm != NLOPT_G_MLSL);
410               opt->force_stop_child = NULL;
411               if (!opt->local_opt) nlopt_destroy(local_opt);
412               return ret;
413          }
414
415          case NLOPT_LD_MMA: {
416               nlopt_opt dual_opt;
417               nlopt_result ret;
418 #define LO(param, def) (opt->local_opt ? opt->local_opt->param : (def))
419               dual_opt = nlopt_create(LO(algorithm,
420                                          nlopt_local_search_alg_deriv),
421                                       nlopt_count_constraints(opt->m,
422                                                               opt->fc));
423               if (!dual_opt) return NLOPT_FAILURE;
424               nlopt_set_ftol_rel(dual_opt, LO(ftol_rel, 1e-12));
425               nlopt_set_ftol_abs(dual_opt, LO(ftol_abs, 0.0));
426               nlopt_set_maxeval(dual_opt, LO(maxeval, 100000));
427 #undef LO
428
429               ret = mma_minimize(n, f, f_data, opt->m, opt->fc,
430                                  lb, ub, x, minf, &stop, dual_opt);
431               nlopt_destroy(dual_opt);
432               return ret;
433          }
434
435          case NLOPT_LN_COBYLA: {
436               nlopt_result ret;
437               int freedx = 0;
438               if (!opt->dx) {
439                    freedx = 1;
440                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
441                         return NLOPT_OUT_OF_MEMORY;
442               }
443               return cobyla_minimize(n, f, f_data, 
444                                      opt->m, opt->fc,
445                                      opt->p, opt->h,
446                                      lb, ub, x, minf, &stop,
447                                      opt->dx);
448               if (freedx) { free(opt->dx); opt->dx = NULL; }
449               return ret;
450          }
451                                      
452          case NLOPT_LN_NEWUOA: {
453               double step;
454               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
455                    return NLOPT_OUT_OF_MEMORY;
456               return newuoa(ni, 2*n+1, x, 0, 0, step,
457                             &stop, minf, f_noderiv, opt);
458          }
459                                      
460          case NLOPT_LN_NEWUOA_BOUND: {
461               double step;
462               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
463                    return NLOPT_OUT_OF_MEMORY;
464               return newuoa(ni, 2*n+1, x, lb, ub, step,
465                             &stop, minf, f_noderiv, opt);
466          }
467
468          case NLOPT_LN_BOBYQA: {
469               nlopt_result ret;
470               int freedx = 0;
471               if (!opt->dx) {
472                    freedx = 1;
473                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
474                         return NLOPT_OUT_OF_MEMORY;
475               }
476               ret = bobyqa(ni, 2*n+1, x, lb, ub, opt->dx,
477                            &stop, minf, opt->f, opt->f_data);
478               if (freedx) { free(opt->dx); opt->dx = NULL; }
479               return ret;
480          }
481
482          case NLOPT_LN_NELDERMEAD: 
483          case NLOPT_LN_SBPLX: 
484          {
485               nlopt_result ret;
486               int freedx = 0;
487               if (!opt->dx) {
488                    freedx = 1;
489                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
490                         return NLOPT_OUT_OF_MEMORY;
491               }
492               if (algorithm == NLOPT_LN_NELDERMEAD)
493                    ret= nldrmd_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
494               else
495                    ret= sbplx_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
496               if (freedx) { free(opt->dx); opt->dx = NULL; }
497               return ret;
498          }
499
500          case NLOPT_AUGLAG:
501          case NLOPT_AUGLAG_EQ:
502          case NLOPT_LN_AUGLAG:
503          case NLOPT_LN_AUGLAG_EQ:
504          case NLOPT_LD_AUGLAG:
505          case NLOPT_LD_AUGLAG_EQ: {
506               nlopt_opt local_opt = opt->local_opt;
507               nlopt_result ret;
508               if ((algorithm == NLOPT_AUGLAG || algorithm == NLOPT_AUGLAG_EQ)
509                   && !local_opt)
510                    return NLOPT_INVALID_ARGS;
511               if (!local_opt) { /* default */
512                    local_opt = nlopt_create(
513                         algorithm == NLOPT_LN_AUGLAG || 
514                         algorithm == NLOPT_LN_AUGLAG_EQ
515                         ? nlopt_local_search_alg_nonderiv
516                         : nlopt_local_search_alg_deriv, n);
517                    if (!local_opt) return NLOPT_FAILURE;
518                    nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
519                    nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
520                    nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
521                    nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
522                    nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
523               }
524               if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
525               opt->force_stop_child = local_opt;
526               ret = auglag_minimize(ni, f, f_data, 
527                                     opt->m, opt->fc, 
528                                     opt->p, opt->h,
529                                     lb, ub, x, minf, &stop,
530                                     local_opt,
531                                     algorithm == NLOPT_AUGLAG_EQ
532                                     || algorithm == NLOPT_LN_AUGLAG_EQ
533                                     || algorithm == NLOPT_LD_AUGLAG_EQ);
534               opt->force_stop_child = NULL;
535               if (!opt->local_opt) nlopt_destroy(local_opt);
536               return ret;
537          }
538
539          case NLOPT_GN_ISRES:
540               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
541               return isres_minimize(ni, f, f_data, 
542                                     (int) (opt->m), opt->fc,
543                                     (int) (opt->p), opt->h,
544                                     lb, ub, x, minf, &stop,
545                                     (int) POP(0));
546
547          case NLOPT_LD_SLSQP:
548               return nlopt_slsqp(n, f, f_data,
549                                  opt->m, opt->fc,
550                                  opt->p, opt->h,
551                                  lb, ub, x, minf, &stop);
552                                      
553          default:
554               return NLOPT_INVALID_ARGS;
555      }
556
557      return NLOPT_SUCCESS; /* never reached */
558 }
559
560 /*********************************************************************/
561
562 typedef struct {
563      nlopt_func f;
564      void *f_data;
565 } f_max_data;
566
567 /* wrapper for maximizing: just flip the sign of f and grad */
568 static double f_max(unsigned n, const double *x, double *grad, void *data)
569 {
570      f_max_data *d = (f_max_data *) data;
571      double val = d->f(n, x, grad, d->f_data);
572      if (grad) {
573           unsigned i;
574           for (i = 0; i < n; ++i)
575                grad[i] = -grad[i];
576      }
577      return -val;
578 }
579
580 nlopt_result 
581 NLOPT_STDCALL nlopt_optimize(nlopt_opt opt, double *x, double *opt_f)
582 {
583      nlopt_func f; void *f_data;
584      f_max_data fmd;
585      int maximize;
586      nlopt_result ret;
587
588      if (!opt || !opt_f || !opt->f) return NLOPT_INVALID_ARGS;
589      f = opt->f; f_data = opt->f_data;
590
591      /* for maximizing, just minimize the f_max wrapper, which 
592         flips the sign of everything */
593      if ((maximize = opt->maximize)) {
594           fmd.f = f; fmd.f_data = f_data;
595           opt->f = f_max; opt->f_data = &fmd;
596           opt->stopval = -opt->stopval;
597           opt->maximize = 0;
598      }
599
600      ret = nlopt_optimize_(opt, x, opt_f);
601
602      if (maximize) { /* restore original signs */
603           opt->maximize = maximize;
604           opt->stopval = -opt->stopval;
605           opt->f = f; opt->f_data = f_data;
606           *opt_f = -*opt_f;
607      }
608
609      return ret;
610 }
611
612 /*********************************************************************/
613
614 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf,
615                                     int maxeval, double maxtime)
616 {
617      int save_maxeval;
618      double save_maxtime;
619      nlopt_result ret;
620
621      if (!opt) return NLOPT_INVALID_ARGS;
622
623      save_maxeval = nlopt_get_maxeval(opt);
624      save_maxtime = nlopt_get_maxtime(opt);
625      
626      /* override opt limits if maxeval and/or maxtime are more stringent */
627      if (save_maxeval <= 0 || (maxeval > 0 && maxeval < save_maxeval))
628           nlopt_set_maxeval(opt, maxeval);
629      if (save_maxtime <= 0 || (maxtime > 0 && maxtime < save_maxtime))
630           nlopt_set_maxtime(opt, maxtime);
631
632      ret = nlopt_optimize(opt, x, minf);
633
634      nlopt_set_maxeval(opt, save_maxeval);
635      nlopt_set_maxtime(opt, save_maxtime);
636
637      return ret;
638 }
639
640 /*********************************************************************/