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.
25 /*************************************************************************/
27 nlopt_algorithm nlopt_local_search_alg_deriv = NLOPT_LD_MMA;
28 nlopt_algorithm nlopt_local_search_alg_nonderiv = NLOPT_LN_COBYLA;
29 int nlopt_local_search_maxeval = -1; /* no maximum by default */
32 NLOPT_STDCALL nlopt_get_local_search_algorithm(nlopt_algorithm * deriv, nlopt_algorithm * nonderiv, int *maxeval)
34 *deriv = nlopt_local_search_alg_deriv;
35 *nonderiv = nlopt_local_search_alg_nonderiv;
36 *maxeval = nlopt_local_search_maxeval;
40 NLOPT_STDCALL nlopt_set_local_search_algorithm(nlopt_algorithm deriv, nlopt_algorithm nonderiv, int maxeval)
42 nlopt_local_search_alg_deriv = deriv;
43 nlopt_local_search_alg_nonderiv = nonderiv;
44 nlopt_local_search_maxeval = maxeval;
47 /*************************************************************************/
49 int nlopt_stochastic_population = 0;
52 NLOPT_STDCALL nlopt_get_stochastic_population(void)
54 return nlopt_stochastic_population;
58 NLOPT_STDCALL nlopt_set_stochastic_population(int pop)
60 nlopt_stochastic_population = pop <= 0 ? 0 : (unsigned) pop;
63 /*************************************************************************/
65 nlopt_result NLOPT_STDCALL nlopt_minimize_econstrained(nlopt_algorithm algorithm, int n, nlopt_func_old f, void *f_data, int m, nlopt_func_old fc, void *fc_data_, ptrdiff_t fc_datum_size, int p, nlopt_func_old h, void *h_data_, ptrdiff_t h_datum_size, const double *lb, const double *ub, /* bounds */
66 double *x, /* in: initial guess, out: minimizer */
67 double *minf, /* out: minimum */
68 double minf_max, double ftol_rel, double ftol_abs,
69 double xtol_rel, const double *xtol_abs, double htol_rel, double htol_abs, int maxeval, double maxtime)
71 char *fc_data = (char *) fc_data_;
72 char *h_data = (char *) h_data_;
77 if (n < 0 || m < 0 || p < 0)
78 return NLOPT_INVALID_ARGS;
80 opt = nlopt_create(algorithm, (unsigned) n);
82 return NLOPT_INVALID_ARGS;
84 ret = nlopt_set_min_objective(opt, (nlopt_func) f, f_data);
85 if (ret != NLOPT_SUCCESS) {
90 for (i = 0; i < m; ++i) {
91 ret = nlopt_add_inequality_constraint(opt, (nlopt_func) fc, fc_data + i * fc_datum_size, 0.0);
92 if (ret != NLOPT_SUCCESS) {
98 (void) htol_rel; /* unused */
99 for (i = 0; i < p; ++i) {
100 ret = nlopt_add_equality_constraint(opt, (nlopt_func) h, h_data + i * h_datum_size, htol_abs);
101 if (ret != NLOPT_SUCCESS) {
107 ret = nlopt_set_lower_bounds(opt, lb);
108 if (ret != NLOPT_SUCCESS) {
112 ret = nlopt_set_upper_bounds(opt, ub);
113 if (ret != NLOPT_SUCCESS) {
118 ret = nlopt_set_stopval(opt, minf_max);
119 if (ret != NLOPT_SUCCESS) {
124 ret = nlopt_set_ftol_rel(opt, ftol_rel);
125 if (ret != NLOPT_SUCCESS) {
129 ret = nlopt_set_ftol_abs(opt, ftol_abs);
130 if (ret != NLOPT_SUCCESS) {
135 ret = nlopt_set_xtol_rel(opt, xtol_rel);
136 if (ret != NLOPT_SUCCESS) {
141 ret = nlopt_set_xtol_abs(opt, xtol_abs);
142 if (ret != NLOPT_SUCCESS) {
147 ret = nlopt_set_maxeval(opt, maxeval);
148 if (ret != NLOPT_SUCCESS) {
153 ret = nlopt_set_maxtime(opt, maxtime);
154 if (ret != NLOPT_SUCCESS) {
159 ret = nlopt_optimize(opt, x, minf);
165 nlopt_result NLOPT_STDCALL nlopt_minimize_constrained(nlopt_algorithm algorithm, int n, nlopt_func_old f, void *f_data, int m, nlopt_func_old fc, void *fc_data, ptrdiff_t fc_datum_size, const double *lb, const double *ub, /* bounds */
166 double *x, /* in: initial guess, out: minimizer */
167 double *minf, /* out: minimum */
168 double minf_max, double ftol_rel, double ftol_abs, double xtol_rel, const double *xtol_abs, int maxeval, double maxtime)
170 return nlopt_minimize_econstrained(algorithm, n, f, f_data,
171 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, ftol_rel, ftol_abs, maxeval, maxtime);
174 nlopt_result NLOPT_STDCALL nlopt_minimize(nlopt_algorithm algorithm, int n, nlopt_func_old f, void *f_data, const double *lb, const double *ub, /* bounds */
175 double *x, /* in: initial guess, out: minimizer */
176 double *minf, /* out: minimum */
177 double minf_max, double ftol_rel, double ftol_abs, double xtol_rel, const double *xtol_abs, int maxeval, double maxtime)
179 return nlopt_minimize_constrained(algorithm, n, f, f_data, 0, NULL, NULL, 0, lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, maxeval, maxtime);