1 /* Copyright (c) 2007-2011 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.
29 #include "nlopt-internal.h"
31 /*************************************************************************/
33 void NLOPT_STDCALL nlopt_destroy(nlopt_opt opt)
37 if (opt->munge_on_destroy) {
38 nlopt_munge munge = opt->munge_on_destroy;
40 for (i = 0; i < opt->m; ++i)
41 munge(opt->fc[i].f_data);
42 for (i = 0; i < opt->p; ++i)
43 munge(opt->h[i].f_data);
45 for (i = 0; i < opt->m; ++i)
47 for (i = 0; i < opt->p; ++i)
49 free(opt->lb); free(opt->ub);
53 nlopt_destroy(opt->local_opt);
60 nlopt_opt NLOPT_STDCALL nlopt_create(nlopt_algorithm algorithm, unsigned n)
64 if (((int) algorithm) < 0 || algorithm >= NLOPT_NUM_ALGORITHMS)
67 opt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
69 opt->algorithm = algorithm;
71 opt->f = NULL; opt->f_data = NULL;
73 opt->munge_on_destroy = opt->munge_on_copy = NULL;
75 opt->lb = opt->ub = NULL;
76 opt->m = opt->m_alloc = 0;
78 opt->p = opt->p_alloc = 0;
81 opt->stopval = -HUGE_VAL;
82 opt->ftol_rel = opt->ftol_abs = 0;
83 opt->xtol_rel = 0; opt->xtol_abs = NULL;
87 opt->force_stop_child = NULL;
89 opt->local_opt = NULL;
90 opt->stochastic_population = 0;
91 opt->vector_storage = 0;
96 opt->lb = (double *) malloc(sizeof(double) * (n));
97 if (!opt->lb) goto oom;
98 opt->ub = (double *) malloc(sizeof(double) * (n));
99 if (!opt->ub) goto oom;
100 opt->xtol_abs = (double *) malloc(sizeof(double) * (n));
101 if (!opt->xtol_abs) goto oom;
102 nlopt_set_lower_bounds1(opt, -HUGE_VAL);
103 nlopt_set_upper_bounds1(opt, +HUGE_VAL);
104 nlopt_set_xtol_abs1(opt, 0.0);
115 nlopt_opt NLOPT_STDCALL nlopt_copy(const nlopt_opt opt)
117 nlopt_opt nopt = NULL;
121 nopt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
123 nopt->lb = nopt->ub = nopt->xtol_abs = NULL;
124 nopt->fc = nopt->h = NULL;
125 nopt->m_alloc = nopt->p_alloc = 0;
126 nopt->local_opt = NULL;
129 opt->force_stop_child = NULL;
131 munge = nopt->munge_on_copy;
132 if (munge && nopt->f_data)
133 if (!(nopt->f_data = munge(nopt->f_data))) goto oom;
136 nopt->lb = (double *) malloc(sizeof(double) * (opt->n));
137 if (!opt->lb) goto oom;
138 nopt->ub = (double *) malloc(sizeof(double) * (opt->n));
139 if (!opt->ub) goto oom;
140 nopt->xtol_abs = (double *) malloc(sizeof(double) * (opt->n));
141 if (!opt->xtol_abs) goto oom;
143 memcpy(nopt->lb, opt->lb, sizeof(double) * (opt->n));
144 memcpy(nopt->ub, opt->ub, sizeof(double) * (opt->n));
145 memcpy(nopt->xtol_abs, opt->xtol_abs, sizeof(double) * (opt->n));
149 nopt->m_alloc = opt->m;
150 nopt->fc = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
152 if (!nopt->fc) goto oom;
153 memcpy(nopt->fc, opt->fc, sizeof(nlopt_constraint) * (opt->m));
154 for (i = 0; i < opt->m; ++i) nopt->fc[i].tol = NULL;
156 for (i = 0; i < opt->m; ++i)
157 if (nopt->fc[i].f_data &&
159 = munge(nopt->fc[i].f_data)))
161 for (i = 0; i < opt->m; ++i)
162 if (opt->fc[i].tol) {
163 nopt->fc[i].tol = (double *) malloc(sizeof(double)
165 if (!nopt->fc[i].tol) goto oom;
166 memcpy(nopt->fc[i].tol, opt->fc[i].tol,
167 sizeof(double) * nopt->fc[i].m);
172 nopt->p_alloc = opt->p;
173 nopt->h = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
175 if (!nopt->h) goto oom;
176 memcpy(nopt->h, opt->h, sizeof(nlopt_constraint) * (opt->p));
177 for (i = 0; i < opt->p; ++i) nopt->h[i].tol = NULL;
179 for (i = 0; i < opt->p; ++i)
180 if (nopt->h[i].f_data &&
182 = munge(nopt->h[i].f_data)))
184 for (i = 0; i < opt->p; ++i)
186 nopt->h[i].tol = (double *) malloc(sizeof(double)
188 if (!nopt->h[i].tol) goto oom;
189 memcpy(nopt->h[i].tol, opt->h[i].tol,
190 sizeof(double) * nopt->h[i].m);
194 if (opt->local_opt) {
195 nopt->local_opt = nlopt_copy(opt->local_opt);
196 if (!nopt->local_opt) goto oom;
200 nopt->dx = (double *) malloc(sizeof(double) * (opt->n));
201 if (!nopt->dx) goto oom;
202 memcpy(nopt->dx, opt->dx, sizeof(double) * (opt->n));
208 nopt->munge_on_destroy = NULL; // better to leak mem than crash
213 /*************************************************************************/
215 nlopt_result NLOPT_STDCALL nlopt_set_min_objective(nlopt_opt opt,
216 nlopt_func f, void *f_data)
219 if (opt->munge_on_destroy) opt->munge_on_destroy(opt->f_data);
220 opt->f = f; opt->f_data = f_data;
222 if (nlopt_isinf(opt->stopval) && opt->stopval > 0)
223 opt->stopval = -HUGE_VAL; /* switch default from max to min */
224 return NLOPT_SUCCESS;
226 return NLOPT_INVALID_ARGS;
229 nlopt_result NLOPT_STDCALL nlopt_set_max_objective(nlopt_opt opt,
230 nlopt_func f, void *f_data)
233 if (opt->munge_on_destroy) opt->munge_on_destroy(opt->f_data);
234 opt->f = f; opt->f_data = f_data;
236 if (nlopt_isinf(opt->stopval) && opt->stopval < 0)
237 opt->stopval = +HUGE_VAL; /* switch default from min to max */
238 return NLOPT_SUCCESS;
240 return NLOPT_INVALID_ARGS;
243 /*************************************************************************/
246 NLOPT_STDCALL nlopt_set_lower_bounds(nlopt_opt opt, const double *lb)
248 if (opt && (opt->n == 0 || lb)) {
249 memcpy(opt->lb, lb, sizeof(double) * (opt->n));
250 return NLOPT_SUCCESS;
252 return NLOPT_INVALID_ARGS;
256 NLOPT_STDCALL nlopt_set_lower_bounds1(nlopt_opt opt, double lb)
260 for (i = 0; i < opt->n; ++i)
262 return NLOPT_SUCCESS;
264 return NLOPT_INVALID_ARGS;
268 NLOPT_STDCALL nlopt_get_lower_bounds(const nlopt_opt opt, double *lb)
270 if (opt && (opt->n == 0 || lb)) {
271 memcpy(lb, opt->lb, sizeof(double) * (opt->n));
272 return NLOPT_SUCCESS;
274 return NLOPT_INVALID_ARGS;
278 NLOPT_STDCALL nlopt_set_upper_bounds(nlopt_opt opt, const double *ub)
280 if (opt && (opt->n == 0 || ub)) {
281 memcpy(opt->ub, ub, sizeof(double) * (opt->n));
282 return NLOPT_SUCCESS;
284 return NLOPT_INVALID_ARGS;
288 NLOPT_STDCALL nlopt_set_upper_bounds1(nlopt_opt opt, double ub)
292 for (i = 0; i < opt->n; ++i)
294 return NLOPT_SUCCESS;
296 return NLOPT_INVALID_ARGS;
300 NLOPT_STDCALL nlopt_get_upper_bounds(const nlopt_opt opt, double *ub)
302 if (opt && (opt->n == 0 || ub)) {
303 memcpy(ub, opt->ub, sizeof(double) * (opt->n));
304 return NLOPT_SUCCESS;
306 return NLOPT_INVALID_ARGS;
309 /*************************************************************************/
311 #define AUGLAG_ALG(a) ((a) == NLOPT_AUGLAG || \
312 (a) == NLOPT_AUGLAG_EQ || \
313 (a) == NLOPT_LN_AUGLAG || \
314 (a) == NLOPT_LN_AUGLAG_EQ || \
315 (a) == NLOPT_LD_AUGLAG || \
316 (a) == NLOPT_LD_AUGLAG_EQ)
319 NLOPT_STDCALL nlopt_remove_inequality_constraints(nlopt_opt opt)
322 if (!opt) return NLOPT_INVALID_ARGS;
323 if (opt->munge_on_destroy) {
324 nlopt_munge munge = opt->munge_on_destroy;
325 for (i = 0; i < opt->m; ++i)
326 munge(opt->fc[i].f_data);
328 for (i = 0; i < opt->m; ++i)
329 free(opt->fc[i].tol);
332 opt->m = opt->m_alloc = 0;
333 return NLOPT_SUCCESS;
336 static nlopt_result add_constraint(unsigned *m, unsigned *m_alloc,
337 nlopt_constraint **c,
338 unsigned fm, nlopt_func fc, nlopt_mfunc mfc,
345 if ((fc && mfc) || (fc && fm != 1) || (!fc && !mfc))
346 return NLOPT_INVALID_ARGS;
348 for (i = 0; i < fm; ++i) if (tol[i] < 0) return NLOPT_INVALID_ARGS;
350 tolcopy = (double *) malloc(sizeof(double) * fm);
351 if (fm && !tolcopy) return NLOPT_OUT_OF_MEMORY;
353 memcpy(tolcopy, tol, sizeof(double) * fm);
355 for (i = 0; i < fm; ++i) tolcopy[i] = 0;
359 /* allocate by repeated doubling so that
360 we end up with O(log m) mallocs rather than O(m). */
362 *c = (nlopt_constraint *) realloc(*c,
363 sizeof(nlopt_constraint)
368 return NLOPT_OUT_OF_MEMORY;
374 (*c)[*m - 1].mf = mfc;
375 (*c)[*m - 1].f_data = fc_data;
376 (*c)[*m - 1].tol = tolcopy;
377 return NLOPT_SUCCESS;
380 static int inequality_ok(nlopt_algorithm algorithm) {
381 /* nonlinear constraints are only supported with some algorithms */
382 return (algorithm == NLOPT_LD_MMA || algorithm == NLOPT_LD_CCSAQ
383 || algorithm == NLOPT_LD_SLSQP
384 || algorithm == NLOPT_LN_COBYLA
385 || AUGLAG_ALG(algorithm)
386 || algorithm == NLOPT_GN_ISRES
387 || algorithm == NLOPT_GN_ORIG_DIRECT
388 || algorithm == NLOPT_GN_ORIG_DIRECT_L);
392 NLOPT_STDCALL nlopt_add_inequality_mconstraint(nlopt_opt opt, unsigned m,
393 nlopt_mfunc fc, void *fc_data,
397 if (!m) { /* empty constraints are always ok */
398 if (opt && opt->munge_on_destroy) opt->munge_on_destroy(fc_data);
399 return NLOPT_SUCCESS;
401 if (!opt || !inequality_ok(opt->algorithm)) ret = NLOPT_INVALID_ARGS;
402 else ret = add_constraint(&opt->m, &opt->m_alloc, &opt->fc,
403 m, NULL, fc, fc_data, tol);
404 if (ret < 0 && opt && opt->munge_on_destroy)
405 opt->munge_on_destroy(fc_data);
410 NLOPT_STDCALL nlopt_add_inequality_constraint(nlopt_opt opt,
411 nlopt_func fc, void *fc_data,
415 if (!opt || !inequality_ok(opt->algorithm)) ret = NLOPT_INVALID_ARGS;
416 else ret = add_constraint(&opt->m, &opt->m_alloc, &opt->fc,
417 1, fc, NULL, fc_data, &tol);
418 if (ret < 0 && opt && opt->munge_on_destroy)
419 opt->munge_on_destroy(fc_data);
424 NLOPT_STDCALL nlopt_remove_equality_constraints(nlopt_opt opt)
427 if (!opt) return NLOPT_INVALID_ARGS;
428 if (opt->munge_on_destroy) {
429 nlopt_munge munge = opt->munge_on_destroy;
430 for (i = 0; i < opt->p; ++i)
431 munge(opt->h[i].f_data);
433 for (i = 0; i < opt->p; ++i)
437 opt->p = opt->p_alloc = 0;
438 return NLOPT_SUCCESS;
441 static int equality_ok(nlopt_algorithm algorithm) {
442 /* equality constraints (h(x) = 0) only via some algorithms */
443 return (AUGLAG_ALG(algorithm)
444 || algorithm == NLOPT_LD_SLSQP
445 || algorithm == NLOPT_GN_ISRES
446 || algorithm == NLOPT_LN_COBYLA);
450 NLOPT_STDCALL nlopt_add_equality_mconstraint(nlopt_opt opt, unsigned m,
451 nlopt_mfunc fc, void *fc_data,
455 if (!m) { /* empty constraints are always ok */
456 if (opt && opt->munge_on_destroy) opt->munge_on_destroy(fc_data);
457 return NLOPT_SUCCESS;
459 if (!opt || !equality_ok(opt->algorithm)
460 || nlopt_count_constraints(opt->p, opt->h) + m > opt->n)
461 ret = NLOPT_INVALID_ARGS;
462 else ret = add_constraint(&opt->p, &opt->p_alloc, &opt->h,
463 m, NULL, fc, fc_data, tol);
464 if (ret < 0 && opt && opt->munge_on_destroy)
465 opt->munge_on_destroy(fc_data);
470 NLOPT_STDCALL nlopt_add_equality_constraint(nlopt_opt opt,
471 nlopt_func fc, void *fc_data,
475 if (!opt || !equality_ok(opt->algorithm)
476 || nlopt_count_constraints(opt->p, opt->h) + 1 > opt->n)
477 ret = NLOPT_INVALID_ARGS;
478 else ret = add_constraint(&opt->p, &opt->p_alloc, &opt->h,
479 1, fc, NULL, fc_data, &tol);
480 if (ret < 0 && opt && opt->munge_on_destroy)
481 opt->munge_on_destroy(fc_data);
485 /*************************************************************************/
487 #define SET(param, T, arg) \
488 nlopt_result NLOPT_STDCALL nlopt_set_##param(nlopt_opt opt, T arg) \
492 return NLOPT_SUCCESS; \
494 return NLOPT_INVALID_ARGS; \
498 #define GET(param, T, arg) T NLOPT_STDCALL \
499 nlopt_get_##param(const nlopt_opt opt) { \
503 #define GETSET(param, T, arg) GET(param, T, arg) SET(param, T, arg)
505 GETSET(stopval, double, stopval)
507 GETSET(ftol_rel, double, ftol_rel)
508 GETSET(ftol_abs, double, ftol_abs)
509 GETSET(xtol_rel, double, xtol_rel)
512 NLOPT_STDCALL nlopt_set_xtol_abs(nlopt_opt opt, const double *xtol_abs)
515 memcpy(opt->xtol_abs, xtol_abs, opt->n * sizeof(double));
516 return NLOPT_SUCCESS;
518 return NLOPT_INVALID_ARGS;
522 NLOPT_STDCALL nlopt_set_xtol_abs1(nlopt_opt opt, double xtol_abs)
526 for (i = 0; i < opt->n; ++i)
527 opt->xtol_abs[i] = xtol_abs;
528 return NLOPT_SUCCESS;
530 return NLOPT_INVALID_ARGS;
534 NLOPT_STDCALL nlopt_get_xtol_abs(const nlopt_opt opt, double *xtol_abs)
536 memcpy(xtol_abs, opt->xtol_abs, opt->n * sizeof(double));
537 return NLOPT_SUCCESS;
540 GETSET(maxeval, int, maxeval)
542 GETSET(maxtime, double, maxtime)
544 /*************************************************************************/
547 NLOPT_STDCALL nlopt_set_force_stop(nlopt_opt opt, int force_stop)
550 opt->force_stop = force_stop;
551 if (opt->force_stop_child)
552 return nlopt_set_force_stop(opt->force_stop_child, force_stop);
553 return NLOPT_SUCCESS;
555 return NLOPT_INVALID_ARGS;
558 GET(force_stop, int, force_stop)
559 nlopt_result NLOPT_STDCALL nlopt_force_stop(nlopt_opt opt) {
560 return nlopt_set_force_stop(opt, 1);
563 /*************************************************************************/
565 GET(algorithm, nlopt_algorithm, algorithm)
566 GET(dimension, unsigned, n)
568 /*************************************************************************/
571 NLOPT_STDCALL nlopt_set_local_optimizer(nlopt_opt opt,
572 const nlopt_opt local_opt)
575 if (local_opt && local_opt->n != opt->n) return NLOPT_INVALID_ARGS;
576 nlopt_destroy(opt->local_opt);
577 opt->local_opt = nlopt_copy(local_opt);
579 if (!opt->local_opt) return NLOPT_OUT_OF_MEMORY;
580 nlopt_set_lower_bounds(opt->local_opt, opt->lb);
581 nlopt_set_upper_bounds(opt->local_opt, opt->ub);
582 nlopt_remove_inequality_constraints(opt->local_opt);
583 nlopt_remove_equality_constraints(opt->local_opt);
584 nlopt_set_min_objective(opt->local_opt, NULL, NULL);
585 nlopt_set_munge(opt->local_opt, NULL, NULL);
586 opt->local_opt->force_stop = 0;
588 return NLOPT_SUCCESS;
590 return NLOPT_INVALID_ARGS;
593 /*************************************************************************/
595 GETSET(population, unsigned, stochastic_population)
596 GETSET(vector_storage, unsigned, vector_storage)
598 /*************************************************************************/
600 nlopt_result NLOPT_STDCALL nlopt_set_initial_step1(nlopt_opt opt, double dx)
603 if (!opt || dx == 0) return NLOPT_INVALID_ARGS;
604 if (!opt->dx && opt->n > 0) {
605 opt->dx = (double *) malloc(sizeof(double) * (opt->n));
606 if (!opt->dx) return NLOPT_OUT_OF_MEMORY;
608 for (i = 0; i < opt->n; ++i) opt->dx[i] = dx;
609 return NLOPT_SUCCESS;
613 NLOPT_STDCALL nlopt_set_initial_step(nlopt_opt opt, const double *dx)
616 if (!opt) return NLOPT_INVALID_ARGS;
618 free(opt->dx); opt->dx = NULL;
619 return NLOPT_SUCCESS;
621 for (i = 0; i < opt->n; ++i) if (dx[i] == 0) return NLOPT_INVALID_ARGS;
622 if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
623 return NLOPT_OUT_OF_MEMORY;
624 memcpy(opt->dx, dx, sizeof(double) * (opt->n));
625 return NLOPT_SUCCESS;
629 NLOPT_STDCALL nlopt_get_initial_step(const nlopt_opt opt, const double *x,
632 if (!opt) return NLOPT_INVALID_ARGS;
633 if (!opt->n) return NLOPT_SUCCESS;
635 nlopt_opt o = (nlopt_opt) opt; /* discard const temporarily */
636 nlopt_result ret = nlopt_set_default_initial_step(o, x);
637 if (ret != NLOPT_SUCCESS) return ret;
638 memcpy(dx, o->dx, sizeof(double) * (opt->n));
639 free(o->dx); o->dx = NULL; /* don't save, since x-dependent */
642 memcpy(dx, opt->dx, sizeof(double) * (opt->n));
643 return NLOPT_SUCCESS;
647 NLOPT_STDCALL nlopt_set_default_initial_step(nlopt_opt opt, const double *x)
649 const double *lb, *ub;
652 if (!opt || !x) return NLOPT_INVALID_ARGS;
653 lb = opt->lb; ub = opt->ub;
655 if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
656 return NLOPT_OUT_OF_MEMORY;
658 /* crude heuristics for initial step size of nonderivative algorithms */
659 for (i = 0; i < opt->n; ++i) {
660 double step = HUGE_VAL;
662 if (!nlopt_isinf(ub[i]) && !nlopt_isinf(lb[i])
663 && (ub[i] - lb[i]) * 0.25 < step && ub[i] > lb[i])
664 step = (ub[i] - lb[i]) * 0.25;
665 if (!nlopt_isinf(ub[i])
666 && ub[i] - x[i] < step && ub[i] > x[i])
667 step = (ub[i] - x[i]) * 0.75;
668 if (!nlopt_isinf(lb[i])
669 && x[i] - lb[i] < step && x[i] > lb[i])
670 step = (x[i] - lb[i]) * 0.75;
672 if (nlopt_isinf(step)) {
673 if (!nlopt_isinf(ub[i])
674 && fabs(ub[i] - x[i]) < fabs(step))
675 step = (ub[i] - x[i]) * 1.1;
676 if (!nlopt_isinf(lb[i])
677 && fabs(x[i] - lb[i]) < fabs(step))
678 step = (x[i] - lb[i]) * 1.1;
680 if (nlopt_isinf(step) || step == 0) {
683 if (nlopt_isinf(step) || step == 0)
688 return NLOPT_SUCCESS;
691 /*************************************************************************/
693 void NLOPT_STDCALL nlopt_set_munge(nlopt_opt opt,
694 nlopt_munge munge_on_destroy,
695 nlopt_munge munge_on_copy) {
697 opt->munge_on_destroy = munge_on_destroy;
698 opt->munge_on_copy = munge_on_copy;
702 /*************************************************************************/