1 /* Copyright (c) 2007-2009 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(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(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(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)
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(int 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;
154 if (!opt || !x || !minf || !opt->f) return NLOPT_INVALID_ARGS;
156 /* copy a few params to local vars for convenience */
158 lb = opt->lb; ub = opt->ub;
159 algorithm = opt->algorithm;
160 f = opt->f; f_data = opt->f_data;
162 if (n == 0) { /* trivial case: no degrees of freedom */
163 *minf = f(n, x, NULL, f_data);
164 return NLOPT_SUCCESS;
174 /* make sure rand generator is inited */
175 if (!nlopt_srand_called)
176 nlopt_srand_time(); /* default is non-deterministic */
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;
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;
190 stop.maxeval = opt->maxeval;
191 stop.maxtime = opt->maxtime;
192 stop.start = nlopt_seconds();
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)));
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,
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)));
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,
225 algorithm == NLOPT_GN_ORIG_DIRECT
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;
242 case DIRECT_SIGMATOL:
243 return NLOPT_XTOL_REACHED;
244 case DIRECT_OUT_OF_MEMORY:
245 return NLOPT_OUT_OF_MEMORY;
250 case NLOPT_GD_STOGO_RAND:
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
256 return NLOPT_FAILURE;
259 return NLOPT_FAILURE;
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;
269 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
270 return NLOPT_OUT_OF_MEMORY;
272 iret = nlopt_subplex(f_bound, minf, x, n, &d, &stop, opt->dx);
273 if (freedx) { free(opt->dx); opt->dx = NULL; }
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 */
289 case NLOPT_LN_PRAXIS: {
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);
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));
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]
314 case -1: return NLOPT_INVALID_ARGS;
315 case -2: default: return NLOPT_FAILURE;
319 *minf = f(n, x, NULL, f_data);
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;
332 return luksan_plis(n, f, f_data, lb, ub, x, minf, &stop);
336 return luksan_plip(n, f, f_data, lb, ub, x, minf, &stop,
337 algorithm == NLOPT_LD_VAR1 ? 1 : 2);
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);
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,
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;
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);
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);
387 ret = mlsl_minimize(n, f, f_data, lb, ub, x, minf, &stop,
389 algorithm >= NLOPT_GN_MLSL_LDS);
390 if (!opt->local_opt) nlopt_destroy(local_opt);
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),
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));
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);
413 case NLOPT_LN_COBYLA: {
415 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
416 return NLOPT_OUT_OF_MEMORY;
417 return cobyla_minimize(n, f, f_data,
419 lb, ub, x, minf, &stop,
423 case NLOPT_LN_NEWUOA: {
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);
431 case NLOPT_LN_NEWUOA_BOUND: {
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);
439 case NLOPT_LN_BOBYQA: {
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);
447 case NLOPT_LN_NELDERMEAD:
454 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
455 return NLOPT_OUT_OF_MEMORY;
457 if (algorithm == NLOPT_LN_NELDERMEAD)
458 ret= nldrmd_minimize(n,f,f_data,lb,ub,x,minf,opt->dx,&stop);
460 ret= sbplx_minimize(n,f,f_data,lb,ub,x,minf,opt->dx,&stop);
461 if (freedx) { free(opt->dx); opt->dx = NULL; }
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;
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);
485 ret = auglag_minimize(n, f, f_data,
488 lb, ub, x, minf, &stop,
490 algorithm == NLOPT_LN_AUGLAG_EQ
491 || algorithm == NLOPT_LD_AUGLAG_EQ);
492 if (!opt->local_opt) nlopt_destroy(local_opt);
497 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
498 return isres_minimize(n, f, f_data,
501 lb, ub, x, minf, &stop,
505 return NLOPT_INVALID_ARGS;
508 return NLOPT_SUCCESS; /* never reached */
511 /*********************************************************************/
513 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf,
514 int maxeval, double maxtime)
520 if (!opt) return NLOPT_INVALID_ARGS;
522 save_maxeval = nlopt_get_maxeval(opt);
523 save_maxtime = nlopt_get_maxtime(opt);
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);
531 ret = nlopt_optimize(opt, x, minf);
533 nlopt_set_maxeval(opt, save_maxeval);
534 nlopt_set_maxtime(opt, save_maxtime);
539 /*********************************************************************/