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 /*********************************************************************/
48 #include "neldermead.h"
55 /*********************************************************************/
57 static double f_bound(int n, const double *x, void *data_)
60 nlopt_opt data = (nlopt_opt) data_;
63 /* some methods do not support bound constraints, but support
64 discontinuous objectives so we can just return Inf for invalid x */
65 for (i = 0; i < n; ++i)
66 if (x[i] < data->lb[i] || x[i] > data->ub[i])
69 f = data->f((unsigned) n, x, NULL, data->f_data);
70 return (nlopt_isnan(f) || nlopt_isinf(f) ? HUGE_VAL : f);
73 static double f_noderiv(int n, const double *x, void *data_)
75 nlopt_opt data = (nlopt_opt) data_;
76 return data->f((unsigned) n, x, NULL, data->f_data);
79 static double f_direct(int n, const double *x, int *undefined, void *data_)
81 nlopt_opt data = (nlopt_opt) data_;
82 double *work = (double*) data->work;
85 f = data->f((unsigned) n, x, NULL, data->f_data);
86 *undefined = nlopt_isnan(f) || nlopt_isinf(f);
87 if (nlopt_get_force_stop(data)) return f;
88 for (i = 0; i < data->m && !*undefined; ++i) {
89 nlopt_eval_constraint(work, NULL, data->fc+i, (unsigned) n, x);
90 if (nlopt_get_force_stop(data)) return f;
91 for (j = 0; j < data->fc[i].m; ++j)
98 /*********************************************************************/
100 /* get min(dx) for algorithms requiring a scalar initial step size */
101 static nlopt_result initial_step(nlopt_opt opt, const double *x, double *step)
103 unsigned freedx = 0, i;
107 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
108 return NLOPT_OUT_OF_MEMORY;
112 for (i = 0; i < opt->n; ++i)
113 if (*step > fabs(opt->dx[i]))
114 *step = fabs(opt->dx[i]);
116 if (freedx) { free(opt->dx); opt->dx = NULL; }
117 return NLOPT_SUCCESS;
120 /*********************************************************************/
122 /* return true if [lb,ub] is finite in every dimension (n dimensions) */
123 static int finite_domain(unsigned n, const double *lb, const double *ub)
126 for (i = 0; i < n; ++i)
127 if (nlopt_isinf(ub[i] - lb[i])) return 0;
131 /*********************************************************************/
132 /* wrapper functions, only for derivative-free methods, that
133 eliminate dimensions with lb == ub. (The gradient-based methods
134 should handle this case directly, since they operate on much
135 larger vectors where I am loathe to make copies unnecessarily.) */
141 unsigned n; /* true dimension */
142 double *x; /* scratch vector of length n */
143 double *grad; /* optional scratch vector of length n */
144 const double *lb, *ub; /* bounds, of length n */
147 static void *elimdim_makedata(nlopt_func f, nlopt_mfunc mf, void *f_data,
148 unsigned n, double *x, const double *lb,
149 const double *ub, double *grad)
151 elimdim_data *d = (elimdim_data *) malloc(sizeof(elimdim_data));
153 d->f = f; d->mf = mf; d->f_data = f_data; d->n = n; d->x = x;
154 d->lb = lb; d->ub = ub;
159 static double elimdim_func(unsigned n0, const double *x0, double *grad, void *d_)
161 elimdim_data *d = (elimdim_data *) d_;
163 const double *lb = d->lb, *ub = d->ub;
165 unsigned n = d->n, i, j;
167 (void) n0; /* unused */
168 for (i = j = 0; i < n; ++i) {
171 else /* assert: j < n0 */
174 val = d->f(n, x, grad ? d->grad : NULL, d->f_data);
176 /* assert: d->grad != NULL */
177 for (i = j = 0; i < n; ++i)
179 grad[j++] = d->grad[i];
185 static void elimdim_mfunc(unsigned m, double *result,
186 unsigned n0, const double *x0, double *grad, void *d_)
188 elimdim_data *d = (elimdim_data *) d_;
190 const double *lb = d->lb, *ub = d->ub;
191 unsigned n = d->n, i, j;
193 (void) n0; /* unused */
194 (void) grad; /* assert: grad == NULL */
195 for (i = j = 0; i < n; ++i) {
198 else /* assert: j < n0 */
201 d->mf(m, result, n, x, NULL, d->f_data);
204 /* compute the eliminated dimension: number of dims with lb[i] != ub[i] */
205 static unsigned elimdim_dimension(unsigned n, const double *lb, const double *ub)
208 for (i = 0; i < n; ++i) n0 += lb[i] != ub[i] ? 1U : 0;
212 /* modify v to "shrunk" version, with dimensions for lb[i] == ub[i] elim'ed */
213 static void elimdim_shrink(unsigned n, double *v,
214 const double *lb, const double *ub)
218 for (i = j = 0; i < n; ++i)
223 /* inverse of elimdim_shrink */
224 static void elimdim_expand(unsigned n, double *v,
225 const double *lb, const double *ub)
229 j = elimdim_dimension(n, lb, ub) - 1;
230 for (i = n - 1; i > 0; --i) {
241 /* given opt, create a new opt with equal-constraint dimensions eliminated */
242 static nlopt_opt elimdim_create(nlopt_opt opt)
245 nlopt_munge munge_copy_save = opt->munge_on_copy;
246 double *x, *grad = NULL;
249 opt->munge_on_copy = 0; /* hack: since this is an internal copy,
250 we can leave it un-munged; see issue #26 */
251 opt0 = nlopt_copy(opt);
252 opt->munge_on_copy = munge_copy_save;
253 if (!opt0) return NULL;
254 x = (double *) malloc(sizeof(double) * opt->n);
255 if (opt->n && !x) { nlopt_destroy(opt0); return NULL; }
257 if (opt->algorithm == NLOPT_GD_STOGO
258 || opt->algorithm == NLOPT_GD_STOGO_RAND) {
259 grad = (double *) malloc(sizeof(double) * opt->n);
260 if (opt->n && !grad) goto bad;
263 opt0->n = elimdim_dimension(opt->n, opt->lb, opt->ub);
264 elimdim_shrink(opt->n, opt0->lb, opt->lb, opt->ub);
265 elimdim_shrink(opt->n, opt0->ub, opt->lb, opt->ub);
266 elimdim_shrink(opt->n, opt0->xtol_abs, opt->lb, opt->ub);
267 elimdim_shrink(opt->n, opt0->dx, opt->lb, opt->ub);
269 opt0->munge_on_destroy = opt0->munge_on_copy = NULL;
271 opt0->f = elimdim_func;
272 opt0->f_data = elimdim_makedata(opt->f, NULL, opt->f_data,
273 opt->n, x, opt->lb, opt->ub, grad);
274 if (!opt0->f_data) goto bad;
276 for (i = 0; i < opt->m; ++i) {
277 opt0->fc[i].f = elimdim_func;
278 opt0->fc[i].mf = elimdim_mfunc;
279 opt0->fc[i].f_data = elimdim_makedata(opt->fc[i].f, opt->fc[i].mf,
281 opt->n, x, opt->lb, opt->ub,
283 if (!opt0->fc[i].f_data) goto bad;
286 for (i = 0; i < opt->p; ++i) {
287 opt0->h[i].f = elimdim_func;
288 opt0->h[i].mf = elimdim_mfunc;
289 opt0->h[i].f_data = elimdim_makedata(opt->h[i].f, opt->h[i].mf,
291 opt->n, x, opt->lb, opt->ub,
293 if (!opt0->h[i].f_data) goto bad;
304 /* like nlopt_destroy, but also frees elimdim_data */
305 static void elimdim_destroy(nlopt_opt opt)
310 free(((elimdim_data*) opt->f_data)->x);
311 free(((elimdim_data*) opt->f_data)->grad);
312 free(opt->f_data); opt->f_data = NULL;
314 for (i = 0; i < opt->m; ++i) {
315 free(opt->fc[i].f_data);
316 opt->fc[i].f_data = NULL;
318 for (i = 0; i < opt->p; ++i) {
319 free(opt->h[i].f_data);
320 opt->h[i].f_data = NULL;
326 /* return whether to use elimdim wrapping. */
327 static int elimdim_wrapcheck(nlopt_opt opt)
330 if (elimdim_dimension(opt->n, opt->lb, opt->ub) == opt->n) return 0;
331 switch (opt->algorithm) {
332 case NLOPT_GN_DIRECT:
333 case NLOPT_GN_DIRECT_L:
334 case NLOPT_GN_DIRECT_L_RAND:
335 case NLOPT_GN_DIRECT_NOSCAL:
336 case NLOPT_GN_DIRECT_L_NOSCAL:
337 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
338 case NLOPT_GN_ORIG_DIRECT:
339 case NLOPT_GN_ORIG_DIRECT_L:
340 case NLOPT_GN_CRS2_LM:
341 case NLOPT_LN_PRAXIS:
342 case NLOPT_LN_COBYLA:
343 case NLOPT_LN_NEWUOA:
344 case NLOPT_LN_NEWUOA_BOUND:
345 case NLOPT_LN_BOBYQA:
346 case NLOPT_LN_NELDERMEAD:
351 case NLOPT_GD_STOGO_RAND:
358 /*********************************************************************/
360 #define POP(defaultpop) (opt->stochastic_population > 0 ? \
361 opt->stochastic_population : \
362 (nlopt_stochastic_population > 0 ? \
363 nlopt_stochastic_population : (defaultpop)))
365 /* unlike nlopt_optimize() below, only handles minimization case */
366 static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
368 const double *lb, *ub;
369 nlopt_algorithm algorithm;
370 nlopt_func f; void *f_data;
375 if (!opt || !x || !minf || !opt->f
376 || opt->maximize) RETURN_ERR(NLOPT_INVALID_ARGS, opt,
377 "NULL args to nlopt_optimize_");
379 /* reset stopping flag */
380 nlopt_set_force_stop(opt, 0);
381 opt->force_stop_child = NULL;
383 /* copy a few params to local vars for convenience */
385 ni = (int) n; /* most of the subroutines take "int" arg */
386 lb = opt->lb; ub = opt->ub;
387 algorithm = opt->algorithm;
388 f = opt->f; f_data = opt->f_data;
390 if (n == 0) { /* trivial case: no degrees of freedom */
391 *minf = opt->f(n, x, NULL, opt->f_data);
392 return NLOPT_SUCCESS;
397 /* make sure rand generator is inited */
398 nlopt_srand_time_default(); /* default is non-deterministic */
400 /* check bound constraints */
401 for (i = 0; i < n; ++i)
402 if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i]) {
403 nlopt_set_errmsg(opt, "bounds %d fail %g <= %g <= %g",
404 i, lb[i], x[i], ub[i]);
405 return NLOPT_INVALID_ARGS;
409 stop.minf_max = opt->stopval;
410 stop.ftol_rel = opt->ftol_rel;
411 stop.ftol_abs = opt->ftol_abs;
412 stop.xtol_rel = opt->xtol_rel;
413 stop.xtol_abs = opt->xtol_abs;
415 stop.maxeval = opt->maxeval;
416 stop.maxtime = opt->maxtime;
417 stop.start = nlopt_seconds();
418 stop.force_stop = &(opt->force_stop);
419 stop.stop_msg = &(opt->errmsg);
422 case NLOPT_GN_DIRECT:
423 case NLOPT_GN_DIRECT_L:
424 case NLOPT_GN_DIRECT_L_RAND:
425 if (!finite_domain(n, lb, ub))
426 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
427 "finite domain required for global algorithm");
428 return cdirect(ni, f, f_data,
429 lb, ub, x, minf, &stop, 0.0,
430 (algorithm != NLOPT_GN_DIRECT)
431 + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND
432 ? 2 : (algorithm != NLOPT_GN_DIRECT))
433 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND
434 ? 1 : (algorithm != NLOPT_GN_DIRECT)));
436 case NLOPT_GN_DIRECT_NOSCAL:
437 case NLOPT_GN_DIRECT_L_NOSCAL:
438 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
439 if (!finite_domain(n, lb, ub))
440 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
441 "finite domain required for global algorithm");
442 return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf,
444 (algorithm != NLOPT_GN_DIRECT)
445 + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
446 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
448 case NLOPT_GN_ORIG_DIRECT:
449 case NLOPT_GN_ORIG_DIRECT_L: {
450 direct_return_code dret;
451 if (!finite_domain(n, lb, ub))
452 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
453 "finite domain required for global algorithm");
454 opt->work = malloc(sizeof(double) *
455 nlopt_max_constraint_dim(opt->m,
457 if (!opt->work) return NLOPT_OUT_OF_MEMORY;
458 dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
460 stop.start, stop.maxtime,
462 pow(stop.xtol_rel, (double) n), -1.0,
466 algorithm == NLOPT_GN_ORIG_DIRECT
469 free(opt->work); opt->work = NULL;
471 case DIRECT_INVALID_BOUNDS:
472 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
473 "invalid bounds for DIRECT");
474 case DIRECT_MAXFEVAL_TOOBIG:
475 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
476 "maxeval too big for DIRECT");
477 case DIRECT_INVALID_ARGS:
478 return NLOPT_INVALID_ARGS;
479 case DIRECT_INIT_FAILED:
480 RETURN_ERR(NLOPT_FAILURE, opt,
481 "init failed for DIRECT");
482 case DIRECT_SAMPLEPOINTS_FAILED:
483 RETURN_ERR(NLOPT_FAILURE, opt,
484 "sample-points failed for DIRECT");
485 case DIRECT_SAMPLE_FAILED:
486 RETURN_ERR(NLOPT_FAILURE, opt,
487 "sample failed for DIRECT");
488 case DIRECT_MAXFEVAL_EXCEEDED:
489 case DIRECT_MAXITER_EXCEEDED:
490 return NLOPT_MAXEVAL_REACHED;
491 case DIRECT_MAXTIME_EXCEEDED:
492 return NLOPT_MAXTIME_REACHED;
493 case DIRECT_GLOBAL_FOUND:
494 return NLOPT_MINF_MAX_REACHED;
496 case DIRECT_SIGMATOL:
497 return NLOPT_XTOL_REACHED;
498 case DIRECT_OUT_OF_MEMORY:
499 return NLOPT_OUT_OF_MEMORY;
500 case DIRECT_FORCED_STOP:
501 return NLOPT_FORCED_STOP;
507 case NLOPT_GD_STOGO_RAND:
509 if (!finite_domain(n, lb, ub))
510 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
511 "finite domain required for global algorithm");
512 if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop,
513 algorithm == NLOPT_GD_STOGO
514 ? 0 : (int) POP(2*n)))
515 return NLOPT_FAILURE;
518 return NLOPT_INVALID_ARGS;
522 /* lacking a free/open-source license, we no longer use
523 Rowan's code, and instead use by "sbplx" re-implementation */
524 case NLOPT_LN_SUBPLEX: {
525 int iret, freedx = 0;
528 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
529 return NLOPT_OUT_OF_MEMORY;
531 iret = nlopt_subplex(f_bound, minf, x, n, opt, &stop, opt->dx);
532 if (freedx) { free(opt->dx); opt->dx = NULL; }
534 case -2: return NLOPT_INVALID_ARGS;
535 case -20: return NLOPT_FORCED_STOP;
536 case -10: return NLOPT_MAXTIME_REACHED;
537 case -1: return NLOPT_MAXEVAL_REACHED;
538 case 0: return NLOPT_XTOL_REACHED;
539 case 1: return NLOPT_SUCCESS;
540 case 2: return NLOPT_MINF_MAX_REACHED;
541 case 20: return NLOPT_FTOL_REACHED;
542 case -200: return NLOPT_OUT_OF_MEMORY;
543 default: return NLOPT_FAILURE; /* unknown return code */
549 case NLOPT_LN_PRAXIS: {
551 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
552 return NLOPT_OUT_OF_MEMORY;
553 return praxis_(0.0, DBL_EPSILON,
554 step, ni, x, f_bound, opt, &stop, minf);
558 return luksan_plis(ni, f, f_data, lb, ub, x, minf,
559 &stop, opt->vector_storage);
563 return luksan_plip(ni, f, f_data, lb, ub, x, minf,
564 &stop, opt->vector_storage,
565 algorithm == NLOPT_LD_VAR1 ? 1 : 2);
567 case NLOPT_LD_TNEWTON:
568 case NLOPT_LD_TNEWTON_RESTART:
569 case NLOPT_LD_TNEWTON_PRECOND:
570 case NLOPT_LD_TNEWTON_PRECOND_RESTART:
571 return luksan_pnet(ni, f, f_data, lb, ub, x, minf,
572 &stop, opt->vector_storage,
573 1 + (algorithm - NLOPT_LD_TNEWTON) % 2,
574 1 + (algorithm - NLOPT_LD_TNEWTON) / 2);
576 case NLOPT_GN_CRS2_LM:
577 if (!finite_domain(n, lb, ub))
578 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
579 "finite domain required for global algorithm");
580 return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
584 case NLOPT_G_MLSL_LDS:
587 case NLOPT_GN_MLSL_LDS:
588 case NLOPT_GD_MLSL_LDS: {
589 nlopt_opt local_opt = opt->local_opt;
591 if (!finite_domain(n, lb, ub))
592 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
593 "finite domain required for global algorithm");
594 if (!local_opt && (algorithm == NLOPT_G_MLSL
595 || algorithm == NLOPT_G_MLSL_LDS))
596 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
597 "local optimizer must be specified for G_MLSL");
598 if (!local_opt) { /* default */
599 nlopt_algorithm local_alg = (algorithm == NLOPT_GN_MLSL ||
600 algorithm == NLOPT_GN_MLSL_LDS)
601 ? nlopt_local_search_alg_nonderiv
602 : nlopt_local_search_alg_deriv;
603 /* don't call MLSL recursively! */
604 if (local_alg >= NLOPT_GN_MLSL
605 && local_alg <= NLOPT_GD_MLSL_LDS)
606 local_alg = (algorithm == NLOPT_GN_MLSL ||
607 algorithm == NLOPT_GN_MLSL_LDS)
608 ? NLOPT_LN_COBYLA : NLOPT_LD_MMA;
609 local_opt = nlopt_create(local_alg, n);
610 if (!local_opt) RETURN_ERR(NLOPT_FAILURE, opt,
611 "failed to create local_opt");
612 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
613 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
614 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
615 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
616 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
618 if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
619 for (i = 0; i < n && stop.xtol_abs[i] > 0; ++i) ;
620 if (local_opt->ftol_rel <= 0 && local_opt->ftol_abs <= 0 &&
621 local_opt->xtol_rel <= 0 && i < n) {
622 /* it is not sensible to call MLSL without *some*
623 nonzero tolerance for the local search */
624 nlopt_set_ftol_rel(local_opt, 1e-15);
625 nlopt_set_xtol_rel(local_opt, 1e-7);
627 opt->force_stop_child = local_opt;
628 ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
629 local_opt, (int) POP(0),
630 algorithm >= NLOPT_GN_MLSL_LDS &&
631 algorithm != NLOPT_G_MLSL);
632 opt->force_stop_child = NULL;
633 if (!opt->local_opt) nlopt_destroy(local_opt);
637 case NLOPT_LD_MMA: case NLOPT_LD_CCSAQ: {
640 #define LO(param, def) (opt->local_opt ? opt->local_opt->param : (def))
641 dual_opt = nlopt_create(LO(algorithm,
642 nlopt_local_search_alg_deriv),
643 nlopt_count_constraints(opt->m,
645 if (!dual_opt) RETURN_ERR(NLOPT_FAILURE, opt,
646 "failed creating dual optimizer");
647 nlopt_set_ftol_rel(dual_opt, LO(ftol_rel, 1e-14));
648 nlopt_set_ftol_abs(dual_opt, LO(ftol_abs, 0.0));
649 nlopt_set_maxeval(dual_opt, LO(maxeval, 100000));
652 if (algorithm == NLOPT_LD_MMA)
653 ret = mma_minimize(n, f, f_data, opt->m, opt->fc,
654 lb, ub, x, minf, &stop, dual_opt);
656 ret = ccsa_quadratic_minimize(
657 n, f, f_data, opt->m, opt->fc, opt->pre,
658 lb, ub, x, minf, &stop, dual_opt);
659 nlopt_destroy(dual_opt);
663 case NLOPT_LN_COBYLA: {
668 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
669 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt,
670 "failed to allocate initial step");
672 ret = cobyla_minimize(n, f, f_data,
675 lb, ub, x, minf, &stop,
677 if (freedx) { free(opt->dx); opt->dx = NULL; }
681 case NLOPT_LN_NEWUOA: {
683 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
684 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt,
685 "failed to allocate initial step");
686 return newuoa(ni, 2*n+1, x, 0, 0, step,
687 &stop, minf, f_noderiv, opt);
690 case NLOPT_LN_NEWUOA_BOUND: {
692 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
693 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt,
694 "failed to allocate initial step");
695 return newuoa(ni, 2*n+1, x, lb, ub, step,
696 &stop, minf, f_noderiv, opt);
699 case NLOPT_LN_BOBYQA: {
704 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
705 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt,
706 "failed to allocate initial step");
708 ret = bobyqa(ni, 2*n+1, x, lb, ub, opt->dx,
709 &stop, minf, opt->f, opt->f_data);
710 if (freedx) { free(opt->dx); opt->dx = NULL; }
714 case NLOPT_LN_NELDERMEAD:
721 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
722 RETURN_ERR(NLOPT_OUT_OF_MEMORY, opt,
723 "failed to allocate initial step");
725 if (algorithm == NLOPT_LN_NELDERMEAD)
726 ret=nldrmd_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
728 ret=sbplx_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
729 if (freedx) { free(opt->dx); opt->dx = NULL; }
734 case NLOPT_AUGLAG_EQ:
735 case NLOPT_LN_AUGLAG:
736 case NLOPT_LN_AUGLAG_EQ:
737 case NLOPT_LD_AUGLAG:
738 case NLOPT_LD_AUGLAG_EQ: {
739 nlopt_opt local_opt = opt->local_opt;
741 if ((algorithm == NLOPT_AUGLAG || algorithm == NLOPT_AUGLAG_EQ)
743 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
744 "local optimizer must be specified for AUGLAG");
745 if (!local_opt) { /* default */
746 local_opt = nlopt_create(
747 algorithm == NLOPT_LN_AUGLAG ||
748 algorithm == NLOPT_LN_AUGLAG_EQ
749 ? nlopt_local_search_alg_nonderiv
750 : nlopt_local_search_alg_deriv, n);
751 if (!local_opt) RETURN_ERR(NLOPT_FAILURE, opt,
752 "failed to create local_opt");
753 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
754 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
755 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
756 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
757 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
759 if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
760 opt->force_stop_child = local_opt;
761 ret = auglag_minimize(ni, f, f_data,
764 lb, ub, x, minf, &stop,
766 algorithm == NLOPT_AUGLAG_EQ
767 || algorithm == NLOPT_LN_AUGLAG_EQ
768 || algorithm == NLOPT_LD_AUGLAG_EQ);
769 opt->force_stop_child = NULL;
770 if (!opt->local_opt) nlopt_destroy(local_opt);
775 if (!finite_domain(n, lb, ub))
776 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
777 "finite domain required for global algorithm");
778 return isres_minimize(ni, f, f_data,
779 (int) (opt->m), opt->fc,
780 (int) (opt->p), opt->h,
781 lb, ub, x, minf, &stop,
785 if (!finite_domain(n, lb, ub))
786 RETURN_ERR(NLOPT_INVALID_ARGS, opt,
787 "finite domain required for global algorithm");
788 return chevolutionarystrategy(n, f, f_data,
789 lb, ub, x, minf, &stop,
791 (unsigned) (POP(0)*1.5));
794 return nlopt_slsqp(n, f, f_data,
797 lb, ub, x, minf, &stop);
800 return NLOPT_INVALID_ARGS;
803 return NLOPT_SUCCESS; /* never reached */
806 /*********************************************************************/
814 /* wrapper for maximizing: just flip the sign of f and grad */
815 static double f_max(unsigned n, const double *x, double *grad, void *data)
817 f_max_data *d = (f_max_data *) data;
818 double val = d->f(n, x, grad, d->f_data);
821 for (i = 0; i < n; ++i)
827 static void pre_max(unsigned n, const double *x, const double *v,
828 double *vpre, void *data)
830 f_max_data *d = (f_max_data *) data;
832 d->pre(n, x, v, vpre, d->f_data);
833 for (i = 0; i < n; ++i) vpre[i] = -vpre[i];
837 NLOPT_STDCALL nlopt_optimize(nlopt_opt opt, double *x, double *opt_f)
839 nlopt_func f; void *f_data; nlopt_precond pre;
844 nlopt_unset_errmsg(opt);
845 if (!opt || !opt_f || !opt->f) RETURN_ERR(NLOPT_INVALID_ARGS, opt,
846 "NULL args to nlopt_optimize");
847 f = opt->f; f_data = opt->f_data; pre = opt->pre;
849 /* for maximizing, just minimize the f_max wrapper, which
850 flips the sign of everything */
851 if ((maximize = opt->maximize)) {
852 fmd.f = f; fmd.f_data = f_data; fmd.pre = pre;
853 opt->f = f_max; opt->f_data = &fmd;
854 if (opt->pre) opt->pre = pre_max;
855 opt->stopval = -opt->stopval;
859 { /* possibly eliminate lb == ub dimensions for some algorithms */
860 nlopt_opt elim_opt = opt;
861 if (elimdim_wrapcheck(opt)) {
862 elim_opt = elimdim_create(opt);
864 nlopt_set_errmsg(opt, "failure allocating elim_opt");
865 ret = NLOPT_OUT_OF_MEMORY;
868 elimdim_shrink(opt->n, x, opt->lb, opt->ub);
871 ret = nlopt_optimize_(elim_opt, x, opt_f);
873 if (elim_opt != opt) {
874 elimdim_destroy(elim_opt);
875 elimdim_expand(opt->n, x, opt->lb, opt->ub);
880 if (maximize) { /* restore original signs */
881 opt->maximize = maximize;
882 opt->stopval = -opt->stopval;
883 opt->f = f; opt->f_data = f_data; opt->pre = pre;
890 /*********************************************************************/
892 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf,
893 int maxeval, double maxtime)
899 nlopt_unset_errmsg(opt);
901 if (!opt) RETURN_ERR(NLOPT_INVALID_ARGS, opt, "NULL opt arg");
903 save_maxeval = nlopt_get_maxeval(opt);
904 save_maxtime = nlopt_get_maxtime(opt);
906 /* override opt limits if maxeval and/or maxtime are more stringent */
907 if (save_maxeval <= 0 || (maxeval > 0 && maxeval < save_maxeval))
908 nlopt_set_maxeval(opt, maxeval);
909 if (save_maxtime <= 0 || (maxtime > 0 && maxtime < save_maxtime))
910 nlopt_set_maxtime(opt, maxtime);
912 ret = nlopt_optimize(opt, x, minf);
914 nlopt_set_maxeval(opt, save_maxeval);
915 nlopt_set_maxtime(opt, save_maxtime);
920 /*********************************************************************/