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"
65 /*********************************************************************/
67 static double f_bound(int n, const double *x, void *data_)
70 nlopt_opt data = (nlopt_opt) data_;
73 /* some methods do not support bound constraints, but support
74 discontinuous objectives so we can just return Inf for invalid x */
75 for (i = 0; i < n; ++i)
76 if (x[i] < data->lb[i] || x[i] > data->ub[i])
79 f = data->f((unsigned) n, x, NULL, data->f_data);
80 return (isnan(f) || nlopt_isinf(f) ? HUGE_VAL : f);
83 static double f_noderiv(int n, const double *x, void *data_)
85 nlopt_opt data = (nlopt_opt) data_;
86 return data->f((unsigned) n, x, NULL, data->f_data);
89 static double f_direct(int n, const double *x, int *undefined, void *data_)
91 nlopt_opt data = (nlopt_opt) data_;
94 f = data->f((unsigned) n, x, NULL, data->f_data);
95 *undefined = isnan(f) || nlopt_isinf(f);
96 if (nlopt_get_force_stop(data)) return f;
97 for (i = 0; i < data->m && !*undefined; ++i) {
98 nlopt_eval_constraint(data->work, NULL, data->fc+i, (unsigned) n, x);
99 if (nlopt_get_force_stop(data)) return f;
100 for (j = 0; j < data->fc[i].m; ++j)
101 if (data->work[j] > 0)
107 /*********************************************************************/
109 /* get min(dx) for algorithms requiring a scalar initial step size */
110 static nlopt_result initial_step(nlopt_opt opt, const double *x, double *step)
112 unsigned freedx = 0, i;
116 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
117 return NLOPT_OUT_OF_MEMORY;
121 for (i = 0; i < opt->n; ++i)
122 if (*step > fabs(opt->dx[i]))
123 *step = fabs(opt->dx[i]);
125 if (freedx) { free(opt->dx); opt->dx = NULL; }
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])) return 0;
140 /*********************************************************************/
141 /* wrapper functions, only for derivative-free methods, that
142 eliminate dimensions with lb == ub. (The gradient-based methods
143 should handle this case directly, since they operate on much
144 larger vectors where I am loathe to make copies unnecessarily.) */
150 unsigned n; /* true dimension */
151 double *x; /* scratch vector of length n */
152 const double *lb, *ub; /* bounds, of length n */
155 static void *elimdim_makedata(nlopt_func f, nlopt_mfunc mf, void *f_data,
156 unsigned n, double *x, const double *lb,
159 elimdim_data *d = (elimdim_data *) malloc(sizeof(elimdim_data));
161 d->f = f; d->mf = mf; d->f_data = f_data; d->n = n; d->x = x;
162 d->lb = lb; d->ub = ub;
166 static double elimdim_func(unsigned n0, const double *x0, double *grad, void *d_)
168 elimdim_data *d = (elimdim_data *) d_;
170 const double *lb = d->lb, *ub = d->ub;
171 unsigned n = d->n, i, j;
173 (void) n0; /* unused */
174 (void) grad; /* assert: grad == NULL */
175 for (i = j = 0; i < n; ++i) {
178 else /* assert: j < n0 */
181 return d->f(n, x, NULL, d->f_data);
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] eliminated */
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)
244 nlopt_opt opt0 = nlopt_copy(opt);
248 if (!opt0) return NULL;
249 x = (double *) malloc(sizeof(double) * opt->n);
250 if (opt->n && !x) { nlopt_destroy(opt0); return NULL; }
252 opt0->n = elimdim_dimension(opt->n, opt->lb, opt->ub);
253 elimdim_shrink(opt->n, opt0->lb, opt->lb, opt->ub);
254 elimdim_shrink(opt->n, opt0->ub, opt->lb, opt->ub);
255 elimdim_shrink(opt->n, opt0->xtol_abs, opt->lb, opt->ub);
256 elimdim_shrink(opt->n, opt0->dx, opt->lb, opt->ub);
258 opt0->munge_on_destroy = opt0->munge_on_copy = NULL;
260 opt0->f = elimdim_func;
261 opt0->f_data = elimdim_makedata(opt->f, NULL, opt->f_data,
262 opt->n, x, opt->lb, opt->ub);
263 if (!opt0->f_data) goto bad;
265 for (i = 0; i < opt->m; ++i) {
266 opt0->fc[i].f = elimdim_func;
267 opt0->fc[i].mf = elimdim_mfunc;
268 opt0->fc[i].f_data = elimdim_makedata(opt->fc[i].f, opt->fc[i].mf,
270 opt->n, x, opt->lb, opt->ub);
271 if (!opt0->fc[i].f_data) goto bad;
274 for (i = 0; i < opt->p; ++i) {
275 opt0->h[i].f = elimdim_func;
276 opt0->h[i].mf = elimdim_mfunc;
277 opt0->h[i].f_data = elimdim_makedata(opt->h[i].f, opt->h[i].mf,
279 opt->n, x, opt->lb, opt->ub);
280 if (!opt0->h[i].f_data) goto bad;
290 /* like nlopt_destroy, but also frees elimdim_data */
291 static void elimdim_destroy(nlopt_opt opt)
296 free(((elimdim_data*) opt->f_data)->x);
297 free(opt->f_data); opt->f_data = NULL;
299 for (i = 0; i < opt->m; ++i) {
300 free(opt->fc[i].f_data);
301 opt->fc[i].f_data = NULL;
303 for (i = 0; i < opt->p; ++i) {
304 free(opt->h[i].f_data);
305 opt->h[i].f_data = NULL;
311 /* return whether to use elimdim wrapping. */
312 static int elimdim_wrapcheck(nlopt_opt opt)
315 if (elimdim_dimension(opt->n, opt->lb, opt->ub) == opt->n) return 0;
316 switch (opt->algorithm) {
317 case NLOPT_GN_DIRECT:
318 case NLOPT_GN_DIRECT_L:
319 case NLOPT_GN_DIRECT_L_RAND:
320 case NLOPT_GN_DIRECT_NOSCAL:
321 case NLOPT_GN_DIRECT_L_NOSCAL:
322 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
323 case NLOPT_GN_ORIG_DIRECT:
324 case NLOPT_GN_ORIG_DIRECT_L:
325 case NLOPT_LN_COBYLA:
326 case NLOPT_LN_NEWUOA:
327 case NLOPT_LN_NEWUOA_BOUND:
328 case NLOPT_LN_BOBYQA:
336 /*********************************************************************/
338 #define POP(defaultpop) (opt->stochastic_population > 0 ? \
339 opt->stochastic_population : \
340 (nlopt_stochastic_population > 0 ? \
341 nlopt_stochastic_population : (defaultpop)))
343 /* unlike nlopt_optimize() below, only handles minimization case */
344 static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
346 const double *lb, *ub;
347 nlopt_algorithm algorithm;
348 nlopt_func f; void *f_data;
353 if (!opt || !x || !minf || !opt->f
354 || opt->maximize) return NLOPT_INVALID_ARGS;
356 /* reset stopping flag */
357 nlopt_set_force_stop(opt, 0);
358 opt->force_stop_child = NULL;
360 /* copy a few params to local vars for convenience */
362 ni = (int) n; /* most of the subroutines take "int" arg */
363 lb = opt->lb; ub = opt->ub;
364 algorithm = opt->algorithm;
365 f = opt->f; f_data = opt->f_data;
367 if (n == 0) { /* trivial case: no degrees of freedom */
368 *minf = opt->f(n, x, NULL, opt->f_data);
369 return NLOPT_SUCCESS;
374 /* make sure rand generator is inited */
375 nlopt_srand_time_default(); /* default is non-deterministic */
377 /* check bound constraints */
378 for (i = 0; i < n; ++i)
379 if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i])
380 return NLOPT_INVALID_ARGS;
383 stop.minf_max = opt->stopval;
384 stop.ftol_rel = opt->ftol_rel;
385 stop.ftol_abs = opt->ftol_abs;
386 stop.xtol_rel = opt->xtol_rel;
387 stop.xtol_abs = opt->xtol_abs;
389 stop.maxeval = opt->maxeval;
390 stop.maxtime = opt->maxtime;
391 stop.start = nlopt_seconds();
392 stop.force_stop = &(opt->force_stop);
395 case NLOPT_GN_DIRECT:
396 case NLOPT_GN_DIRECT_L:
397 case NLOPT_GN_DIRECT_L_RAND:
398 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
399 return cdirect(ni, f, f_data,
400 lb, ub, x, minf, &stop, 0.0,
401 (algorithm != NLOPT_GN_DIRECT)
402 + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND
403 ? 2 : (algorithm != NLOPT_GN_DIRECT))
404 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND
405 ? 1 : (algorithm != NLOPT_GN_DIRECT)));
407 case NLOPT_GN_DIRECT_NOSCAL:
408 case NLOPT_GN_DIRECT_L_NOSCAL:
409 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
410 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
411 return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf,
413 (algorithm != NLOPT_GN_DIRECT)
414 + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
415 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
417 case NLOPT_GN_ORIG_DIRECT:
418 case NLOPT_GN_ORIG_DIRECT_L: {
419 direct_return_code dret;
420 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
421 opt->work = (double*) malloc(sizeof(double) *
422 nlopt_max_constraint_dim(opt->m,
424 if (!opt->work) return NLOPT_OUT_OF_MEMORY;
425 dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
427 stop.start, stop.maxtime,
429 pow(stop.xtol_rel, (double) n), -1.0,
433 algorithm == NLOPT_GN_ORIG_DIRECT
436 free(opt->work); opt->work = NULL;
438 case DIRECT_INVALID_BOUNDS:
439 case DIRECT_MAXFEVAL_TOOBIG:
440 case DIRECT_INVALID_ARGS:
441 return NLOPT_INVALID_ARGS;
442 case DIRECT_INIT_FAILED:
443 case DIRECT_SAMPLEPOINTS_FAILED:
444 case DIRECT_SAMPLE_FAILED:
445 return NLOPT_FAILURE;
446 case DIRECT_MAXFEVAL_EXCEEDED:
447 case DIRECT_MAXITER_EXCEEDED:
448 return NLOPT_MAXEVAL_REACHED;
449 case DIRECT_MAXTIME_EXCEEDED:
450 return NLOPT_MAXTIME_REACHED;
451 case DIRECT_GLOBAL_FOUND:
452 return NLOPT_MINF_MAX_REACHED;
454 case DIRECT_SIGMATOL:
455 return NLOPT_XTOL_REACHED;
456 case DIRECT_OUT_OF_MEMORY:
457 return NLOPT_OUT_OF_MEMORY;
458 case DIRECT_FORCED_STOP:
459 return NLOPT_FORCED_STOP;
465 case NLOPT_GD_STOGO_RAND:
467 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
468 if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop,
469 algorithm == NLOPT_GD_STOGO
470 ? 0 : (int) POP(2*n)))
471 return NLOPT_FAILURE;
474 return NLOPT_INVALID_ARGS;
478 /* lacking a free/open-source license, we no longer use
479 Rowan's code, and instead use by "sbplx" re-implementation */
480 case NLOPT_LN_SUBPLEX: {
481 int iret, freedx = 0;
484 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
485 return NLOPT_OUT_OF_MEMORY;
487 iret = nlopt_subplex(f_bound, minf, x, n, opt, &stop, opt->dx);
488 if (freedx) { free(opt->dx); opt->dx = NULL; }
490 case -2: return NLOPT_INVALID_ARGS;
491 case -20: return NLOPT_FORCED_STOP;
492 case -10: return NLOPT_MAXTIME_REACHED;
493 case -1: return NLOPT_MAXEVAL_REACHED;
494 case 0: return NLOPT_XTOL_REACHED;
495 case 1: return NLOPT_SUCCESS;
496 case 2: return NLOPT_MINF_MAX_REACHED;
497 case 20: return NLOPT_FTOL_REACHED;
498 case -200: return NLOPT_OUT_OF_MEMORY;
499 default: return NLOPT_FAILURE; /* unknown return code */
505 case NLOPT_LN_PRAXIS: {
507 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
508 return NLOPT_OUT_OF_MEMORY;
509 return praxis_(0.0, DBL_EPSILON,
510 step, ni, x, f_bound, opt, &stop, minf);
514 case NLOPT_LD_LBFGS_NOCEDAL: {
515 int iret, *nbd = (int *) malloc(sizeof(int) * n);
516 if (!nbd) return NLOPT_OUT_OF_MEMORY;
517 for (i = 0; i < n; ++i) {
518 int linf = nlopt_isinf(lb[i]) && lb[i] < 0;
519 int uinf = nlopt_isinf(ub[i]) && ub[i] > 0;
520 nbd[i] = linf && uinf ? 0 : (uinf ? 1 : (linf ? 3 : 2));
522 iret = lbfgsb_minimize(ni, f, f_data, x, nbd, lb, ub,
523 ni < 5 ? ni : 5, 0.0, stop.ftol_rel,
524 stop.xtol_abs[0] > 0 ? stop.xtol_abs[0]
530 case -1: return NLOPT_INVALID_ARGS;
531 case -2: default: return NLOPT_FAILURE;
535 *minf = f(n, x, NULL, f_data);
537 case 5: return NLOPT_MAXEVAL_REACHED;
538 case 2: return NLOPT_XTOL_REACHED;
539 case 1: return NLOPT_FTOL_REACHED;
540 default: return NLOPT_SUCCESS;
548 return luksan_plis(ni, f, f_data, lb, ub, x, minf,
549 &stop, opt->vector_storage);
553 return luksan_plip(ni, f, f_data, lb, ub, x, minf,
554 &stop, opt->vector_storage,
555 algorithm == NLOPT_LD_VAR1 ? 1 : 2);
557 case NLOPT_LD_TNEWTON:
558 case NLOPT_LD_TNEWTON_RESTART:
559 case NLOPT_LD_TNEWTON_PRECOND:
560 case NLOPT_LD_TNEWTON_PRECOND_RESTART:
561 return luksan_pnet(ni, f, f_data, lb, ub, x, minf,
562 &stop, opt->vector_storage,
563 1 + (algorithm - NLOPT_LD_TNEWTON) % 2,
564 1 + (algorithm - NLOPT_LD_TNEWTON) / 2);
566 case NLOPT_GN_CRS2_LM:
567 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
568 return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
572 case NLOPT_G_MLSL_LDS:
575 case NLOPT_GN_MLSL_LDS:
576 case NLOPT_GD_MLSL_LDS: {
577 nlopt_opt local_opt = opt->local_opt;
579 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
580 if (!local_opt && (algorithm == NLOPT_G_MLSL
581 || algorithm == NLOPT_G_MLSL_LDS))
582 return NLOPT_INVALID_ARGS;
583 if (!local_opt) { /* default */
584 nlopt_algorithm local_alg = (algorithm == NLOPT_GN_MLSL ||
585 algorithm == NLOPT_GN_MLSL_LDS)
586 ? nlopt_local_search_alg_nonderiv
587 : nlopt_local_search_alg_deriv;
588 /* don't call MLSL recursively! */
589 if (local_alg >= NLOPT_GN_MLSL
590 && local_alg <= NLOPT_GD_MLSL_LDS)
591 local_alg = (algorithm == NLOPT_GN_MLSL ||
592 algorithm == NLOPT_GN_MLSL_LDS)
593 ? NLOPT_LN_COBYLA : NLOPT_LD_MMA;
594 local_opt = nlopt_create(local_alg, n);
595 if (!local_opt) return NLOPT_FAILURE;
596 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
597 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
598 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
599 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
600 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
602 if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
603 for (i = 0; i < n && stop.xtol_abs[i] > 0; ++i) ;
604 if (local_opt->ftol_rel <= 0 && local_opt->ftol_abs <= 0 &&
605 local_opt->xtol_rel <= 0 && i < n) {
606 /* it is not sensible to call MLSL without *some*
607 nonzero tolerance for the local search */
608 nlopt_set_ftol_rel(local_opt, 1e-15);
609 nlopt_set_xtol_rel(local_opt, 1e-7);
611 opt->force_stop_child = local_opt;
612 ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
613 local_opt, (int) POP(0),
614 algorithm >= NLOPT_GN_MLSL_LDS &&
615 algorithm != NLOPT_G_MLSL);
616 opt->force_stop_child = NULL;
617 if (!opt->local_opt) nlopt_destroy(local_opt);
624 #define LO(param, def) (opt->local_opt ? opt->local_opt->param : (def))
625 dual_opt = nlopt_create(LO(algorithm,
626 nlopt_local_search_alg_deriv),
627 nlopt_count_constraints(opt->m,
629 if (!dual_opt) return NLOPT_FAILURE;
630 nlopt_set_ftol_rel(dual_opt, LO(ftol_rel, 1e-12));
631 nlopt_set_ftol_abs(dual_opt, LO(ftol_abs, 0.0));
632 nlopt_set_maxeval(dual_opt, LO(maxeval, 100000));
635 ret = mma_minimize(n, f, f_data, opt->m, opt->fc,
636 lb, ub, x, minf, &stop, dual_opt);
637 nlopt_destroy(dual_opt);
641 case NLOPT_LN_COBYLA: {
646 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
647 return NLOPT_OUT_OF_MEMORY;
649 return cobyla_minimize(n, f, f_data,
652 lb, ub, x, minf, &stop,
654 if (freedx) { free(opt->dx); opt->dx = NULL; }
658 case NLOPT_LN_NEWUOA: {
660 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
661 return NLOPT_OUT_OF_MEMORY;
662 return newuoa(ni, 2*n+1, x, 0, 0, step,
663 &stop, minf, f_noderiv, opt);
666 case NLOPT_LN_NEWUOA_BOUND: {
668 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
669 return NLOPT_OUT_OF_MEMORY;
670 return newuoa(ni, 2*n+1, x, lb, ub, step,
671 &stop, minf, f_noderiv, opt);
674 case NLOPT_LN_BOBYQA: {
679 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
680 return NLOPT_OUT_OF_MEMORY;
682 ret = bobyqa(ni, 2*n+1, x, lb, ub, opt->dx,
683 &stop, minf, opt->f, opt->f_data);
684 if (freedx) { free(opt->dx); opt->dx = NULL; }
688 case NLOPT_LN_NELDERMEAD:
695 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
696 return NLOPT_OUT_OF_MEMORY;
698 if (algorithm == NLOPT_LN_NELDERMEAD)
699 ret= nldrmd_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
701 ret= sbplx_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
702 if (freedx) { free(opt->dx); opt->dx = NULL; }
707 case NLOPT_AUGLAG_EQ:
708 case NLOPT_LN_AUGLAG:
709 case NLOPT_LN_AUGLAG_EQ:
710 case NLOPT_LD_AUGLAG:
711 case NLOPT_LD_AUGLAG_EQ: {
712 nlopt_opt local_opt = opt->local_opt;
714 if ((algorithm == NLOPT_AUGLAG || algorithm == NLOPT_AUGLAG_EQ)
716 return NLOPT_INVALID_ARGS;
717 if (!local_opt) { /* default */
718 local_opt = nlopt_create(
719 algorithm == NLOPT_LN_AUGLAG ||
720 algorithm == NLOPT_LN_AUGLAG_EQ
721 ? nlopt_local_search_alg_nonderiv
722 : nlopt_local_search_alg_deriv, n);
723 if (!local_opt) return NLOPT_FAILURE;
724 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
725 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
726 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
727 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
728 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
730 if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
731 opt->force_stop_child = local_opt;
732 ret = auglag_minimize(ni, f, f_data,
735 lb, ub, x, minf, &stop,
737 algorithm == NLOPT_AUGLAG_EQ
738 || algorithm == NLOPT_LN_AUGLAG_EQ
739 || algorithm == NLOPT_LD_AUGLAG_EQ);
740 opt->force_stop_child = NULL;
741 if (!opt->local_opt) nlopt_destroy(local_opt);
746 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
747 return isres_minimize(ni, f, f_data,
748 (int) (opt->m), opt->fc,
749 (int) (opt->p), opt->h,
750 lb, ub, x, minf, &stop,
754 return nlopt_slsqp(n, f, f_data,
757 lb, ub, x, minf, &stop);
760 return NLOPT_INVALID_ARGS;
763 return NLOPT_SUCCESS; /* never reached */
766 /*********************************************************************/
773 /* wrapper for maximizing: just flip the sign of f and grad */
774 static double f_max(unsigned n, const double *x, double *grad, void *data)
776 f_max_data *d = (f_max_data *) data;
777 double val = d->f(n, x, grad, d->f_data);
780 for (i = 0; i < n; ++i)
787 NLOPT_STDCALL nlopt_optimize(nlopt_opt opt, double *x, double *opt_f)
789 nlopt_func f; void *f_data;
794 if (!opt || !opt_f || !opt->f) return NLOPT_INVALID_ARGS;
795 f = opt->f; f_data = opt->f_data;
797 /* for maximizing, just minimize the f_max wrapper, which
798 flips the sign of everything */
799 if ((maximize = opt->maximize)) {
800 fmd.f = f; fmd.f_data = f_data;
801 opt->f = f_max; opt->f_data = &fmd;
802 opt->stopval = -opt->stopval;
806 { /* possibly eliminate lb == ub dimensions for some algorithms */
807 nlopt_opt elim_opt = opt;
808 if (elimdim_wrapcheck(opt)) {
809 elim_opt = elimdim_create(opt);
810 if (!elim_opt) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
811 elimdim_shrink(opt->n, x, opt->lb, opt->ub);
814 ret = nlopt_optimize_(elim_opt, x, opt_f);
816 if (elim_opt != opt) {
817 elimdim_destroy(elim_opt);
818 elimdim_expand(opt->n, x, opt->lb, opt->ub);
823 if (maximize) { /* restore original signs */
824 opt->maximize = maximize;
825 opt->stopval = -opt->stopval;
826 opt->f = f; opt->f_data = f_data;
833 /*********************************************************************/
835 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf,
836 int maxeval, double maxtime)
842 if (!opt) return NLOPT_INVALID_ARGS;
844 save_maxeval = nlopt_get_maxeval(opt);
845 save_maxtime = nlopt_get_maxtime(opt);
847 /* override opt limits if maxeval and/or maxtime are more stringent */
848 if (save_maxeval <= 0 || (maxeval > 0 && maxeval < save_maxeval))
849 nlopt_set_maxeval(opt, maxeval);
850 if (save_maxtime <= 0 || (maxtime > 0 && maxtime < save_maxtime))
851 nlopt_set_maxtime(opt, maxtime);
853 ret = nlopt_optimize(opt, x, minf);
855 nlopt_set_maxeval(opt, save_maxeval);
856 nlopt_set_maxtime(opt, save_maxtime);
861 /*********************************************************************/