Ignore:
Timestamp:
Feb 21, 2008, 6:43:57 PM (17 years ago)
Author:
mmc
Message:

Optimization part is getting better. Fleshed out the plug-in for
PGApack, and integrated a first cut that includes the data handling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/optimizer/src/rp_optimizer.c

    r897 r898  
    3333 */
    3434RpOptimEnv*
    35 RpOptimCreate(pluginData, cleanupPtr)
     35RpOptimCreate(pluginData, cleanupProc)
    3636    ClientData pluginData;        /* special data created for this env */
    37     RpOptimCleanup *cleanupPtr;   /* routine to clean up pluginData */
     37    RpOptimCleanup *cleanupProc;  /* routine to clean up pluginData */
    3838{
    3939    RpOptimEnv *envPtr;
    4040    envPtr = (RpOptimEnv*)malloc(sizeof(RpOptimEnv));
    41     envPtr->pluginData = pluginData;
    42     envPtr->cleanupPtr = cleanupPtr;
     41    envPtr->pluginData  = pluginData;
     42    envPtr->cleanupProc = cleanupProc;
     43    envPtr->toolData    = NULL;
    4344
    4445    envPtr->numParams = 0;
     
    106107    numPtr->base.name = strdup(name);
    107108    numPtr->base.type = RP_OPTIMPARAM_NUMBER;
    108     numPtr->base.value.num = 0.0;
     109    numPtr->base.value.dval = 0.0;
    109110    numPtr->min = -DBL_MAX;
    110111    numPtr->max = DBL_MAX;
     
    134135    strPtr->base.name = strdup(name);
    135136    strPtr->base.type = RP_OPTIMPARAM_STRING;
    136     strPtr->base.value.str = NULL;
     137    strPtr->base.value.sval.num = -1;
     138    strPtr->base.value.sval.str = NULL;
    137139    strPtr->values = NULL;
     140    strPtr->numValues = 0;
    138141
    139142    RpOptimAddParam(envPtr, (RpOptimParam*)strPtr);
     
    194197/*
    195198 * ----------------------------------------------------------------------
    196  * RpOptimPerform()
    197  *
    198  * Used to perform an optimization in the given context.  Each run is
    199  * performed by calling an evaluation function represented by a
    200  * function pointer.  If an optimum value is found within the limit
    201  * on the number of runs, then this procedure returns RP_OPTIM_SUCCESS.
    202  * Values for the optimum input parameters are returned through the
    203  * paramList within the context.  If the optimization fails, this
    204  * function returns RP_OPTIM_FAILURE.
    205  * ----------------------------------------------------------------------
    206  */
    207 RpOptimStatus
    208 RpOptimPerform(envPtr, evalFuncPtr, maxRuns)
    209     RpOptimEnv *envPtr;               /* context for this optimization */
    210     RpOptimEvaluator *evalFuncPtr;    /* function called to handle run */
    211     int maxRuns;                      /* limit on number of runs,
    212                                        * or 0 for no limit */
    213 {
    214     RpOptimStatus status = RP_OPTIM_UNKNOWN;
    215 
    216     int n, nruns, ival, nvals;
    217     double dval, fitness;
    218     RpOptimParamNumber *numPtr;
    219     RpOptimParamString *strPtr;
    220     RpOptimStatus runStatus;
    221 
    222     if (envPtr->numParams == 0) {    /* no input parameters? */
    223         return RP_OPTIM_FAILURE;     /* then we can't optimize! */
    224     }
    225 
    226     /*
    227      * Call the evaluation function a number of times with different
    228      * values and perform the optimization.
    229      */
    230     nruns = 0;
    231     while (status == RP_OPTIM_UNKNOWN) {
    232         /*
    233          * Pick random values for all inputs.
    234          */
    235         for (n=0; n < envPtr->numParams; n++) {
    236             switch (envPtr->paramList[n]->type) {
    237                 case RP_OPTIMPARAM_NUMBER:
    238                     numPtr = (RpOptimParamNumber*)envPtr->paramList[n];
    239                     dval = drand48();
    240                     envPtr->paramList[n]->value.num =
    241                         (numPtr->max - numPtr->min)*dval + numPtr->min;
    242                     break;
    243                 case RP_OPTIMPARAM_STRING:
    244                     strPtr = (RpOptimParamString*)envPtr->paramList[n];
    245                     for (nvals=0; strPtr->values[nvals]; nvals++)
    246                         ;  /* count values */
    247                     ival = (int)floor(drand48() * nvals);
    248                     envPtr->paramList[n]->value.str = strPtr->values[ival];
    249                     break;
    250             }
    251         }
    252 
    253         /*
    254          * Call the evaluation function to get the fitness value.
    255          */
    256         runStatus = (*evalFuncPtr)(envPtr->paramList, envPtr->numParams,
    257             &fitness);
    258 
    259         if (runStatus == RP_OPTIM_SUCCESS) {
    260             /*
    261              * Is the fitness function any better?
    262              * Change the input values here based on fitness.
    263              *  ...
    264              */
    265         }
    266 
    267         if (++nruns >= maxRuns && maxRuns > 0) {
    268             status = RP_OPTIM_FAILURE;  /* reached the limit of runs */
    269         }
    270     }
    271 
    272     return status;
    273 }
    274 
    275 /*
    276  * ----------------------------------------------------------------------
    277199 * RpOptimDelete()
    278200 *
     
    292214        RpOptimCleanupParam(envPtr->paramList[n]);
    293215    }
    294     if (envPtr->cleanupPtr) {
    295         (*envPtr->cleanupPtr)(envPtr->pluginData);
     216    if (envPtr->cleanupProc) {
     217        (*envPtr->cleanupProc)(envPtr->pluginData);
    296218    }
    297219    free(envPtr->paramList);
Note: See TracChangeset for help on using the changeset viewer.