3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Fabrice Rossi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* Modified by Tuomo Keskitalo to add Nelder Mead Simplex test suite */
24 #include <gsl/gsl_test.h>
25 #include <gsl/gsl_blas.h>
26 #include <gsl/gsl_multimin.h>
27 #include <gsl/gsl_ieee_utils.h>
29 #include "test_funcs.h"
31 unsigned int fcount, gcount;
34 test_fdf(const char * desc, gsl_multimin_function_fdf *f,
35 initpt_function initpt, const gsl_multimin_fdfminimizer_type *T);
38 test_f(const char * desc, gsl_multimin_function *f, initpt_function initpt);
43 const gsl_multimin_fdfminimizer_type *fdfminimizers[6];
44 const gsl_multimin_fdfminimizer_type ** T;
46 gsl_ieee_env_setup ();
48 fdfminimizers[0] = gsl_multimin_fdfminimizer_steepest_descent;
49 fdfminimizers[1] = gsl_multimin_fdfminimizer_conjugate_pr;
50 fdfminimizers[2] = gsl_multimin_fdfminimizer_conjugate_fr;
51 fdfminimizers[3] = gsl_multimin_fdfminimizer_vector_bfgs;
52 fdfminimizers[4] = gsl_multimin_fdfminimizer_vector_bfgs2;
59 test_fdf("Roth", &roth, roth_initpt,*T);
60 test_fdf("Wood", &wood, wood_initpt,*T);
61 test_fdf("Rosenbrock", &rosenbrock, rosenbrock_initpt,*T);
65 test_f("Roth", &roth_fmin, roth_initpt);
66 test_f("Wood", &wood_fmin, wood_initpt);
67 test_f("Rosenbrock", &rosenbrock_fmin, rosenbrock_initpt);
73 test_fdf("NRoth", &Nroth, roth_initpt,*T);
74 test_fdf("NWood", &Nwood, wood_initpt,*T);
75 test_fdf("NRosenbrock", &Nrosenbrock, rosenbrock_initpt,*T);
79 exit (gsl_test_summary());
83 test_fdf(const char * desc,
84 gsl_multimin_function_fdf *f,
85 initpt_function initpt,
86 const gsl_multimin_fdfminimizer_type *T)
92 gsl_vector *x = gsl_vector_alloc (f->n);
94 gsl_multimin_fdfminimizer *s;
95 fcount = 0; gcount = 0;
99 step_size = 0.1 * gsl_blas_dnrm2 (x);
101 s = gsl_multimin_fdfminimizer_alloc(T, f->n);
103 gsl_multimin_fdfminimizer_set (s, f, x, step_size, 0.1);
106 printf("x "); gsl_vector_fprintf (stdout, s->x, "%g");
107 printf("g "); gsl_vector_fprintf (stdout, s->gradient, "%g");
113 status = gsl_multimin_fdfminimizer_iterate(s);
116 printf("%i: \n",iter);
117 printf("x "); gsl_vector_fprintf (stdout, s->x, "%g");
118 printf("g "); gsl_vector_fprintf (stdout, s->gradient, "%g");
119 printf("f(x) %g\n",s->f);
120 printf("dx %g\n",gsl_blas_dnrm2(s->dx));
124 status = gsl_multimin_test_gradient(s->gradient,1e-3);
126 while (iter < 5000 && status == GSL_CONTINUE);
128 status |= (fabs(s->f) > 1e-5);
130 gsl_test(status, "%s, on %s: %i iters (fn+g=%d+%d), f(x)=%g",
131 gsl_multimin_fdfminimizer_name(s),desc, iter, fcount, gcount, s->f);
133 gsl_multimin_fdfminimizer_free(s);
140 test_f(const char * desc, gsl_multimin_function *f, initpt_function initpt)
142 /* currently this function tests only nmsimplex */
146 const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
148 gsl_vector *x = gsl_vector_alloc (f->n);
150 gsl_vector *step_size = gsl_vector_alloc (f->n);
152 gsl_multimin_fminimizer *s;
154 fcount = 0; gcount = 0;
157 for (i = 0; i < f->n; i++)
158 gsl_vector_set (step_size, i, 1);
160 s = gsl_multimin_fminimizer_alloc(T, f->n);
162 gsl_multimin_fminimizer_set (s, f, x, step_size);
165 printf("x "); gsl_vector_fprintf (stdout, s->x, "%g");
171 status = gsl_multimin_fminimizer_iterate(s);
174 printf("%i: \n",iter);
175 printf("x "); gsl_vector_fprintf (stdout, s->x, "%g");
176 printf("f(x) %g\n", gsl_multimin_fminimizer_minimum (s));
177 printf("size: %g\n", gsl_multimin_fminimizer_size (s));
181 status = gsl_multimin_test_size (gsl_multimin_fminimizer_size (s),
184 while (iter < 5000 && status == GSL_CONTINUE);
186 status |= (fabs(s->fval) > 1e-5);
188 gsl_test(status, "%s, on %s: %d iter (fn=%d), f(x)=%g",
189 gsl_multimin_fminimizer_name(s),desc, iter, fcount, s->fval);
191 gsl_multimin_fminimizer_free(s);
193 gsl_vector_free(step_size);