1 /* Copyright (c) 2007-2014 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.
30 #include "nlopt-internal.h"
32 #define ERR(err, opt, msg) (nlopt_set_errmsg(opt, msg) ? err : err)
34 /*************************************************************************/
36 void NLOPT_STDCALL nlopt_destroy(nlopt_opt opt)
40 if (opt->munge_on_destroy) {
41 nlopt_munge munge = opt->munge_on_destroy;
43 for (i = 0; i < opt->m; ++i)
44 munge(opt->fc[i].f_data);
45 for (i = 0; i < opt->p; ++i)
46 munge(opt->h[i].f_data);
48 for (i = 0; i < opt->m; ++i)
50 for (i = 0; i < opt->p; ++i)
52 free(opt->lb); free(opt->ub);
56 nlopt_destroy(opt->local_opt);
64 nlopt_opt NLOPT_STDCALL nlopt_create(nlopt_algorithm algorithm, unsigned n)
68 if (((int) algorithm) < 0 || algorithm >= NLOPT_NUM_ALGORITHMS)
71 opt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
73 opt->algorithm = algorithm;
75 opt->f = NULL; opt->f_data = NULL; opt->pre = NULL;
77 opt->munge_on_destroy = opt->munge_on_copy = NULL;
79 opt->lb = opt->ub = NULL;
80 opt->m = opt->m_alloc = 0;
82 opt->p = opt->p_alloc = 0;
85 opt->stopval = -HUGE_VAL;
86 opt->ftol_rel = opt->ftol_abs = 0;
87 opt->xtol_rel = 0; opt->xtol_abs = NULL;
91 opt->force_stop_child = NULL;
93 opt->local_opt = NULL;
94 opt->stochastic_population = 0;
95 opt->vector_storage = 0;
101 opt->lb = (double *) calloc(n, sizeof(double));
102 if (!opt->lb) goto oom;
103 opt->ub = (double *) calloc(n, sizeof(double));
104 if (!opt->ub) goto oom;
105 opt->xtol_abs = (double *) calloc(n, sizeof(double));
106 if (!opt->xtol_abs) goto oom;
107 nlopt_set_lower_bounds1(opt, -HUGE_VAL);
108 nlopt_set_upper_bounds1(opt, +HUGE_VAL);
109 nlopt_set_xtol_abs1(opt, 0.0);
120 nlopt_opt NLOPT_STDCALL nlopt_copy(const nlopt_opt opt)
122 nlopt_opt nopt = NULL;
126 nopt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
128 nopt->lb = nopt->ub = nopt->xtol_abs = NULL;
129 nopt->fc = nopt->h = NULL;
130 nopt->m_alloc = nopt->p_alloc = 0;
131 nopt->local_opt = NULL;
135 nopt->force_stop_child = NULL;
137 munge = nopt->munge_on_copy;
138 if (munge && nopt->f_data)
139 if (!(nopt->f_data = munge(nopt->f_data))) goto oom;
142 nopt->lb = (double *) malloc(sizeof(double) * (opt->n));
143 if (!opt->lb) goto oom;
144 nopt->ub = (double *) malloc(sizeof(double) * (opt->n));
145 if (!opt->ub) goto oom;
146 nopt->xtol_abs = (double *) malloc(sizeof(double) * (opt->n));
147 if (!opt->xtol_abs) goto oom;
149 memcpy(nopt->lb, opt->lb, sizeof(double) * (opt->n));
150 memcpy(nopt->ub, opt->ub, sizeof(double) * (opt->n));
151 memcpy(nopt->xtol_abs, opt->xtol_abs, sizeof(double) * (opt->n));
155 nopt->m_alloc = opt->m;
156 nopt->fc = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
158 if (!nopt->fc) goto oom;
159 memcpy(nopt->fc, opt->fc, sizeof(nlopt_constraint) * (opt->m));
160 for (i = 0; i < opt->m; ++i) nopt->fc[i].tol = NULL;
162 for (i = 0; i < opt->m; ++i)
163 if (nopt->fc[i].f_data &&
165 = munge(nopt->fc[i].f_data)))
167 for (i = 0; i < opt->m; ++i)
168 if (opt->fc[i].tol) {
169 nopt->fc[i].tol = (double *) malloc(sizeof(double)
171 if (!nopt->fc[i].tol) goto oom;
172 memcpy(nopt->fc[i].tol, opt->fc[i].tol,
173 sizeof(double) * nopt->fc[i].m);
178 nopt->p_alloc = opt->p;
179 nopt->h = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
181 if (!nopt->h) goto oom;
182 memcpy(nopt->h, opt->h, sizeof(nlopt_constraint) * (opt->p));
183 for (i = 0; i < opt->p; ++i) nopt->h[i].tol = NULL;
185 for (i = 0; i < opt->p; ++i)
186 if (nopt->h[i].f_data &&
188 = munge(nopt->h[i].f_data)))
190 for (i = 0; i < opt->p; ++i)
192 nopt->h[i].tol = (double *) malloc(sizeof(double)
194 if (!nopt->h[i].tol) goto oom;
195 memcpy(nopt->h[i].tol, opt->h[i].tol,
196 sizeof(double) * nopt->h[i].m);
200 if (opt->local_opt) {
201 nopt->local_opt = nlopt_copy(opt->local_opt);
202 if (!nopt->local_opt) goto oom;
206 nopt->dx = (double *) malloc(sizeof(double) * (opt->n));
207 if (!nopt->dx) goto oom;
208 memcpy(nopt->dx, opt->dx, sizeof(double) * (opt->n));
214 nopt->munge_on_destroy = NULL; /* better to leak mem than crash */
219 /*************************************************************************/
221 nlopt_result NLOPT_STDCALL nlopt_set_precond_min_objective(nlopt_opt opt,
227 nlopt_unset_errmsg(opt);
228 if (opt->munge_on_destroy) opt->munge_on_destroy(opt->f_data);
229 opt->f = f; opt->f_data = f_data; opt->pre = pre;
231 if (nlopt_isinf(opt->stopval) && opt->stopval > 0)
232 opt->stopval = -HUGE_VAL; /* switch default from max to min */
233 return NLOPT_SUCCESS;
235 return NLOPT_INVALID_ARGS;
238 nlopt_result NLOPT_STDCALL nlopt_set_min_objective(nlopt_opt opt,
239 nlopt_func f, void *f_data)
241 return nlopt_set_precond_min_objective(opt, f, NULL, f_data);
244 nlopt_result NLOPT_STDCALL nlopt_set_precond_max_objective(nlopt_opt opt,
250 nlopt_unset_errmsg(opt);
251 if (opt->munge_on_destroy) opt->munge_on_destroy(opt->f_data);
252 opt->f = f; opt->f_data = f_data; opt->pre = pre;
254 if (nlopt_isinf(opt->stopval) && opt->stopval < 0)
255 opt->stopval = +HUGE_VAL; /* switch default from min to max */
256 return NLOPT_SUCCESS;
258 return NLOPT_INVALID_ARGS;
261 nlopt_result NLOPT_STDCALL nlopt_set_max_objective(nlopt_opt opt,
262 nlopt_func f, void *f_data)
264 return nlopt_set_precond_max_objective(opt, f, NULL, f_data);
267 /*************************************************************************/
270 NLOPT_STDCALL nlopt_set_lower_bounds(nlopt_opt opt, const double *lb)
272 nlopt_unset_errmsg(opt);
273 if (opt && (opt->n == 0 || lb)) {
275 memcpy(opt->lb, lb, sizeof(double) * (opt->n));
276 for (i = 0; i < opt->n; ++i)
277 if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
278 opt->lb[i] = opt->ub[i];
279 return NLOPT_SUCCESS;
281 return NLOPT_INVALID_ARGS;
285 NLOPT_STDCALL nlopt_set_lower_bounds1(nlopt_opt opt, double lb)
287 nlopt_unset_errmsg(opt);
290 for (i = 0; i < opt->n; ++i) {
292 if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
293 opt->lb[i] = opt->ub[i];
295 return NLOPT_SUCCESS;
297 return NLOPT_INVALID_ARGS;
301 NLOPT_STDCALL nlopt_get_lower_bounds(const nlopt_opt opt, double *lb)
303 nlopt_unset_errmsg(opt);
304 if (opt && (opt->n == 0 || lb)) {
305 memcpy(lb, opt->lb, sizeof(double) * (opt->n));
306 return NLOPT_SUCCESS;
308 return NLOPT_INVALID_ARGS;
312 NLOPT_STDCALL nlopt_set_upper_bounds(nlopt_opt opt, const double *ub)
314 nlopt_unset_errmsg(opt);
315 if (opt && (opt->n == 0 || ub)) {
317 memcpy(opt->ub, ub, sizeof(double) * (opt->n));
318 for (i = 0; i < opt->n; ++i)
319 if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
320 opt->ub[i] = opt->lb[i];
321 return NLOPT_SUCCESS;
323 return NLOPT_INVALID_ARGS;
327 NLOPT_STDCALL nlopt_set_upper_bounds1(nlopt_opt opt, double ub)
329 nlopt_unset_errmsg(opt);
332 for (i = 0; i < opt->n; ++i) {
334 if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
335 opt->ub[i] = opt->lb[i];
337 return NLOPT_SUCCESS;
339 return NLOPT_INVALID_ARGS;
343 NLOPT_STDCALL nlopt_get_upper_bounds(const nlopt_opt opt, double *ub)
345 nlopt_unset_errmsg(opt);
346 if (opt && (opt->n == 0 || ub)) {
347 memcpy(ub, opt->ub, sizeof(double) * (opt->n));
348 return NLOPT_SUCCESS;
350 return NLOPT_INVALID_ARGS;
353 /*************************************************************************/
355 #define AUGLAG_ALG(a) ((a) == NLOPT_AUGLAG || \
356 (a) == NLOPT_AUGLAG_EQ || \
357 (a) == NLOPT_LN_AUGLAG || \
358 (a) == NLOPT_LN_AUGLAG_EQ || \
359 (a) == NLOPT_LD_AUGLAG || \
360 (a) == NLOPT_LD_AUGLAG_EQ)
363 NLOPT_STDCALL nlopt_remove_inequality_constraints(nlopt_opt opt)
366 nlopt_unset_errmsg(opt);
367 if (!opt) return NLOPT_INVALID_ARGS;
368 if (opt->munge_on_destroy) {
369 nlopt_munge munge = opt->munge_on_destroy;
370 for (i = 0; i < opt->m; ++i)
371 munge(opt->fc[i].f_data);
373 for (i = 0; i < opt->m; ++i)
374 free(opt->fc[i].tol);
377 opt->m = opt->m_alloc = 0;
378 return NLOPT_SUCCESS;
381 static nlopt_result add_constraint(nlopt_opt opt,
382 unsigned *m, unsigned *m_alloc,
383 nlopt_constraint **c,
384 unsigned fm, nlopt_func fc, nlopt_mfunc mfc,
392 if ((fc && mfc) || (fc && fm != 1) || (!fc && !mfc))
393 return NLOPT_INVALID_ARGS;
395 for (i = 0; i < fm; ++i) if (tol[i] < 0)
396 return ERR(NLOPT_INVALID_ARGS, opt, "negative constraint tolerance");
398 tolcopy = (double *) malloc(sizeof(double) * fm);
399 if (fm && !tolcopy) return NLOPT_OUT_OF_MEMORY;
401 memcpy(tolcopy, tol, sizeof(double) * fm);
403 for (i = 0; i < fm; ++i) tolcopy[i] = 0;
407 /* allocate by repeated doubling so that
408 we end up with O(log m) mallocs rather than O(m). */
410 *c = (nlopt_constraint *) realloc(*c,
411 sizeof(nlopt_constraint)
416 return NLOPT_OUT_OF_MEMORY;
422 (*c)[*m - 1].pre = pre;
423 (*c)[*m - 1].mf = mfc;
424 (*c)[*m - 1].f_data = fc_data;
425 (*c)[*m - 1].tol = tolcopy;
426 return NLOPT_SUCCESS;
429 static int inequality_ok(nlopt_algorithm algorithm) {
430 /* nonlinear constraints are only supported with some algorithms */
431 return (algorithm == NLOPT_LD_MMA || algorithm == NLOPT_LD_CCSAQ
432 || algorithm == NLOPT_LD_SLSQP
433 || algorithm == NLOPT_LN_COBYLA
434 || AUGLAG_ALG(algorithm)
435 || algorithm == NLOPT_GN_ISRES
436 || algorithm == NLOPT_GN_ORIG_DIRECT
437 || algorithm == NLOPT_GN_ORIG_DIRECT_L);
441 NLOPT_STDCALL nlopt_add_inequality_mconstraint(nlopt_opt opt, unsigned m,
442 nlopt_mfunc fc, void *fc_data,
446 nlopt_unset_errmsg(opt);
447 if (!m) { /* empty constraints are always ok */
448 if (opt && opt->munge_on_destroy) opt->munge_on_destroy(fc_data);
449 return NLOPT_SUCCESS;
451 if (!opt) ret = NLOPT_INVALID_ARGS;
452 else if (!inequality_ok(opt->algorithm))
453 ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
454 else ret = add_constraint(opt, &opt->m, &opt->m_alloc, &opt->fc,
455 m, NULL, fc, NULL, fc_data, tol);
456 if (ret < 0 && opt && opt->munge_on_destroy)
457 opt->munge_on_destroy(fc_data);
462 NLOPT_STDCALL nlopt_add_precond_inequality_constraint(nlopt_opt opt,
469 nlopt_unset_errmsg(opt);
470 if (!opt) ret = NLOPT_INVALID_ARGS;
471 else if (!inequality_ok(opt->algorithm))
472 ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
473 else ret = add_constraint(opt, &opt->m, &opt->m_alloc, &opt->fc,
474 1, fc, NULL, pre, fc_data, &tol);
475 if (ret < 0 && opt && opt->munge_on_destroy)
476 opt->munge_on_destroy(fc_data);
481 NLOPT_STDCALL nlopt_add_inequality_constraint(nlopt_opt opt,
482 nlopt_func fc, void *fc_data,
485 return nlopt_add_precond_inequality_constraint(opt, fc, NULL, fc_data,
490 NLOPT_STDCALL nlopt_remove_equality_constraints(nlopt_opt opt)
493 nlopt_unset_errmsg(opt);
494 if (!opt) return NLOPT_INVALID_ARGS;
495 if (opt->munge_on_destroy) {
496 nlopt_munge munge = opt->munge_on_destroy;
497 for (i = 0; i < opt->p; ++i)
498 munge(opt->h[i].f_data);
500 for (i = 0; i < opt->p; ++i)
504 opt->p = opt->p_alloc = 0;
505 return NLOPT_SUCCESS;
508 static int equality_ok(nlopt_algorithm algorithm) {
509 /* equality constraints (h(x) = 0) only via some algorithms */
510 return (AUGLAG_ALG(algorithm)
511 || algorithm == NLOPT_LD_SLSQP
512 || algorithm == NLOPT_GN_ISRES
513 || algorithm == NLOPT_LN_COBYLA);
517 NLOPT_STDCALL nlopt_add_equality_mconstraint(nlopt_opt opt, unsigned m,
518 nlopt_mfunc fc, void *fc_data,
522 nlopt_unset_errmsg(opt);
523 if (!m) { /* empty constraints are always ok */
524 if (opt && opt->munge_on_destroy) opt->munge_on_destroy(fc_data);
525 return NLOPT_SUCCESS;
527 if (!opt) ret = NLOPT_INVALID_ARGS;
528 else if (!equality_ok(opt->algorithm))
529 ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
530 else if (nlopt_count_constraints(opt->p, opt->h) + m > opt->n)
531 ret = ERR(NLOPT_INVALID_ARGS, opt, "too many equality constraints");
532 else ret = add_constraint(opt, &opt->p, &opt->p_alloc, &opt->h,
533 m, NULL, fc, NULL, fc_data, tol);
534 if (ret < 0 && opt && opt->munge_on_destroy)
535 opt->munge_on_destroy(fc_data);
540 NLOPT_STDCALL nlopt_add_precond_equality_constraint(nlopt_opt opt,
547 nlopt_unset_errmsg(opt);
548 if (!opt) ret = NLOPT_INVALID_ARGS;
549 else if (!equality_ok(opt->algorithm))
550 ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
551 else if (nlopt_count_constraints(opt->p, opt->h) + 1 > opt->n)
552 ret = ERR(NLOPT_INVALID_ARGS, opt, "too many equality constraints");
553 else ret = add_constraint(opt, &opt->p, &opt->p_alloc, &opt->h,
554 1, fc, NULL, pre, fc_data, &tol);
555 if (ret < 0 && opt && opt->munge_on_destroy)
556 opt->munge_on_destroy(fc_data);
561 NLOPT_STDCALL nlopt_add_equality_constraint(nlopt_opt opt,
562 nlopt_func fc, void *fc_data,
565 return nlopt_add_precond_equality_constraint(opt, fc, NULL, fc_data, tol);
568 /*************************************************************************/
570 #define SET(param, T, arg) \
571 nlopt_result NLOPT_STDCALL nlopt_set_##param(nlopt_opt opt, T arg) \
574 nlopt_unset_errmsg(opt); \
576 return NLOPT_SUCCESS; \
578 return NLOPT_INVALID_ARGS; \
582 #define GET(param, T, arg) T NLOPT_STDCALL \
583 nlopt_get_##param(const nlopt_opt opt) { \
587 #define GETSET(param, T, arg) GET(param, T, arg) SET(param, T, arg)
589 GETSET(stopval, double, stopval)
591 GETSET(ftol_rel, double, ftol_rel)
592 GETSET(ftol_abs, double, ftol_abs)
593 GETSET(xtol_rel, double, xtol_rel)
596 NLOPT_STDCALL nlopt_set_xtol_abs(nlopt_opt opt, const double *xtol_abs)
599 nlopt_unset_errmsg(opt);
600 memcpy(opt->xtol_abs, xtol_abs, opt->n * sizeof(double));
601 return NLOPT_SUCCESS;
603 return NLOPT_INVALID_ARGS;
607 NLOPT_STDCALL nlopt_set_xtol_abs1(nlopt_opt opt, double xtol_abs)
611 nlopt_unset_errmsg(opt);
612 for (i = 0; i < opt->n; ++i)
613 opt->xtol_abs[i] = xtol_abs;
614 return NLOPT_SUCCESS;
616 return NLOPT_INVALID_ARGS;
620 NLOPT_STDCALL nlopt_get_xtol_abs(const nlopt_opt opt, double *xtol_abs)
622 memcpy(xtol_abs, opt->xtol_abs, opt->n * sizeof(double));
623 return NLOPT_SUCCESS;
626 GETSET(maxeval, int, maxeval)
628 GETSET(maxtime, double, maxtime)
630 /*************************************************************************/
633 NLOPT_STDCALL nlopt_set_force_stop(nlopt_opt opt, int force_stop)
636 nlopt_unset_errmsg(opt);
637 opt->force_stop = force_stop;
638 if (opt->force_stop_child)
639 return nlopt_set_force_stop(opt->force_stop_child, force_stop);
640 return NLOPT_SUCCESS;
642 return NLOPT_INVALID_ARGS;
645 GET(force_stop, int, force_stop)
646 nlopt_result NLOPT_STDCALL nlopt_force_stop(nlopt_opt opt) {
647 return nlopt_set_force_stop(opt, 1);
650 /*************************************************************************/
652 GET(algorithm, nlopt_algorithm, algorithm)
653 GET(dimension, unsigned, n)
655 /*************************************************************************/
658 NLOPT_STDCALL nlopt_set_local_optimizer(nlopt_opt opt,
659 const nlopt_opt local_opt)
662 nlopt_unset_errmsg(opt);
663 if (local_opt && local_opt->n != opt->n)
664 return ERR(NLOPT_INVALID_ARGS, opt, "dimension mismatch in local optimizer");
665 nlopt_destroy(opt->local_opt);
666 opt->local_opt = nlopt_copy(local_opt);
668 if (!opt->local_opt) return NLOPT_OUT_OF_MEMORY;
669 nlopt_set_lower_bounds(opt->local_opt, opt->lb);
670 nlopt_set_upper_bounds(opt->local_opt, opt->ub);
671 nlopt_remove_inequality_constraints(opt->local_opt);
672 nlopt_remove_equality_constraints(opt->local_opt);
673 nlopt_set_min_objective(opt->local_opt, NULL, NULL);
674 nlopt_set_munge(opt->local_opt, NULL, NULL);
675 opt->local_opt->force_stop = 0;
677 return NLOPT_SUCCESS;
679 return NLOPT_INVALID_ARGS;
682 /*************************************************************************/
684 GETSET(population, unsigned, stochastic_population)
685 GETSET(vector_storage, unsigned, vector_storage)
687 /*************************************************************************/
689 nlopt_result NLOPT_STDCALL nlopt_set_initial_step1(nlopt_opt opt, double dx)
692 if (!opt) return NLOPT_INVALID_ARGS;
693 nlopt_unset_errmsg(opt);
694 if (dx == 0) return ERR(NLOPT_INVALID_ARGS, opt, "zero step size");
695 if (!opt->dx && opt->n > 0) {
696 opt->dx = (double *) malloc(sizeof(double) * (opt->n));
697 if (!opt->dx) return NLOPT_OUT_OF_MEMORY;
699 for (i = 0; i < opt->n; ++i) opt->dx[i] = dx;
700 return NLOPT_SUCCESS;
704 NLOPT_STDCALL nlopt_set_initial_step(nlopt_opt opt, const double *dx)
707 if (!opt) return NLOPT_INVALID_ARGS;
708 nlopt_unset_errmsg(opt);
710 free(opt->dx); opt->dx = NULL;
711 return NLOPT_SUCCESS;
713 for (i = 0; i < opt->n; ++i)
714 if (dx[i] == 0) return ERR(NLOPT_INVALID_ARGS, opt, "zero step size");
715 if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
716 return NLOPT_OUT_OF_MEMORY;
717 memcpy(opt->dx, dx, sizeof(double) * (opt->n));
718 return NLOPT_SUCCESS;
722 NLOPT_STDCALL nlopt_get_initial_step(const nlopt_opt opt, const double *x,
725 if (!opt) return NLOPT_INVALID_ARGS;
726 nlopt_unset_errmsg(opt);
727 if (!opt->n) return NLOPT_SUCCESS;
729 nlopt_opt o = (nlopt_opt) opt; /* discard const temporarily */
730 nlopt_result ret = nlopt_set_default_initial_step(o, x);
731 if (ret != NLOPT_SUCCESS) return ret;
732 memcpy(dx, o->dx, sizeof(double) * (opt->n));
733 free(o->dx); o->dx = NULL; /* don't save, since x-dependent */
736 memcpy(dx, opt->dx, sizeof(double) * (opt->n));
737 return NLOPT_SUCCESS;
741 NLOPT_STDCALL nlopt_set_default_initial_step(nlopt_opt opt, const double *x)
743 const double *lb, *ub;
746 nlopt_unset_errmsg(opt);
747 if (!opt || !x) return NLOPT_INVALID_ARGS;
748 lb = opt->lb; ub = opt->ub;
750 if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
751 return NLOPT_OUT_OF_MEMORY;
753 /* crude heuristics for initial step size of nonderivative algorithms */
754 for (i = 0; i < opt->n; ++i) {
755 double step = HUGE_VAL;
757 if (!nlopt_isinf(ub[i]) && !nlopt_isinf(lb[i])
758 && (ub[i] - lb[i]) * 0.25 < step && ub[i] > lb[i])
759 step = (ub[i] - lb[i]) * 0.25;
760 if (!nlopt_isinf(ub[i])
761 && ub[i] - x[i] < step && ub[i] > x[i])
762 step = (ub[i] - x[i]) * 0.75;
763 if (!nlopt_isinf(lb[i])
764 && x[i] - lb[i] < step && x[i] > lb[i])
765 step = (x[i] - lb[i]) * 0.75;
767 if (nlopt_isinf(step)) {
768 if (!nlopt_isinf(ub[i])
769 && fabs(ub[i] - x[i]) < fabs(step))
770 step = (ub[i] - x[i]) * 1.1;
771 if (!nlopt_isinf(lb[i])
772 && fabs(x[i] - lb[i]) < fabs(step))
773 step = (x[i] - lb[i]) * 1.1;
775 if (nlopt_isinf(step) || nlopt_istiny(step)) {
778 if (nlopt_isinf(step) || step == 0.0)
783 return NLOPT_SUCCESS;
786 /*************************************************************************/
788 void NLOPT_STDCALL nlopt_set_munge(nlopt_opt opt,
789 nlopt_munge munge_on_destroy,
790 nlopt_munge munge_on_copy) {
792 opt->munge_on_destroy = munge_on_destroy;
793 opt->munge_on_copy = munge_on_copy;
797 void NLOPT_STDCALL nlopt_munge_data(nlopt_opt opt,
798 nlopt_munge2 munge, void *data) {
801 opt->f_data = munge(opt->f_data, data);
802 for (i = 0; i < opt->m; ++i)
803 opt->fc[i].f_data = munge(opt->fc[i].f_data, data);
804 for (i = 0; i < opt->p; ++i)
805 opt->h[i].f_data = munge(opt->h[i].f_data, data);
809 /*************************************************************************/
811 const char *nlopt_set_errmsg(nlopt_opt opt, const char *format, ...)
814 va_start(ap, format);
815 opt->errmsg = nlopt_vsprintf(opt->errmsg, format, ap);
820 void nlopt_unset_errmsg(nlopt_opt opt)
828 const char *nlopt_get_errmsg(nlopt_opt opt)
833 /*************************************************************************/