gsl_multimin__conjugate_pr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* multimin/conjugate_pr.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Fabrice Rossi
  4. *
  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.
  9. *
  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.
  14. *
  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.
  18. */
  19. /* conjugate_pr.c -- Conjugate gradient Polak-Ribiere algorithm */
  20. /* Modified by Brian Gough to use single iteration structure */
  21. #include "gsl__config.h"
  22. #include "gsl_multimin.h"
  23. #include "gsl_blas.h"
  24. #include "gsl_multimin__directional_minimize.c"
  25. typedef struct
  26. {
  27. int iter;
  28. double step;
  29. double max_step;
  30. double tol;
  31. gsl_vector *x1;
  32. gsl_vector *dx1;
  33. gsl_vector *x2;
  34. double pnorm;
  35. gsl_vector *p;
  36. double g0norm;
  37. gsl_vector *g0;
  38. }
  39. conjugate_pr_state_t;
  40. static int
  41. conjugate_pr_alloc (void *vstate, size_t n)
  42. {
  43. conjugate_pr_state_t *state = (conjugate_pr_state_t *) vstate;
  44. state->x1 = gsl_vector_calloc (n);
  45. if (state->x1 == 0)
  46. {
  47. GSL_ERROR ("failed to allocate space for x1", GSL_ENOMEM);
  48. }
  49. state->dx1 = gsl_vector_calloc (n);
  50. if (state->dx1 == 0)
  51. {
  52. gsl_vector_free (state->x1);
  53. GSL_ERROR ("failed to allocate space for dx1", GSL_ENOMEM);
  54. }
  55. state->x2 = gsl_vector_calloc (n);
  56. if (state->x2 == 0)
  57. {
  58. gsl_vector_free (state->dx1);
  59. gsl_vector_free (state->x1);
  60. GSL_ERROR ("failed to allocate space for x2", GSL_ENOMEM);
  61. }
  62. state->p = gsl_vector_calloc (n);
  63. if (state->p == 0)
  64. {
  65. gsl_vector_free (state->x2);
  66. gsl_vector_free (state->dx1);
  67. gsl_vector_free (state->x1);
  68. GSL_ERROR ("failed to allocate space for p", GSL_ENOMEM);
  69. }
  70. state->g0 = gsl_vector_calloc (n);
  71. if (state->g0 == 0)
  72. {
  73. gsl_vector_free (state->p);
  74. gsl_vector_free (state->x2);
  75. gsl_vector_free (state->dx1);
  76. gsl_vector_free (state->x1);
  77. GSL_ERROR ("failed to allocate space for g0", GSL_ENOMEM);
  78. }
  79. return GSL_SUCCESS;
  80. }
  81. static int
  82. conjugate_pr_set (void *vstate, gsl_multimin_function_fdf * fdf,
  83. const gsl_vector * x, double *f, gsl_vector * gradient,
  84. double step_size, double tol)
  85. {
  86. conjugate_pr_state_t *state = (conjugate_pr_state_t *) vstate;
  87. state->iter = 0;
  88. state->step = step_size;
  89. state->max_step = step_size;
  90. state->tol = tol;
  91. GSL_MULTIMIN_FN_EVAL_F_DF (fdf, x, f, gradient);
  92. /* Use the gradient as the initial direction */
  93. gsl_vector_memcpy (state->p, gradient);
  94. gsl_vector_memcpy (state->g0, gradient);
  95. {
  96. double gnorm = gsl_blas_dnrm2 (gradient);
  97. state->pnorm = gnorm;
  98. state->g0norm = gnorm;
  99. }
  100. return GSL_SUCCESS;
  101. }
  102. static void
  103. conjugate_pr_free (void *vstate)
  104. {
  105. conjugate_pr_state_t *state = (conjugate_pr_state_t *) vstate;
  106. gsl_vector_free (state->g0);
  107. gsl_vector_free (state->p);
  108. gsl_vector_free (state->x2);
  109. gsl_vector_free (state->dx1);
  110. gsl_vector_free (state->x1);
  111. }
  112. static int
  113. conjugate_pr_restart (void *vstate)
  114. {
  115. conjugate_pr_state_t *state = (conjugate_pr_state_t *) vstate;
  116. state->iter = 0;
  117. return GSL_SUCCESS;
  118. }
  119. static int
  120. conjugate_pr_iterate (void *vstate, gsl_multimin_function_fdf * fdf,
  121. gsl_vector * x, double *f,
  122. gsl_vector * gradient, gsl_vector * dx)
  123. {
  124. conjugate_pr_state_t *state = (conjugate_pr_state_t *) vstate;
  125. gsl_vector *x1 = state->x1;
  126. gsl_vector *dx1 = state->dx1;
  127. gsl_vector *x2 = state->x2;
  128. gsl_vector *p = state->p;
  129. gsl_vector *g0 = state->g0;
  130. double pnorm = state->pnorm;
  131. double g0norm = state->g0norm;
  132. double fa = *f, fb, fc;
  133. double dir;
  134. double stepa = 0.0, stepb, stepc = state->step, tol = state->tol;
  135. double g1norm;
  136. double pg;
  137. if (pnorm == 0.0 || g0norm == 0.0)
  138. {
  139. gsl_vector_set_zero (dx);
  140. return GSL_ENOPROG;
  141. }
  142. /* Determine which direction is downhill, +p or -p */
  143. gsl_blas_ddot (p, gradient, &pg);
  144. dir = (pg >= 0.0) ? +1.0 : -1.0;
  145. /* Compute new trial point at x_c= x - step * p, where p is the
  146. current direction */
  147. take_step (x, p, stepc, dir / pnorm, x1, dx);
  148. /* Evaluate function and gradient at new point xc */
  149. fc = GSL_MULTIMIN_FN_EVAL_F (fdf, x1);
  150. if (fc < fa)
  151. {
  152. /* Success, reduced the function value */
  153. state->step = stepc * 2.0;
  154. *f = fc;
  155. gsl_vector_memcpy (x, x1);
  156. GSL_MULTIMIN_FN_EVAL_DF (fdf, x1, gradient);
  157. return GSL_SUCCESS;
  158. }
  159. #ifdef DEBUG
  160. printf ("got stepc = %g fc = %g\n", stepc, fc);
  161. #endif
  162. /* Do a line minimisation in the region (xa,fa) (xc,fc) to find an
  163. intermediate (xb,fb) satisifying fa > fb < fc. Choose an initial
  164. xb based on parabolic interpolation */
  165. intermediate_point (fdf, x, p, dir / pnorm, pg,
  166. stepa, stepc, fa, fc, x1, dx1, gradient, &stepb, &fb);
  167. if (stepb == 0.0)
  168. {
  169. return GSL_ENOPROG;
  170. }
  171. minimize (fdf, x, p, dir / pnorm,
  172. stepa, stepb, stepc, fa, fb, fc, tol,
  173. x1, dx1, x2, dx, gradient, &(state->step), f, &g1norm);
  174. gsl_vector_memcpy (x, x2);
  175. /* Choose a new conjugate direction for the next step */
  176. state->iter = (state->iter + 1) % x->size;
  177. if (state->iter == 0)
  178. {
  179. gsl_vector_memcpy (p, gradient);
  180. state->pnorm = g1norm;
  181. }
  182. else
  183. {
  184. /* p' = g1 - beta * p */
  185. double g0g1, beta;
  186. gsl_blas_daxpy (-1.0, gradient, g0); /* g0' = g0 - g1 */
  187. gsl_blas_ddot(g0, gradient, &g0g1); /* g1g0 = (g0-g1).g1 */
  188. beta = g0g1 / (g0norm*g0norm); /* beta = -((g1 - g0).g1)/(g0.g0) */
  189. gsl_blas_dscal (-beta, p);
  190. gsl_blas_daxpy (1.0, gradient, p);
  191. state->pnorm = gsl_blas_dnrm2 (p);
  192. }
  193. state->g0norm = g1norm;
  194. gsl_vector_memcpy (g0, gradient);
  195. #ifdef DEBUG
  196. printf ("updated conjugate directions\n");
  197. printf ("p: ");
  198. gsl_vector_fprintf (stdout, p, "%g");
  199. printf ("g: ");
  200. gsl_vector_fprintf (stdout, gradient, "%g");
  201. #endif
  202. return GSL_SUCCESS;
  203. }
  204. static const gsl_multimin_fdfminimizer_type conjugate_pr_type = {
  205. "conjugate_pr", /* name */
  206. sizeof (conjugate_pr_state_t),
  207. &conjugate_pr_alloc,
  208. &conjugate_pr_set,
  209. &conjugate_pr_iterate,
  210. &conjugate_pr_restart,
  211. &conjugate_pr_free
  212. };
  213. const gsl_multimin_fdfminimizer_type
  214. * gsl_multimin_fdfminimizer_conjugate_pr = &conjugate_pr_type;