chiark / gitweb /
595f44e92a8c079fa32a0a2ea8ee318656f55074
[nlopt.git] / api / optimize.c
1 /* Copyright (c) 2007-2009 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 typedef struct {
67      nlopt_func f;
68      void *f_data;
69      const double *lb, *ub;
70 } nlopt_data;
71
72 #include "praxis.h"
73
74 static double f_bound(int n, const double *x, void *data_)
75 {
76      int i;
77      nlopt_data *data = (nlopt_data *) data_;
78      double f;
79
80      /* some methods do not support bound constraints, but support
81         discontinuous objectives so we can just return Inf for invalid x */
82      for (i = 0; i < n; ++i)
83           if (x[i] < data->lb[i] || x[i] > data->ub[i])
84                return HUGE_VAL;
85
86      f = data->f(n, x, NULL, data->f_data);
87      return (isnan(f) || nlopt_isinf(f) ? HUGE_VAL : f);
88 }
89
90 static double f_noderiv(int n, const double *x, void *data_)
91 {
92      nlopt_data *data = (nlopt_data *) data_;
93      return data->f(n, x, NULL, data->f_data);
94 }
95
96 static double f_direct(int n, const double *x, int *undefined, void *data_)
97 {
98      nlopt_data *data = (nlopt_data *) data_;
99      double f;
100      f = data->f(n, x, NULL, data->f_data);
101      *undefined = isnan(f) || nlopt_isinf(f);
102      return f;
103 }
104
105 /*********************************************************************/
106
107 /* get min(dx) for algorithms requiring a scalar initial step size */
108 static nlopt_result initial_step(nlopt_opt opt, const double *x, double *step)
109 {
110      int freedx = 0, i;
111
112      if (!opt->dx) {
113           freedx = 1;
114           if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
115                return NLOPT_OUT_OF_MEMORY;
116      }
117
118      *step = HUGE_VAL;
119      for (i = 0; i < opt->n; ++i)
120           if (*step > fabs(opt->dx[i]))
121                *step = fabs(opt->dx[i]);
122
123      if (freedx) { free(opt->dx); opt->dx = NULL; }
124      return NLOPT_SUCCESS;
125 }
126
127 /*********************************************************************/
128
129 /* return true if [lb,ub] is finite in every dimension (n dimensions) */
130 static int finite_domain(int n, const double *lb, const double *ub)
131 {
132      int i;
133      for (i = 0; i < n; ++i)
134           if (nlopt_isinf(ub[i] - lb[i])) return 0;
135      return 1;
136 }
137
138 /*********************************************************************/
139
140 #define POP(defaultpop) (opt->stochastic_population > 0 ?               \
141                          opt->stochastic_population :                   \
142                          (nlopt_stochastic_population > 0 ?             \
143                           nlopt_stochastic_population : (defaultpop)))
144
145 nlopt_result nlopt_optimize(nlopt_opt opt, double *x, double *minf)
146 {
147      const double *lb, *ub;
148      nlopt_algorithm algorithm;
149      nlopt_func f; void *f_data;
150      int n, i;
151      nlopt_data d;
152      nlopt_stopping stop;
153
154      if (!opt || !x || !minf || !opt->f) return NLOPT_INVALID_ARGS;
155
156      /* copy a few params to local vars for convenience */
157      n = opt->n;
158      lb = opt->lb; ub = opt->ub;
159      algorithm = opt->algorithm;
160      f = opt->f; f_data = opt->f_data;
161
162      if (n == 0) { /* trivial case: no degrees of freedom */
163           *minf = f(n, x, NULL, f_data);
164           return NLOPT_SUCCESS;
165      }
166
167      *minf = HUGE_VAL;
168      
169      d.f = f;
170      d.f_data = f_data;
171      d.lb = lb;
172      d.ub = ub;
173
174      /* make sure rand generator is inited */
175      if (!nlopt_srand_called)
176           nlopt_srand_time(); /* default is non-deterministic */
177
178      /* check bound constraints */
179      for (i = 0; i < n; ++i)
180           if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i])
181                return NLOPT_INVALID_ARGS;
182
183      stop.n = n;
184      stop.minf_max = opt->minf_max;
185      stop.ftol_rel = opt->ftol_rel;
186      stop.ftol_abs = opt->ftol_abs;
187      stop.xtol_rel = opt->xtol_rel;
188      stop.xtol_abs = opt->xtol_abs;
189      stop.nevals = 0;
190      stop.maxeval = opt->maxeval;
191      stop.maxtime = opt->maxtime;
192      stop.start = nlopt_seconds();
193
194      switch (algorithm) {
195          case NLOPT_GN_DIRECT:
196          case NLOPT_GN_DIRECT_L: 
197          case NLOPT_GN_DIRECT_L_RAND: 
198               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
199               return cdirect(n, f, f_data, 
200                              lb, ub, x, minf, &stop, 0.0, 
201                              (algorithm != NLOPT_GN_DIRECT)
202                              + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND 
203                                     ? 2 : (algorithm != NLOPT_GN_DIRECT))
204                              + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND 
205                                     ? 1 : (algorithm != NLOPT_GN_DIRECT)));
206               
207          case NLOPT_GN_DIRECT_NOSCAL:
208          case NLOPT_GN_DIRECT_L_NOSCAL: 
209          case NLOPT_GN_DIRECT_L_RAND_NOSCAL: 
210               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
211               return cdirect_unscaled(n, f, f_data, lb, ub, x, minf, 
212                                       &stop, 0.0, 
213                                       (algorithm != NLOPT_GN_DIRECT)
214                                       + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
215                                       + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
216               
217          case NLOPT_GN_ORIG_DIRECT:
218          case NLOPT_GN_ORIG_DIRECT_L: 
219               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
220               switch (direct_optimize(f_direct, &d, n, lb, ub, x, minf,
221                                       stop.maxeval, -1, 0.0, 0.0,
222                                       pow(stop.xtol_rel, (double) n), -1.0,
223                                       stop.minf_max, 0.0,
224                                       NULL, 
225                                       algorithm == NLOPT_GN_ORIG_DIRECT
226                                       ? DIRECT_ORIGINAL
227                                       : DIRECT_GABLONSKY)) {
228                   case DIRECT_INVALID_BOUNDS:
229                   case DIRECT_MAXFEVAL_TOOBIG:
230                   case DIRECT_INVALID_ARGS:
231                        return NLOPT_INVALID_ARGS;
232                   case DIRECT_INIT_FAILED:
233                   case DIRECT_SAMPLEPOINTS_FAILED:
234                   case DIRECT_SAMPLE_FAILED:
235                        return NLOPT_FAILURE;
236                   case DIRECT_MAXFEVAL_EXCEEDED:
237                   case DIRECT_MAXITER_EXCEEDED:
238                        return NLOPT_MAXEVAL_REACHED;
239                   case DIRECT_GLOBAL_FOUND:
240                        return NLOPT_MINF_MAX_REACHED;
241                   case DIRECT_VOLTOL:
242                   case DIRECT_SIGMATOL:
243                        return NLOPT_XTOL_REACHED;
244                   case DIRECT_OUT_OF_MEMORY:
245                        return NLOPT_OUT_OF_MEMORY;
246               break;
247          }
248
249          case NLOPT_GD_STOGO:
250          case NLOPT_GD_STOGO_RAND:
251 #ifdef WITH_CXX
252               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
253               if (!stogo_minimize(n, f, f_data, x, minf, lb, ub, &stop,
254                                   algorithm == NLOPT_GD_STOGO
255                                   ? 0 : POP(2*n)))
256                    return NLOPT_FAILURE;
257               break;
258 #else
259               return NLOPT_FAILURE;
260 #endif
261
262 #if 0
263               /* lacking a free/open-source license, we no longer use
264                  Rowan's code, and instead use by "sbplx" re-implementation */
265          case NLOPT_LN_SUBPLEX: {
266               int iret, freedx = 0;
267               if (!opt->dx) {
268                    freedx = 1;
269                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
270                         return NLOPT_OUT_OF_MEMORY;
271               }                
272               iret = nlopt_subplex(f_bound, minf, x, n, &d, &stop, opt->dx);
273               if (freedx) { free(opt->dx); opt->dx = NULL; }
274               switch (iret) {
275                   case -2: return NLOPT_INVALID_ARGS;
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, n, x, f_bound, &d, &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(n, f, f_data, x, nbd, lb, ub,
307                                      MIN(n, 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(n, f, f_data, lb, ub, x, minf, &stop);
333
334          case NLOPT_LD_VAR1: 
335          case NLOPT_LD_VAR2: 
336               return luksan_plip(n, 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(n, 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(n, f, f_data, lb, ub, x, minf, &stop, 
350                                   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               ret = mlsl_minimize(n, f, f_data, lb, ub, x, minf, &stop,
388                                   local_opt, POP(0),
389                                   algorithm >= NLOPT_GN_MLSL_LDS);
390               if (!opt->local_opt) nlopt_destroy(local_opt);
391               return ret;
392          }
393
394          case NLOPT_LD_MMA: {
395               nlopt_opt dual_opt;
396               nlopt_result ret;
397 #define LO(param, def) (opt->local_opt ? opt->local_opt->param : (def))
398               dual_opt = nlopt_create(LO(algorithm,
399                                          nlopt_local_search_alg_deriv),
400                                       opt->m);
401               if (!dual_opt) return NLOPT_FAILURE;
402               nlopt_set_ftol_rel(dual_opt, LO(ftol_rel, 1e-12));
403               nlopt_set_ftol_abs(dual_opt, LO(ftol_abs, 0.0));
404               nlopt_set_maxeval(dual_opt, LO(maxeval, 100000));
405 #undef LO
406
407               ret = mma_minimize(n, f, f_data, opt->m, opt->fc,
408                                  lb, ub, x, minf, &stop, dual_opt);
409               nlopt_destroy(dual_opt);
410               return ret;
411          }
412
413          case NLOPT_LN_COBYLA: {
414               double step;
415               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
416                    return NLOPT_OUT_OF_MEMORY;
417               return cobyla_minimize(n, f, f_data, 
418                                      opt->m, opt->fc,
419                                      lb, ub, x, minf, &stop,
420                                      step);
421          }
422                                      
423          case NLOPT_LN_NEWUOA: {
424               double step;
425               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
426                    return NLOPT_OUT_OF_MEMORY;
427               return newuoa(n, 2*n+1, x, 0, 0, step,
428                             &stop, minf, f_noderiv, &d);
429          }
430                                      
431          case NLOPT_LN_NEWUOA_BOUND: {
432               double step;
433               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
434                    return NLOPT_OUT_OF_MEMORY;
435               return newuoa(n, 2*n+1, x, lb, ub, step,
436                             &stop, minf, f_noderiv, &d);
437          }
438
439          case NLOPT_LN_BOBYQA: {
440               double step;
441               if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
442                    return NLOPT_OUT_OF_MEMORY;
443               return bobyqa(n, 2*n+1, x, lb, ub, step,
444                             &stop, minf, f_noderiv, &d);
445          }
446
447          case NLOPT_LN_NELDERMEAD: 
448          case NLOPT_LN_SBPLX: 
449          {
450               nlopt_result ret;
451               int freedx = 0;
452               if (!opt->dx) {
453                    freedx = 1;
454                    if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
455                         return NLOPT_OUT_OF_MEMORY;
456               }
457               if (algorithm == NLOPT_LN_NELDERMEAD)
458                    ret= nldrmd_minimize(n,f,f_data,lb,ub,x,minf,opt->dx,&stop);
459               else
460                    ret= sbplx_minimize(n,f,f_data,lb,ub,x,minf,opt->dx,&stop);
461               if (freedx) { free(opt->dx); opt->dx = NULL; }
462               return ret;
463          }
464
465          case NLOPT_LN_AUGLAG:
466          case NLOPT_LN_AUGLAG_EQ:
467          case NLOPT_LD_AUGLAG:
468          case NLOPT_LD_AUGLAG_EQ: {
469               nlopt_opt local_opt = opt->local_opt;
470               nlopt_result ret;
471               if (!local_opt) { /* default */
472                    local_opt = nlopt_create(
473                         algorithm == NLOPT_LN_AUGLAG || 
474                         algorithm == NLOPT_LN_AUGLAG_EQ
475                         ? nlopt_local_search_alg_nonderiv
476                         : nlopt_local_search_alg_deriv, n);
477                    if (!local_opt) return NLOPT_FAILURE;
478                    nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
479                    nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
480                    nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
481                    nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
482                    nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
483                    nlopt_set_initial_step(local_opt, opt->dx);
484               }
485               ret = auglag_minimize(n, f, f_data, 
486                                     opt->m, opt->fc, 
487                                     opt->p, opt->h,
488                                     lb, ub, x, minf, &stop,
489                                     local_opt,
490                                     algorithm == NLOPT_LN_AUGLAG_EQ
491                                     || algorithm == NLOPT_LD_AUGLAG_EQ);
492               if (!opt->local_opt) nlopt_destroy(local_opt);
493               return ret;
494          }
495
496          case NLOPT_GN_ISRES:
497               if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
498               return isres_minimize(n, f, f_data, 
499                                     opt->m, opt->fc,
500                                     opt->p, opt->h,
501                                     lb, ub, x, minf, &stop,
502                                     POP(0));
503
504          default:
505               return NLOPT_INVALID_ARGS;
506      }
507
508      return NLOPT_SUCCESS; /* never reached */
509 }
510
511 /*********************************************************************/
512
513 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf,
514                                     int maxeval, double maxtime)
515 {
516      int save_maxeval;
517      double save_maxtime;
518      nlopt_result ret;
519
520      if (!opt) return NLOPT_INVALID_ARGS;
521
522      save_maxeval = nlopt_get_maxeval(opt);
523      save_maxtime = nlopt_get_maxtime(opt);
524      
525      /* override opt limits if maxeval and/or maxtime are more stringent */
526      if (save_maxeval <= 0 || (maxeval > 0 && maxeval < save_maxeval))
527           nlopt_set_maxeval(opt, maxeval);
528      if (save_maxtime <= 0 || (maxtime > 0 && maxtime < save_maxtime))
529           nlopt_set_maxtime(opt, maxtime);
530
531      ret = nlopt_optimize(opt, x, minf);
532
533      nlopt_set_maxeval(opt, save_maxeval);
534      nlopt_set_maxtime(opt, save_maxtime);
535
536      return ret;
537 }
538
539 /*********************************************************************/