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.
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.
27 All of the functions in this file have underscores in their names,
28 which means that they are treated differently for name-mangling
29 (thank you, g77 and f2c) than names without underscores.
31 The return value of a function is converted to the first argument
34 void F77_(nlo_create,NLO_CREATE)(nlopt_opt *opt, int *alg, int *n)
36 if (*n < 0) *opt = NULL;
38 *opt = nlopt_create((nlopt_algorithm) *alg, (unsigned) *n);
39 nlopt_set_munge(*opt, free_f77_func_data, dup_f77_func_data);
43 void F77_(nlo_copy,NLO_COPY)(nlopt_opt *nopt, nlopt_opt *opt)
45 *nopt = nlopt_copy(*opt);
48 void F77_(nlo_destroy,NLO_DESTROY)(nlopt_opt *opt)
53 void F77_(nlo_optimize,NLO_OPTIMIZE)(int *ret,
54 nlopt_opt *opt, double *x, double *optf)
56 *ret = (int) nlopt_optimize(*opt, x, optf);
59 void F77_(nlo_set_min_objective,NLO_SET_MIN_OBJECTIVE)(
60 int *ret, nlopt_opt *opt, nlopt_f77_func f, void *f_data)
62 f77_func_data *d = (f77_func_data*) malloc(sizeof(f77_func_data));
63 if (!d) { *ret = (int) NLOPT_OUT_OF_MEMORY; return; }
66 *ret = (int) nlopt_set_min_objective(*opt, f77_func_wrap, d);
69 void F77_(nlo_set_max_objective,NLO_SET_MAX_OBJECTIVE)(
70 int *ret, nlopt_opt *opt, nlopt_f77_func f, void *f_data)
72 f77_func_data *d = (f77_func_data*) malloc(sizeof(f77_func_data));
73 if (!d) { *ret = (int) NLOPT_OUT_OF_MEMORY; return; }
76 *ret = (int) nlopt_set_max_objective(*opt, f77_func_wrap, d);
79 F77_GET(algorithm, ALGORITHM, int)
80 F77_GET(dimension, DIMENSION, int)
82 F77_GETSETA(lower_bounds, LOWER_BOUNDS, double)
83 F77_GETSETA(upper_bounds, UPPER_BOUNDS, double)
85 void F77_(nlo_remove_inequality_constraints,NLO_REMOVE_INEQUALITY_CONSTRAINTS)(
86 int *ret, nlopt_opt *opt) {
87 *ret = (int) nlopt_remove_inequality_constraints(*opt);
90 void F77_(nlo_add_inequality_constraint,NLO_ADD_INEQUALITY_CONSTRAINT)(
91 int *ret, nlopt_opt *opt, nlopt_f77_func fc, void *fc_data, double *tol)
93 f77_func_data *d = (f77_func_data*) malloc(sizeof(f77_func_data));
94 if (!d) { *ret = (int) NLOPT_OUT_OF_MEMORY; return; }
97 *ret = (int) nlopt_add_inequality_constraint(*opt, f77_func_wrap,d, *tol);
100 void F77_(nlo_add_inequality_mconstraint,NLO_ADD_INEQUALITY_MCONSTRAINT)(
101 int *ret, nlopt_opt *opt, int *m,
102 nlopt_f77_mfunc mfc, void *mfc_data, double *tol)
105 if (*m < 0) { *ret = (int) NLOPT_INVALID_ARGS; return; }
106 if (*m == 0) { *ret = (int) NLOPT_SUCCESS; return; }
107 d = (f77_func_data*) malloc(sizeof(f77_func_data));
108 if (!d) { *ret = (int) NLOPT_OUT_OF_MEMORY; return; }
110 d->f_data = mfc_data;
111 *ret = (int) nlopt_add_inequality_mconstraint(*opt, (unsigned) *m,
112 f77_mfunc_wrap, d, tol);
115 void F77_(nlo_remove_equality_constraints,NLO_REMOVE_EQUALITY_CONSTRAINTS)(
116 int *ret, nlopt_opt *opt) {
117 *ret = (int) nlopt_remove_equality_constraints(*opt);
120 void F77_(nlo_add_equality_constraint,NLO_ADD_EQUALITY_CONSTRAINT)(
121 int *ret, nlopt_opt *opt, nlopt_f77_func fc, void *fc_data, double *tol)
123 f77_func_data *d = (f77_func_data*) malloc(sizeof(f77_func_data));
124 if (!d) { *ret = (int) NLOPT_OUT_OF_MEMORY; return; }
127 *ret = (int) nlopt_add_equality_constraint(*opt, f77_func_wrap,d, *tol);
130 void F77_(nlo_add_equality_mconstraint,NLO_ADD_EQUALITY_MCONSTRAINT)(
131 int *ret, nlopt_opt *opt, int *m,
132 nlopt_f77_mfunc mfc, void *mfc_data, double *tol)
135 if (*m < 0) { *ret = (int) NLOPT_INVALID_ARGS; return; }
136 if (*m == 0) { *ret = (int) NLOPT_SUCCESS; return; }
137 d = (f77_func_data*) malloc(sizeof(f77_func_data));
138 if (!d) { *ret = (int) NLOPT_OUT_OF_MEMORY; return; }
140 d->f_data = mfc_data;
141 *ret = (int) nlopt_add_equality_mconstraint(*opt, (unsigned) *m,
142 f77_mfunc_wrap, d, tol);
145 F77_GETSET(stopval, STOPVAL, double)
146 F77_GETSET(ftol_rel, FTOL_REL, double)
147 F77_GETSET(ftol_abs, FTOL_ABS, double)
148 F77_GETSET(xtol_rel, XTOL_REL, double)
149 F77_GETSETA(xtol_abs, XTOL_ABS, double)
150 F77_GETSET(maxeval, MAXEVAL, int)
151 F77_GETSET(maxtime, MAXTIME, double)
153 F77_GETSET(force_stop, FORCE_STOP, int)
154 void F77_(nlo_force_stop,NLO_FORCE_STOP)(int *ret, nlopt_opt *opt) {
155 *ret = (int) nlopt_force_stop(*opt);
158 F77_SET(local_optimizer, LOCAL_OPTIMIZER, nlopt_opt)
159 F77_GETSET(population, POPULATION, unsigned)
160 F77_GETSET(vector_storage, vector_storage, unsigned)
162 F77_SETA(default_initial_step, DEFAULT_INITIAL_STEP, double)
163 F77_SETA(initial_step, INITIAL_STEP, double)
164 F77_SET(initial_step1, INITIAL_STEP1, double)
165 void F77_(nlo_get_initial_step, NLO_GET_INITIAL_STEP)(
166 int *ret, nlopt_opt *opt, const double *x, double *dx) {
167 *ret = (int) nlopt_get_initial_step(*opt, x, dx);