options.c 27.0 KB
Newer Older
1
/* Copyright (c) 2007-2014 Massachusetts Institute of Technology
2 3 4 5 6 7 8 9
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
10
 *
11 12
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
13
 *
14 15 16 17 18 19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * 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.
21 22 23 24 25 26 27
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <float.h>
28
#include <stdarg.h>
29 30 31

#include "nlopt-internal.h"

32 33
#define ERR(err, opt, msg) (nlopt_set_errmsg(opt, msg) ? err : err)

34 35
/*************************************************************************/

36
void NLOPT_STDCALL nlopt_destroy(nlopt_opt opt)
37
{
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    if (opt) {
        unsigned i;
        if (opt->munge_on_destroy) {
            nlopt_munge munge = opt->munge_on_destroy;
            munge(opt->f_data);
            for (i = 0; i < opt->m; ++i)
                munge(opt->fc[i].f_data);
            for (i = 0; i < opt->p; ++i)
                munge(opt->h[i].f_data);
        }
        for (i = 0; i < opt->m; ++i)
            free(opt->fc[i].tol);
        for (i = 0; i < opt->p; ++i)
            free(opt->h[i].tol);
        free(opt->lb);
        free(opt->ub);
        free(opt->xtol_abs);
        free(opt->fc);
        free(opt->h);
        nlopt_destroy(opt->local_opt);
        free(opt->dx);
        free(opt->work);
        free(opt->errmsg);
        free(opt);
    }
63 64
}

65
nlopt_opt NLOPT_STDCALL nlopt_create(nlopt_algorithm algorithm, unsigned n)
66
{
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    nlopt_opt opt;

    if (((int) algorithm) < 0 || algorithm >= NLOPT_NUM_ALGORITHMS)
        return NULL;

    opt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
    if (opt) {
        opt->algorithm = algorithm;
        opt->n = n;
        opt->f = NULL;
        opt->f_data = NULL;
        opt->pre = NULL;
        opt->maximize = 0;
        opt->munge_on_destroy = opt->munge_on_copy = NULL;

        opt->lb = opt->ub = NULL;
        opt->m = opt->m_alloc = 0;
        opt->fc = NULL;
        opt->p = opt->p_alloc = 0;
        opt->h = NULL;

        opt->stopval = -HUGE_VAL;
        opt->ftol_rel = opt->ftol_abs = 0;
        opt->xtol_rel = 0;
        opt->xtol_abs = NULL;
        opt->maxeval = 0;
        opt->numevals = 0;
        opt->maxtime = 0;
        opt->force_stop = 0;
        opt->force_stop_child = NULL;

        opt->local_opt = NULL;
        opt->stochastic_population = 0;
        opt->vector_storage = 0;
        opt->dx = NULL;
        opt->work = NULL;
        opt->errmsg = NULL;

        if (n > 0) {
            opt->lb = (double *) calloc(n, sizeof(double));
            if (!opt->lb)
                goto oom;
            opt->ub = (double *) calloc(n, sizeof(double));
            if (!opt->ub)
                goto oom;
            opt->xtol_abs = (double *) calloc(n, sizeof(double));
            if (!opt->xtol_abs)
                goto oom;
            nlopt_set_lower_bounds1(opt, -HUGE_VAL);
            nlopt_set_upper_bounds1(opt, +HUGE_VAL);
            nlopt_set_xtol_abs1(opt, 0.0);
        }
    }

    return opt;

  oom:
    nlopt_destroy(opt);
    return NULL;
126 127
}

