chiark / gitweb /
cmake
[nlopt.git] / src / api / deprecated.c
1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
2  *
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:
10  * 
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  * 
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. 
21  */
22
23 #include "nlopt.h"
24
25 /*************************************************************************/
26
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 */
30
31 void
32 NLOPT_STDCALL nlopt_get_local_search_algorithm(nlopt_algorithm * deriv, nlopt_algorithm * nonderiv, int *maxeval)
33 {
34     *deriv = nlopt_local_search_alg_deriv;
35     *nonderiv = nlopt_local_search_alg_nonderiv;
36     *maxeval = nlopt_local_search_maxeval;
37 }
38
39 void
40 NLOPT_STDCALL nlopt_set_local_search_algorithm(nlopt_algorithm deriv, nlopt_algorithm nonderiv, int maxeval)
41 {
42     nlopt_local_search_alg_deriv = deriv;
43     nlopt_local_search_alg_nonderiv = nonderiv;
44     nlopt_local_search_maxeval = maxeval;
45 }
46
47 /*************************************************************************/
48
49 int nlopt_stochastic_population = 0;
50
51 int
52 NLOPT_STDCALL nlopt_get_stochastic_population(void)
53 {
54     return nlopt_stochastic_population;
55 }
56
57 void
58 NLOPT_STDCALL nlopt_set_stochastic_population(int pop)
59 {
60     nlopt_stochastic_population = pop <= 0 ? 0 : (unsigned) pop;
61 }
62
63 /*************************************************************************/
64
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)
70 {
71     char *fc_data = (char *) fc_data_;
72     char *h_data = (char *) h_data_;
73     nlopt_opt opt;
74     nlopt_result ret;
75     int i;
76
77     if (n < 0 || m < 0 || p < 0)
78         return NLOPT_INVALID_ARGS;
79
80     opt = nlopt_create(algorithm, (unsigned) n);
81     if (!opt)
82         return NLOPT_INVALID_ARGS;
83
84     ret = nlopt_set_min_objective(opt, (nlopt_func) f, f_data);
85     if (ret != NLOPT_SUCCESS) {
86         nlopt_destroy(opt);
87         return ret;
88     }
89
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) {
93             nlopt_destroy(opt);
94             return ret;
95         }
96     }
97
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) {
102             nlopt_destroy(opt);
103             return ret;
104         }
105     }
106
107     ret = nlopt_set_lower_bounds(opt, lb);
108     if (ret != NLOPT_SUCCESS) {
109         nlopt_destroy(opt);
110         return ret;
111     }
112     ret = nlopt_set_upper_bounds(opt, ub);
113     if (ret != NLOPT_SUCCESS) {
114         nlopt_destroy(opt);
115         return ret;
116     }
117
118     ret = nlopt_set_stopval(opt, minf_max);
119     if (ret != NLOPT_SUCCESS) {
120         nlopt_destroy(opt);
121         return ret;
122     }
123
124     ret = nlopt_set_ftol_rel(opt, ftol_rel);
125     if (ret != NLOPT_SUCCESS) {
126         nlopt_destroy(opt);
127         return ret;
128     }
129     ret = nlopt_set_ftol_abs(opt, ftol_abs);
130     if (ret != NLOPT_SUCCESS) {
131         nlopt_destroy(opt);
132         return ret;
133     }
134
135     ret = nlopt_set_xtol_rel(opt, xtol_rel);
136     if (ret != NLOPT_SUCCESS) {
137         nlopt_destroy(opt);
138         return ret;
139     }
140     if (xtol_abs)
141         ret = nlopt_set_xtol_abs(opt, xtol_abs);
142     if (ret != NLOPT_SUCCESS) {
143         nlopt_destroy(opt);
144         return ret;
145     }
146
147     ret = nlopt_set_maxeval(opt, maxeval);
148     if (ret != NLOPT_SUCCESS) {
149         nlopt_destroy(opt);
150         return ret;
151     }
152
153     ret = nlopt_set_maxtime(opt, maxtime);
154     if (ret != NLOPT_SUCCESS) {
155         nlopt_destroy(opt);
156         return ret;
157     }
158
159     ret = nlopt_optimize(opt, x, minf);
160
161     nlopt_destroy(opt);
162     return ret;
163 }
164
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)
169 {
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);
172 }
173
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)
178 {
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);
180 }