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