1 /* Copyright (c) 2007-2014 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 /*********************************************************************/
51 #include "neldermead.h"
58 /*********************************************************************/
60 static double f_bound(int n, const double *x, void *data_)
63 nlopt_opt data = (nlopt_opt) data_;
66 /* some methods do not support bound constraints, but support
67 discontinuous objectives so we can just return Inf for invalid x */
68 for (i = 0; i < n; ++i)
69 if (x[i] < data->lb[i] || x[i] > data->ub[i])
72 f = data->f((unsigned) n, x, NULL, data->f_data);
73 return (nlopt_isnan(f) || nlopt_isinf(f) ? HUGE_VAL : f);
76 static double f_noderiv(int n, const double *x, void *data_)
78 nlopt_opt data = (nlopt_opt) data_;
79 return data->f((unsigned) n, x, NULL, data->f_data);
82 static double f_direct(int n, const double *x, int *undefined, void *data_)
84 nlopt_opt data = (nlopt_opt) data_;
85 double *work = (double *) data->work;
88 f = data->f((unsigned) n, x, NULL, data->f_data);
90 *undefined = nlopt_isnan(f) || nlopt_isinf(f);
91 if (nlopt_get_force_stop(data))
93 for (i = 0; i < data->m && !*undefined; ++i) {
94 nlopt_eval_constraint(work, NULL, data->fc + i, (unsigned) n, x);
95 if (nlopt_get_force_stop(data))
97 for (j = 0; j < data->fc[i].m; ++j)
104 /*********************************************************************/
106 /* get min(dx) for algorithms requiring a scalar initial step size */
107 static nlopt_result initial_step(nlopt_opt opt, const double *x, double *step)
109 unsigned freedx = 0, i;
113 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
114 return NLOPT_OUT_OF_MEMORY;
118 for (i = 0; i < opt->n; ++i)
119 if (*step > fabs(opt->dx[i]))
120 *step = fabs(opt->dx[i]);
126 return NLOPT_SUCCESS;
129 /*********************************************************************/
131 /* return true if [lb,ub] is finite in every dimension (n dimensions) */
132 static int finite_domain(unsigned n, const double *lb, const double *ub)
135 for (i = 0; i < n; ++i)
136 if (nlopt_isinf(ub[i] - lb[i]))
141 /*********************************************************************/
142 /* wrapper functions, only for derivative-free methods, that
143 eliminate dimensions with lb == ub. (The gradient-based methods
144 should handle this case directly, since they operate on much
145 larger vectors where I am loathe to make copies unnecessarily.) */
151 unsigned n; /* true dimension */
152 double *x; /* scratch vector of length n */
153 double *grad; /* optional scratch vector of length n */
154 const double *lb, *ub; /* bounds, of length n */
157 static void *elimdim_makedata(nlopt_func f, nlopt_mfunc mf, void *f_data, unsigned n, double *x, const double *lb, const double *ub, double *grad)
159 elimdim_data *d = (elimdim_data *) malloc(sizeof(elimdim_data));
173 static double elimdim_func(unsigned n0, const double *x0, double *grad, void *d_)
175 elimdim_data *d = (elimdim_data *) d_;
177 const double *lb = d->lb, *ub = d->ub;
179 unsigned n = d->n, i, j;
181 (void) n0; /* unused */
182 for (i = j = 0; i < n; ++i) {
185 else /* assert: j < n0 */
188 val = d->f(n, x, grad ? d->grad : NULL, d->f_data);
190 /* assert: d->grad != NULL */
191 for (i = j = 0; i < n; ++i)
193 grad[j++] = d->grad[i];
198 static void elimdim_mfunc(unsigned m, double *result, unsigned n0, const double *x0, double *grad, void *d_)
200 elimdim_data *d = (elimdim_data *) d_;
202 const double *lb = d->lb, *ub = d->ub;
203 unsigned n = d->n, i, j;
205 (void) n0; /* unused */
206 (void) grad; /* assert: grad == NULL */
207 for (i = j = 0; i < n; ++i) {
210 else /* assert: j < n0 */
213 d->mf(m, result, n, x, NULL, d->f_data);
216 /* compute the eliminated dimension: number of dims with lb[i] != ub[i] */
217 static unsigned elimdim_dimension(unsigned n, const double *lb, const double *ub)
220 for (i = 0; i < n; ++i)
221 n0 += lb[i] != ub[i] ? 1U : 0;
225 /* modify v to "shrunk" version, with dimensions for lb[i] == ub[i] elim'ed */
226 static void elimdim_shrink(unsigned n, double *v, const double *lb, const double *ub)
230 for (i = j = 0; i < n; ++i)
235 /* inverse of elimdim_shrink */
236 static void elimdim_expand(unsigned n, double *v, const double *lb, const double *ub)
240 j = elimdim_dimension(n, lb, ub) - 1;
241 for (i = n - 1; i > 0; --i) {
252 /* given opt, create a new opt with equal-constraint dimensions eliminated */
253 static nlopt_opt elimdim_create(nlopt_opt opt)
256 nlopt_munge munge_copy_save = opt->munge_on_copy;
257 double *x, *grad = NULL;
260 opt->munge_on_copy = 0; /* hack: since this is an internal copy,
261 we can leave it un-munged; see issue #26 */
262 opt0 = nlopt_copy(opt);
263 opt->munge_on_copy = munge_copy_save;
266 x = (double *) malloc(sizeof(double) * opt->n);
272 if (opt->algorithm == NLOPT_GD_STOGO || opt->algorithm == NLOPT_GD_STOGO_RAND) {
273 grad = (double *) malloc(sizeof(double) * opt->n);
278 opt0->n = elimdim_dimension(opt->n, opt->lb, opt->ub);
279 elimdim_shrink(opt->n, opt0->lb, opt->lb, opt->ub);
280 elimdim_shrink(opt->n, opt0->ub, opt->lb, opt->ub);
281 elimdim_shrink(opt->n, opt0->xtol_abs, opt->lb, opt->ub);
282 elimdim_shrink(opt->n, opt0->dx, opt->lb, opt->ub);
284 opt0->munge_on_destroy = opt0->munge_on_copy = NULL;
286 opt0->f = elimdim_func;
287 opt0->f_data = elimdim_makedata(opt->f, NULL, opt->f_data, opt->n, x, opt->lb, opt->ub, grad);
291 for (i = 0; i < opt->m; ++i) {
292 opt0->fc[i].f = opt0->fc[i].f ? elimdim_func : NULL;
293 opt0->fc[i].mf = opt0->fc[i].mf ? elimdim_mfunc : NULL;
294 opt0->fc[i].f_data = elimdim_makedata(opt->fc[i].f, opt->fc[i].mf, opt->fc[i].f_data, opt->n, x, opt->lb, opt->ub, NULL);
295 if (!opt0->fc[i].f_data)
299 for (i = 0; i < opt->p; ++i) {
300 opt0->h[i].f = opt0->h[i].f ? elimdim_func : NULL;
301 opt0->h[i].mf = opt0->h[i].mf ? elimdim_mfunc : NULL;
302 opt0->h[i].f_data = elimdim_makedata(opt->h[i].f, opt->h[i].mf, opt->h[i].f_data, opt->n, x, opt->lb, opt->ub, NULL);
303 if (!opt0->h[i].f_data)
315 /* like nlopt_destroy, but also frees elimdim_data */
316 static void elimdim_destroy(nlopt_opt opt)
322 free(((elimdim_data *) opt->f_data)->x);
323 free(((elimdim_data *) opt->f_data)->grad);
327 for (i = 0; i < opt->m; ++i) {
328 free(opt->fc[i].f_data);
329 opt->fc[i].f_data = NULL;
331 for (i = 0; i < opt->p; ++i) {
332 free(opt->h[i].f_data);
333 opt->h[i].f_data = NULL;
339 /* return whether to use elimdim wrapping. */
340 static int elimdim_wrapcheck(nlopt_opt opt)
344 if (elimdim_dimension(opt->n, opt->lb, opt->ub) == opt->n)
346 switch (opt->algorithm) {
347 case NLOPT_GN_DIRECT:
348 case NLOPT_GN_DIRECT_L:
349 case NLOPT_GN_DIRECT_L_RAND:
350 case NLOPT_GN_DIRECT_NOSCAL:
351 case NLOPT_GN_DIRECT_L_NOSCAL:
352 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
353 case NLOPT_GN_ORIG_DIRECT:
354 case NLOPT_GN_ORIG_DIRECT_L:
355 case NLOPT_GN_CRS2_LM:
356 case NLOPT_LN_PRAXIS:
357 case NLOPT_LN_COBYLA:
358 case NLOPT_LN_NEWUOA:
359 case NLOPT_LN_NEWUOA_BOUND:
360 case NLOPT_LN_BOBYQA:
361 case NLOPT_LN_NELDERMEAD:
367 case NLOPT_GD_STOGO_RAND:
375 /*********************************************************************/
377 #define POP(defaultpop) (opt->stochastic_population > 0 ? opt->stochastic_population : (nlopt_stochastic_population > 0 ? nlopt_stochastic_population : (defaultpop)))
379 /* unlike nlopt_optimize() below, only handles minimization case */
380 static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
382 const double *lb, *ub;
383 nlopt_algorithm algorithm;
390 if (!opt || !x || !minf || !opt->f || opt->maximize)
391 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "NULL args to nlopt_optimize_");
393 /* reset stopping flag */
394 nlopt_set_force_stop(opt, 0);
395 opt->force_stop_child = NULL;
397 /* copy a few params to local vars for convenience */
399 ni = (int) n; /* most of the subroutines take "int" arg */
402 algorithm = opt->algorithm;
404 f_data = opt->f_data;
406 if (n == 0) { /* trivial case: no degrees of freedom */
407 *minf = opt->f(n, x, NULL, opt->f_data);
408 return NLOPT_SUCCESS;
413 /* make sure rand generator is inited */
414 nlopt_srand_time_default(); /* default is non-deterministic */
416 /* check bound constraints */
417 for (i = 0; i < n; ++i)
418 if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i]) {
419 nlopt_set_errmsg(opt, "bounds %d fail %g <= %g <= %g", i, lb[i], x[i], ub[i]);
420 return NLOPT_INVALID_ARGS;
424 stop.minf_max = opt->stopval;
425 stop.ftol_rel = opt->ftol_rel;
426 stop.ftol_abs = opt->ftol_abs;
427 stop.xtol_rel = opt->xtol_rel;
428 stop.xtol_abs = opt->xtol_abs;
430 stop.nevals_p = &(opt->numevals);
431 stop.maxeval = opt->maxeval;
432 stop.maxtime = opt->maxtime;
433 stop.start = nlopt_seconds();
434 stop.force_stop = &(opt->force_stop);
435 stop.stop_msg = &(opt->errmsg);
438 case NLOPT_GN_DIRECT:
439 case NLOPT_GN_DIRECT_L:
440 case NLOPT_GN_DIRECT_L_RAND:
441 if (!finite_domain(n, lb, ub))
442 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
443 return cdirect(ni, f, f_data, lb, ub, x, minf, &stop, 0.0, (algorithm != NLOPT_GN_DIRECT) + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
444 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
446 case NLOPT_GN_DIRECT_NOSCAL:
447 case NLOPT_GN_DIRECT_L_NOSCAL:
448 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
449 if (!finite_domain(n, lb, ub))
450 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
451 return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf, &stop, 0.0, (algorithm != NLOPT_GN_DIRECT) + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
452 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
454 case NLOPT_GN_ORIG_DIRECT:
455 case NLOPT_GN_ORIG_DIRECT_L:
457 direct_return_code dret;
458 if (!finite_domain(n, lb, ub))
459 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
460 opt->work = malloc(sizeof(double) * nlopt_max_constraint_dim(opt->m, opt->fc));
462 return NLOPT_OUT_OF_MEMORY;
463 dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
465 stop.start, stop.maxtime,
466 0.0, 0.0, pow(stop.xtol_rel, (double) n), -1.0, stop.force_stop, stop.minf_max, 0.0, NULL, algorithm == NLOPT_GN_ORIG_DIRECT ? DIRECT_ORIGINAL : DIRECT_GABLONSKY);
470 case DIRECT_INVALID_BOUNDS:
471 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "invalid bounds for DIRECT");
472 case DIRECT_MAXFEVAL_TOOBIG:
473 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "maxeval too big for DIRECT");
474 case DIRECT_INVALID_ARGS:
475 return NLOPT_INVALID_ARGS;
476 case DIRECT_INIT_FAILED:
477 RETURN_ERR(NLOPT_FAILURE, opt, "init failed for DIRECT");
478 case DIRECT_SAMPLEPOINTS_FAILED:
479 RETURN_ERR(NLOPT_FAILURE, opt, "sample-points failed for DIRECT");
480 case DIRECT_SAMPLE_FAILED:
481 RETURN_ERR(NLOPT_FAILURE, opt, "sample failed for DIRECT");
482 case DIRECT_MAXFEVAL_EXCEEDED:
483 case DIRECT_MAXITER_EXCEEDED:
484 return NLOPT_MAXEVAL_REACHED;
485 case DIRECT_MAXTIME_EXCEEDED:
486 return NLOPT_MAXTIME_REACHED;
487 case DIRECT_GLOBAL_FOUND:
488 return NLOPT_MINF_MAX_REACHED;
490 case DIRECT_SIGMATOL:
491 return NLOPT_XTOL_REACHED;
492 case DIRECT_OUT_OF_MEMORY:
493 return NLOPT_OUT_OF_MEMORY;
494 case DIRECT_FORCED_STOP:
495 return NLOPT_FORCED_STOP;
502 if (!finite_domain(n, lb, ub))
503 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
504 return ags_minimize(ni, f, f_data, opt->m, opt->fc, x, minf, lb, ub, &stop);
507 return NLOPT_INVALID_ARGS;
511 case NLOPT_GD_STOGO_RAND:
513 if (!finite_domain(n, lb, ub))
514 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
515 if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop, algorithm == NLOPT_GD_STOGO ? 0 : (int) POP(2 * n)))
516 return NLOPT_FAILURE;
519 return NLOPT_INVALID_ARGS;
523 /* lacking a free/open-source license, we no longer use
524 Rowan's code, and instead use by "sbplx" re-implementation */
525 case NLOPT_LN_SUBPLEX:
527 int iret, freedx = 0;
530 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
531 return NLOPT_OUT_OF_MEMORY;
533 iret = nlopt_subplex(f_bound, minf, x, n, opt, &stop, opt->dx);
540 return NLOPT_INVALID_ARGS;
542 return NLOPT_FORCED_STOP;
544 return NLOPT_MAXTIME_REACHED;
546 return NLOPT_MAXEVAL_REACHED;
548 return NLOPT_XTOL_REACHED;
550 return NLOPT_SUCCESS;
552 return NLOPT_MINF_MAX_REACHED;
554 return NLOPT_FTOL_REACHED;
556 return NLOPT_OUT_OF_MEMORY;
558 return NLOPT_FAILURE; /* unknown return code */
564 case NLOPT_LN_PRAXIS:
567 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
568 return NLOPT_OUT_OF_MEMORY;
569 return praxis_(0.0, DBL_EPSILON, step, ni, x, f_bound, opt, &stop, minf);
573 return luksan_plis(ni, f, f_data, lb, ub, x, minf, &stop, opt->vector_storage);
577 return luksan_plip(ni, f, f_data, lb, ub, x, minf, &stop, opt->vector_storage, algorithm == NLOPT_LD_VAR1 ? 1 : 2);
579 case NLOPT_LD_TNEWTON:
580 case NLOPT_LD_TNEWTON_RESTART:
581 case NLOPT_LD_TNEWTON_PRECOND:
582 case NLOPT_LD_TNEWTON_PRECOND_RESTART:
583 return luksan_pnet(ni, f, f_data, lb, ub, x, minf, &stop, opt->vector_storage, 1 + (algorithm - NLOPT_LD_TNEWTON) % 2, 1 + (algorithm - NLOPT_LD_TNEWTON) / 2);
585 case NLOPT_GN_CRS2_LM:
586 if (!finite_domain(n, lb, ub))
587 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
588 return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop, (int) POP(0), 0);
591 case NLOPT_G_MLSL_LDS:
594 case NLOPT_GN_MLSL_LDS:
595 case NLOPT_GD_MLSL_LDS:
597 nlopt_opt local_opt = opt->local_opt;
599 if (!finite_domain(n, lb, ub))
600 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
601 if (!local_opt && (algorithm == NLOPT_G_MLSL || algorithm == NLOPT_G_MLSL_LDS))
602 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "local optimizer must be specified for G_MLSL");
603 if (!local_opt) { /* default */
604 nlopt_algorithm local_alg = (algorithm == NLOPT_GN_MLSL || algorithm == NLOPT_GN_MLSL_LDS)
605 ? nlopt_local_search_alg_nonderiv : nlopt_local_search_alg_deriv;
606 /* don't call MLSL recursively! */
607 if (local_alg >= NLOPT_GN_MLSL && local_alg <= NLOPT_GD_MLSL_LDS)
608 local_alg = (algorithm == NLOPT_GN_MLSL || algorithm == NLOPT_GN_MLSL_LDS)
609 ? NLOPT_LN_COBYLA : NLOPT_LD_MMA;
610 local_opt = nlopt_create(local_alg, n);
612 RETURN_ERR(NLOPT_FAILURE, opt, "failed to create local_opt");
613 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
614 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
615 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
616 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
617 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
620 nlopt_set_initial_step(local_opt, opt->dx);
621 for (i = 0; i < n && stop.xtol_abs[i] > 0; ++i);
622 if (local_opt->ftol_rel <= 0 && local_opt->ftol_abs <= 0 && local_opt->xtol_rel <= 0 && i < n) {
623 /* it is not sensible to call MLSL without *some*
624 nonzero tolerance for the local search */
625 nlopt_set_ftol_rel(local_opt, 1e-15);
626 nlopt_set_xtol_rel(local_opt, 1e-7);
628 opt->force_stop_child = local_opt;
629 ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop, local_opt, (int) POP(0), algorithm >= NLOPT_GN_MLSL_LDS && algorithm != NLOPT_G_MLSL);
630 opt->force_stop_child = NULL;
632 nlopt_destroy(local_opt);
641 #define LO(param, def) (opt->local_opt ? opt->local_opt->param : (def))
642 dual_opt = nlopt_create(LO(algorithm, nlopt_local_search_alg_deriv), nlopt_count_constraints(opt->m, opt->fc));
644 RETURN_ERR(NLOPT_FAILURE, opt, "failed creating dual optimizer");
645 nlopt_set_ftol_rel(dual_opt, LO(ftol_rel, 1e-14));
646 nlopt_set_ftol_abs(dual_opt, LO(ftol_abs, 0.0));
647 nlopt_set_maxeval(dual_opt, LO(maxeval, 100000));
650 if (algorithm == NLOPT_LD_MMA)
651 ret = mma_minimize(n, f, f_data, opt->m, opt->fc, lb, ub, x, minf, &stop, dual_opt);
653 ret = ccsa_quadratic_minimize(n, f, f_data, opt->m, opt->fc, opt->pre, lb, ub, x, minf, &stop, dual_opt);
654 nlopt_destroy(dual_opt);
658 case NLOPT_LN_COBYLA:
664 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
665 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt, "failed to allocate initial step");
667 ret = cobyla_minimize(n, f, f_data, opt->m, opt->fc, opt->p, opt->h, lb, ub, x, minf, &stop, opt->dx);
675 case NLOPT_LN_NEWUOA:
678 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
679 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt, "failed to allocate initial step");
680 return newuoa(ni, 2 * n + 1, x, 0, 0, step, &stop, minf, f_noderiv, opt);
683 case NLOPT_LN_NEWUOA_BOUND:
686 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
687 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt, "failed to allocate initial step");
688 return newuoa(ni, 2 * n + 1, x, lb, ub, step, &stop, minf, f_noderiv, opt);
691 case NLOPT_LN_BOBYQA:
697 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
698 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt, "failed to allocate initial step");
700 ret = bobyqa(ni, 2 * n + 1, x, lb, ub, opt->dx, &stop, minf, opt->f, opt->f_data);
708 case NLOPT_LN_NELDERMEAD:
715 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
716 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt, "failed to allocate initial step");
718 if (algorithm == NLOPT_LN_NELDERMEAD)
719 ret = nldrmd_minimize(ni, f, f_data, lb, ub, x, minf, opt->dx, &stop);
721 ret = sbplx_minimize(ni, f, f_data, lb, ub, x, minf, opt->dx, &stop);
730 case NLOPT_AUGLAG_EQ:
731 case NLOPT_LN_AUGLAG:
732 case NLOPT_LN_AUGLAG_EQ:
733 case NLOPT_LD_AUGLAG:
734 case NLOPT_LD_AUGLAG_EQ:
736 nlopt_opt local_opt = opt->local_opt;
738 if ((algorithm == NLOPT_AUGLAG || algorithm == NLOPT_AUGLAG_EQ)
740 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "local optimizer must be specified for AUGLAG");
741 if (!local_opt) { /* default */
742 local_opt = nlopt_create(algorithm == NLOPT_LN_AUGLAG || algorithm == NLOPT_LN_AUGLAG_EQ ? nlopt_local_search_alg_nonderiv : nlopt_local_search_alg_deriv, n);
744 RETURN_ERR(NLOPT_FAILURE, opt, "failed to create local_opt");
745 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
746 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
747 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
748 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
749 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
752 nlopt_set_initial_step(local_opt, opt->dx);
753 opt->force_stop_child = local_opt;
754 ret = auglag_minimize(ni, f, f_data,
756 opt->p, opt->h, lb, ub, x, minf, &stop, local_opt, algorithm == NLOPT_AUGLAG_EQ || algorithm == NLOPT_LN_AUGLAG_EQ || algorithm == NLOPT_LD_AUGLAG_EQ);
757 opt->force_stop_child = NULL;
759 nlopt_destroy(local_opt);
764 if (!finite_domain(n, lb, ub))
765 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
766 return isres_minimize(ni, f, f_data, (int) (opt->m), opt->fc, (int) (opt->p), opt->h, lb, ub, x, minf, &stop, (int) POP(0));
769 if (!finite_domain(n, lb, ub))
770 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "finite domain required for global algorithm");
771 return chevolutionarystrategy(n, f, f_data, lb, ub, x, minf, &stop, (unsigned) POP(0), (unsigned) (POP(0) * 1.5));
774 return nlopt_slsqp(n, f, f_data, opt->m, opt->fc, opt->p, opt->h, lb, ub, x, minf, &stop);
777 return NLOPT_INVALID_ARGS;
780 return NLOPT_SUCCESS; /* never reached */
783 /*********************************************************************/
791 /* wrapper for maximizing: just flip the sign of f and grad */
792 static double f_max(unsigned n, const double *x, double *grad, void *data)
794 f_max_data *d = (f_max_data *) data;
795 double val = d->f(n, x, grad, d->f_data);
798 for (i = 0; i < n; ++i)
804 static void pre_max(unsigned n, const double *x, const double *v, double *vpre, void *data)
806 f_max_data *d = (f_max_data *) data;
808 d->pre(n, x, v, vpre, d->f_data);
809 for (i = 0; i < n; ++i)
813 nlopt_result NLOPT_STDCALL nlopt_optimize(nlopt_opt opt, double *x, double *opt_f)
822 nlopt_unset_errmsg(opt);
823 if (!opt || !opt_f || !opt->f)
824 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "NULL args to nlopt_optimize");
826 f_data = opt->f_data;
829 /* for maximizing, just minimize the f_max wrapper, which
830 flips the sign of everything */
831 if ((maximize = opt->maximize)) {
839 opt->stopval = -opt->stopval;
843 { /* possibly eliminate lb == ub dimensions for some algorithms */
844 nlopt_opt elim_opt = opt;
845 if (elimdim_wrapcheck(opt)) {
846 elim_opt = elimdim_create(opt);
848 nlopt_set_errmsg(opt, "failure allocating elim_opt");
849 ret = NLOPT_OUT_OF_MEMORY;
852 elimdim_shrink(opt->n, x, opt->lb, opt->ub);
855 ret = nlopt_optimize_(elim_opt, x, opt_f);
857 if (elim_opt != opt) {
858 elimdim_destroy(elim_opt);
859 elimdim_expand(opt->n, x, opt->lb, opt->ub);
864 if (maximize) { /* restore original signs */
865 opt->maximize = maximize;
866 opt->stopval = -opt->stopval;
868 opt->f_data = f_data;
876 /*********************************************************************/
878 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf, int maxeval, double maxtime)
884 nlopt_unset_errmsg(opt);
887 RETURN_ERR(NLOPT_INVALID_ARGS, opt, "NULL opt arg");
889 save_maxeval = nlopt_get_maxeval(opt);
890 save_maxtime = nlopt_get_maxtime(opt);
892 /* override opt limits if maxeval and/or maxtime are more stringent */
893 if (save_maxeval <= 0 || (maxeval > 0 && maxeval < save_maxeval))
894 nlopt_set_maxeval(opt, maxeval);
895 if (save_maxtime <= 0 || (maxtime > 0 && maxtime < save_maxtime))
896 nlopt_set_maxtime(opt, maxtime);
898 ret = nlopt_optimize(opt, x, minf);
900 nlopt_set_maxeval(opt, save_maxeval);
901 nlopt_set_maxtime(opt, save_maxtime);
906 /*********************************************************************/