Changeset 1122 for trunk/psLib/src/dataManip/psMinimize.c
- Timestamp:
- Jun 28, 2004, 7:37:45 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/dataManip/psMinimize.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/dataManip/psMinimize.c
r1107 r1122 5 5 #include <float.h> 6 6 #include <math.h> 7 #include <gsl/gsl_rng.h>8 #include <gsl/gsl_randist.h>9 #include <gsl/gsl_vector.h>10 #include <gsl/gsl_blas.h>11 #include <gsl/gsl_multifit_nlin.h>12 7 13 8 #include "psMemory.h" … … 23 18 #include <math.h> 24 19 20 #include <gsl/gsl_multifit_nlin.h> 21 #include <gsl/gsl_rng.h> 22 #include <gsl/gsl_randist.h> 23 #include <gsl/gsl_vector.h> 24 #include <gsl/gsl_blas.h> 25 25 26 #define MAX_LMM_NUM_ITERATIONS 500 26 27 typedef struct 27 28 { 28 29 size_t n; // Number of data points points in domain. 29 int count; // Number of non-masked parameters.30 int paramCount; // Number of non-masked parameters. 30 31 psVector *restrict initialGuess; 31 32 const psImage *restrict domain; … … 33 34 const psVector *restrict errors; 34 35 const psVector *restrict paramMask; 35 float (*evalModel) ( psVector *,psVector *);36 float (*d_evalModel) ( psVector *,psVector *, int);36 float (*evalModel) (const psVector *, const psVector *); 37 float (*d_evalModel) (const psVector *, const psVector *, int); 37 38 } 38 39 psModelData; … … 66 67 only call this routine, which then calls the user-supplied function. The 67 68 arguments are: 68 x: These are the parameter which are to be varied by GSL in order to69 minimized chi2 over the data set.70 params:71 f:72 69 73 70 x: These are the parameters which are to be varied by GSL in order to 71 minimized chi2 over the data set. 72 params: this data structure contains the input values over which the 73 function will be evaluated, the expected value of the function at 74 those points, the amount of error tolerable at those points, a mask 75 vector which specifies which parameters to the function are to be 76 constant, and an initial guess at the parameters. 77 outData: The function is evaluated at each point, then subtract the 78 expected value and divide by the error. 74 79 ****************************************************************************** 75 80 *****************************************************************************/ 76 81 int gsl_function_f(const gsl_vector *x, 77 82 void *params, 78 gsl_vector *f) 79 { 80 return 0; 83 gsl_vector *outData) 84 { 81 85 int i; // Loop index variable. 82 86 int j; // Loop index variable. … … 87 91 const psVector *restrict mask = ((psModelData *) params)->paramMask; 88 92 psVector *restrict initialGuess = ((psModelData *)params)->initialGuess; 89 float (*evalModel)( psVector *,psVector *) = ((psModelData *) params)->evalModel;93 float (*evalModel)(const psVector *, const psVector *) = ((psModelData *) params)->evalModel; 90 94 psVector *inputParameterList = NULL; 91 95 psVector *tmpVecPtr = NULL; 92 93 96 94 97 tmpVecPtr = psVectorAlloc(domain->numCols, PS_TYPE_F32); … … 114 117 } 115 118 } 119 for (i=0;i<inputParameterList->n;i++) { 120 printf("(gsl_f(): inputParameterList->data.F32[%d] is %.1f\n", i, inputParameterList->data.F32[i]); 121 } 116 122 117 123 for (i=0;i<domain->numRows;i++) { 118 for (j=0;j<tmpVecPtr->n;j++) { 124 // printf("Data item %d is ( ", i); 125 for (j=0;j<domain->numCols;j++) { 119 126 tmpVecPtr->data.F32[j] = domain->data.F32[i][j]; 120 } 121 tmpf = evalModel(inputParameterList, tmpVecPtr); 122 gsl_vector_set(f, i, (tmpf - data->data.F32[i])/errors->data.F32[i]); 127 // printf("%.1f ", tmpVecPtr->data.F32[j]); 128 } 129 tmpf = evalModel(tmpVecPtr, inputParameterList); 130 // printf(" ). Output is %.1f\n", tmpf); 131 132 gsl_vector_set(outData, i, (tmpf - data->data.F32[i])/ 133 errors->data.F32[i]); 134 //printf("--------- MYFUNC((%.1f) %.1f %.1f %.1f) is [%.1f] ---------\n", 135 //tmpVecPtr->data.F32[0], 136 //inputParameterList->data.F32[0], 137 //inputParameterList->data.F32[1], 138 //inputParameterList->data.F32[2], 139 //(tmpf - data->data.F32[i])/errors->data.F32[i]); 140 141 } 142 143 for (i=0;i<domain->numRows;i++) { 144 printf("gsl_vector_set(outData, %d, %.1f)\n", i, 145 gsl_vector_get(outData, i)); 123 146 } 124 147 … … 138 161 psVector *inputParameterList = NULL; 139 162 psVector *tmpVecPtr = NULL; 140 float (*d_evalModel)( psVector *,psVector *, int) = ((psModelData *) params)->d_evalModel;163 float (*d_evalModel)(const psVector *, const psVector *, int) = ((psModelData *) params)->d_evalModel; 141 164 142 165 size_t i; … … 150 173 // have those parameters removed. Here will create a new parameter list 151 174 // with the masked parameters added (we expand initialGuess). 152 153 psMemCheckCorruption(1);154 printf("Calling psVectorAlloc(%d)\n", initialGuess->n);155 156 175 inputParameterList = psVectorAlloc(initialGuess->n, PS_TYPE_F32); 157 printf("Called psVectorAlloc(%d)\n", initialGuess->n);158 176 159 177 if (mask != NULL) { … … 178 196 179 197 for (j=0;j<inputParameterList->n;j++) { 180 tmpf = d_evalModel(inputParameterList, tmpVecPtr, j); 198 tmpf = d_evalModel(tmpVecPtr, inputParameterList, j); 199 200 //printf("--------- MYFUNCDERIV((%.1f) %.1f %.1f %.1f, %d) is [%.1f] ---------\n", 201 //tmpVecPtr->data.F32[0], 202 //inputParameterList->data.F32[0], 203 //inputParameterList->data.F32[1], 204 //inputParameterList->data.F32[2], 205 //j, 206 //tmpf/errors->data.F32[i]); 207 181 208 gsl_matrix_set(J, i, j, (tmpf/errors->data.F32[i])); 182 209 } 183 210 } 211 212 213 for (i=0;i<domain->numRows;i++) { 214 for (j=0;j<inputParameterList->n;j++) { 215 printf("gsl_matrix_set(J, %d, %d, %.1f)\n", i, j, 216 gsl_matrix_get(J, i, j)); 217 } 218 } 219 220 184 221 185 222 psFree(inputParameterList); … … 193 230 gsl_matrix *J) 194 231 { 195 return 0;196 232 gsl_function_f(x, params, f); 197 233 gsl_function_df(x, params, J); … … 215 251 float *chiSq) 216 252 { 217 218 int numData = domain->numCols; // Number of data points 253 printf("Calling psMinimizeChi2()\n"); 254 255 int numData = domain->numRows; // Number of data points 219 256 int status; // Return status for the GSL solver. 220 257 int i = 0; // Loop index variable. … … 225 262 double *xInit = NULL; // The initial guess at the parameters 226 263 // with masked parameters removed. 227 const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;264 const gsl_multifit_fdfsolver_type *T; 228 265 // This tells GSL to use the Levenberg- 229 266 // Marquardt algorithm for chi2 230 267 // minimization. 231 268 gsl_multifit_fdfsolver *s; // GSL data structure. 232 233 269 psModelData inputData; 270 234 271 inputData.n = numData; 235 inputData. count = 0;272 inputData.paramCount = 0; 236 273 inputData.initialGuess = initialGuess; 237 274 inputData.domain = domain; 238 inputData.count = 0; 275 inputData.data = data; 276 inputData.errors = errors; 277 inputData.paramMask = paramMask; 278 inputData.evalModel = evalModel; 279 inputData.d_evalModel = DevalModel; 280 239 281 if (paramMask != NULL) { 240 282 for (i=0;i<paramMask->n;i++) { 241 283 if (paramMask->data.U8[i] != 0) { 242 inputData. count++;284 inputData.paramCount++; 243 285 } 244 286 } 245 287 } else { 246 inputData. count= initialGuess->n;288 inputData.paramCount= initialGuess->n; 247 289 } 248 290 … … 251 293 // parameters are masked out. 252 294 253 xInit = (double *) psAlloc(inputData. count * sizeof(double));295 xInit = (double *) psAlloc(inputData.paramCount * sizeof(double)); 254 296 if (paramMask != NULL) { 255 297 j = 0; 256 298 for (i=0;i<initialGuess->n;i++) { 257 299 if (paramMask->data.U8[i] == 0) { 258 xInit[j++] = (double) initialGuess->data.F32[i]; 300 printf("xInit[%d] is %f (masked loop)\n", j, initialGuess->data.F32[i]); 301 xInit[j++] = initialGuess->data.F32[i]; 259 302 } 260 303 } 261 304 } else { 262 305 for (i=0;i<initialGuess->n;i++) { 263 xInit[i] = (double) initialGuess->data.F32[i]; 306 xInit[i] = initialGuess->data.F32[i]; 307 printf("xInit[%d] is %f\n", i, initialGuess->data.F32[i]); 264 308 } 265 309 } … … 281 325 f.fdf = &gsl_my_function_fdf; 282 326 f.n = numData; 283 f.p = inputData. count;327 f.p = inputData.paramCount; 284 328 f.params = &inputData; 285 329 286 printf("inputData. count is %d\n", inputData.count);330 printf("inputData.paramCount is %d\n", inputData.paramCount); 287 331 printf("numData is %d\n", numData); 288 332 // Creates the vector for x which GSL uses. Must deallocate. 289 gsl_vector_view x = gsl_vector_view_array(xInit, inputData.count); 333 gsl_vector_view x = gsl_vector_view_array(xInit, inputData.paramCount); 334 T = gsl_multifit_fdfsolver_lmsder; 290 335 291 336 // Create an instance of the GSL solver that we will be iterating on. 292 // It will have numData data points and inputData.count parameters. 293 s = gsl_multifit_fdfsolver_alloc(T, numData, inputData.count); 294 295 printf("HERE 04 (HMMM)\n"); 337 // It will have numData data points and inputData.paramCount parameters. 338 s = gsl_multifit_fdfsolver_alloc(T, numData, inputData.paramCount); 339 296 340 // Initialize the GSL minimizer to use function defined by the data 297 341 // structure "f" and x.vector as an initial guess for the parameters. 298 psMemCheckCorruption(1); 299 printf("HERE 05 (HMMM)\n"); 342 300 343 gsl_multifit_fdfsolver_set(s, &f, &x.vector); 344 301 345 // Each iteration of the following loop will perform one step in an 302 346 // attempt to minimized chi-squared for the function. The loop exits 303 347 // either when the change in parameters is small enough, or when the 304 348 // maximum number of iterations is reached. 305 printf("HERE 06.0\n");306 349 do { 307 350 iter++; 351 printf("##################################################\n"); 352 printf("##################################################\n"); 353 printf("##################################################\n"); 354 printf("################## Iteration %d ##################\n", iter); 355 printf("##################################################\n"); 356 printf("##################################################\n"); 357 printf("##################################################\n"); 358 308 359 // Perform an iteration of the GSL solver. 309 printf("HERE 06\n"); 360 310 361 status = gsl_multifit_fdfsolver_iterate(s); 311 printf("HERE 07\n");312 362 // If there was a problem, abort. 313 363 if (status) { 314 exit(1); 315 psAbort(__func__, "gsl_multifit_fdfsolver_iterate()\n"); 316 } 317 printf("HERE 08\n"); 364 printf("ABORT: status is %d\n", status); 365 // psAbort(__func__, "gsl_multifit_fdfsolver_iterate()\n"); 366 } 318 367 319 368 // Test if the parameters changed by a small enough amount. 320 369 status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4); 321 printf("HERE 09\n");322 370 } while (status == GSL_CONTINUE && iter < MAX_LMM_NUM_ITERATIONS); 323 printf("HERE 12\n");324 371 325 372
Note:
See TracChangeset
for help on using the changeset viewer.
