chiark / gitweb /
added get/set stochastic_population functions for greater control over stochastic...
[nlopt.git] / api / f77funcs.h
1 /* Copyright (c) 2007-2008 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 /* Fortran API wrappers, using the F77 macro defined in f77api.c.
24    This header file is #included one or more times from f77api.c
25    in order to define verions of the Fortran API for various compilers. 
26
27    In homage to Fortran 77, we stick with 6-character subroutine names.
28    The return value of the function is converted into the first argument
29    of the subroutine. */
30
31 /* nlopt_minimize_constrained */
32 void F77(nloptc,NLOPTC)(int *info, 
33                         const int *algorithm,
34                         const int *n, nlopt_f77_func f, void *f_data,
35                         const int *m, nlopt_f77_func fc, 
36                         char *fc_data, char *fc_second_datum,
37                         const double *lb, const double *ub,
38                         double *x, double *minf,
39                         const double *minf_max,
40                         const double *ftol_rel, const double *ftol_abs,
41                         const double *xtol_rel, const double *xtol_abs,
42                         const int *have_xtol_abs,
43                         const int *maxeval, const double *maxtime)
44 {
45      f77_func_data d, *dc;
46      int i;
47
48      d.f = f; d.f_data = f_data;
49      if (*m < 0) { *info = NLOPT_INVALID_ARGS; return; }
50      dc = (f77_func_data *) malloc(sizeof(f77_func_data) * *m);
51      if (*m > 0 && !dc) { *info = NLOPT_OUT_OF_MEMORY; return; }
52      for (i = 0; i < *m; ++i) {
53           dc[i].f = fc;
54           dc[i].f_data = fc_data + i * (fc_second_datum - fc_data);
55      }
56
57      *info = nlopt_minimize_constrained((nlopt_algorithm) *algorithm, 
58                                         *n, f77_func_wrap, &d,
59                                         *m, f77_func_wrap, 
60                                         dc, sizeof(f77_func_data),
61                                         lb, ub, x, minf,
62                                         *minf_max, *ftol_rel, *ftol_abs,
63                                         *xtol_rel,
64                                         *have_xtol_abs ? xtol_abs : 0,
65                                         *maxeval, *maxtime);
66
67      if (dc) free(dc);
68 }
69
70 /* nlopt_minimize */
71 void F77(nloptm,NLOPTM)(int *info, 
72                         const int *algorithm,
73                         const int *n, nlopt_f77_func f, void *f_data,
74                         const double *lb, const double *ub,
75                         double *x, double *minf,
76                         const double *minf_max,
77                         const double *ftol_rel, const double *ftol_abs,
78                         const double *xtol_rel, const double *xtol_abs,
79                         const int *have_xtol_abs,
80                         const int *maxeval, const double *maxtime)
81 {
82      int m = 0;
83      F77(nloptc,NLOPTC)(info, algorithm, n, f, f_data, &m, 0, 0, 0,
84                         lb, ub, x, minf, minf_max, ftol_rel, ftol_abs,
85                         xtol_rel, xtol_abs, have_xtol_abs, maxeval, maxtime);
86 }
87
88 void F77(nlosr,NLOSR)(const int *seed) { nlopt_srand((unsigned long) *seed); }
89 void F77(nlosrt,NLOSRT)(void) { nlopt_srand_time(); }
90 void F77(nloptv,NLOPTV)(int *major, int *minor, int *bugfix) {
91      nlopt_version(major, minor, bugfix);
92 }
93 void F77(nlogls,NLOGLS)(int *ideriv, int *inonderiv, int *maxeval)
94 {
95      nlopt_algorithm deriv, nonderiv;
96      nlopt_get_local_search_algorithm(&deriv, &nonderiv, maxeval);
97      *ideriv = deriv;
98      *inonderiv = nonderiv;
99 }
100 void F77(nlosls,NLOSLS)(int *ideriv, int *inonderiv, int *maxeval)
101 {
102      nlopt_algorithm deriv = (nlopt_algorithm) *ideriv;
103      nlopt_algorithm nonderiv = (nlopt_algorithm) *inonderiv;
104      nlopt_set_local_search_algorithm(deriv, nonderiv, *maxeval);
105 }
106
107 void F77(nlogsp,NLOGSP)(int *pop)
108 {
109      *pop = nlopt_get_stochastic_population();
110 }
111 void F77(nlossp,NLOSSP)(const int *pop)
112 {
113      nlopt_set_stochastic_population(*pop);
114 }