chiark / gitweb /
version, copyright-year bump
[nlopt.git] / api / deprecated.c
1 /* Copyright (c) 2007-2010 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 nlopt_get_local_search_algorithm(nlopt_algorithm *deriv,
32                                       nlopt_algorithm *nonderiv,
33                                       int *maxeval)
34 {
35      *deriv = nlopt_local_search_alg_deriv;
36      *nonderiv = nlopt_local_search_alg_nonderiv;
37      *maxeval = nlopt_local_search_maxeval;
38 }
39
40 void nlopt_set_local_search_algorithm(nlopt_algorithm deriv,
41                                       nlopt_algorithm nonderiv,
42                                       int maxeval)
43 {
44      nlopt_local_search_alg_deriv = deriv;
45      nlopt_local_search_alg_nonderiv = nonderiv;
46      nlopt_local_search_maxeval = maxeval;
47 }
48
49 /*************************************************************************/
50
51 int nlopt_stochastic_population = 0;
52
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 <= 0 ? 0 : (unsigned) pop; }
57
58 /*************************************************************************/
59
60 nlopt_result nlopt_minimize_econstrained(
61      nlopt_algorithm algorithm,
62      int n, nlopt_func_old f, void *f_data,
63      int m, nlopt_func_old fc, void *fc_data_, ptrdiff_t fc_datum_size,
64      int p, nlopt_func_old 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)
72 {
73      char *fc_data = (char *) fc_data_;
74      char *h_data = (char *) h_data_;
75      nlopt_opt opt;
76      nlopt_result ret;
77      int i;
78
79      if (n < 0 || m < 0 || p < 0) return NLOPT_INVALID_ARGS;
80
81      opt = nlopt_create(algorithm, (unsigned) n);
82      if (!opt) return NLOPT_INVALID_ARGS;
83
84      ret = nlopt_set_min_objective(opt, (nlopt_func) f, f_data);
85      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
86
87      for (i = 0; i < m; ++i) {
88           ret = nlopt_add_inequality_constraint(opt, (nlopt_func) fc, 
89                                                 fc_data + i*fc_datum_size,
90                                                 0.0);
91           if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
92      }
93
94      (void) htol_rel; /* unused */
95      for (i = 0; i < p; ++i) {
96           ret = nlopt_add_equality_constraint(opt, (nlopt_func) h, 
97                                               h_data + i*h_datum_size,
98                                               htol_abs);
99           if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
100      }
101
102      ret = nlopt_set_lower_bounds(opt, lb);
103      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
104      ret = nlopt_set_upper_bounds(opt, ub);
105      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
106
107      ret = nlopt_set_stopval(opt, minf_max);
108      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
109
110      ret = nlopt_set_ftol_rel(opt, ftol_rel);
111      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
112      ret = nlopt_set_ftol_abs(opt, ftol_abs);
113      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
114
115      ret = nlopt_set_xtol_rel(opt, xtol_rel);
116      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
117      ret = nlopt_set_xtol_abs(opt, xtol_abs);
118      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
119      
120      ret = nlopt_set_maxeval(opt, maxeval);
121      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
122
123      ret = nlopt_set_maxtime(opt, maxtime);
124      if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
125
126      ret = nlopt_optimize(opt, x, minf);
127
128      nlopt_destroy(opt);
129      return ret;
130 }
131
132 nlopt_result nlopt_minimize_constrained(
133      nlopt_algorithm algorithm,
134      int n, nlopt_func_old f, void *f_data,
135      int m, nlopt_func_old fc, void *fc_data, ptrdiff_t fc_datum_size,
136      const double *lb, const double *ub, /* bounds */
137      double *x, /* in: initial guess, out: minimizer */
138      double *minf, /* out: minimum */
139      double minf_max, double ftol_rel, double ftol_abs,
140      double xtol_rel, const double *xtol_abs,
141      int maxeval, double maxtime)
142 {
143      return nlopt_minimize_econstrained(
144           algorithm, n, f, f_data, 
145           m, fc, fc_data, fc_datum_size, 0, NULL, NULL, 0,
146           lb, ub, x, minf, minf_max, ftol_rel, ftol_abs,
147           xtol_rel, xtol_abs, ftol_rel, ftol_abs, maxeval, maxtime);
148 }
149
150 nlopt_result nlopt_minimize(
151      nlopt_algorithm algorithm,
152      int n, nlopt_func_old f, void *f_data,
153      const double *lb, const double *ub, /* bounds */
154      double *x, /* in: initial guess, out: minimizer */
155      double *minf, /* out: minimum */
156      double minf_max, double ftol_rel, double ftol_abs,
157      double xtol_rel, const double *xtol_abs,
158      int maxeval, double maxtime)
159 {
160      return nlopt_minimize_constrained(
161           algorithm, n, f, f_data, 0, NULL, NULL, 0,
162           lb, ub, x, minf, minf_max, ftol_rel, ftol_abs,
163           xtol_rel, xtol_abs, maxeval, maxtime);
164 }