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,
33 nlopt_algorithm *nonderiv,
36 *deriv = nlopt_local_search_alg_deriv;
37 *nonderiv = nlopt_local_search_alg_nonderiv;
38 *maxeval = nlopt_local_search_maxeval;
42 NLOPT_STDCALL nlopt_set_local_search_algorithm(nlopt_algorithm deriv,
43 nlopt_algorithm nonderiv,
46 nlopt_local_search_alg_deriv = deriv;
47 nlopt_local_search_alg_nonderiv = nonderiv;
48 nlopt_local_search_maxeval = maxeval;
51 /*************************************************************************/
53 int nlopt_stochastic_population = 0;
56 NLOPT_STDCALL nlopt_get_stochastic_population(void) {
57 return nlopt_stochastic_population; }
59 NLOPT_STDCALL nlopt_set_stochastic_population(int pop) {
60 nlopt_stochastic_population = pop <= 0 ? 0 : (unsigned) pop; }
62 /*************************************************************************/
65 NLOPT_STDCALL nlopt_minimize_econstrained(
66 nlopt_algorithm algorithm,
67 int n, nlopt_func_old f, void *f_data,
68 int m, nlopt_func_old fc, void *fc_data_, ptrdiff_t fc_datum_size,
69 int p, nlopt_func_old h, void *h_data_, ptrdiff_t h_datum_size,
70 const double *lb, const double *ub, /* bounds */
71 double *x, /* in: initial guess, out: minimizer */
72 double *minf, /* out: minimum */
73 double minf_max, double ftol_rel, double ftol_abs,
74 double xtol_rel, const double *xtol_abs,
75 double htol_rel, double htol_abs,
76 int maxeval, double maxtime)
78 char *fc_data = (char *) fc_data_;
79 char *h_data = (char *) h_data_;
84 if (n < 0 || m < 0 || p < 0) return NLOPT_INVALID_ARGS;
86 opt = nlopt_create(algorithm, (unsigned) n);
87 if (!opt) return NLOPT_INVALID_ARGS;
89 ret = nlopt_set_min_objective(opt, (nlopt_func) f, f_data);
90 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
92 for (i = 0; i < m; ++i) {
93 ret = nlopt_add_inequality_constraint(opt, (nlopt_func) fc,
94 fc_data + i*fc_datum_size,
96 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
99 (void) htol_rel; /* unused */
100 for (i = 0; i < p; ++i) {
101 ret = nlopt_add_equality_constraint(opt, (nlopt_func) h,
102 h_data + i*h_datum_size,
104 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
107 ret = nlopt_set_lower_bounds(opt, lb);
108 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
109 ret = nlopt_set_upper_bounds(opt, ub);
110 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
112 ret = nlopt_set_stopval(opt, minf_max);
113 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
115 ret = nlopt_set_ftol_rel(opt, ftol_rel);
116 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
117 ret = nlopt_set_ftol_abs(opt, ftol_abs);
118 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
120 ret = nlopt_set_xtol_rel(opt, xtol_rel);
121 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
122 if (xtol_abs) ret = nlopt_set_xtol_abs(opt, xtol_abs);
123 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
125 ret = nlopt_set_maxeval(opt, maxeval);
126 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
128 ret = nlopt_set_maxtime(opt, maxtime);
129 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
131 ret = nlopt_optimize(opt, x, minf);
138 NLOPT_STDCALL nlopt_minimize_constrained(
139 nlopt_algorithm algorithm,
140 int n, nlopt_func_old f, void *f_data,
141 int m, nlopt_func_old fc, void *fc_data, ptrdiff_t fc_datum_size,
142 const double *lb, const double *ub, /* bounds */
143 double *x, /* in: initial guess, out: minimizer */
144 double *minf, /* out: minimum */
145 double minf_max, double ftol_rel, double ftol_abs,
146 double xtol_rel, const double *xtol_abs,
147 int maxeval, double maxtime)
149 return nlopt_minimize_econstrained(
150 algorithm, n, f, f_data,
151 m, fc, fc_data, fc_datum_size, 0, NULL, NULL, 0,
152 lb, ub, x, minf, minf_max, ftol_rel, ftol_abs,
153 xtol_rel, xtol_abs, ftol_rel, ftol_abs, maxeval, maxtime);
157 NLOPT_STDCALL nlopt_minimize(
158 nlopt_algorithm algorithm,
159 int n, nlopt_func_old f, void *f_data,
160 const double *lb, const double *ub, /* bounds */
161 double *x, /* in: initial guess, out: minimizer */
162 double *minf, /* out: minimum */
163 double minf_max, double ftol_rel, double ftol_abs,
164 double xtol_rel, const double *xtol_abs,
165 int maxeval, double maxtime)
167 return nlopt_minimize_constrained(
168 algorithm, n, f, f_data, 0, NULL, NULL, 0,
169 lb, ub, x, minf, minf_max, ftol_rel, ftol_abs,
170 xtol_rel, xtol_abs, maxeval, maxtime);