gsl_multimin__steepest_descent.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* multimin/steepest_descent.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. /* steepest_descent.c -- the steepest descent 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_types.h"
  24. #include "gsl_blas.h"
  25. typedef struct
  26. {
  27. double step;
  28. double max_step;
  29. double tol;
  30. gsl_vector *x1;
  31. gsl_vector *g1;
  32. }
  33. steepest_descent_state_t;
  34. static int
  35. steepest_descent_alloc (void *vstate, size_t n)
  36. {
  37. steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
  38. state->x1 = gsl_vector_alloc (n);
  39. if (state->x1 == NULL)
  40. {
  41. GSL_ERROR ("failed to allocate space for x1", GSL_ENOMEM);
  42. }
  43. state->g1 = gsl_vector_alloc (n);
  44. if (state->g1 == NULL)
  45. {
  46. gsl_vector_free (state->x1);
  47. GSL_ERROR ("failed to allocate space for g1", GSL_ENOMEM);
  48. }
  49. return GSL_SUCCESS;
  50. }
  51. static int
  52. steepest_descent_set (void *vstate, gsl_multimin_function_fdf * fdf,
  53. const gsl_vector * x, double *f,
  54. gsl_vector * gradient, double step_size, double tol)
  55. {
  56. steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
  57. GSL_MULTIMIN_FN_EVAL_F_DF (fdf, x, f, gradient);
  58. state->step = step_size;
  59. state->max_step = step_size;
  60. state->tol = tol;
  61. return GSL_SUCCESS;
  62. }
  63. static void
  64. steepest_descent_free (void *vstate)
  65. {
  66. steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
  67. gsl_vector_free (state->x1);
  68. gsl_vector_free (state->g1);
  69. }
  70. static int
  71. steepest_descent_restart (void *vstate)
  72. {
  73. steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
  74. state->step = state->max_step;
  75. return GSL_SUCCESS;
  76. }
  77. static int
  78. steepest_descent_iterate (void *vstate, gsl_multimin_function_fdf * fdf,
  79. gsl_vector * x, double *f,
  80. gsl_vector * gradient, gsl_vector * dx)
  81. {
  82. steepest_descent_state_t *state = (steepest_descent_state_t *) vstate;
  83. gsl_vector *x1 = state->x1;
  84. gsl_vector *g1 = state->g1;
  85. double f0 = *f;
  86. double f1;
  87. double step = state->step, tol = state->tol;
  88. int failed = 0;
  89. /* compute new trial point at x1= x - step * dir, where dir is the
  90. normalized gradient */
  91. double gnorm = gsl_blas_dnrm2 (gradient);
  92. if (gnorm == 0.0)
  93. {
  94. gsl_vector_set_zero (dx);
  95. return GSL_ENOPROG;
  96. }
  97. trial:
  98. gsl_vector_set_zero (dx);
  99. gsl_blas_daxpy (-step / gnorm, gradient, dx);
  100. gsl_vector_memcpy (x1, x);
  101. gsl_blas_daxpy (1.0, dx, x1);
  102. /* evaluate function and gradient at new point x1 */
  103. GSL_MULTIMIN_FN_EVAL_F_DF (fdf, x1, &f1, g1);
  104. if (f1 > f0)
  105. {
  106. /* downhill step failed, reduce step-size and try again */
  107. failed = 1;
  108. step *= tol;
  109. goto trial;
  110. }
  111. if (failed)
  112. step *= tol;
  113. else
  114. step *= 2.0;
  115. state->step = step;
  116. gsl_vector_memcpy (x, x1);
  117. gsl_vector_memcpy (gradient, g1);
  118. *f = f1;
  119. return GSL_SUCCESS;
  120. }
  121. static const gsl_multimin_fdfminimizer_type steepest_descent_type =
  122. { "steepest_descent", /* name */
  123. sizeof (steepest_descent_state_t),
  124. &steepest_descent_alloc,
  125. &steepest_descent_set,
  126. &steepest_descent_iterate,
  127. &steepest_descent_restart,
  128. &steepest_descent_free
  129. };
  130. const gsl_multimin_fdfminimizer_type
  131. * gsl_multimin_fdfminimizer_steepest_descent = &steepest_descent_type;