Changeset 898 for trunk/optimizer/src/rp_optimizer.c
- Timestamp:
- Feb 21, 2008, 6:43:57 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/optimizer/src/rp_optimizer.c
r897 r898 33 33 */ 34 34 RpOptimEnv* 35 RpOptimCreate(pluginData, cleanupP tr)35 RpOptimCreate(pluginData, cleanupProc) 36 36 ClientData pluginData; /* special data created for this env */ 37 RpOptimCleanup *cleanupP tr;/* routine to clean up pluginData */37 RpOptimCleanup *cleanupProc; /* routine to clean up pluginData */ 38 38 { 39 39 RpOptimEnv *envPtr; 40 40 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; 43 44 44 45 envPtr->numParams = 0; … … 106 107 numPtr->base.name = strdup(name); 107 108 numPtr->base.type = RP_OPTIMPARAM_NUMBER; 108 numPtr->base.value. num= 0.0;109 numPtr->base.value.dval = 0.0; 109 110 numPtr->min = -DBL_MAX; 110 111 numPtr->max = DBL_MAX; … … 134 135 strPtr->base.name = strdup(name); 135 136 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; 137 139 strPtr->values = NULL; 140 strPtr->numValues = 0; 138 141 139 142 RpOptimAddParam(envPtr, (RpOptimParam*)strPtr); … … 194 197 /* 195 198 * ---------------------------------------------------------------------- 196 * RpOptimPerform()197 *198 * Used to perform an optimization in the given context. Each run is199 * performed by calling an evaluation function represented by a200 * function pointer. If an optimum value is found within the limit201 * on the number of runs, then this procedure returns RP_OPTIM_SUCCESS.202 * Values for the optimum input parameters are returned through the203 * paramList within the context. If the optimization fails, this204 * function returns RP_OPTIM_FAILURE.205 * ----------------------------------------------------------------------206 */207 RpOptimStatus208 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 different228 * 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 * ----------------------------------------------------------------------277 199 * RpOptimDelete() 278 200 * … … 292 214 RpOptimCleanupParam(envPtr->paramList[n]); 293 215 } 294 if (envPtr->cleanupP tr) {295 (*envPtr->cleanupP tr)(envPtr->pluginData);216 if (envPtr->cleanupProc) { 217 (*envPtr->cleanupProc)(envPtr->pluginData); 296 218 } 297 219 free(envPtr->paramList);
Note: See TracChangeset
for help on using the changeset viewer.