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.
29 #include "nlopt-internal.h"
30 #include "nlopt-util.h"
32 /*************************************************************************/
34 void nlopt_destroy(nlopt_opt opt)
37 free(opt->lb); free(opt->ub);
41 nlopt_destroy(opt->local_opt);
47 nlopt_opt nlopt_create(nlopt_algorithm algorithm, unsigned n)
51 if (((int) algorithm) < 0 || algorithm >= NLOPT_NUM_ALGORITHMS)
54 opt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
56 opt->algorithm = algorithm;
58 opt->f = NULL; opt->f_data = NULL;
61 opt->lb = opt->ub = NULL;
62 opt->m = opt->m_alloc = 0;
64 opt->p = opt->p_alloc = 0;
67 opt->stopval = -HUGE_VAL;
68 opt->ftol_rel = opt->ftol_abs = 0;
69 opt->xtol_rel = 0; opt->xtol_abs = NULL;
73 opt->force_stop_child = NULL;
75 opt->local_opt = NULL;
76 opt->stochastic_population = 0;
80 opt->lb = (double *) malloc(sizeof(double) * (n));
81 if (!opt->lb) goto oom;
82 opt->ub = (double *) malloc(sizeof(double) * (n));
83 if (!opt->ub) goto oom;
84 opt->xtol_abs = (double *) malloc(sizeof(double) * (n));
85 if (!opt->xtol_abs) goto oom;
86 nlopt_set_lower_bounds1(opt, -HUGE_VAL);
87 nlopt_set_upper_bounds1(opt, +HUGE_VAL);
88 nlopt_set_xtol_abs1(opt, 0.0);
99 nlopt_opt nlopt_copy(const nlopt_opt opt)
101 nlopt_opt nopt = NULL;
103 nopt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
105 nopt->lb = nopt->ub = nopt->xtol_abs = NULL;
106 nopt->fc = nopt->h = NULL;
107 nopt->m_alloc = nopt->p_alloc = 0;
108 nopt->local_opt = NULL;
110 opt->force_stop_child = NULL;
113 nopt->lb = (double *) malloc(sizeof(double) * (opt->n));
114 if (!opt->lb) goto oom;
115 nopt->ub = (double *) malloc(sizeof(double) * (opt->n));
116 if (!opt->ub) goto oom;
117 nopt->xtol_abs = (double *) malloc(sizeof(double) * (opt->n));
118 if (!opt->xtol_abs) goto oom;
120 memcpy(nopt->lb, opt->lb, sizeof(double) * (opt->n));
121 memcpy(nopt->ub, opt->ub, sizeof(double) * (opt->n));
122 memcpy(nopt->xtol_abs, opt->xtol_abs, sizeof(double) * (opt->n));
126 nopt->m_alloc = opt->m;
127 nopt->fc = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
129 if (!nopt->fc) goto oom;
130 memcpy(nopt->fc, opt->fc, sizeof(nlopt_constraint) * (opt->m));
134 nopt->p_alloc = opt->p;
135 nopt->h = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
137 if (!nopt->h) goto oom;
138 memcpy(nopt->h, opt->h, sizeof(nlopt_constraint) * (opt->p));
141 if (opt->local_opt) {
142 nopt->local_opt = nlopt_copy(opt->local_opt);
143 if (!nopt->local_opt) goto oom;
147 nopt->dx = (double *) malloc(sizeof(double) * (opt->n));
148 if (!nopt->dx) goto oom;
149 memcpy(nopt->dx, opt->dx, sizeof(double) * (opt->n));
159 /*************************************************************************/
161 nlopt_result nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, void *f_data)
164 opt->f = f; opt->f_data = f_data;
166 if (nlopt_isinf(opt->stopval) && opt->stopval > 0)
167 opt->stopval = -HUGE_VAL; /* switch default from max to min */
168 return NLOPT_SUCCESS;
170 return NLOPT_INVALID_ARGS;
173 nlopt_result nlopt_set_max_objective(nlopt_opt opt, nlopt_func f, void *f_data)
176 opt->f = f; opt->f_data = f_data;
178 if (nlopt_isinf(opt->stopval) && opt->stopval < 0)
179 opt->stopval = +HUGE_VAL; /* switch default from min to max */
180 return NLOPT_SUCCESS;
182 return NLOPT_INVALID_ARGS;
185 /*************************************************************************/
187 nlopt_result nlopt_set_lower_bounds(nlopt_opt opt, const double *lb)
189 if (opt && (opt->n == 0 || lb)) {
190 memcpy(opt->lb, lb, sizeof(double) * (opt->n));
191 return NLOPT_SUCCESS;
193 return NLOPT_INVALID_ARGS;
196 nlopt_result nlopt_set_lower_bounds1(nlopt_opt opt, double lb)
200 for (i = 0; i < opt->n; ++i)
202 return NLOPT_SUCCESS;
204 return NLOPT_INVALID_ARGS;
207 nlopt_result nlopt_get_lower_bounds(nlopt_opt opt, double *lb)
209 if (opt && (opt->n == 0 || lb)) {
210 memcpy(lb, opt->lb, sizeof(double) * (opt->n));
211 return NLOPT_SUCCESS;
213 return NLOPT_INVALID_ARGS;
216 nlopt_result nlopt_set_upper_bounds(nlopt_opt opt, const double *ub)
218 if (opt && (opt->n == 0 || ub)) {
219 memcpy(opt->ub, ub, sizeof(double) * (opt->n));
220 return NLOPT_SUCCESS;
222 return NLOPT_INVALID_ARGS;
225 nlopt_result nlopt_set_upper_bounds1(nlopt_opt opt, double ub)
229 for (i = 0; i < opt->n; ++i)
231 return NLOPT_SUCCESS;
233 return NLOPT_INVALID_ARGS;
236 nlopt_result nlopt_get_upper_bounds(nlopt_opt opt, double *ub)
238 if (opt && (opt->n == 0 || ub)) {
239 memcpy(ub, opt->ub, sizeof(double) * (opt->n));
240 return NLOPT_SUCCESS;
242 return NLOPT_INVALID_ARGS;
245 /*************************************************************************/
247 #define AUGLAG_ALG(a) ((a) == NLOPT_LN_AUGLAG || \
248 (a) == NLOPT_LN_AUGLAG_EQ || \
249 (a) == NLOPT_LD_AUGLAG || \
250 (a) == NLOPT_LD_AUGLAG_EQ)
252 nlopt_result nlopt_remove_inequality_constraints(nlopt_opt opt)
256 opt->m = opt->m_alloc = 0;
257 return NLOPT_SUCCESS;
260 static nlopt_result add_constraint(unsigned *m, unsigned *m_alloc,
261 nlopt_constraint **c,
262 nlopt_func fc, void *fc_data,
267 /* allocate by repeated doubling so that
268 we end up with O(log m) mallocs rather than O(m). */
270 *c = (nlopt_constraint *) realloc(*c,
271 sizeof(nlopt_constraint)
275 return NLOPT_OUT_OF_MEMORY;
280 (*c)[*m - 1].f_data = fc_data;
281 (*c)[*m - 1].tol = tol;
282 return NLOPT_SUCCESS;
285 nlopt_result nlopt_add_inequality_constraint(nlopt_opt opt,
286 nlopt_func fc, void *fc_data,
289 if (opt && fc && tol >= 0) {
290 /* nonlinear constraints are only supported with some algorithms */
291 if (opt->algorithm != NLOPT_LD_MMA
292 && opt->algorithm != NLOPT_LN_COBYLA
293 && !AUGLAG_ALG(opt->algorithm)
294 && opt->algorithm != NLOPT_GN_ISRES
295 && opt->algorithm != NLOPT_GN_ORIG_DIRECT
296 && opt->algorithm != NLOPT_GN_ORIG_DIRECT_L)
297 return NLOPT_INVALID_ARGS;
299 return add_constraint(&opt->m, &opt->m_alloc, &opt->fc,
302 return NLOPT_INVALID_ARGS;
305 nlopt_result nlopt_remove_equality_constraints(nlopt_opt opt)
309 opt->p = opt->p_alloc = 0;
310 return NLOPT_SUCCESS;
313 nlopt_result nlopt_add_equality_constraint(nlopt_opt opt,
314 nlopt_func h, void *h_data,
317 if (opt && h && tol >= 0) {
318 /* equality constraints (h(x) = 0) only via some algorithms */
319 if (!AUGLAG_ALG(opt->algorithm) && opt->algorithm != NLOPT_GN_ISRES)
320 return NLOPT_INVALID_ARGS;
322 return add_constraint(&opt->p, &opt->p_alloc, &opt->h,
325 return NLOPT_INVALID_ARGS;
328 /*************************************************************************/
330 #define SET(param, T, arg) \
331 nlopt_result nlopt_set_##param(nlopt_opt opt, T arg) \
335 return NLOPT_SUCCESS; \
337 return NLOPT_INVALID_ARGS; \
341 #define GET(param, T, arg) T nlopt_get_##param(const nlopt_opt opt) { \
345 #define GETSET(param, T, arg) GET(param, T, arg) SET(param, T, arg)
347 GETSET(stopval, double, stopval)
349 GETSET(ftol_rel, double, ftol_rel)
350 GETSET(ftol_abs, double, ftol_abs)
351 GETSET(xtol_rel, double, xtol_rel)
353 nlopt_result nlopt_set_xtol_abs(nlopt_opt opt, const double *xtol_abs)
356 memcpy(opt->xtol_abs, xtol_abs, opt->n & sizeof(double));
357 return NLOPT_SUCCESS;
359 return NLOPT_INVALID_ARGS;
362 nlopt_result nlopt_set_xtol_abs1(nlopt_opt opt, const double xtol_abs)
366 for (i = 0; i < opt->n; ++i)
367 opt->xtol_abs[i] = xtol_abs;
368 return NLOPT_SUCCESS;
370 return NLOPT_INVALID_ARGS;
373 nlopt_result nlopt_get_xtol_abs(const nlopt_opt opt, double *xtol_abs)
375 memcpy(xtol_abs, opt->xtol_abs, opt->n & sizeof(double));
376 return NLOPT_SUCCESS;
379 GETSET(maxeval, int, maxeval)
381 GETSET(maxtime, double, maxtime)
383 /*************************************************************************/
385 nlopt_result nlopt_set_force_stop(nlopt_opt opt, int force_stop)
388 opt->force_stop = force_stop;
389 if (opt->force_stop_child)
390 return nlopt_set_force_stop(opt->force_stop_child, force_stop);
391 return NLOPT_SUCCESS;
393 return NLOPT_INVALID_ARGS;
396 GET(force_stop, int, force_stop)
397 nlopt_result nlopt_force_stop(nlopt_opt opt) { nlopt_set_force_stop(opt, 1); }
399 /*************************************************************************/
401 GET(algorithm, nlopt_algorithm, algorithm)
402 GET(dimension, unsigned, n)
404 /*************************************************************************/
406 nlopt_result nlopt_set_local_optimizer(nlopt_opt opt,
407 const nlopt_opt local_opt)
410 if (local_opt && local_opt->n != opt->n) return NLOPT_INVALID_ARGS;
411 nlopt_destroy(opt->local_opt);
412 opt->local_opt = nlopt_copy(local_opt);
414 if (!opt->local_opt) return NLOPT_OUT_OF_MEMORY;
415 nlopt_set_lower_bounds(opt->local_opt, opt->lb);
416 nlopt_set_upper_bounds(opt->local_opt, opt->ub);
417 nlopt_remove_inequality_constraints(opt->local_opt);
418 nlopt_remove_equality_constraints(opt->local_opt);
419 nlopt_set_min_objective(opt->local_opt, NULL, NULL);
420 opt->local_opt->force_stop = 0;
422 return NLOPT_SUCCESS;
424 return NLOPT_INVALID_ARGS;
427 /*************************************************************************/
429 GETSET(population, unsigned, stochastic_population)
431 /*************************************************************************/
433 nlopt_result nlopt_set_initial_step1(nlopt_opt opt, double dx)
436 if (!opt || dx == 0) return NLOPT_INVALID_ARGS;
437 if (!opt->dx && opt->n > 0) {
438 opt->dx = (double *) malloc(sizeof(double) * (opt->n));
439 if (!opt->dx) return NLOPT_OUT_OF_MEMORY;
441 for (i = 0; i < opt->n; ++i) opt->dx[i] = dx;
442 return NLOPT_SUCCESS;
445 nlopt_result nlopt_set_initial_step(nlopt_opt opt, const double *dx)
448 if (!opt || !dx) return NLOPT_INVALID_ARGS;
449 for (i = 0; i < opt->n; ++i) if (dx[i] == 0) return NLOPT_INVALID_ARGS;
450 if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
451 return NLOPT_OUT_OF_MEMORY;
452 memcpy(opt->dx, dx, sizeof(double) * (opt->n));
453 return NLOPT_SUCCESS;
456 nlopt_result nlopt_get_initial_step(const nlopt_opt opt, const double *x,
459 if (!opt) return NLOPT_INVALID_ARGS;
460 if (!opt->n) return NLOPT_SUCCESS;
462 nlopt_opt o = (nlopt_opt) opt; /* discard const temporarily */
463 nlopt_result ret = nlopt_set_default_initial_step(o, x);
464 if (ret != NLOPT_SUCCESS) return ret;
465 memcpy(dx, o->dx, sizeof(double) * (opt->n));
466 free(o->dx); o->dx = NULL; /* don't save, since x-dependent */
469 memcpy(dx, opt->dx, sizeof(double) * (opt->n));
470 return NLOPT_SUCCESS;
473 nlopt_result nlopt_set_default_initial_step(nlopt_opt opt, const double *x)
475 const double *lb, *ub;
478 if (!opt || !x) return NLOPT_INVALID_ARGS;
479 lb = opt->lb; ub = opt->ub;
481 if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
482 return NLOPT_OUT_OF_MEMORY;
484 /* crude heuristics for initial step size of nonderivative algorithms */
485 for (i = 0; i < opt->n; ++i) {
486 double step = HUGE_VAL;
488 if (!nlopt_isinf(ub[i]) && !nlopt_isinf(lb[i])
489 && (ub[i] - lb[i]) * 0.25 < step && ub[i] > lb[i])
490 step = (ub[i] - lb[i]) * 0.25;
491 if (!nlopt_isinf(ub[i])
492 && ub[i] - x[i] < step && ub[i] > x[i])
493 step = (ub[i] - x[i]) * 0.75;
494 if (!nlopt_isinf(lb[i])
495 && x[i] - lb[i] < step && x[i] > lb[i])
496 step = (x[i] - lb[i]) * 0.75;
498 if (nlopt_isinf(step)) {
499 if (!nlopt_isinf(ub[i])
500 && fabs(ub[i] - x[i]) < fabs(step))
501 step = (ub[i] - x[i]) * 1.1;
502 if (!nlopt_isinf(lb[i])
503 && fabs(x[i] - lb[i]) < fabs(step))
504 step = (x[i] - lb[i]) * 1.1;
506 if (nlopt_isinf(step) || step == 0) {
509 if (nlopt_isinf(step) || step == 0)
514 return NLOPT_SUCCESS;
517 /*************************************************************************/