1 /* Copyright (c) 2007-2008 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 */
31 void nlopt_get_local_search_algorithm(nlopt_algorithm *deriv,
32 nlopt_algorithm *nonderiv,
35 *deriv = nlopt_local_search_alg_deriv;
36 *nonderiv = nlopt_local_search_alg_nonderiv;
37 *maxeval = nlopt_local_search_maxeval;
40 void nlopt_set_local_search_algorithm(nlopt_algorithm deriv,
41 nlopt_algorithm nonderiv,
44 nlopt_local_search_alg_deriv = deriv;
45 nlopt_local_search_alg_nonderiv = nonderiv;
46 nlopt_local_search_maxeval = maxeval;
49 /*************************************************************************/
51 int nlopt_stochastic_population = 0;
53 int nlopt_get_stochastic_population(void) {
54 return nlopt_stochastic_population; }
55 void nlopt_set_stochastic_population(int pop) {
56 nlopt_stochastic_population = pop; }
58 /*************************************************************************/
60 nlopt_result nlopt_minimize_econstrained(
61 nlopt_algorithm algorithm,
62 int n, nlopt_func f, void *f_data,
63 int m, nlopt_func fc, void *fc_data_, ptrdiff_t fc_datum_size,
64 int p, nlopt_func h, void *h_data_, ptrdiff_t h_datum_size,
65 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,
70 double htol_rel, double htol_abs,
71 int maxeval, double maxtime)
73 char *fc_data = (char *) fc_data_;
74 char *h_data = (char *) h_data_;
75 nlopt_opt opt = nlopt_create(algorithm, n);
79 if (!opt) return NLOPT_INVALID_ARGS;
81 ret = nlopt_set_min_objective(opt, f, f_data);
82 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
84 for (i = 0; i < m; ++i) {
85 ret = nlopt_add_inequality_constraint(opt, fc,
86 fc_data + i*fc_datum_size,
88 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
91 (void) htol_rel; /* unused */
92 for (i = 0; i < p; ++i) {
93 ret = nlopt_add_equality_constraint(opt, h,
94 h_data + i*h_datum_size,
96 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
99 ret = nlopt_set_lower_bounds(opt, lb);
100 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
101 ret = nlopt_set_upper_bounds(opt, ub);
102 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
104 ret = nlopt_set_stopval(opt, minf_max);
105 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
107 ret = nlopt_set_ftol_rel(opt, ftol_rel);
108 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
109 ret = nlopt_set_ftol_abs(opt, ftol_abs);
110 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
112 ret = nlopt_set_xtol_rel(opt, xtol_rel);
113 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
114 ret = nlopt_set_xtol_abs(opt, xtol_abs);
115 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
117 ret = nlopt_set_maxeval(opt, maxeval);
118 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
120 ret = nlopt_set_maxtime(opt, maxtime);
121 if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
123 ret = nlopt_optimize(opt, x, minf);
129 nlopt_result nlopt_minimize_constrained(
130 nlopt_algorithm algorithm,
131 int n, nlopt_func f, void *f_data,
132 int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size,
133 const double *lb, const double *ub, /* bounds */
134 double *x, /* in: initial guess, out: minimizer */
135 double *minf, /* out: minimum */
136 double minf_max, double ftol_rel, double ftol_abs,
137 double xtol_rel, const double *xtol_abs,
138 int maxeval, double maxtime)
140 return nlopt_minimize_econstrained(
141 algorithm, n, f, f_data,
142 m, fc, fc_data, fc_datum_size, 0, NULL, NULL, 0,
143 lb, ub, x, minf, minf_max, ftol_rel, ftol_abs,
144 xtol_rel, xtol_abs, ftol_rel, ftol_abs, maxeval, maxtime);
147 nlopt_result nlopt_minimize(
148 nlopt_algorithm algorithm,
149 int n, nlopt_func f, void *f_data,
150 const double *lb, const double *ub, /* bounds */
151 double *x, /* in: initial guess, out: minimizer */
152 double *minf, /* out: minimum */
153 double minf_max, double ftol_rel, double ftol_abs,
154 double xtol_rel, const double *xtol_abs,
155 int maxeval, double maxtime)
157 return nlopt_minimize_constrained(
158 algorithm, n, f, f_data, 0, NULL, NULL, 0,
159 lb, ub, x, minf, minf_max, ftol_rel, ftol_abs,
160 xtol_rel, xtol_abs, maxeval, maxtime);