1 /* Copyright (c) 2007-2012 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 /*********************************************************************/
55 #include "neldermead.h"
62 /*********************************************************************/
64 static double f_bound(int n, const double *x, void *data_)
67 nlopt_opt data = (nlopt_opt) data_;
70 /* some methods do not support bound constraints, but support
71 discontinuous objectives so we can just return Inf for invalid x */
72 for (i = 0; i < n; ++i)
73 if (x[i] < data->lb[i] || x[i] > data->ub[i])
76 f = data->f((unsigned) n, x, NULL, data->f_data);
77 return (isnan(f) || nlopt_isinf(f) ? HUGE_VAL : f);
80 static double f_noderiv(int n, const double *x, void *data_)
82 nlopt_opt data = (nlopt_opt) data_;
83 return data->f((unsigned) n, x, NULL, data->f_data);
86 static double f_direct(int n, const double *x, int *undefined, void *data_)
88 nlopt_opt data = (nlopt_opt) data_;
89 double *work = (double*) data->work;
92 f = data->f((unsigned) n, x, NULL, data->f_data);
93 *undefined = isnan(f) || nlopt_isinf(f);
94 if (nlopt_get_force_stop(data)) return f;
95 for (i = 0; i < data->m && !*undefined; ++i) {
96 nlopt_eval_constraint(work, NULL, data->fc+i, (unsigned) n, x);
97 if (nlopt_get_force_stop(data)) return f;
98 for (j = 0; j < data->fc[i].m; ++j)
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 /*********************************************************************/
139 /* wrapper functions, only for derivative-free methods, that
140 eliminate dimensions with lb == ub. (The gradient-based methods
141 should handle this case directly, since they operate on much
142 larger vectors where I am loathe to make copies unnecessarily.) */
148 unsigned n; /* true dimension */
149 double *x; /* scratch vector of length n */
150 double *grad; /* optional scratch vector of length n */
151 const double *lb, *ub; /* bounds, of length n */
154 static void *elimdim_makedata(nlopt_func f, nlopt_mfunc mf, void *f_data,
155 unsigned n, double *x, const double *lb,
156 const double *ub, double *grad)
158 elimdim_data *d = (elimdim_data *) malloc(sizeof(elimdim_data));
160 d->f = f; d->mf = mf; d->f_data = f_data; d->n = n; d->x = x;
161 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;
172 unsigned n = d->n, i, j;
174 (void) n0; /* unused */
175 for (i = j = 0; i < n; ++i) {
178 else /* assert: j < n0 */
181 val = d->f(n, x, grad ? d->grad : NULL, d->f_data);
183 /* assert: d->grad != NULL */
184 for (i = j = 0; i < n; ++i)
186 grad[j++] = d->grad[i];
192 static void elimdim_mfunc(unsigned m, double *result,
193 unsigned n0, const double *x0, double *grad, void *d_)
195 elimdim_data *d = (elimdim_data *) d_;
197 const double *lb = d->lb, *ub = d->ub;
198 unsigned n = d->n, i, j;
200 (void) n0; /* unused */
201 (void) grad; /* assert: grad == NULL */
202 for (i = j = 0; i < n; ++i) {
205 else /* assert: j < n0 */
208 d->mf(m, result, n, x, NULL, d->f_data);
211 /* compute the eliminated dimension: number of dims with lb[i] != ub[i] */
212 static unsigned elimdim_dimension(unsigned n, const double *lb, const double *ub)
215 for (i = 0; i < n; ++i) n0 += lb[i] != ub[i] ? 1U : 0;
219 /* modify v to "shrunk" version, with dimensions for lb[i] == ub[i] elim'ed */
220 static void elimdim_shrink(unsigned n, double *v,
221 const double *lb, const double *ub)
225 for (i = j = 0; i < n; ++i)
230 /* inverse of elimdim_shrink */
231 static void elimdim_expand(unsigned n, double *v,
232 const double *lb, const double *ub)
236 j = elimdim_dimension(n, lb, ub) - 1;
237 for (i = n - 1; i > 0; --i) {
248 /* given opt, create a new opt with equal-constraint dimensions eliminated */
249 static nlopt_opt elimdim_create(nlopt_opt opt)
251 nlopt_opt opt0 = nlopt_copy(opt);
252 double *x, *grad = NULL;
255 if (!opt0) return NULL;
256 x = (double *) malloc(sizeof(double) * opt->n);
257 if (opt->n && !x) { nlopt_destroy(opt0); return NULL; }
259 if (opt->algorithm == NLOPT_GD_STOGO
260 || opt->algorithm == NLOPT_GD_STOGO_RAND) {
261 grad = (double *) malloc(sizeof(double) * opt->n);
262 if (opt->n && !grad) goto bad;
265 opt0->n = elimdim_dimension(opt->n, opt->lb, opt->ub);
266 elimdim_shrink(opt->n, opt0->lb, opt->lb, opt->ub);
267 elimdim_shrink(opt->n, opt0->ub, opt->lb, opt->ub);
268 elimdim_shrink(opt->n, opt0->xtol_abs, opt->lb, opt->ub);
269 elimdim_shrink(opt->n, opt0->dx, opt->lb, opt->ub);
271 opt0->munge_on_destroy = opt0->munge_on_copy = NULL;
273 opt0->f = elimdim_func;
274 opt0->f_data = elimdim_makedata(opt->f, NULL, opt->f_data,
275 opt->n, x, opt->lb, opt->ub, grad);
276 if (!opt0->f_data) goto bad;
278 for (i = 0; i < opt->m; ++i) {
279 opt0->fc[i].f = elimdim_func;
280 opt0->fc[i].mf = elimdim_mfunc;
281 opt0->fc[i].f_data = elimdim_makedata(opt->fc[i].f, opt->fc[i].mf,
283 opt->n, x, opt->lb, opt->ub,
285 if (!opt0->fc[i].f_data) goto bad;
288 for (i = 0; i < opt->p; ++i) {
289 opt0->h[i].f = elimdim_func;
290 opt0->h[i].mf = elimdim_mfunc;
291 opt0->h[i].f_data = elimdim_makedata(opt->h[i].f, opt->h[i].mf,
293 opt->n, x, opt->lb, opt->ub,
295 if (!opt0->h[i].f_data) goto bad;
306 /* like nlopt_destroy, but also frees elimdim_data */
307 static void elimdim_destroy(nlopt_opt opt)
312 free(((elimdim_data*) opt->f_data)->x);
313 free(((elimdim_data*) opt->f_data)->grad);
314 free(opt->f_data); opt->f_data = NULL;
316 for (i = 0; i < opt->m; ++i) {
317 free(opt->fc[i].f_data);
318 opt->fc[i].f_data = NULL;
320 for (i = 0; i < opt->p; ++i) {
321 free(opt->h[i].f_data);
322 opt->h[i].f_data = NULL;
328 /* return whether to use elimdim wrapping. */
329 static int elimdim_wrapcheck(nlopt_opt opt)
332 if (elimdim_dimension(opt->n, opt->lb, opt->ub) == opt->n) return 0;
333 switch (opt->algorithm) {
334 case NLOPT_GN_DIRECT:
335 case NLOPT_GN_DIRECT_L:
336 case NLOPT_GN_DIRECT_L_RAND:
337 case NLOPT_GN_DIRECT_NOSCAL:
338 case NLOPT_GN_DIRECT_L_NOSCAL:
339 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
340 case NLOPT_GN_ORIG_DIRECT:
341 case NLOPT_GN_ORIG_DIRECT_L:
342 case NLOPT_LN_PRAXIS:
343 case NLOPT_LN_COBYLA:
344 case NLOPT_LN_NEWUOA:
345 case NLOPT_LN_NEWUOA_BOUND:
346 case NLOPT_LN_BOBYQA:
347 case NLOPT_LN_NELDERMEAD:
352 case NLOPT_GD_STOGO_RAND:
359 /*********************************************************************/
361 #define POP(defaultpop) (opt->stochastic_population > 0 ? \
362 opt->stochastic_population : \
363 (nlopt_stochastic_population > 0 ? \
364 nlopt_stochastic_population : (defaultpop)))
366 /* unlike nlopt_optimize() below, only handles minimization case */
367 static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
369 const double *lb, *ub;
370 nlopt_algorithm algorithm;
371 nlopt_func f; void *f_data;
376 if (!opt || !x || !minf || !opt->f
377 || opt->maximize) return NLOPT_INVALID_ARGS;
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 return NLOPT_INVALID_ARGS;
406 stop.minf_max = opt->stopval;
407 stop.ftol_rel = opt->ftol_rel;
408 stop.ftol_abs = opt->ftol_abs;
409 stop.xtol_rel = opt->xtol_rel;
410 stop.xtol_abs = opt->xtol_abs;
412 stop.maxeval = opt->maxeval;
413 stop.maxtime = opt->maxtime;
414 stop.start = nlopt_seconds();
415 stop.force_stop = &(opt->force_stop);
418 case NLOPT_GN_DIRECT:
419 case NLOPT_GN_DIRECT_L:
420 case NLOPT_GN_DIRECT_L_RAND:
421 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
422 return cdirect(ni, f, f_data,
423 lb, ub, x, minf, &stop, 0.0,
424 (algorithm != NLOPT_GN_DIRECT)
425 + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND
426 ? 2 : (algorithm != NLOPT_GN_DIRECT))
427 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND
428 ? 1 : (algorithm != NLOPT_GN_DIRECT)));
430 case NLOPT_GN_DIRECT_NOSCAL:
431 case NLOPT_GN_DIRECT_L_NOSCAL:
432 case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
433 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
434 return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf,
436 (algorithm != NLOPT_GN_DIRECT)
437 + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
438 + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
440 case NLOPT_GN_ORIG_DIRECT:
441 case NLOPT_GN_ORIG_DIRECT_L: {
442 direct_return_code dret;
443 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
444 opt->work = malloc(sizeof(double) *
445 nlopt_max_constraint_dim(opt->m,
447 if (!opt->work) return NLOPT_OUT_OF_MEMORY;
448 dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
450 stop.start, stop.maxtime,
452 pow(stop.xtol_rel, (double) n), -1.0,
456 algorithm == NLOPT_GN_ORIG_DIRECT
459 free(opt->work); opt->work = NULL;
461 case DIRECT_INVALID_BOUNDS:
462 case DIRECT_MAXFEVAL_TOOBIG:
463 case DIRECT_INVALID_ARGS:
464 return NLOPT_INVALID_ARGS;
465 case DIRECT_INIT_FAILED:
466 case DIRECT_SAMPLEPOINTS_FAILED:
467 case DIRECT_SAMPLE_FAILED:
468 return NLOPT_FAILURE;
469 case DIRECT_MAXFEVAL_EXCEEDED:
470 case DIRECT_MAXITER_EXCEEDED:
471 return NLOPT_MAXEVAL_REACHED;
472 case DIRECT_MAXTIME_EXCEEDED:
473 return NLOPT_MAXTIME_REACHED;
474 case DIRECT_GLOBAL_FOUND:
475 return NLOPT_MINF_MAX_REACHED;
477 case DIRECT_SIGMATOL:
478 return NLOPT_XTOL_REACHED;
479 case DIRECT_OUT_OF_MEMORY:
480 return NLOPT_OUT_OF_MEMORY;
481 case DIRECT_FORCED_STOP:
482 return NLOPT_FORCED_STOP;
488 case NLOPT_GD_STOGO_RAND:
490 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
491 if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop,
492 algorithm == NLOPT_GD_STOGO
493 ? 0 : (int) POP(2*n)))
494 return NLOPT_FAILURE;
497 return NLOPT_INVALID_ARGS;
501 /* lacking a free/open-source license, we no longer use
502 Rowan's code, and instead use by "sbplx" re-implementation */
503 case NLOPT_LN_SUBPLEX: {
504 int iret, freedx = 0;
507 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
508 return NLOPT_OUT_OF_MEMORY;
510 iret = nlopt_subplex(f_bound, minf, x, n, opt, &stop, opt->dx);
511 if (freedx) { free(opt->dx); opt->dx = NULL; }
513 case -2: return NLOPT_INVALID_ARGS;
514 case -20: return NLOPT_FORCED_STOP;
515 case -10: return NLOPT_MAXTIME_REACHED;
516 case -1: return NLOPT_MAXEVAL_REACHED;
517 case 0: return NLOPT_XTOL_REACHED;
518 case 1: return NLOPT_SUCCESS;
519 case 2: return NLOPT_MINF_MAX_REACHED;
520 case 20: return NLOPT_FTOL_REACHED;
521 case -200: return NLOPT_OUT_OF_MEMORY;
522 default: return NLOPT_FAILURE; /* unknown return code */
528 case NLOPT_LN_PRAXIS: {
530 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
531 return NLOPT_OUT_OF_MEMORY;
532 return praxis_(0.0, DBL_EPSILON,
533 step, ni, x, f_bound, opt, &stop, minf);
537 return luksan_plis(ni, f, f_data, lb, ub, x, minf,
538 &stop, opt->vector_storage);
542 return luksan_plip(ni, f, f_data, lb, ub, x, minf,
543 &stop, opt->vector_storage,
544 algorithm == NLOPT_LD_VAR1 ? 1 : 2);
546 case NLOPT_LD_TNEWTON:
547 case NLOPT_LD_TNEWTON_RESTART:
548 case NLOPT_LD_TNEWTON_PRECOND:
549 case NLOPT_LD_TNEWTON_PRECOND_RESTART:
550 return luksan_pnet(ni, f, f_data, lb, ub, x, minf,
551 &stop, opt->vector_storage,
552 1 + (algorithm - NLOPT_LD_TNEWTON) % 2,
553 1 + (algorithm - NLOPT_LD_TNEWTON) / 2);
555 case NLOPT_GN_CRS2_LM:
556 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
557 return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
561 case NLOPT_G_MLSL_LDS:
564 case NLOPT_GN_MLSL_LDS:
565 case NLOPT_GD_MLSL_LDS: {
566 nlopt_opt local_opt = opt->local_opt;
568 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
569 if (!local_opt && (algorithm == NLOPT_G_MLSL
570 || algorithm == NLOPT_G_MLSL_LDS))
571 return NLOPT_INVALID_ARGS;
572 if (!local_opt) { /* default */
573 nlopt_algorithm local_alg = (algorithm == NLOPT_GN_MLSL ||
574 algorithm == NLOPT_GN_MLSL_LDS)
575 ? nlopt_local_search_alg_nonderiv
576 : nlopt_local_search_alg_deriv;
577 /* don't call MLSL recursively! */
578 if (local_alg >= NLOPT_GN_MLSL
579 && local_alg <= NLOPT_GD_MLSL_LDS)
580 local_alg = (algorithm == NLOPT_GN_MLSL ||
581 algorithm == NLOPT_GN_MLSL_LDS)
582 ? NLOPT_LN_COBYLA : NLOPT_LD_MMA;
583 local_opt = nlopt_create(local_alg, n);
584 if (!local_opt) return NLOPT_FAILURE;
585 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
586 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
587 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
588 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
589 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
591 if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
592 for (i = 0; i < n && stop.xtol_abs[i] > 0; ++i) ;
593 if (local_opt->ftol_rel <= 0 && local_opt->ftol_abs <= 0 &&
594 local_opt->xtol_rel <= 0 && i < n) {
595 /* it is not sensible to call MLSL without *some*
596 nonzero tolerance for the local search */
597 nlopt_set_ftol_rel(local_opt, 1e-15);
598 nlopt_set_xtol_rel(local_opt, 1e-7);
600 opt->force_stop_child = local_opt;
601 ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
602 local_opt, (int) POP(0),
603 algorithm >= NLOPT_GN_MLSL_LDS &&
604 algorithm != NLOPT_G_MLSL);
605 opt->force_stop_child = NULL;
606 if (!opt->local_opt) nlopt_destroy(local_opt);
610 case NLOPT_LD_MMA: case NLOPT_LD_CCSAQ: {
613 #define LO(param, def) (opt->local_opt ? opt->local_opt->param : (def))
614 dual_opt = nlopt_create(LO(algorithm,
615 nlopt_local_search_alg_deriv),
616 nlopt_count_constraints(opt->m,
618 if (!dual_opt) return NLOPT_FAILURE;
619 nlopt_set_ftol_rel(dual_opt, LO(ftol_rel, 1e-14));
620 nlopt_set_ftol_abs(dual_opt, LO(ftol_abs, 0.0));
621 nlopt_set_maxeval(dual_opt, LO(maxeval, 100000));
624 if (algorithm == NLOPT_LD_MMA)
625 ret = mma_minimize(n, f, f_data, opt->m, opt->fc,
626 lb, ub, x, minf, &stop, dual_opt);
628 ret = ccsa_quadratic_minimize(
629 n, f, f_data, opt->m, opt->fc, opt->pre,
630 lb, ub, x, minf, &stop, dual_opt);
631 nlopt_destroy(dual_opt);
635 case NLOPT_LN_COBYLA: {
640 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
641 return NLOPT_OUT_OF_MEMORY;
643 return cobyla_minimize(n, f, f_data,
646 lb, ub, x, minf, &stop,
648 if (freedx) { free(opt->dx); opt->dx = NULL; }
652 case NLOPT_LN_NEWUOA: {
654 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
655 return NLOPT_OUT_OF_MEMORY;
656 return newuoa(ni, 2*n+1, x, 0, 0, step,
657 &stop, minf, f_noderiv, opt);
660 case NLOPT_LN_NEWUOA_BOUND: {
662 if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
663 return NLOPT_OUT_OF_MEMORY;
664 return newuoa(ni, 2*n+1, x, lb, ub, step,
665 &stop, minf, f_noderiv, opt);
668 case NLOPT_LN_BOBYQA: {
673 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
674 return NLOPT_OUT_OF_MEMORY;
676 ret = bobyqa(ni, 2*n+1, x, lb, ub, opt->dx,
677 &stop, minf, opt->f, opt->f_data);
678 if (freedx) { free(opt->dx); opt->dx = NULL; }
682 case NLOPT_LN_NELDERMEAD:
689 if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
690 return NLOPT_OUT_OF_MEMORY;
692 if (algorithm == NLOPT_LN_NELDERMEAD)
693 ret= nldrmd_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
695 ret= sbplx_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
696 if (freedx) { free(opt->dx); opt->dx = NULL; }
701 case NLOPT_AUGLAG_EQ:
702 case NLOPT_LN_AUGLAG:
703 case NLOPT_LN_AUGLAG_EQ:
704 case NLOPT_LD_AUGLAG:
705 case NLOPT_LD_AUGLAG_EQ: {
706 nlopt_opt local_opt = opt->local_opt;
708 if ((algorithm == NLOPT_AUGLAG || algorithm == NLOPT_AUGLAG_EQ)
710 return NLOPT_INVALID_ARGS;
711 if (!local_opt) { /* default */
712 local_opt = nlopt_create(
713 algorithm == NLOPT_LN_AUGLAG ||
714 algorithm == NLOPT_LN_AUGLAG_EQ
715 ? nlopt_local_search_alg_nonderiv
716 : nlopt_local_search_alg_deriv, n);
717 if (!local_opt) return NLOPT_FAILURE;
718 nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
719 nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
720 nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
721 nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
722 nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
724 if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
725 opt->force_stop_child = local_opt;
726 ret = auglag_minimize(ni, f, f_data,
729 lb, ub, x, minf, &stop,
731 algorithm == NLOPT_AUGLAG_EQ
732 || algorithm == NLOPT_LN_AUGLAG_EQ
733 || algorithm == NLOPT_LD_AUGLAG_EQ);
734 opt->force_stop_child = NULL;
735 if (!opt->local_opt) nlopt_destroy(local_opt);
740 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
741 return isres_minimize(ni, f, f_data,
742 (int) (opt->m), opt->fc,
743 (int) (opt->p), opt->h,
744 lb, ub, x, minf, &stop,
748 if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
749 return chevolutionarystrategy(n, f, f_data,
750 lb, ub, x, minf, &stop,
752 (unsigned) (POP(0)*1.5));
755 return nlopt_slsqp(n, f, f_data,
758 lb, ub, x, minf, &stop);
761 return NLOPT_INVALID_ARGS;
764 return NLOPT_SUCCESS; /* never reached */
767 /*********************************************************************/
775 /* wrapper for maximizing: just flip the sign of f and grad */
776 static double f_max(unsigned n, const double *x, double *grad, void *data)
778 f_max_data *d = (f_max_data *) data;
779 double val = d->f(n, x, grad, d->f_data);
782 for (i = 0; i < n; ++i)
788 static void pre_max(unsigned n, const double *x, const double *v,
789 double *vpre, void *data)
791 f_max_data *d = (f_max_data *) data;
793 d->pre(n, x, v, vpre, d->f_data);
794 for (i = 0; i < n; ++i) vpre[i] = -vpre[i];
798 NLOPT_STDCALL nlopt_optimize(nlopt_opt opt, double *x, double *opt_f)
800 nlopt_func f; void *f_data; nlopt_precond pre;
805 if (!opt || !opt_f || !opt->f) return NLOPT_INVALID_ARGS;
806 f = opt->f; f_data = opt->f_data; pre = opt->pre;
808 /* for maximizing, just minimize the f_max wrapper, which
809 flips the sign of everything */
810 if ((maximize = opt->maximize)) {
811 fmd.f = f; fmd.f_data = f_data; fmd.pre = pre;
812 opt->f = f_max; opt->f_data = &fmd;
813 if (opt->pre) opt->pre = pre_max;
814 opt->stopval = -opt->stopval;
818 { /* possibly eliminate lb == ub dimensions for some algorithms */
819 nlopt_opt elim_opt = opt;
820 if (elimdim_wrapcheck(opt)) {
821 elim_opt = elimdim_create(opt);
822 if (!elim_opt) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
823 elimdim_shrink(opt->n, x, opt->lb, opt->ub);
826 ret = nlopt_optimize_(elim_opt, x, opt_f);
828 if (elim_opt != opt) {
829 elimdim_destroy(elim_opt);
830 elimdim_expand(opt->n, x, opt->lb, opt->ub);
835 if (maximize) { /* restore original signs */
836 opt->maximize = maximize;
837 opt->stopval = -opt->stopval;
838 opt->f = f; opt->f_data = f_data; opt->pre = pre;
845 /*********************************************************************/
847 nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf,
848 int maxeval, double maxtime)
854 if (!opt) return NLOPT_INVALID_ARGS;
856 save_maxeval = nlopt_get_maxeval(opt);
857 save_maxtime = nlopt_get_maxtime(opt);
859 /* override opt limits if maxeval and/or maxtime are more stringent */
860 if (save_maxeval <= 0 || (maxeval > 0 && maxeval < save_maxeval))
861 nlopt_set_maxeval(opt, maxeval);
862 if (save_maxtime <= 0 || (maxtime > 0 && maxtime < save_maxtime))
863 nlopt_set_maxtime(opt, maxtime);
865 ret = nlopt_optimize(opt, x, minf);
867 nlopt_set_maxeval(opt, save_maxeval);
868 nlopt_set_maxtime(opt, save_maxtime);
873 /*********************************************************************/