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