22#include <gsl/gsl_errno.h>
23#include <gsl/gsl_math.h>
24#include <gsl/gsl_min.h>
25#include <gsl/gsl_multimin.h>
39typedef void (*func_d)(
const int *,
const double *,
double *,
const int *,
46static void my_fdf(
const gsl_vector *v,
void *params,
double *f,
48 double *x, *gradient, ff2;
52 p = (param_fdf_t *)params;
55 x = (
double *)malloc(dim *
sizeof(
double));
56 gradient = (
double *)malloc(dim *
sizeof(
double));
58 for (
i = 0;
i < dim;
i++)
59 x[
i] = gsl_vector_get(v,
i);
60 getgrad = (df == NULL) ? 0 : 1;
62 p->func(&dim, x, &ff2, &getgrad, gradient);
68 for (
i = 0;
i < dim;
i++)
69 gsl_vector_set(df,
i, gradient[
i]);
76static double my_f(
const gsl_vector *v,
void *params) {
79 my_fdf(v, params, &val, NULL);
84static void my_df(
const gsl_vector *v,
void *params, gsl_vector *df) {
85 my_fdf(v, params, NULL, df);
88typedef void (*print_f_ptr)(
const int *,
const int *,
const double *,
89 const double *,
const double *,
const double *);
91int FC_FUNC_(oct_minimize,
92 OCT_MINIMIZE)(
const int *method,
const int *dim,
double *point,
93 const double *step,
const double *line_tol,
94 const double *tolgrad,
const double *toldr,
96 const print_f_ptr write_info,
double *
minimum) {
99 double maxgrad, maxdr;
104 const gsl_multimin_fdfminimizer_type *T = NULL;
105 gsl_multimin_fdfminimizer *
s;
107 gsl_vector *absgrad, *absdr;
108 gsl_multimin_function_fdf my_func;
114 oldpoint = (
double *)malloc(*dim *
sizeof(
double));
115 grad = (
double *)malloc(*dim *
sizeof(
double));
119 my_func.fdf = &my_fdf;
121 my_func.params = (
void *)&p;
124 x = gsl_vector_alloc(*dim);
125 for (
i = 0;
i < *dim;
i++)
126 gsl_vector_set(x,
i, point[
i]);
129 absgrad = gsl_vector_alloc(*dim);
130 absdr = gsl_vector_alloc(*dim);
135 T = gsl_multimin_fdfminimizer_steepest_descent;
138 T = gsl_multimin_fdfminimizer_conjugate_fr;
141 T = gsl_multimin_fdfminimizer_conjugate_pr;
144 T = gsl_multimin_fdfminimizer_vector_bfgs;
147 T = gsl_multimin_fdfminimizer_vector_bfgs2;
151 s = gsl_multimin_fdfminimizer_alloc(T, *dim);
153 gsl_multimin_fdfminimizer_set(
s, &my_func, x, *step, *line_tol);
156 for (
i = 0;
i < *dim;
i++)
157 oldpoint[
i] = point[
i];
160 status = gsl_multimin_fdfminimizer_iterate(
s);
163 *
minimum = gsl_multimin_fdfminimizer_minimum(
s);
164 for (
i = 0;
i < *dim;
i++)
165 point[
i] = gsl_vector_get(gsl_multimin_fdfminimizer_x(
s),
i);
166 for (
i = 0;
i < *dim;
i++)
167 grad[
i] = gsl_vector_get(gsl_multimin_fdfminimizer_gradient(
s),
i);
170 for (
i = 0;
i < *dim;
i++)
171 gsl_vector_set(absdr,
i,
fabs(point[
i] - oldpoint[
i]));
172 maxdr = gsl_vector_max(absdr);
173 for (
i = 0;
i < *dim;
i++)
174 gsl_vector_set(absgrad,
i,
fabs(grad[
i]));
175 maxgrad = gsl_vector_max(absgrad);
178 write_info(&iter, dim,
minimum, &maxdr, &maxgrad, point);
181 for (
i = 0;
i < *dim;
i++)
182 oldpoint[
i] = point[
i];
187 if ((maxgrad <= *tolgrad) || (maxdr <= *toldr))
188 status = GSL_SUCCESS;
190 status = GSL_CONTINUE;
191 }
while (status == GSL_CONTINUE && iter <= *
maxiter);
193 if (status == GSL_CONTINUE)
196 gsl_multimin_fdfminimizer_free(
s);
198 gsl_vector_free(absgrad);
199 gsl_vector_free(absdr);
214typedef void (*func1)(
const double *,
double *);
219double fn1(
double x,
void *params) {
220 param_f1_t *p = (param_f1_t *)params;
226void FC_FUNC_(oct_1dminimize, OCT_1DMINIMIZE)(
double *a,
double *b,
double *m,
227 func1 f,
int *status) {
230 const gsl_min_fminimizer_type *T;
231 gsl_min_fminimizer *
s;
238 F.params = (
void *)&p;
240 T = gsl_min_fminimizer_brent;
241 s = gsl_min_fminimizer_alloc(T);
243 *status = gsl_min_fminimizer_set(
s, &F, *m, *a, *b);
245 gsl_set_error_handler_off();
249 *status = gsl_min_fminimizer_iterate(
s);
251 *m = gsl_min_fminimizer_x_minimum(
s);
252 *a = gsl_min_fminimizer_x_lower(
s);
253 *b = gsl_min_fminimizer_x_upper(
s);
255 *status = gsl_min_test_interval(*a, *b, 0.00001, 0.0);
259 }
while (*status == GSL_CONTINUE && iter < max_iter);
260 gsl_min_fminimizer_free(
s);
270typedef void (*funcn)(
int *,
double *,
double *);
275double fn(
const gsl_vector *v,
void *params) {
281 p = (param_fn_t *)params;
283 x = (
double *)malloc(dim *
sizeof(
double));
285 for (
i = 0;
i < dim;
i++)
286 x[
i] = gsl_vector_get(v,
i);
287 p->func(&dim, x, &val);
293typedef void (*print_f_fn_ptr)(
const int *,
const int *,
const double *,
294 const double *,
const double *);
296int FC_FUNC_(oct_minimize_direct,
297 OCT_MINIMIZE_DIRECT)(
const int *method,
const int *dim,
298 double *point,
const double *step,
299 const double *toldr,
const int *
maxiter,
300 funcn f,
const print_f_fn_ptr write_info,
302 int iter = 0, status,
i;
305 const gsl_multimin_fminimizer_type *T = NULL;
306 gsl_multimin_fminimizer *
s = NULL;
308 gsl_multimin_function my_func;
315 my_func.params = (
void *)&p;
318 ss = gsl_vector_alloc(*dim);
319 gsl_vector_set_all(ss, *step);
322 x = gsl_vector_alloc(*dim);
323 for (
i = 0;
i < *dim;
i++)
324 gsl_vector_set(x,
i, point[
i]);
328 T = gsl_multimin_fminimizer_nmsimplex;
332 s = gsl_multimin_fminimizer_alloc(T, *dim);
333 gsl_multimin_fminimizer_set(
s, &my_func, x, ss);
337 status = gsl_multimin_fminimizer_iterate(
s);
342 *
minimum = gsl_multimin_fminimizer_minimum(
s);
343 for (
i = 0;
i < *dim;
i++)
344 point[
i] = gsl_vector_get(gsl_multimin_fminimizer_x(
s),
i);
346 size = gsl_multimin_fminimizer_size(
s);
347 status = gsl_multimin_test_size(size, *toldr);
349 write_info(&iter, dim,
minimum, &size, point);
351 }
while (status == GSL_CONTINUE && iter < *
maxiter);
353 if (status == GSL_CONTINUE)
358 gsl_multimin_fminimizer_free(
s);
double fabs(double __x) __attribute__((__nothrow__
real(real64) function func(r1, rn, n, a)
integer, parameter, public minimum
real(real64) function s()