128
nlopt_opt NLOPT_STDCALL nlopt_copy(const nlopt_opt opt)
129
{
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    nlopt_opt nopt = NULL;
    unsigned i;
    if (opt) {
        nlopt_munge munge;
        nopt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
        *nopt = *opt;
        nopt->lb = nopt->ub = nopt->xtol_abs = NULL;
        nopt->fc = nopt->h = NULL;
        nopt->m_alloc = nopt->p_alloc = 0;
        nopt->local_opt = NULL;
        nopt->dx = NULL;
        nopt->work = NULL;
        nopt->errmsg = NULL;
        nopt->force_stop_child = NULL;

        munge = nopt->munge_on_copy;
        if (munge && nopt->f_data)
            if (!(nopt->f_data = munge(nopt->f_data)))
                goto oom;

        if (opt->n > 0) {
            nopt->lb = (double *) malloc(sizeof(double) * (opt->n));
            if (!opt->lb)
                goto oom;
            nopt->ub = (double *) malloc(sizeof(double) * (opt->n));
            if (!opt->ub)
                goto oom;
            nopt->xtol_abs = (double *) malloc(sizeof(double) * (opt->n));
            if (!opt->xtol_abs)
                goto oom;

            memcpy(nopt->lb, opt->lb, sizeof(double) * (opt->n));
            memcpy(nopt->ub, opt->ub, sizeof(double) * (opt->n));
            memcpy(nopt->xtol_abs, opt->xtol_abs, sizeof(double) * (opt->n));
        }

        if (opt->m) {
            nopt->m_alloc = opt->m;
            nopt->fc = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
                                                   * (opt->m));
            if (!nopt->fc)
                goto oom;
            memcpy(nopt->fc, opt->fc, sizeof(nlopt_constraint) * (opt->m));
            for (i = 0; i < opt->m; ++i)
                nopt->fc[i].tol = NULL;
            if (munge)
                for (i = 0; i < opt->m; ++i)
                    if (nopt->fc[i].f_data && !(nopt->fc[i].f_data = munge(nopt->fc[i].f_data)))
                        goto oom;
            for (i = 0; i < opt->m; ++i)
                if (opt->fc[i].tol) {
                    nopt->fc[i].tol = (double *) malloc(sizeof(double)
                                                        * nopt->fc[i].m);
                    if (!nopt->fc[i].tol)
                        goto oom;
                    memcpy(nopt->fc[i].tol, opt->fc[i].tol, sizeof(double) * nopt->fc[i].m);
                }
        }

        if (opt->p) {
            nopt->p_alloc = opt->p;
            nopt->h = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
                                                  * (opt->p));
            if (!nopt->h)
                goto oom;
            memcpy(nopt->h, opt->h, sizeof(nlopt_constraint) * (opt->p));
            for (i = 0; i < opt->p; ++i)
                nopt->h[i].tol = NULL;
            if (munge)
                for (i = 0; i < opt->p; ++i)
                    if (nopt->h[i].f_data && !(nopt->h[i].f_data = munge(nopt->h[i].f_data)))
                        goto oom;
            for (i = 0; i < opt->p; ++i)
                if (opt->h[i].tol) {
                    nopt->h[i].tol = (double *) malloc(sizeof(double)
                                                       * nopt->h[i].m);
                    if (!nopt->h[i].tol)
                        goto oom;
                    memcpy(nopt->h[i].tol, opt->h[i].tol, sizeof(double) * nopt->h[i].m);
                }
        }

        if (opt->local_opt) {
            nopt->local_opt = nlopt_copy(opt->local_opt);
            if (!nopt->local_opt)
                goto oom;
        }

        if (opt->dx) {
            nopt->dx = (double *) malloc(sizeof(double) * (opt->n));
            if (!nopt->dx)
                goto oom;
            memcpy(nopt->dx, opt->dx, sizeof(double) * (opt->n));
        }
    }
    return nopt;

  oom:
    nopt->munge_on_destroy = NULL;      /* better to leak mem than crash */
    nlopt_destroy(nopt);
    return NULL;
231 232 233 234
}

/*************************************************************************/

