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 if (opt->free_f_data) {
40 for (i = 0; i < opt->m; ++i)
41 free(opt->fc[i].f_data);
42 for (i = 0; i < opt->p; ++i)
43 free(opt->h[i].f_data);
45 free(opt->lb); free(opt->ub);
49 nlopt_destroy(opt->local_opt);
55 nlopt_opt nlopt_create(nlopt_algorithm algorithm, unsigned n)
59 if (((int) algorithm) < 0 || algorithm >= NLOPT_NUM_ALGORITHMS)
62 opt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
64 opt->algorithm = algorithm;
66 opt->f = NULL; opt->f_data = NULL;
70 opt->lb = opt->ub = NULL;
71 opt->m = opt->m_alloc = 0;
73 opt->p = opt->p_alloc = 0;
76 opt->stopval = -HUGE_VAL;
77 opt->ftol_rel = opt->ftol_abs = 0;
78 opt->xtol_rel = 0; opt->xtol_abs = NULL;
82 opt->force_stop_child = NULL;
84 opt->local_opt = NULL;
85 opt->stochastic_population = 0;
89 opt->lb = (double *) malloc(sizeof(double) * (n));
90 if (!opt->lb) goto oom;
91 opt->ub = (double *) malloc(sizeof(double) * (n));
92 if (!opt->ub) goto oom;
93 opt->xtol_abs = (double *) malloc(sizeof(double) * (n));
94 if (!opt->xtol_abs) goto oom;
95 nlopt_set_lower_bounds1(opt, -HUGE_VAL);
96 nlopt_set_upper_bounds1(opt, +HUGE_VAL);
97 nlopt_set_xtol_abs1(opt, 0.0);
108 nlopt_opt nlopt_copy(const nlopt_opt opt)
110 nlopt_opt nopt = NULL;
112 nopt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
114 nopt->lb = nopt->ub = nopt->xtol_abs = NULL;
115 nopt->fc = nopt->h = NULL;
116 nopt->m_alloc = nopt->p_alloc = 0;
117 nopt->local_opt = NULL;
119 opt->force_stop_child = NULL;
120 opt->free_f_data = 0;
123 nopt->lb = (double *) malloc(sizeof(double) * (opt->n));
124 if (!opt->lb) goto oom;
125 nopt->ub = (double *) malloc(sizeof(double) * (opt->n));
126 if (!opt->ub) goto oom;
127 nopt->xtol_abs = (double *) malloc(sizeof(double) * (opt->n));
128 if (!opt->xtol_abs) goto oom;
130 memcpy(nopt->lb, opt->lb, sizeof(double) * (opt->n));
131 memcpy(nopt->ub, opt->ub, sizeof(double) * (opt->n));
132 memcpy(nopt->xtol_abs, opt->xtol_abs, sizeof(double) * (opt->n));
136 nopt->m_alloc = opt->m;
137 nopt->fc = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
139 if (!nopt->fc) goto oom;
140 memcpy(nopt->fc, opt->fc, sizeof(nlopt_constraint) * (opt->m));
144 nopt->p_alloc = opt->p;
145 nopt->h = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
147 if (!nopt->h) goto oom;
148 memcpy(nopt->h, opt->h, sizeof(nlopt_constraint) * (opt->p));
151 if (opt->local_opt) {
152 nopt->local_opt = nlopt_copy(opt->local_opt);
153 if (!nopt->local_opt) goto oom;
157 nopt->dx = (double *) malloc(sizeof(double) * (opt->n));
158 if (!nopt->dx) goto oom;
159 memcpy(nopt->dx, opt->dx, sizeof(double) * (opt->n));
169 /*************************************************************************/
171 nlopt_result nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, void *f_data)
174 opt->f = f; opt->f_data = f_data;
176 if (nlopt_isinf(opt->stopval) && opt->stopval > 0)
177 opt->stopval = -HUGE_VAL; /* switch default from max to min */
178 return NLOPT_SUCCESS;
180 return NLOPT_INVALID_ARGS;
183 nlopt_result nlopt_set_max_objective(nlopt_opt opt, nlopt_func f, void *f_data)
186 opt->f = f; opt->f_data = f_data;
188 if (nlopt_isinf(opt->stopval) && opt->stopval < 0)
189 opt->stopval = +HUGE_VAL; /* switch default from min to max */
190 return NLOPT_SUCCESS;
192 return NLOPT_INVALID_ARGS;
195 /*************************************************************************/
197 nlopt_result nlopt_set_lower_bounds(nlopt_opt opt, const double *lb)
199 if (opt && (opt->n == 0 || lb)) {
200 memcpy(opt->lb, lb, sizeof(double) * (opt->n));
201 return NLOPT_SUCCESS;
203 return NLOPT_INVALID_ARGS;
206 nlopt_result nlopt_set_lower_bounds1(nlopt_opt opt, double lb)
210 for (i = 0; i < opt->n; ++i)
212 return NLOPT_SUCCESS;
214 return NLOPT_INVALID_ARGS;
217 nlopt_result nlopt_get_lower_bounds(nlopt_opt opt, double *lb)
219 if (opt && (opt->n == 0 || lb)) {
220 memcpy(lb, opt->lb, sizeof(double) * (opt->n));
221 return NLOPT_SUCCESS;
223 return NLOPT_INVALID_ARGS;
226 nlopt_result nlopt_set_upper_bounds(nlopt_opt opt, const double *ub)
228 if (opt && (opt->n == 0 || ub)) {
229 memcpy(opt->ub, ub, sizeof(double) * (opt->n));
230 return NLOPT_SUCCESS;
232 return NLOPT_INVALID_ARGS;
235 nlopt_result nlopt_set_upper_bounds1(nlopt_opt opt, double ub)
239 for (i = 0; i < opt->n; ++i)
241 return NLOPT_SUCCESS;
243 return NLOPT_INVALID_ARGS;
246 nlopt_result nlopt_get_upper_bounds(nlopt_opt opt, double *ub)
248 if (opt && (opt->n == 0 || ub)) {
249 memcpy(ub, opt->ub, sizeof(double) * (opt->n));
250 return NLOPT_SUCCESS;
252 return NLOPT_INVALID_ARGS;
255 /*************************************************************************/
257 #define AUGLAG_ALG(a) ((a) == NLOPT_LN_AUGLAG || \
258 (a) == NLOPT_LN_AUGLAG_EQ || \
259 (a) == NLOPT_LD_AUGLAG || \
260 (a) == NLOPT_LD_AUGLAG_EQ)
262 nlopt_result nlopt_remove_inequality_constraints(nlopt_opt opt)
266 opt->m = opt->m_alloc = 0;
267 return NLOPT_SUCCESS;
270 static nlopt_result add_constraint(unsigned *m, unsigned *m_alloc,
271 nlopt_constraint **c,
272 nlopt_func fc, void *fc_data,
277 /* allocate by repeated doubling so that
278 we end up with O(log m) mallocs rather than O(m). */
280 *c = (nlopt_constraint *) realloc(*c,
281 sizeof(nlopt_constraint)
285 return NLOPT_OUT_OF_MEMORY;
290 (*c)[*m - 1].f_data = fc_data;
291 (*c)[*m - 1].tol = tol;
292 return NLOPT_SUCCESS;
295 nlopt_result nlopt_add_inequality_constraint(nlopt_opt opt,
296 nlopt_func fc, void *fc_data,
299 if (opt && fc && tol >= 0) {
300 /* nonlinear constraints are only supported with some algorithms */
301 if (opt->algorithm != NLOPT_LD_MMA
302 && opt->algorithm != NLOPT_LN_COBYLA
303 && !AUGLAG_ALG(opt->algorithm)
304 && opt->algorithm != NLOPT_GN_ISRES
305 && opt->algorithm != NLOPT_GN_ORIG_DIRECT
306 && opt->algorithm != NLOPT_GN_ORIG_DIRECT_L)
307 return NLOPT_INVALID_ARGS;
308 return add_constraint(&opt->m, &opt->m_alloc, &opt->fc,
311 return NLOPT_INVALID_ARGS;
314 nlopt_result nlopt_remove_equality_constraints(nlopt_opt opt)
318 opt->p = opt->p_alloc = 0;
319 return NLOPT_SUCCESS;
322 nlopt_result nlopt_add_equality_constraint(nlopt_opt opt,
323 nlopt_func h, void *h_data,
326 if (opt && h && tol >= 0) {
327 /* equality constraints (h(x) = 0) only via some algorithms */
328 if (!AUGLAG_ALG(opt->algorithm) && opt->algorithm != NLOPT_GN_ISRES)
329 return NLOPT_INVALID_ARGS;
330 return add_constraint(&opt->p, &opt->p_alloc, &opt->h,
333 return NLOPT_INVALID_ARGS;
336 /*************************************************************************/
338 #define SET(param, T, arg) \
339 nlopt_result nlopt_set_##param(nlopt_opt opt, T arg) \
343 return NLOPT_SUCCESS; \
345 return NLOPT_INVALID_ARGS; \
349 #define GET(param, T, arg) T nlopt_get_##param(const nlopt_opt opt) { \
353 #define GETSET(param, T, arg) GET(param, T, arg) SET(param, T, arg)
355 GETSET(stopval, double, stopval)
357 GETSET(ftol_rel, double, ftol_rel)
358 GETSET(ftol_abs, double, ftol_abs)
359 GETSET(xtol_rel, double, xtol_rel)
361 nlopt_result nlopt_set_xtol_abs(nlopt_opt opt, const double *xtol_abs)
364 memcpy(opt->xtol_abs, xtol_abs, opt->n & sizeof(double));
365 return NLOPT_SUCCESS;
367 return NLOPT_INVALID_ARGS;
370 nlopt_result nlopt_set_xtol_abs1(nlopt_opt opt, const double xtol_abs)
374 for (i = 0; i < opt->n; ++i)
375 opt->xtol_abs[i] = xtol_abs;
376 return NLOPT_SUCCESS;
378 return NLOPT_INVALID_ARGS;
381 nlopt_result nlopt_get_xtol_abs(const nlopt_opt opt, double *xtol_abs)
383 memcpy(xtol_abs, opt->xtol_abs, opt->n & sizeof(double));
384 return NLOPT_SUCCESS;
387 GETSET(maxeval, int, maxeval)
389 GETSET(maxtime, double, maxtime)
391 /*************************************************************************/
393 nlopt_result nlopt_set_force_stop(nlopt_opt opt, int force_stop)
396 opt->force_stop = force_stop;
397 if (opt->force_stop_child)
398 return nlopt_set_force_stop(opt->force_stop_child, force_stop);
399 return NLOPT_SUCCESS;
401 return NLOPT_INVALID_ARGS;
404 GET(force_stop, int, force_stop)
405 nlopt_result nlopt_force_stop(nlopt_opt opt) { nlopt_set_force_stop(opt, 1); }
407 /*************************************************************************/
409 GET(algorithm, nlopt_algorithm, algorithm)
410 GET(dimension, unsigned, n)
412 /*************************************************************************/
414 nlopt_result nlopt_set_local_optimizer(nlopt_opt opt,
415 const nlopt_opt local_opt)
418 if (local_opt && local_opt->n != opt->n) return NLOPT_INVALID_ARGS;
419 nlopt_destroy(opt->local_opt);
420 opt->local_opt = nlopt_copy(local_opt);
422 if (!opt->local_opt) return NLOPT_OUT_OF_MEMORY;
423 nlopt_set_lower_bounds(opt->local_opt, opt->lb);
424 nlopt_set_upper_bounds(opt->local_opt, opt->ub);
425 nlopt_remove_inequality_constraints(opt->local_opt);
426 nlopt_remove_equality_constraints(opt->local_opt);
427 nlopt_set_min_objective(opt->local_opt, NULL, NULL);
428 opt->local_opt->force_stop = 0;
430 return NLOPT_SUCCESS;
432 return NLOPT_INVALID_ARGS;
435 /*************************************************************************/
437 GETSET(population, unsigned, stochastic_population)
439 /*************************************************************************/
441 nlopt_result nlopt_set_initial_step1(nlopt_opt opt, double dx)
444 if (!opt || dx == 0) return NLOPT_INVALID_ARGS;
445 if (!opt->dx && opt->n > 0) {
446 opt->dx = (double *) malloc(sizeof(double) * (opt->n));
447 if (!opt->dx) return NLOPT_OUT_OF_MEMORY;
449 for (i = 0; i < opt->n; ++i) opt->dx[i] = dx;
450 return NLOPT_SUCCESS;
453 nlopt_result nlopt_set_initial_step(nlopt_opt opt, const double *dx)
456 if (!opt || !dx) return NLOPT_INVALID_ARGS;
457 for (i = 0; i < opt->n; ++i) if (dx[i] == 0) return NLOPT_INVALID_ARGS;
458 if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
459 return NLOPT_OUT_OF_MEMORY;
460 memcpy(opt->dx, dx, sizeof(double) * (opt->n));
461 return NLOPT_SUCCESS;
464 nlopt_result nlopt_get_initial_step(const nlopt_opt opt, const double *x,
467 if (!opt) return NLOPT_INVALID_ARGS;
468 if (!opt->n) return NLOPT_SUCCESS;
470 nlopt_opt o = (nlopt_opt) opt; /* discard const temporarily */
471 nlopt_result ret = nlopt_set_default_initial_step(o, x);
472 if (ret != NLOPT_SUCCESS) return ret;
473 memcpy(dx, o->dx, sizeof(double) * (opt->n));
474 free(o->dx); o->dx = NULL; /* don't save, since x-dependent */
477 memcpy(dx, opt->dx, sizeof(double) * (opt->n));
478 return NLOPT_SUCCESS;
481 nlopt_result nlopt_set_default_initial_step(nlopt_opt opt, const double *x)
483 const double *lb, *ub;
486 if (!opt || !x) return NLOPT_INVALID_ARGS;
487 lb = opt->lb; ub = opt->ub;
489 if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
490 return NLOPT_OUT_OF_MEMORY;
492 /* crude heuristics for initial step size of nonderivative algorithms */
493 for (i = 0; i < opt->n; ++i) {
494 double step = HUGE_VAL;
496 if (!nlopt_isinf(ub[i]) && !nlopt_isinf(lb[i])
497 && (ub[i] - lb[i]) * 0.25 < step && ub[i] > lb[i])
498 step = (ub[i] - lb[i]) * 0.25;
499 if (!nlopt_isinf(ub[i])
500 && ub[i] - x[i] < step && ub[i] > x[i])
501 step = (ub[i] - x[i]) * 0.75;
502 if (!nlopt_isinf(lb[i])
503 && x[i] - lb[i] < step && x[i] > lb[i])
504 step = (x[i] - lb[i]) * 0.75;
506 if (nlopt_isinf(step)) {
507 if (!nlopt_isinf(ub[i])
508 && fabs(ub[i] - x[i]) < fabs(step))
509 step = (ub[i] - x[i]) * 1.1;
510 if (!nlopt_isinf(lb[i])
511 && fabs(x[i] - lb[i]) < fabs(step))
512 step = (x[i] - lb[i]) * 1.1;
514 if (nlopt_isinf(step) || step == 0) {
517 if (nlopt_isinf(step) || step == 0)
522 return NLOPT_SUCCESS;
525 /*************************************************************************/
527 GETSET(free_f_data, int, free_f_data)
529 /*************************************************************************/