From 72d4e7dd11a81a8ce2444884ab1669b9600f1648 Mon Sep 17 00:00:00 2001 From: stevenj Date: Tue, 25 Nov 2008 14:37:07 -0500 Subject: [PATCH] skeleton of future equality-constraint support darcs-hash:20081125193707-c8de0-5a57e367a994ab4bd65f667197312bfd4e8c0a02.gz --- api/nlopt.c | 39 ++++++++++++++++++++++++++++++++++----- api/nlopt.h | 12 ++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/api/nlopt.c b/api/nlopt.c index 056165f..28fd621 100644 --- a/api/nlopt.c +++ b/api/nlopt.c @@ -252,11 +252,13 @@ void nlopt_set_local_search_algorithm(nlopt_algorithm deriv, /*************************************************************************/ -/* same as nlopt_minimize, but xtol_abs is required to be non-NULL */ +/* same as nlopt_minimize_econstrained, + but xtol_abs is required to be non-NULL */ static nlopt_result nlopt_minimize_( nlopt_algorithm algorithm, int n, nlopt_func f, void *f_data, int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size, + int p, nlopt_func h, void *h_data, ptrdiff_t h_datum_size, const double *lb, const double *ub, /* bounds */ double *x, /* in: initial guess, out: minimizer */ double *minf, /* out: minimum */ @@ -269,7 +271,7 @@ static nlopt_result nlopt_minimize_( nlopt_stopping stop; /* some basic argument checks */ - if (!minf || !f || n < 0 || m < 0 + if (!minf || !f || n < 0 || m < 0 || p < 0 || (p > 0 && !h) || (m > 0 && !fc)) return NLOPT_INVALID_ARGS; if (n == 0) { /* trivial case: no degrees of freedom */ *minf = f(n, x, NULL, f_data); @@ -282,6 +284,10 @@ static nlopt_result nlopt_minimize_( if (m != 0 && algorithm != NLOPT_LD_MMA && algorithm != NLOPT_LN_COBYLA) return NLOPT_INVALID_ARGS; + /* nonlinear equality constraints (h(x) = 0) not yet supported */ + if (p != 0) + return NLOPT_INVALID_ARGS; + d.f = f; d.f_data = f_data; d.lb = lb; @@ -515,10 +521,11 @@ static nlopt_result nlopt_minimize_( return NLOPT_SUCCESS; } -nlopt_result nlopt_minimize_constrained( +nlopt_result nlopt_minimize_econstrained( nlopt_algorithm algorithm, int n, nlopt_func f, void *f_data, int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size, + int p, nlopt_func h, void *h_data, ptrdiff_t h_datum_size, const double *lb, const double *ub, /* bounds */ double *x, /* in: initial guess, out: minimizer */ double *minf, /* out: minimum */ @@ -529,7 +536,9 @@ nlopt_result nlopt_minimize_constrained( nlopt_result ret; if (xtol_abs) ret = nlopt_minimize_(algorithm, n, f, f_data, - m, fc, fc_data, fc_datum_size, lb, ub, + m, fc, fc_data, fc_datum_size, + p, h, h_data, h_datum_size, + lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, maxeval, maxtime); else { @@ -538,7 +547,9 @@ nlopt_result nlopt_minimize_constrained( if (!xtol) return NLOPT_OUT_OF_MEMORY; for (i = 0; i < n; ++i) xtol[i] = -1; ret = nlopt_minimize_(algorithm, n, f, f_data, - m, fc, fc_data, fc_datum_size, lb, ub, + m, fc, fc_data, fc_datum_size, + p, h, h_data, h_datum_size, + lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol, maxeval, maxtime); free(xtol); @@ -546,6 +557,24 @@ nlopt_result nlopt_minimize_constrained( return ret; } +nlopt_result nlopt_minimize_constrained( + nlopt_algorithm algorithm, + int n, nlopt_func f, void *f_data, + int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size, + const double *lb, const double *ub, /* bounds */ + double *x, /* in: initial guess, out: minimizer */ + double *minf, /* out: minimum */ + double minf_max, double ftol_rel, double ftol_abs, + double xtol_rel, const double *xtol_abs, + int maxeval, double maxtime) +{ + return nlopt_minimize_econstrained( + algorithm, n, f, f_data, + m, fc, fc_data, fc_datum_size, 0, NULL, NULL, 0, + lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, + xtol_rel, xtol_abs, maxeval, maxtime); +} + nlopt_result nlopt_minimize( nlopt_algorithm algorithm, int n, nlopt_func f, void *f_data, diff --git a/api/nlopt.h b/api/nlopt.h index 46670d6..e2362be 100644 --- a/api/nlopt.h +++ b/api/nlopt.h @@ -130,6 +130,18 @@ extern nlopt_result nlopt_minimize_constrained( double xtol_rel, const double *xtol_abs, int maxeval, double maxtime); +extern nlopt_result nlopt_minimize_econstrained( + nlopt_algorithm algorithm, + int n, nlopt_func f, void *f_data, + int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size, + int p, nlopt_func h, void *h_data, ptrdiff_t h_datum_size, + const double *lb, const double *ub, /* bounds */ + double *x, /* in: initial guess, out: minimizer */ + double *minf, /* out: minimum */ + double minf_max, double ftol_rel, double ftol_abs, + double xtol_rel, const double *xtol_abs, + int maxeval, double maxtime); + extern void nlopt_srand(unsigned long seed); extern void nlopt_srand_time(void); -- 2.30.2