235
nlopt_result NLOPT_STDCALL nlopt_set_precond_min_objective(nlopt_opt opt, nlopt_func f, nlopt_precond pre, void *f_data)
236
{
237 238 239 240 241 242 243 244 245 246 247 248 249
    if (opt) {
        nlopt_unset_errmsg(opt);
        if (opt->munge_on_destroy)
            opt->munge_on_destroy(opt->f_data);
        opt->f = f;
        opt->f_data = f_data;
        opt->pre = pre;
        opt->maximize = 0;
        if (nlopt_isinf(opt->stopval) && opt->stopval > 0)
            opt->stopval = -HUGE_VAL;   /* switch default from max to min */
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
250 251
}

252
nlopt_result NLOPT_STDCALL nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, void *f_data)
253
{
254
    return nlopt_set_precond_min_objective(opt, f, NULL, f_data);
255 256
}

257
nlopt_result NLOPT_STDCALL nlopt_set_precond_max_objective(nlopt_opt opt, nlopt_func f, nlopt_precond pre, void *f_data)
258
{
259 260 261 262 263 264 265 266 267 268 269 270 271
    if (opt) {
        nlopt_unset_errmsg(opt);
        if (opt->munge_on_destroy)
            opt->munge_on_destroy(opt->f_data);
        opt->f = f;
        opt->f_data = f_data;
        opt->pre = pre;
        opt->maximize = 1;
        if (nlopt_isinf(opt->stopval) && opt->stopval < 0)
            opt->stopval = +HUGE_VAL;   /* switch default from min to max */
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
272 273
}

274
nlopt_result NLOPT_STDCALL nlopt_set_max_objective(nlopt_opt opt, nlopt_func f, void *f_data)
275
{
276
    return nlopt_set_precond_max_objective(opt, f, NULL, f_data);
277 278
}

279 280
/*************************************************************************/

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
nlopt_result NLOPT_STDCALL nlopt_set_lower_bounds(nlopt_opt opt, const double *lb)
{
    nlopt_unset_errmsg(opt);
    if (opt && (opt->n == 0 || lb)) {
        unsigned int i;
        if (opt->n > 0)
            memcpy(opt->lb, lb, sizeof(double) * (opt->n));
        for (i = 0; i < opt->n; ++i)
            if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
                opt->lb[i] = opt->ub[i];
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
}

nlopt_result NLOPT_STDCALL nlopt_set_lower_bounds1(nlopt_opt opt, double lb)
{
    nlopt_unset_errmsg(opt);
    if (opt) {
        unsigned i;
        for (i = 0; i < opt->n; ++i) {
            opt->lb[i] = lb;
            if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
                opt->lb[i] = opt->ub[i];
        }
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324
nlopt_result NLOPT_STDCALL nlopt_set_lower_bound(nlopt_opt opt, int i, double lb)
{
    nlopt_unset_errmsg(opt);
    if (opt) {
        if (i < 0 || i >= opt->n)
            return ERR(NLOPT_INVALID_ARGS, opt, "invalid bound index");
        opt->lb[i] = lb;
        if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
            opt->lb[i] = opt->ub[i];
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
}

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
nlopt_result NLOPT_STDCALL nlopt_get_lower_bounds(const nlopt_opt opt, double *lb)
{
    nlopt_unset_errmsg(opt);
    if (opt && (opt->n == 0 || lb)) {
        memcpy(lb, opt->lb, sizeof(double) * (opt->n));
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
}

nlopt_result NLOPT_STDCALL nlopt_set_upper_bounds(nlopt_opt opt, const double *ub)
{
    nlopt_unset_errmsg(opt);
    if (opt && (opt->n == 0 || ub)) {
        unsigned int i;
        if (opt->n > 0)
            memcpy(opt->ub, ub, sizeof(double) * (opt->n));
        for (i = 0; i < opt->n; ++i)
            if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
                opt->ub[i] = opt->lb[i];
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
}

nlopt_result NLOPT_STDCALL nlopt_set_upper_bounds1(nlopt_opt opt, double ub)
{
    nlopt_unset_errmsg(opt);
    if (opt) {
        unsigned i;
        for (i = 0; i < opt->n; ++i) {
            opt->ub[i] = ub;
            if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
                opt->ub[i] = opt->lb[i];
        }
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378
nlopt_result NLOPT_STDCALL nlopt_set_upper_bound(nlopt_opt opt, int i, double ub)
{
    nlopt_unset_errmsg(opt);
    if (opt) {
        if (i < 0 || i >= opt->n)
            return ERR(NLOPT_INVALID_ARGS, opt, "invalid bound index");
        opt->ub[i] = ub;
        if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
            opt->ub[i] = opt->lb[i];
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
}

379 380 381 382 383 384 385 386
nlopt_result NLOPT_STDCALL nlopt_get_upper_bounds(const nlopt_opt opt, double *ub)
{
    nlopt_unset_errmsg(opt);
    if (opt && (opt->n == 0 || ub)) {
        memcpy(ub, opt->ub, sizeof(double) * (opt->n));
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
S
stevenj 已提交
387 388
}

389 390
/*************************************************************************/

391 392 393
#define AUGLAG_ALG(a) ((a) == NLOPT_AUGLAG ||		\
	               (a) == NLOPT_AUGLAG_EQ ||        \
	               (a) == NLOPT_LN_AUGLAG ||        \
394 395 396 397
		       (a) == NLOPT_LN_AUGLAG_EQ ||     \
		       (a) == NLOPT_LD_AUGLAG ||        \
		       (a) == NLOPT_LD_AUGLAG_EQ)

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
nlopt_result NLOPT_STDCALL nlopt_remove_inequality_constraints(nlopt_opt opt)
{
    unsigned i;
    nlopt_unset_errmsg(opt);
    if (!opt)
        return NLOPT_INVALID_ARGS;
    if (opt->munge_on_destroy) {
        nlopt_munge munge = opt->munge_on_destroy;
        for (i = 0; i < opt->m; ++i)
            munge(opt->fc[i].f_data);
    }
    for (i = 0; i < opt->m; ++i)
        free(opt->fc[i].tol);
    free(opt->fc);
    opt->fc = NULL;
    opt->m = opt->m_alloc = 0;
    return NLOPT_SUCCESS;
415 416
}

417
static nlopt_result add_constraint(nlopt_opt opt,
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                                   unsigned *m, unsigned *m_alloc, nlopt_constraint ** c, unsigned fm, nlopt_func fc, nlopt_mfunc mfc, nlopt_precond pre, void *fc_data, const double *tol)
{
    double *tolcopy;
    unsigned i;

    if ((fc && mfc) || (fc && fm != 1) || (!fc && !mfc))
        return NLOPT_INVALID_ARGS;
    if (tol)
        for (i = 0; i < fm; ++i)
            if (tol[i] < 0)
                return ERR(NLOPT_INVALID_ARGS, opt, "negative constraint tolerance");

    tolcopy = (double *) malloc(sizeof(double) * fm);
    if (fm && !tolcopy)
        return NLOPT_OUT_OF_MEMORY;
    if (tol)
        memcpy(tolcopy, tol, sizeof(double) * fm);
    else
        for (i = 0; i < fm; ++i)
            tolcopy[i] = 0;

    *m += 1;
    if (*m > *m_alloc) {
441
        /* allocate by repeated doubling so that
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
           we end up with O(log m) mallocs rather than O(m). */
        *m_alloc = 2 * (*m);
        *c = (nlopt_constraint *) realloc(*c, sizeof(nlopt_constraint)
                                          * (*m_alloc));
        if (!*c) {
            *m_alloc = *m = 0;
            free(tolcopy);
            return NLOPT_OUT_OF_MEMORY;
        }
    }

    (*c)[*m - 1].m = fm;
    (*c)[*m - 1].f = fc;
    (*c)[*m - 1].pre = pre;
    (*c)[*m - 1].mf = mfc;
    (*c)[*m - 1].f_data = fc_data;
    (*c)[*m - 1].tol = tolcopy;
    return NLOPT_SUCCESS;
}

static int inequality_ok(nlopt_algorithm algorithm)
{
    /* nonlinear constraints are only supported with some algorithms */
    return (algorithm == NLOPT_LD_MMA || algorithm == NLOPT_LD_CCSAQ || algorithm == NLOPT_LD_SLSQP || algorithm == NLOPT_LN_COBYLA || AUGLAG_ALG(algorithm)
            || algorithm == NLOPT_GN_ISRES || algorithm == NLOPT_GN_ORIG_DIRECT || algorithm == NLOPT_GN_ORIG_DIRECT_L || algorithm == NLOPT_GN_AGS);
}

nlopt_result NLOPT_STDCALL nlopt_add_inequality_mconstraint(nlopt_opt opt, unsigned m, nlopt_mfunc fc, void *fc_data, const double *tol)
{
    nlopt_result ret;
    nlopt_unset_errmsg(opt);
    if (!m) {                   /* empty constraints are always ok */
        if (opt && opt->munge_on_destroy)
            opt->munge_on_destroy(fc_data);
        return NLOPT_SUCCESS;
    }
    if (!opt)
        ret = NLOPT_INVALID_ARGS;
    else if (!inequality_ok(opt->algorithm))
        ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
    else
        ret = add_constraint(opt, &opt->m, &opt->m_alloc, &opt->fc, m, NULL, fc, NULL, fc_data, tol);
    if (ret < 0 && opt && opt->munge_on_destroy)
        opt->munge_on_destroy(fc_data);
    return ret;
}

nlopt_result NLOPT_STDCALL nlopt_add_precond_inequality_constraint(nlopt_opt opt, nlopt_func fc, nlopt_precond pre, void *fc_data, double tol)
{
    nlopt_result ret;
    nlopt_unset_errmsg(opt);
    if (!opt)
        ret = NLOPT_INVALID_ARGS;
    else if (!inequality_ok(opt->algorithm))
        ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
    else
        ret = add_constraint(opt, &opt->m, &opt->m_alloc, &opt->fc, 1, fc, NULL, pre, fc_data, &tol);
    if (ret < 0 && opt && opt->munge_on_destroy)
        opt->munge_on_destroy(fc_data);
    return ret;
}

nlopt_result NLOPT_STDCALL nlopt_add_inequality_constraint(nlopt_opt opt, nlopt_func fc, void *fc_data, double tol)
{
    return nlopt_add_precond_inequality_constraint(opt, fc, NULL, fc_data, tol);
}

nlopt_result NLOPT_STDCALL nlopt_remove_equality_constraints(nlopt_opt opt)
{
    unsigned i;
    nlopt_unset_errmsg(opt);
    if (!opt)
        return NLOPT_INVALID_ARGS;
    if (opt->munge_on_destroy) {
        nlopt_munge munge = opt->munge_on_destroy;
        for (i = 0; i < opt->p; ++i)
            munge(opt->h[i].f_data);
    }
    for (i = 0; i < opt->p; ++i)
        free(opt->h[i].tol);
    free(opt->h);
    opt->h = NULL;
    opt->p = opt->p_alloc = 0;
    return NLOPT_SUCCESS;
}

static int equality_ok(nlopt_algorithm algorithm)
{
    /* equality constraints (h(x) = 0) only via some algorithms */
    return (AUGLAG_ALG(algorithm)
            || algorithm == NLOPT_LD_SLSQP || algorithm == NLOPT_GN_ISRES || algorithm == NLOPT_LN_COBYLA);
}

nlopt_result NLOPT_STDCALL nlopt_add_equality_mconstraint(nlopt_opt opt, unsigned m, nlopt_mfunc fc, void *fc_data, const double *tol)
{
    nlopt_result ret;
    nlopt_unset_errmsg(opt);
    if (!m) {                   /* empty constraints are always ok */
        if (opt && opt->munge_on_destroy)
            opt->munge_on_destroy(fc_data);
        return NLOPT_SUCCESS;
    }
    if (!opt)
        ret = NLOPT_INVALID_ARGS;
    else if (!equality_ok(opt->algorithm))
        ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
    else if (nlopt_count_constraints(opt->p, opt->h) + m > opt->n)
        ret = ERR(NLOPT_INVALID_ARGS, opt, "too many equality constraints");
    else
        ret = add_constraint(opt, &opt->p, &opt->p_alloc, &opt->h, m, NULL, fc, NULL, fc_data, tol);
    if (ret < 0 && opt && opt->munge_on_destroy)
        opt->munge_on_destroy(fc_data);
    return ret;
}

nlopt_result NLOPT_STDCALL nlopt_add_precond_equality_constraint(nlopt_opt opt, nlopt_func fc, nlopt_precond pre, void *fc_data, double tol)
{
    nlopt_result ret;
    nlopt_unset_errmsg(opt);
    if (!opt)
        ret = NLOPT_INVALID_ARGS;
    else if (!equality_ok(opt->algorithm))
        ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
    else if (nlopt_count_constraints(opt->p, opt->h) + 1 > opt->n)
        ret = ERR(NLOPT_INVALID_ARGS, opt, "too many equality constraints");
    else
        ret = add_constraint(opt, &opt->p, &opt->p_alloc, &opt->h, 1, fc, NULL, pre, fc_data, &tol);
    if (ret < 0 && opt && opt->munge_on_destroy)
        opt->munge_on_destroy(fc_data);
    return ret;
}

nlopt_result NLOPT_STDCALL nlopt_add_equality_constraint(nlopt_opt opt, nlopt_func fc, void *fc_data, double tol)
{
    return nlopt_add_precond_equality_constraint(opt, fc, NULL, fc_data, tol);
577 578
}

579 580
/*************************************************************************/

S
stevenj 已提交
581
#define SET(param, T, arg)						\
582
   nlopt_result NLOPT_STDCALL nlopt_set_##param(nlopt_opt opt, T arg)	\
S
stevenj 已提交
583 584
   {									\
	if (opt) {							\
585
             nlopt_unset_errmsg(opt);                                   \
S
stevenj 已提交
586 587 588 589
	     opt->arg = arg;						\
	     return NLOPT_SUCCESS;					\
	}								\
	return NLOPT_INVALID_ARGS;					\
590 591 592
   }


593 594
#define GET(param, T, arg) T NLOPT_STDCALL	\
   nlopt_get_##param(const nlopt_opt opt) {	\
S
stevenj 已提交
595
        return opt->arg;			\
596 597 598 599
   }

#define GETSET(param, T, arg) GET(param, T, arg) SET(param, T, arg)

600
GETSET(stopval, double, stopval)
601

602 603
GETSET(ftol_rel, double, ftol_rel) GETSET(ftol_abs, double, ftol_abs) GETSET(xtol_rel, double, xtol_rel)
 nlopt_result NLOPT_STDCALL nlopt_set_xtol_abs(nlopt_opt opt, const double *xtol_abs)
604
{
605 606 607 608 609 610
    if (opt) {
        nlopt_unset_errmsg(opt);
        memcpy(opt->xtol_abs, xtol_abs, opt->n * sizeof(double));
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
611 612
}

613
nlopt_result NLOPT_STDCALL nlopt_set_xtol_abs1(nlopt_opt opt, double xtol_abs)
614
{
615 616 617 618 619 620 621 622
    if (opt) {
        unsigned i;
        nlopt_unset_errmsg(opt);
        for (i = 0; i < opt->n; ++i)
            opt->xtol_abs[i] = xtol_abs;
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
623 624
}

625
nlopt_result NLOPT_STDCALL nlopt_get_xtol_abs(const nlopt_opt opt, double *xtol_abs)
626
{
627 628
    memcpy(xtol_abs, opt->xtol_abs, opt->n * sizeof(double));
    return NLOPT_SUCCESS;
629 630 631 632
}

GETSET(maxeval, int, maxeval)

633 634
    GET(numevals, int, numevals)
 GETSET(maxtime, double, maxtime)
635 636

/*************************************************************************/
637
nlopt_result NLOPT_STDCALL nlopt_set_force_stop(nlopt_opt opt, int force_stop)
S
stevenj 已提交
638
{
639 640 641 642 643 644 645 646
    if (opt) {
        nlopt_unset_errmsg(opt);
        opt->force_stop = force_stop;
        if (opt->force_stop_child)
            return nlopt_set_force_stop(opt->force_stop_child, force_stop);
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
S
stevenj 已提交
647 648 649
}

GET(force_stop, int, force_stop)
650 651 652
nlopt_result NLOPT_STDCALL nlopt_force_stop(nlopt_opt opt)
{
    return nlopt_set_force_stop(opt, 1);
653
}
S
stevenj 已提交
654 655 656

/*************************************************************************/

S
stevenj 已提交
657
GET(algorithm, nlopt_algorithm, algorithm)
658
    GET(dimension, unsigned, n)
S
stevenj 已提交
659 660

/*************************************************************************/
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    nlopt_result NLOPT_STDCALL nlopt_set_local_optimizer(nlopt_opt opt, const nlopt_opt local_opt)
{
    if (opt) {
        nlopt_unset_errmsg(opt);
        if (local_opt && local_opt->n != opt->n)
            return ERR(NLOPT_INVALID_ARGS, opt, "dimension mismatch in local optimizer");
        nlopt_destroy(opt->local_opt);
        opt->local_opt = nlopt_copy(local_opt);
        if (local_opt) {
            if (!opt->local_opt)
                return NLOPT_OUT_OF_MEMORY;
            nlopt_set_lower_bounds(opt->local_opt, opt->lb);
            nlopt_set_upper_bounds(opt->local_opt, opt->ub);
            nlopt_remove_inequality_constraints(opt->local_opt);
            nlopt_remove_equality_constraints(opt->local_opt);
            nlopt_set_min_objective(opt->local_opt, NULL, NULL);
            nlopt_set_munge(opt->local_opt, NULL, NULL);
            opt->local_opt->force_stop = 0;
        }
        return NLOPT_SUCCESS;
    }
    return NLOPT_INVALID_ARGS;
683 684 685 686
}

/*************************************************************************/

687
GETSET(population, unsigned, stochastic_population)
688
    GETSET(vector_storage, unsigned, vector_storage)
689 690

/*************************************************************************/
691
nlopt_result NLOPT_STDCALL nlopt_set_initial_step1(nlopt_opt opt, double dx)
692
{
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    unsigned i;
    if (!opt)
        return NLOPT_INVALID_ARGS;
    nlopt_unset_errmsg(opt);
    if (dx == 0)
        return ERR(NLOPT_INVALID_ARGS, opt, "zero step size");
    if (!opt->dx && opt->n > 0) {
        opt->dx = (double *) malloc(sizeof(double) * (opt->n));
        if (!opt->dx)
            return NLOPT_OUT_OF_MEMORY;
    }
    for (i = 0; i < opt->n; ++i)
        opt->dx[i] = dx;
    return NLOPT_SUCCESS;
}

nlopt_result NLOPT_STDCALL nlopt_set_initial_step(nlopt_opt opt, const double *dx)
{
    unsigned i;
    if (!opt)
        return NLOPT_INVALID_ARGS;
    nlopt_unset_errmsg(opt);
    if (!dx) {
        free(opt->dx);
        opt->dx = NULL;
        return NLOPT_SUCCESS;
    }
    for (i = 0; i < opt->n; ++i)
        if (dx[i] == 0)
            return ERR(NLOPT_INVALID_ARGS, opt, "zero step size");
    if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
        return NLOPT_OUT_OF_MEMORY;
    memcpy(opt->dx, dx, sizeof(double) * (opt->n));
    return NLOPT_SUCCESS;
}

nlopt_result NLOPT_STDCALL nlopt_get_initial_step(const nlopt_opt opt, const double *x, double *dx)
{
    if (!opt)
        return NLOPT_INVALID_ARGS;
    nlopt_unset_errmsg(opt);
    if (!opt->n)
        return NLOPT_SUCCESS;
    if (!opt->dx) {
        nlopt_opt o = (nlopt_opt) opt;  /* discard const temporarily */
        nlopt_result ret = nlopt_set_default_initial_step(o, x);
        if (ret != NLOPT_SUCCESS)
            return ret;
        memcpy(dx, o->dx, sizeof(double) * (opt->n));
        free(o->dx);
        o->dx = NULL;           /* don't save, since x-dependent */
    } else
        memcpy(dx, opt->dx, sizeof(double) * (opt->n));
    return NLOPT_SUCCESS;
}

nlopt_result NLOPT_STDCALL nlopt_set_default_initial_step(nlopt_opt opt, const double *x)
{
    const double *lb, *ub;
    unsigned i;

    nlopt_unset_errmsg(opt);
    if (!opt || !x)
        return NLOPT_INVALID_ARGS;
    lb = opt->lb;
    ub = opt->ub;

    if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
        return NLOPT_OUT_OF_MEMORY;

    /* crude heuristics for initial step size of nonderivative algorithms */
    for (i = 0; i < opt->n; ++i) {
        double step = HUGE_VAL;

        if (!nlopt_isinf(ub[i]) && !nlopt_isinf(lb[i])
            && (ub[i] - lb[i]) * 0.25 < step && ub[i] > lb[i])
            step = (ub[i] - lb[i]) * 0.25;
        if (!nlopt_isinf(ub[i])
            && ub[i] - x[i] < step && ub[i] > x[i])
            step = (ub[i] - x[i]) * 0.75;
        if (!nlopt_isinf(lb[i])
            && x[i] - lb[i] < step && x[i] > lb[i])
            step = (x[i] - lb[i]) * 0.75;

        if (nlopt_isinf(step)) {
            if (!nlopt_isinf(ub[i])
                && fabs(ub[i] - x[i]) < fabs(step))
                step = (ub[i] - x[i]) * 1.1;
            if (!nlopt_isinf(lb[i])
                && fabs(x[i] - lb[i]) < fabs(step))
                step = (x[i] - lb[i]) * 1.1;
        }
        if (nlopt_isinf(step) || nlopt_istiny(step)) {
            step = x[i];
        }
        if (nlopt_isinf(step) || step == 0.0)
            step = 1;

        opt->dx[i] = step;
    }
    return NLOPT_SUCCESS;
794 795 796
}

/*************************************************************************/
S
stevenj 已提交
797

798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
void NLOPT_STDCALL nlopt_set_munge(nlopt_opt opt, nlopt_munge munge_on_destroy, nlopt_munge munge_on_copy)
{
    if (opt) {
        opt->munge_on_destroy = munge_on_destroy;
        opt->munge_on_copy = munge_on_copy;
    }
}

void NLOPT_STDCALL nlopt_munge_data(nlopt_opt opt, nlopt_munge2 munge, void *data)
{
    if (opt && munge) {
        unsigned i;
        opt->f_data = munge(opt->f_data, data);
        for (i = 0; i < opt->m; ++i)
            opt->fc[i].f_data = munge(opt->fc[i].f_data, data);
        for (i = 0; i < opt->p; ++i)
            opt->h[i].f_data = munge(opt->h[i].f_data, data);
    }
816 817
}

S
stevenj 已提交
818
/*************************************************************************/
819 820 821 822 823 824 825 826 827 828 829 830

const char *nlopt_set_errmsg(nlopt_opt opt, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    opt->errmsg = nlopt_vsprintf(opt->errmsg, format, ap);
    va_end(ap);
    return opt->errmsg;
}

void nlopt_unset_errmsg(nlopt_opt opt)
{
831 832 833 834
    if (opt) {
        free(opt->errmsg);
        opt->errmsg = NULL;
    }
835 836 837 838 839 840 841 842
}

const char *nlopt_get_errmsg(nlopt_opt opt)
{
    return opt->errmsg;
}

/*************************************************************************/