1 /* multimin/vector_bfgs2.c
3 * Copyright (C) 2007 Brian Gough
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
21 /* vector_bfgs2.c -- Fletcher's implementation of the BFGS method,
22 from R.Fletcher, "Practical Method's of Optimization", Second
23 Edition, ISBN 0471915475. Algorithms 2.6.2 and 2.6.4. */
25 /* Thanks to Alan Irwin irwin@beluga.phys.uvic.ca. for suggesting this
26 algorithm and providing sample fortran benchmarks */
29 #include <gsl/gsl_multimin.h>
30 #include <gsl/gsl_blas.h>
32 #include "linear_minimize.c"
33 #include "linear_wrapper.c"
42 double fp0; /* f'(0) for f(x-alpha*p) */
51 /* wrapper function */
53 /* minimization parameters */
64 vector_bfgs2_alloc (void *vstate, size_t n)
66 vector_bfgs2_state_t *state = (vector_bfgs2_state_t *) vstate;
68 state->p = gsl_vector_calloc (n);
72 GSL_ERROR ("failed to allocate space for p", GSL_ENOMEM);
75 state->x0 = gsl_vector_calloc (n);
79 gsl_vector_free (state->p);
80 GSL_ERROR ("failed to allocate space for g0", GSL_ENOMEM);
83 state->g0 = gsl_vector_calloc (n);
87 gsl_vector_free (state->x0);
88 gsl_vector_free (state->p);
89 GSL_ERROR ("failed to allocate space for g0", GSL_ENOMEM);
92 state->dx0 = gsl_vector_calloc (n);
96 gsl_vector_free (state->g0);
97 gsl_vector_free (state->x0);
98 gsl_vector_free (state->p);
99 GSL_ERROR ("failed to allocate space for g0", GSL_ENOMEM);
102 state->dg0 = gsl_vector_calloc (n);
106 gsl_vector_free (state->dx0);
107 gsl_vector_free (state->g0);
108 gsl_vector_free (state->x0);
109 gsl_vector_free (state->p);
110 GSL_ERROR ("failed to allocate space for g0", GSL_ENOMEM);
113 state->x_alpha = gsl_vector_calloc (n);
115 if (state->x_alpha == 0)
117 gsl_vector_free (state->dg0);
118 gsl_vector_free (state->dx0);
119 gsl_vector_free (state->g0);
120 gsl_vector_free (state->x0);
121 gsl_vector_free (state->p);
122 GSL_ERROR ("failed to allocate space for g0", GSL_ENOMEM);
125 state->g_alpha = gsl_vector_calloc (n);
127 if (state->g_alpha == 0)
129 gsl_vector_free (state->x_alpha);
130 gsl_vector_free (state->dg0);
131 gsl_vector_free (state->dx0);
132 gsl_vector_free (state->g0);
133 gsl_vector_free (state->x0);
134 gsl_vector_free (state->p);
135 GSL_ERROR ("failed to allocate space for g0", GSL_ENOMEM);
142 vector_bfgs2_set (void *vstate, gsl_multimin_function_fdf * fdf,
143 const gsl_vector * x, double *f, gsl_vector * gradient,
144 double step_size, double tol)
146 vector_bfgs2_state_t *state = (vector_bfgs2_state_t *) vstate;
149 state->step = step_size;
152 GSL_MULTIMIN_FN_EVAL_F_DF (fdf, x, f, gradient);
154 /* Use the gradient as the initial direction */
156 gsl_vector_memcpy (state->x0, x);
157 gsl_vector_memcpy (state->g0, gradient);
158 state->g0norm = gsl_blas_dnrm2 (state->g0);
160 gsl_vector_memcpy (state->p, gradient);
161 gsl_blas_dscal (-1 / state->g0norm, state->p);
162 state->pnorm = gsl_blas_dnrm2 (state->p); /* should be 1 */
163 state->fp0 = -state->g0norm;
165 /* Prepare the wrapper */
167 prepare_wrapper (&state->wrap, fdf,
168 state->x0, *f, state->g0,
169 state->p, state->x_alpha, state->g_alpha);
171 /* Prepare 1d minimisation parameters */
178 state->order = 3; /* use cubic interpolation where possible */
184 vector_bfgs2_free (void *vstate)
186 vector_bfgs2_state_t *state = (vector_bfgs2_state_t *) vstate;
188 gsl_vector_free (state->x_alpha);
189 gsl_vector_free (state->g_alpha);
190 gsl_vector_free (state->dg0);
191 gsl_vector_free (state->dx0);
192 gsl_vector_free (state->g0);
193 gsl_vector_free (state->x0);
194 gsl_vector_free (state->p);
198 vector_bfgs2_restart (void *vstate)
200 vector_bfgs2_state_t *state = (vector_bfgs2_state_t *) vstate;
207 vector_bfgs2_iterate (void *vstate, gsl_multimin_function_fdf * fdf,
208 gsl_vector * x, double *f,
209 gsl_vector * gradient, gsl_vector * dx)
211 vector_bfgs2_state_t *state = (vector_bfgs2_state_t *) vstate;
212 double alpha = 0.0, alpha1;
213 gsl_vector *x0 = state->x0;
214 gsl_vector *g0 = state->g0;
215 gsl_vector *p = state->p;
217 double g0norm = state->g0norm;
218 double pnorm = state->pnorm;
219 double delta_f = state->delta_f;
225 if (pnorm == 0.0 || g0norm == 0.0 || state->fp0 == 0)
227 gsl_vector_set_zero (dx);
233 double del = GSL_MAX_DBL (-delta_f, 10 * GSL_DBL_EPSILON * fabs(f0));
234 alpha1 = GSL_MIN_DBL (1.0, 2.0 * del / (-state->fp0));
238 alpha1 = fabs(state->step);
241 /* line minimisation, with cubic interpolation (order = 3) */
243 status = minimize (&state->wrap.fdf_linear, state->rho, state->sigma,
244 state->tau1, state->tau2, state->tau3, state->order,
247 if (status != GSL_SUCCESS)
252 update_position (&(state->wrap), alpha, x, f, gradient);
254 state->delta_f = *f - f0;
256 /* Choose a new direction for the next step */
259 /* This is the BFGS update: */
260 /* p' = g1 - A dx - B dg */
261 /* A = - (1+ dg.dg/dx.dg) B + dg.g/dx.dg */
264 gsl_vector *dx0 = state->dx0;
265 gsl_vector *dg0 = state->dg0;
267 double dxg, dgg, dxdg, dgnorm, A, B;
270 gsl_vector_memcpy (dx0, x);
271 gsl_blas_daxpy (-1.0, x0, dx0);
273 gsl_vector_memcpy (dx, dx0); /* keep a copy */
276 gsl_vector_memcpy (dg0, gradient);
277 gsl_blas_daxpy (-1.0, g0, dg0);
279 gsl_blas_ddot (dx0, gradient, &dxg);
280 gsl_blas_ddot (dg0, gradient, &dgg);
281 gsl_blas_ddot (dx0, dg0, &dxdg);
283 dgnorm = gsl_blas_dnrm2 (dg0);
288 A = -(1.0 + dgnorm * dgnorm / dxdg) * B + dgg / dxdg;
296 gsl_vector_memcpy (p, gradient);
297 gsl_blas_daxpy (-A, dx0, p);
298 gsl_blas_daxpy (-B, dg0, p);
301 gsl_vector_memcpy (g0, gradient);
302 gsl_vector_memcpy (x0, x);
303 state->g0norm = gsl_blas_dnrm2 (g0);
304 state->pnorm = gsl_blas_dnrm2 (p);
306 /* update direction and fp0 */
308 gsl_blas_ddot (p, gradient, &pg);
309 dir = (pg >= 0.0) ? -1.0 : +1.0;
310 gsl_blas_dscal (dir / state->pnorm, p);
311 state->pnorm = gsl_blas_dnrm2 (p);
312 gsl_blas_ddot (p, g0, &state->fp0);
314 change_direction (&state->wrap);
319 static const gsl_multimin_fdfminimizer_type vector_bfgs2_type = {
320 "vector_bfgs2", /* name */
321 sizeof (vector_bfgs2_state_t),
324 &vector_bfgs2_iterate,
325 &vector_bfgs2_restart,
329 const gsl_multimin_fdfminimizer_type
330 * gsl_multimin_fdfminimizer_vector_bfgs2 = &vector_bfgs2_type;