IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 28, 2004, 7:37:45 PM (22 years ago)
Author:
gusciora
Message:

...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/dataManip/psMinimize.c

    r1107 r1122  
    55#include <float.h>
    66#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>
    127
    138#include "psMemory.h"
     
    2318#include <math.h>
    2419
     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
    2526#define MAX_LMM_NUM_ITERATIONS 500
    2627typedef struct
    2728{
    2829    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.
    3031    psVector *restrict       initialGuess;
    3132    const psImage *restrict  domain;
     
    3334    const psVector *restrict  errors;
    3435    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);
    3738}
    3839psModelData;
     
    6667only call this routine, which then calls the user-supplied function.  The
    6768arguments are:
    68     x: These are the parameter which are to be varied by GSL in order to
    69        minimized chi2 over the data set.
    70     params:
    71     f:
    7269 
    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.
    7479 ******************************************************************************
    7580 *****************************************************************************/
    7681int gsl_function_f(const gsl_vector *x,
    7782                   void *params,
    78                    gsl_vector *f)
    79 {
    80     return 0;
     83                   gsl_vector *outData)
     84{
    8185    int i;    // Loop index variable.
    8286    int j;    // Loop index variable.
     
    8791    const psVector *restrict mask     = ((psModelData *) params)->paramMask;
    8892    psVector *restrict initialGuess = ((psModelData *)params)->initialGuess;
    89     float (*evalModel)(psVector *, psVector *) = ((psModelData *) params)->evalModel;
     93    float (*evalModel)(const psVector *, const psVector *) = ((psModelData *) params)->evalModel;
    9094    psVector *inputParameterList = NULL;
    9195    psVector *tmpVecPtr = NULL;
    92 
    9396
    9497    tmpVecPtr = psVectorAlloc(domain->numCols, PS_TYPE_F32);
     
    114117        }
    115118    }
     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    }
    116122
    117123    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++) {
    119126            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));
    123146    }
    124147
     
    138161    psVector *inputParameterList = NULL;
    139162    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;
    141164
    142165    size_t i;
     
    150173    // have those parameters removed.  Here will create a new parameter list
    151174    // with the masked parameters added (we expand initialGuess).
    152 
    153     psMemCheckCorruption(1);
    154     printf("Calling psVectorAlloc(%d)\n", initialGuess->n);
    155 
    156175    inputParameterList = psVectorAlloc(initialGuess->n, PS_TYPE_F32);
    157     printf("Called psVectorAlloc(%d)\n", initialGuess->n);
    158176
    159177    if (mask != NULL) {
     
    178196
    179197        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
    181208            gsl_matrix_set(J, i, j, (tmpf/errors->data.F32[i]));
    182209        }
    183210    }
     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
    184221
    185222    psFree(inputParameterList);
     
    193230                        gsl_matrix *J)
    194231{
    195     return 0;
    196232    gsl_function_f(x, params, f);
    197233    gsl_function_df(x, params, J);
     
    215251               float *chiSq)
    216252{
    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
    219256    int status;    // Return status for the GSL solver.
    220257    int i = 0;    // Loop index variable.
     
    225262    double *xInit = NULL;     // The initial guess at the parameters
    226263    // with masked parameters removed.
    227     const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;
     264    const gsl_multifit_fdfsolver_type *T;
    228265    // This tells GSL to use the Levenberg-
    229266    // Marquardt algorithm for chi2
    230267    // minimization.
    231268    gsl_multifit_fdfsolver *s; // GSL data structure.
    232 
    233269    psModelData inputData;
     270
    234271    inputData.n = numData;
    235     inputData.count = 0;
     272    inputData.paramCount = 0;
    236273    inputData.initialGuess = initialGuess;
    237274    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
    239281    if (paramMask != NULL) {
    240282        for (i=0;i<paramMask->n;i++) {
    241283            if (paramMask->data.U8[i] != 0) {
    242                 inputData.count++;
     284                inputData.paramCount++;
    243285            }
    244286        }
    245287    } else {
    246         inputData.count= initialGuess->n;
     288        inputData.paramCount= initialGuess->n;
    247289    }
    248290
     
    251293    // parameters are masked out.
    252294
    253     xInit = (double *) psAlloc(inputData.count * sizeof(double));
     295    xInit = (double *) psAlloc(inputData.paramCount * sizeof(double));
    254296    if (paramMask != NULL) {
    255297        j = 0;
    256298        for (i=0;i<initialGuess->n;i++) {
    257299            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];
    259302            }
    260303        }
    261304    } else {
    262305        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]);
    264308        }
    265309    }
     
    281325    f.fdf = &gsl_my_function_fdf;
    282326    f.n = numData;
    283     f.p = inputData.count;
     327    f.p = inputData.paramCount;
    284328    f.params = &inputData;
    285329
    286     printf("inputData.count is %d\n", inputData.count);
     330    printf("inputData.paramCount is %d\n", inputData.paramCount);
    287331    printf("numData is %d\n", numData);
    288332    // 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;
    290335
    291336    // 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
    296340    // Initialize the GSL minimizer to use function defined by the data
    297341    // structure "f" and x.vector as an initial guess for the parameters.
    298     psMemCheckCorruption(1);
    299     printf("HERE 05 (HMMM)\n");
     342
    300343    gsl_multifit_fdfsolver_set(s, &f, &x.vector);
     344
    301345    // Each iteration of the following loop will perform one step in an
    302346    // attempt to minimized chi-squared for the function.  The loop exits
    303347    // either when the change in parameters is small enough, or when the
    304348    // maximum number of iterations is reached.
    305     printf("HERE 06.0\n");
    306349    do {
    307350        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
    308359        // Perform an iteration of the GSL solver.
    309         printf("HERE 06\n");
     360
    310361        status = gsl_multifit_fdfsolver_iterate(s);
    311         printf("HERE 07\n");
    312362        // If there was a problem, abort.
    313363        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        }
    318367
    319368        // Test if the parameters changed by a small enough amount.
    320369        status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
    321         printf("HERE 09\n");
    322370    } while (status == GSL_CONTINUE && iter < MAX_LMM_NUM_ITERATIONS);
    323     printf("HERE 12\n");
    324371
    325372
Note: See TracChangeset for help on using the changeset viewer.