| 1 | /* multimin/conjugate_fr.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 | |
| 20 | /* conjugate_fr.c -- Conjugate gradient Fletcher-Reeve algorithm */ |
| 21 | |
| 22 | /* Modified by Brian Gough to use single iteration structure */ |
| 23 | |
| 24 | #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/ |
| 25 | #include "gsl_multimin.h"cxx . . $ `<file>/external/gsl/gsl_multimin.h`/ |
| 26 | #include "gsl_blas.h"cxx . . $ `<file>/external/gsl/gsl_blas.h`/ |
| 27 | |
| 28 | #include "gsl_multimin__directional_minimize.c"cxx . . $ `<file>/external/gsl/gsl_multimin__directional_minimize.c`/ |
| 29 | |
| 30 | typedef structcxx . . $ $anonymous_type_642e06b475bfe295_0# |
| 31 | { |
| 32 | int itercxx . . $ $anonymous_type_642e06b475bfe295_0#iter.; |
| 33 | double stepcxx . . $ $anonymous_type_642e06b475bfe295_0#step.; |
| 34 | double max_stepcxx . . $ $anonymous_type_642e06b475bfe295_0#max_step.; |
| 35 | double tolcxx . . $ $anonymous_type_642e06b475bfe295_0#tol.; |
| 36 | gsl_vectorcxx . . $ gsl_vector# *x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1.; |
| 37 | gsl_vectorcxx . . $ gsl_vector# *dx1cxx . . $ $anonymous_type_642e06b475bfe295_0#dx1.; |
| 38 | gsl_vectorcxx . . $ gsl_vector# *x2cxx . . $ $anonymous_type_642e06b475bfe295_0#x2.; |
| 39 | double pnormcxx . . $ $anonymous_type_642e06b475bfe295_0#pnorm.; |
| 40 | gsl_vectorcxx . . $ gsl_vector# *pcxx . . $ $anonymous_type_642e06b475bfe295_0#p.; |
| 41 | double g0normcxx . . $ $anonymous_type_642e06b475bfe295_0#g0norm.; |
| 42 | gsl_vectorcxx . . $ gsl_vector# *g0cxx . . $ $anonymous_type_642e06b475bfe295_0#g0.; |
| 43 | } |
| 44 | conjugate_fr_state_tcxx . . $ conjugate_fr_state_t#; |
| 45 | |
| 46 | static int |
| 47 | conjugate_fr_alloccxx . . $ conjugate_fr_alloc(c0a5f96701d3f89). (void *vstate, size_tcxx . . $ size_t# n) |
| 48 | { |
| 49 | conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *state = (conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *) vstate; |
| 50 | |
| 51 | state->x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1. = gsl_vector_calloccxx . . $ gsl_vector_calloc(71d9e095f27e8ea9). (n); |
| 52 | |
| 53 | if (state->x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1. == 0) |
| 54 | { |
| 55 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for x1", GSL_ENOMEM); |
| 56 | } |
| 57 | |
| 58 | state->dx1cxx . . $ $anonymous_type_642e06b475bfe295_0#dx1. = gsl_vector_calloccxx . . $ gsl_vector_calloc(71d9e095f27e8ea9). (n); |
| 59 | |
| 60 | if (state->dx1cxx . . $ $anonymous_type_642e06b475bfe295_0#dx1. == 0) |
| 61 | { |
| 62 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1.); |
| 63 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for dx1", GSL_ENOMEM); |
| 64 | } |
| 65 | |
| 66 | state->x2cxx . . $ $anonymous_type_642e06b475bfe295_0#x2. = gsl_vector_calloccxx . . $ gsl_vector_calloc(71d9e095f27e8ea9). (n); |
| 67 | |
| 68 | if (state->x2cxx . . $ $anonymous_type_642e06b475bfe295_0#x2. == 0) |
| 69 | { |
| 70 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->dx1cxx . . $ $anonymous_type_642e06b475bfe295_0#dx1.); |
| 71 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1.); |
| 72 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for x2", GSL_ENOMEM); |
| 73 | } |
| 74 | |
| 75 | state->pcxx . . $ $anonymous_type_642e06b475bfe295_0#p. = gsl_vector_calloccxx . . $ gsl_vector_calloc(71d9e095f27e8ea9). (n); |
| 76 | |
| 77 | if (state->pcxx . . $ $anonymous_type_642e06b475bfe295_0#p. == 0) |
| 78 | { |
| 79 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x2cxx . . $ $anonymous_type_642e06b475bfe295_0#x2.); |
| 80 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->dx1cxx . . $ $anonymous_type_642e06b475bfe295_0#dx1.); |
| 81 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1.); |
| 82 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for p", GSL_ENOMEM); |
| 83 | } |
| 84 | |
| 85 | state->g0cxx . . $ $anonymous_type_642e06b475bfe295_0#g0. = gsl_vector_calloccxx . . $ gsl_vector_calloc(71d9e095f27e8ea9). (n); |
| 86 | |
| 87 | if (state->g0cxx . . $ $anonymous_type_642e06b475bfe295_0#g0. == 0) |
| 88 | { |
| 89 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->pcxx . . $ $anonymous_type_642e06b475bfe295_0#p.); |
| 90 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x2cxx . . $ $anonymous_type_642e06b475bfe295_0#x2.); |
| 91 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->dx1cxx . . $ $anonymous_type_642e06b475bfe295_0#dx1.); |
| 92 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1.); |
| 93 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("failed to allocate space for g0", GSL_ENOMEM); |
| 94 | } |
| 95 | |
| 96 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 97 | } |
| 98 | |
| 99 | static int |
| 100 | conjugate_fr_setcxx . . $ conjugate_fr_set(aff3d705de048e1c). (void *vstate, gsl_multimin_function_fdfcxx . . $ gsl_multimin_function_fdf# * fdf, |
| 101 | const gsl_vectorcxx . . $ gsl_vector# * x, double *f, gsl_vectorcxx . . $ gsl_vector# * gradient, |
| 102 | double step_size, double tol) |
| 103 | { |
| 104 | conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *state = (conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *) vstate; |
| 105 | |
| 106 | state->itercxx . . $ $anonymous_type_642e06b475bfe295_0#iter. = 0; |
| 107 | state->stepcxx . . $ $anonymous_type_642e06b475bfe295_0#step. = step_size; |
| 108 | state->max_stepcxx . . $ $anonymous_type_642e06b475bfe295_0#max_step. = step_size; |
| 109 | state->tolcxx . . $ $anonymous_type_642e06b475bfe295_0#tol. = tol; |
| 110 | |
| 111 | GSL_MULTIMIN_FN_EVAL_F_DFcxx . . $ `external/gsl/gsl_multimin.h:73:9`! (fdf, x, f, gradient); |
| 112 | |
| 113 | /* Use the gradient as the initial direction */ |
| 114 | |
| 115 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (state->pcxx . . $ $anonymous_type_642e06b475bfe295_0#p., gradient); |
| 116 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (state->g0cxx . . $ $anonymous_type_642e06b475bfe295_0#g0., gradient); |
| 117 | |
| 118 | { |
| 119 | double gnorm = gsl_blas_dnrm2cxx . . $ gsl_blas_dnrm2(1b712e435b5359e9). (gradient); |
| 120 | |
| 121 | state->pnormcxx . . $ $anonymous_type_642e06b475bfe295_0#pnorm. = gnorm; |
| 122 | state->g0normcxx . . $ $anonymous_type_642e06b475bfe295_0#g0norm. = gnorm; |
| 123 | } |
| 124 | |
| 125 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | conjugate_fr_freecxx . . $ conjugate_fr_free(5d22bdacc48458e8). (void *vstate) |
| 130 | { |
| 131 | conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *state = (conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *) vstate; |
| 132 | |
| 133 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->g0cxx . . $ $anonymous_type_642e06b475bfe295_0#g0.); |
| 134 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->pcxx . . $ $anonymous_type_642e06b475bfe295_0#p.); |
| 135 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x2cxx . . $ $anonymous_type_642e06b475bfe295_0#x2.); |
| 136 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->dx1cxx . . $ $anonymous_type_642e06b475bfe295_0#dx1.); |
| 137 | gsl_vector_freecxx . . $ gsl_vector_free(5d65852ca0326259). (state->x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1.); |
| 138 | } |
| 139 | |
| 140 | static int |
| 141 | conjugate_fr_restartcxx . . $ conjugate_fr_restart(dedf1e9b1c614a07). (void *vstate) |
| 142 | { |
| 143 | conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *state = (conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *) vstate; |
| 144 | |
| 145 | state->itercxx . . $ $anonymous_type_642e06b475bfe295_0#iter. = 0; |
| 146 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 147 | } |
| 148 | |
| 149 | static int |
| 150 | conjugate_fr_iteratecxx . . $ conjugate_fr_iterate(26454239738d3d2d). (void *vstate, gsl_multimin_function_fdfcxx . . $ gsl_multimin_function_fdf# * fdf, |
| 151 | gsl_vectorcxx . . $ gsl_vector# * x, double *f, |
| 152 | gsl_vectorcxx . . $ gsl_vector# * gradient, gsl_vectorcxx . . $ gsl_vector# * dx) |
| 153 | { |
| 154 | conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *state = (conjugate_fr_state_tcxx . . $ conjugate_fr_state_t# *) vstate; |
| 155 | |
| 156 | gsl_vectorcxx . . $ gsl_vector# *x1 = state->x1cxx . . $ $anonymous_type_642e06b475bfe295_0#x1.; |
| 157 | gsl_vectorcxx . . $ gsl_vector# *dx1 = state->dx1cxx . . $ $anonymous_type_642e06b475bfe295_0#dx1.; |
| 158 | gsl_vectorcxx . . $ gsl_vector# *x2 = state->x2cxx . . $ $anonymous_type_642e06b475bfe295_0#x2.; |
| 159 | gsl_vectorcxx . . $ gsl_vector# *p = state->pcxx . . $ $anonymous_type_642e06b475bfe295_0#p.; |
| 160 | gsl_vectorcxx . . $ gsl_vector# *g0 = state->g0cxx . . $ $anonymous_type_642e06b475bfe295_0#g0.; |
| 161 | |
| 162 | double pnorm = state->pnormcxx . . $ $anonymous_type_642e06b475bfe295_0#pnorm.; |
| 163 | double g0norm = state->g0normcxx . . $ $anonymous_type_642e06b475bfe295_0#g0norm.; |
| 164 | |
| 165 | double fa = *f, fb, fc; |
| 166 | double dir; |
| 167 | double stepa = 0.0, stepb, stepc = state->stepcxx . . $ $anonymous_type_642e06b475bfe295_0#step., tol = state->tolcxx . . $ $anonymous_type_642e06b475bfe295_0#tol.; |
| 168 | |
| 169 | double g1norm; |
| 170 | double pg; |
| 171 | |
| 172 | if (pnorm == 0.0 || g0norm == 0.0) |
| 173 | { |
| 174 | gsl_vector_set_zerocxx . . $ gsl_vector_set_zero(5d65852ca0326259). (dx); |
| 175 | return GSL_ENOPROGcxx . . $ GSL_ENOPROG.; |
| 176 | } |
| 177 | |
| 178 | /* Determine which direction is downhill, +p or -p */ |
| 179 | |
| 180 | gsl_blas_ddotcxx . . $ gsl_blas_ddot(1ee5503a249d268f). (p, gradient, &pg); |
| 181 | |
| 182 | dir = (pg >= 0.0) ? +1.0 : -1.0; |
| 183 | |
| 184 | /* Compute new trial point at x_c= x - step * p, where p is the |
| 185 | current direction */ |
| 186 | |
| 187 | take_stepcxx . . $ take_step(e6b9d70ab3ecefb7). (x, p, stepc, dir / pnorm, x1, dx); |
| 188 | |
| 189 | /* Evaluate function and gradient at new point xc */ |
| 190 | |
| 191 | fc = GSL_MULTIMIN_FN_EVAL_Fcxx . . $ `external/gsl/gsl_multimin.h:71:9`! (fdf, x1); |
| 192 | |
| 193 | if (fc < fa) |
| 194 | { |
| 195 | /* Success, reduced the function value */ |
| 196 | state->stepcxx . . $ $anonymous_type_642e06b475bfe295_0#step. = stepc * 2.0; |
| 197 | *f = fc; |
| 198 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (x, x1); |
| 199 | GSL_MULTIMIN_FN_EVAL_DFcxx . . $ `external/gsl/gsl_multimin.h:72:9`! (fdf, x1, gradient); |
| 200 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 201 | } |
| 202 | |
| 203 | #ifdef DEBUG |
| 204 | printf ("got stepc = %g fc = %g\n", stepc, fc); |
| 205 | #endif |
| 206 | |
| 207 | /* Do a line minimisation in the region (xa,fa) (xc,fc) to find an |
| 208 | intermediate (xb,fb) satisifying fa > fb < fc. Choose an initial |
| 209 | xb based on parabolic interpolation */ |
| 210 | |
| 211 | intermediate_pointcxx . . $ intermediate_point(1451a91833ff4007). (fdf, x, p, dir / pnorm, pg, |
| 212 | stepa, stepc, fa, fc, x1, dx1, gradient, &stepb, &fb); |
| 213 | |
| 214 | if (stepb == 0.0) |
| 215 | { |
| 216 | return GSL_ENOPROGcxx . . $ GSL_ENOPROG.; |
| 217 | } |
| 218 | |
| 219 | minimizecxx . . $ minimize(888440c29ed85de7). (fdf, x, p, dir / pnorm, |
| 220 | stepa, stepb, stepc, fa, fb, fc, tol, |
| 221 | x1, dx1, x2, dx, gradient, &(state->stepcxx . . $ $anonymous_type_642e06b475bfe295_0#step.), f, &g1norm); |
| 222 | |
| 223 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (x, x2); |
| 224 | |
| 225 | /* Choose a new conjugate direction for the next step */ |
| 226 | |
| 227 | state->itercxx . . $ $anonymous_type_642e06b475bfe295_0#iter. = (state->itercxx . . $ $anonymous_type_642e06b475bfe295_0#iter. + 1) % x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.; |
| 228 | |
| 229 | if (state->itercxx . . $ $anonymous_type_642e06b475bfe295_0#iter. == 0) |
| 230 | { |
| 231 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (p, gradient); |
| 232 | state->pnormcxx . . $ $anonymous_type_642e06b475bfe295_0#pnorm. = g1norm; |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | /* p' = g1 - beta * p */ |
| 237 | |
| 238 | double beta = -powcxx . . $ pow(d8e7e0733fa41c87). (g1norm / g0norm, 2.0); |
| 239 | gsl_blas_dscalcxx . . $ gsl_blas_dscal(f8d4363c21ae79a0). (-beta, p); |
| 240 | gsl_blas_daxpycxx . . $ gsl_blas_daxpy(b9b02baa1ffb1e94). (1.0, gradient, p); |
| 241 | state->pnormcxx . . $ $anonymous_type_642e06b475bfe295_0#pnorm. = gsl_blas_dnrm2cxx . . $ gsl_blas_dnrm2(1b712e435b5359e9). (p); |
| 242 | } |
| 243 | |
| 244 | state->g0normcxx . . $ $anonymous_type_642e06b475bfe295_0#g0norm. = g1norm; |
| 245 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (g0, gradient); |
| 246 | |
| 247 | #ifdef DEBUG |
| 248 | printf ("updated conjugate directions\n"); |
| 249 | printf ("p: "); |
| 250 | gsl_vector_fprintf (stdout, p, "%g"); |
| 251 | printf ("g: "); |
| 252 | gsl_vector_fprintf (stdout, gradient, "%g"); |
| 253 | #endif |
| 254 | |
| 255 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | static const gsl_multimin_fdfminimizer_typecxx . . $ gsl_multimin_fdfminimizer_type# conjugate_fr_typecxx . . $ conjugate_fr_type. = { |
| 261 | "conjugate_fr", /* name */ |
| 262 | sizeof (conjugate_fr_state_tcxx . . $ conjugate_fr_state_t#), |
| 263 | &conjugate_fr_alloccxx . . $ conjugate_fr_alloc(c0a5f96701d3f89)., |
| 264 | &conjugate_fr_setcxx . . $ conjugate_fr_set(aff3d705de048e1c)., |
| 265 | &conjugate_fr_iteratecxx . . $ conjugate_fr_iterate(26454239738d3d2d)., |
| 266 | &conjugate_fr_restartcxx . . $ conjugate_fr_restart(dedf1e9b1c614a07)., |
| 267 | &conjugate_fr_freecxx . . $ conjugate_fr_free(5d22bdacc48458e8). |
| 268 | }; |
| 269 | |
| 270 | const gsl_multimin_fdfminimizer_typecxx . . $ gsl_multimin_fdfminimizer_type# |
| 271 | * gsl_multimin_fdfminimizer_conjugate_frcxx . . $ gsl_multimin_fdfminimizer_conjugate_fr. = &conjugate_fr_typecxx . . $ conjugate_fr_type.; |