1 /* Copyright (c) 2007-2010 Massachusetts Institute of Technology
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:
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
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.
27 #include "nlopt-internal.h"
29 /*********************************************************************/
32 static int my_isnan(double x) { return x != x; }
33 # define isnan my_isnan
36 /*********************************************************************/
48 # include "l-bfgs-b.h"
59 #include "neldermead.h"
64 /*********************************************************************/
69 const double *lb, *ub;
74 static double f_bound(int n, const double *x, void *data_)
77 nlopt_data *data = (nlopt_data *) data_;
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])
86 f = data->f((unsigned) n, x, NULL, data->f_data);
87 return (isnan(f) || nlopt_isinf(f) ? HUGE_VAL : f);
90 static double f_noderiv(int n, const double *x, void *data_)
92 nlopt_data *data = (nlopt_data *) data_;
93 return data->f((unsigned) n, x, NULL, data->f_data);
96 static double f_direct(int n, const double *x, int *undefined, void *data_)
98 nlopt_data *data = (nlopt_data *) data_;
100 f = data->f((unsigned) n, x, NULL, data->f_data);
101 *undefined = isnan(f) || nlopt_isinf(f);
105 /*********************************************************************/
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)
110 unsigned freedx = 0, i;
114 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
115 return NLOPT_OUT_OF_MEMORY;
119 for (i = 0; i < opt->n; ++i)
120 if (*step > fabs(opt->dx[i]))
121 *step = fabs(opt->dx[i]);
123 if (freedx) { free(opt->dx); opt->dx = NULL; }
124 return NLOPT_SUCCESS;
127 /*********************************************************************/
129 /* return true if [lb,ub] is finite in every dimension (n dimensions) */
130 static int finite_domain(unsigned n, const double *lb, const double *ub)
133 for (i = 0; i < n; ++i)
134 if (nlopt_isinf(ub[i] - lb[i])) return 0;
138 /*********************************************************************/
140 #define POP(defaultpop) (opt->stochastic_population > 0 ? \
141 opt->stochastic_population : \
142 (nlopt_stochastic_population > 0 ? \
143 nlopt_stochastic_population : (defaultpop)))
145 nlopt_result nlopt_optimize(nlopt_opt opt, double *x, double *minf)
147 const double *lb, *ub;
148 nlopt_algorithm algorithm;
149 nlopt_func f; void *f_data;
155 if (!opt || !x || !minf || !opt->f) return NLOPT_INVALID_ARGS;
157 /* copy a few params to local vars for convenience */
159 ni = (int) n; /* most of the subroutines take "int" arg */
160 lb = opt->lb; ub = opt->ub;
161 algorithm = opt->algorithm;
162 f = opt->f; f_data = opt->f_data;
164 if (n == 0) { /* trivial case: no degrees of freedom */
165 *minf = f(n, x, NULL, f_data);
166 return NLOPT_SUCCESS;
176 /* make sure rand generator is inited */
177 if (!nlopt_srand_called)
178 nlopt_srand_time(); /* default is non-deterministic */
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;
186 stop.minf_max = opt->minf_max;
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;
192 stop.maxeval = opt->maxeval;
193 stop.maxtime = opt->maxtime;
194 stop.start = nlopt_seconds();
197 case NLOPT_GN_DIRECT:
198 case NLOPT_GN_DIRECT_L:
199 case NLOPT_GN_DIRECT_L_RAND:
200 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
201 return cdirect(ni, f, f_data,
202 lb, ub, x, minf, &stop, 0.0,
203 (algorithm != NLOPT_GN_DIRECT)
204 + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND
205 ? 2 : (algorithm != NLOPT_GN_DIRECT))
206 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND
207 ? 1 : (algorithm != NLOPT_GN_DIRECT)));
209 case NLOPT_GN_DIRECT_NOSCAL:
210 case NLOPT_GN_DIRECT_L_NOSCAL:
211 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
212 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
213 return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf,
215 (algorithm != NLOPT_GN_DIRECT)
216 + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
217 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
219 case NLOPT_GN_ORIG_DIRECT:
220 case NLOPT_GN_ORIG_DIRECT_L:
221 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
222 switch (direct_optimize(f_direct, &d, ni, lb, ub, x, minf,
223 stop.maxeval, -1, 0.0, 0.0,
224 pow(stop.xtol_rel, (double) n), -1.0,
227 algorithm == NLOPT_GN_ORIG_DIRECT
229 : DIRECT_GABLONSKY)) {
230 case DIRECT_INVALID_BOUNDS:
231 case DIRECT_MAXFEVAL_TOOBIG:
232 case DIRECT_INVALID_ARGS:
233 return NLOPT_INVALID_ARGS;
234 case DIRECT_INIT_FAILED:
235 case DIRECT_SAMPLEPOINTS_FAILED:
236 case DIRECT_SAMPLE_FAILED:
237 return NLOPT_FAILURE;
238 case DIRECT_MAXFEVAL_EXCEEDED:
239 case DIRECT_MAXITER_EXCEEDED:
240 return NLOPT_MAXEVAL_REACHED;
241 case DIRECT_GLOBAL_FOUND:
242 return NLOPT_MINF_MAX_REACHED;
244 case DIRECT_SIGMATOL:
245 return NLOPT_XTOL_REACHED;
246 case DIRECT_OUT_OF_MEMORY:
247 return NLOPT_OUT_OF_MEMORY;
252 case NLOPT_GD_STOGO_RAND:
254 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
255 if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop,
256 algorithm == NLOPT_GD_STOGO
257 ? 0 : (int) POP(2*n)))
258 return NLOPT_FAILURE;
261 return NLOPT_FAILURE;
265 /* lacking a free/open-source license, we no longer use
266 Rowan's code, and instead use by "sbplx" re-implementation */
267 case NLOPT_LN_SUBPLEX: {
268 int iret, freedx = 0;
271 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
272 return NLOPT_OUT_OF_MEMORY;
274 iret = nlopt_subplex(f_bound, minf, x, n, &d, &stop, opt->dx);
275 if (freedx) { free(opt->dx); opt->dx = NULL; }
277 case -2: return NLOPT_INVALID_ARGS;
278 case -10: return NLOPT_MAXTIME_REACHED;
279 case -1: return NLOPT_MAXEVAL_REACHED;
280 case 0: return NLOPT_XTOL_REACHED;
281 case 1: return NLOPT_SUCCESS;
282 case 2: return NLOPT_MINF_MAX_REACHED;
283 case 20: return NLOPT_FTOL_REACHED;
284 case -200: return NLOPT_OUT_OF_MEMORY;
285 default: return NLOPT_FAILURE; /* unknown return code */
291 case NLOPT_LN_PRAXIS: {
293 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
294 return NLOPT_OUT_OF_MEMORY;
295 return praxis_(0.0, DBL_EPSILON,
296 step, ni, x, f_bound, &d, &stop, minf);
300 case NLOPT_LD_LBFGS_NOCEDAL: {
301 int iret, *nbd = (int *) malloc(sizeof(int) * n);
302 if (!nbd) return NLOPT_OUT_OF_MEMORY;
303 for (i = 0; i < n; ++i) {
304 int linf = nlopt_isinf(lb[i]) && lb[i] < 0;
305 int uinf = nlopt_isinf(ub[i]) && ub[i] > 0;
306 nbd[i] = linf && uinf ? 0 : (uinf ? 1 : (linf ? 3 : 2));
308 iret = lbfgsb_minimize(ni, f, f_data, x, nbd, lb, ub,
309 MIN(ni, 5), 0.0, stop.ftol_rel,
310 stop.xtol_abs[0] > 0 ? stop.xtol_abs[0]
316 case -1: return NLOPT_INVALID_ARGS;
317 case -2: default: return NLOPT_FAILURE;
321 *minf = f(n, x, NULL, f_data);
323 case 5: return NLOPT_MAXEVAL_REACHED;
324 case 2: return NLOPT_XTOL_REACHED;
325 case 1: return NLOPT_FTOL_REACHED;
326 default: return NLOPT_SUCCESS;
334 return luksan_plis(ni, f, f_data, lb, ub, x, minf, &stop);
338 return luksan_plip(ni, f, f_data, lb, ub, x, minf, &stop,
339 algorithm == NLOPT_LD_VAR1 ? 1 : 2);
341 case NLOPT_LD_TNEWTON:
342 case NLOPT_LD_TNEWTON_RESTART:
343 case NLOPT_LD_TNEWTON_PRECOND:
344 case NLOPT_LD_TNEWTON_PRECOND_RESTART:
345 return luksan_pnet(ni, f, f_data, lb, ub, x, minf, &stop,
346 1 + (algorithm - NLOPT_LD_TNEWTON) % 2,
347 1 + (algorithm - NLOPT_LD_TNEWTON) / 2);
349 case NLOPT_GN_CRS2_LM:
350 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
351 return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
356 case NLOPT_GN_MLSL_LDS:
357 case NLOPT_GD_MLSL_LDS: {
358 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
359 nlopt_opt local_opt = opt->local_opt;
361 if (!local_opt) { /* default */
362 nlopt_algorithm local_alg = (algorithm == NLOPT_GN_MLSL ||
363 algorithm == NLOPT_GN_MLSL_LDS)
364 ? nlopt_local_search_alg_nonderiv
365 : nlopt_local_search_alg_deriv;
366 /* don't call MLSL recursively! */
367 if (local_alg >= NLOPT_GN_MLSL
368 && local_alg <= NLOPT_GD_MLSL_LDS)
369 local_alg = (algorithm == NLOPT_GN_MLSL ||
370 algorithm == NLOPT_GN_MLSL_LDS)
371 ? NLOPT_LN_COBYLA : NLOPT_LD_MMA;
372 local_opt = nlopt_create(local_alg, n);
373 if (!local_opt) return NLOPT_FAILURE;
374 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
375 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
376 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
377 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
378 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
379 nlopt_set_initial_step(local_opt, opt->dx);
381 for (i = 0; i < n && stop.xtol_abs[i] > 0; ++i) ;
382 if (local_opt->ftol_rel <= 0 && local_opt->ftol_abs <= 0 &&
383 local_opt->xtol_rel <= 0 && i < n) {
384 /* it is not sensible to call MLSL without *some*
385 nonzero tolerance for the local search */
386 nlopt_set_ftol_rel(local_opt, 1e-15);
387 nlopt_set_xtol_rel(local_opt, 1e-7);
389 ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
390 local_opt, (int) POP(0),
391 algorithm >= NLOPT_GN_MLSL_LDS);
392 if (!opt->local_opt) nlopt_destroy(local_opt);
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),
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));
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);
415 case NLOPT_LN_COBYLA: {
417 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
418 return NLOPT_OUT_OF_MEMORY;
419 return cobyla_minimize(ni, f, f_data,
421 lb, ub, x, minf, &stop,
425 case NLOPT_LN_NEWUOA: {
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, &d);
433 case NLOPT_LN_NEWUOA_BOUND: {
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, &d);
441 case NLOPT_LN_BOBYQA: {
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, &d);
449 case NLOPT_LN_NELDERMEAD:
456 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
457 return NLOPT_OUT_OF_MEMORY;
459 if (algorithm == NLOPT_LN_NELDERMEAD)
460 ret= nldrmd_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
462 ret= sbplx_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
463 if (freedx) { free(opt->dx); opt->dx = NULL; }
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;
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);
487 ret = auglag_minimize(ni, f, f_data,
490 lb, ub, x, minf, &stop,
492 algorithm == NLOPT_LN_AUGLAG_EQ
493 || algorithm == NLOPT_LD_AUGLAG_EQ);
494 if (!opt->local_opt) nlopt_destroy(local_opt);
499 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
500 return isres_minimize(ni, f, f_data,
501 (int) (opt->m), opt->fc,
502 (int) (opt->p), opt->h,
503 lb, ub, x, minf, &stop,
507 return NLOPT_INVALID_ARGS;
510 return NLOPT_SUCCESS; /* never reached */
513 /*********************************************************************/
515 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf,
516 int maxeval, double maxtime)
522 if (!opt) return NLOPT_INVALID_ARGS;
524 save_maxeval = nlopt_get_maxeval(opt);
525 save_maxtime = nlopt_get_maxtime(opt);
527 /* override opt limits if maxeval and/or maxtime are more stringent */
528 if (save_maxeval <= 0 || (maxeval > 0 && maxeval < save_maxeval))
529 nlopt_set_maxeval(opt, maxeval);
530 if (save_maxtime <= 0 || (maxtime > 0 && maxtime < save_maxtime))
531 nlopt_set_maxtime(opt, maxtime);
533 ret = nlopt_optimize(opt, x, minf);
535 nlopt_set_maxeval(opt, save_maxeval);
536 nlopt_set_maxtime(opt, save_maxtime);
541 /*********************************************************************/