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