- Timestamp:
- Jun 26, 2008, 7:46:14 PM (16 years ago)
- Location:
- trunk/packages/optimizer/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/optimizer/src/plugin_pgapack.c
r1067 r1070 291 291 case RP_OPTIMPARAM_NUMBER: 292 292 numPtr = (RpOptimParamNumber*)envPtr->paramList[n]; 293 dval = PGARandom01(ctx,0); 294 newParamPtr[n].value.dval = 293 if(numPtr->randdist == RAND_NUMBER_DIST_UNIFORM){ 294 dval = PGARandom01(ctx,0); 295 newParamPtr[n].value.dval = 295 296 (numPtr->max - numPtr->min)*dval + numPtr->min; 297 }else if(numPtr->randdist == RAND_NUMBER_DIST_GAUSSIAN){ 298 dval = PGARandomGaussian(ctx,numPtr->mean,numPtr->stddev); 299 if(numPtr->strictmax){ 300 if(dval>numPtr->max){ 301 dval = numPtr->max; 302 } 303 } 304 if(numPtr->strictmin){ 305 if(dval<numPtr->min){ 306 dval = numPtr->min; 307 } 308 } 309 newParamPtr[n].value.dval = dval; 310 }else{ 311 panic("Incorrect Random Number distribution option in PgapcreateString()"); 312 } 296 313 break; 297 314 case RP_OPTIMPARAM_STRING: … … 325 342 int count = 0; /* number of mutations */ 326 343 327 int n, ival ;344 int n, ival,tempmr; 328 345 RpOptimEnv *envPtr; 329 346 RpOptimParam *paramPtr; … … 335 352 336 353 for (n=0; n < envPtr->numParams; n++) { 337 if (PGARandomFlip(ctx, mr)) { 338 /* won the coin toss -- change this parameter */ 339 count++; 354 340 355 341 356 switch (paramPtr[n].type) { 342 357 case RP_OPTIMPARAM_NUMBER: 343 /* bump the value up/down a little, randomly */344 if (PGARandomFlip(ctx, 0.5)){345 paramPtr[n].value.dval += 0.1*paramPtr[n].value.dval;346 } else{347 paramPtr[n].value.dval -= 0.1*paramPtr[n].value.dval;358 numPtr = (RpOptimParamNumber*)envPtr->paramList[n]; 359 if(numPtr->mutnrate!=PARAM_NUM_UNSPEC_MUTN_RATE){ 360 tempmr = numPtr->mutnrate; 361 }else{ 362 tempmr = mr; 348 363 } 349 /* make sure the resulting value is still in bounds */ 350 numPtr = (RpOptimParamNumber*)envPtr->paramList[n]; 351 if (paramPtr[n].value.dval > numPtr->max) { 352 paramPtr[n].value.dval = numPtr->max; 353 } 354 if (paramPtr[n].value.dval < numPtr->min) { 355 paramPtr[n].value.dval = numPtr->min; 364 if (PGARandomFlip(ctx, tempmr)) { 365 /* won the coin toss -- change this parameter */ 366 count++; 367 368 /* bump the value up/down a little, randomly */ 369 if (PGARandomFlip(ctx, 0.5)) { 370 paramPtr[n].value.dval += 0.1*paramPtr[n].value.dval; 371 } else { 372 paramPtr[n].value.dval -= 0.1*paramPtr[n].value.dval; 373 } 374 /* make sure the resulting value is still in bounds */ 375 if(numPtr->randdist == RAND_NUMBER_DIST_UNIFORM || 376 (numPtr->randdist == RAND_NUMBER_DIST_GAUSSIAN && numPtr->strictmax)){ 377 if (paramPtr[n].value.dval > numPtr->max) { 378 paramPtr[n].value.dval = numPtr->max; 379 } 380 } 381 /*also make sure it obeys configured parameters when gaussian*/ 382 if(numPtr->randdist == RAND_NUMBER_DIST_UNIFORM || 383 (numPtr->randdist == RAND_NUMBER_DIST_GAUSSIAN && numPtr->strictmin)){ 384 if (paramPtr[n].value.dval < numPtr->min) { 385 paramPtr[n].value.dval = numPtr->min; 386 } 387 } 388 356 389 } 357 390 break; 391 358 392 359 393 case RP_OPTIMPARAM_STRING: 360 ival = paramPtr[n].value.sval.num; 361 if (PGARandomFlip(ctx, 0.5)) { 362 ival += 1; 363 } else { 364 ival -= 1; 365 } 366 strPtr = (RpOptimParamString*)envPtr->paramList[n]; 367 if (ival < 0) ival = 0; 368 if (ival >= strPtr->numValues) ival = strPtr->numValues-1; 369 paramPtr[n].value.sval.num = ival; 370 paramPtr[n].value.sval.str = strPtr->values[ival]; 371 break; 372 394 if (PGARandomFlip(ctx, mr)) { 395 /* won the coin toss -- change this parameter */ 396 count++; 397 398 ival = paramPtr[n].value.sval.num; 399 if (PGARandomFlip(ctx, 0.5)) { 400 ival += 1; 401 } else { 402 ival -= 1; 403 } 404 strPtr = (RpOptimParamString*)envPtr->paramList[n]; 405 if (ival < 0) ival = 0; 406 if (ival >= strPtr->numValues) ival = strPtr->numValues-1; 407 paramPtr[n].value.sval.num = ival; 408 paramPtr[n].value.sval.str = strPtr->values[ival]; 409 } 410 411 break; 412 373 413 default: 374 414 panic("bad parameter type in PgapMutation()"); 375 415 } 376 }416 377 417 } 378 418 return count; -
trunk/packages/optimizer/src/rp_optimizer.c
r899 r1070 114 114 numPtr->min = -DBL_MAX; 115 115 numPtr->max = DBL_MAX; 116 numPtr->mutnrate = PARAM_NUM_UNSPEC_MUTN_RATE; /*Unspecified by default, should result in global mutn rate being applied*/ 117 numPtr->randdist = RAND_NUMBER_DIST_GAUSSIAN ;/*Gaussian by default*/ 118 numPtr->stddev = 1; 119 numPtr->mean = 0; 120 numPtr->strictmax = 0; /*no strict truncation by default*/ 121 numPtr->strictmax = 0; /*no strict maximum truncation by default*/ 116 122 117 123 RpOptimAddParam(envPtr, (RpOptimParam*)numPtr); -
trunk/packages/optimizer/src/rp_optimizer.h
r1062 r1070 27 27 #define PGAPACK_RUNTIME_TABLE_DEFAULT_SIZE 5000 /*Approx Number of Samples in a run*/ 28 28 #define SINGLE_SAMPLE_DATA_BUFFER_DEFAULT_SIZE 5000 29 30 /*User defined Random Number Distributions*/ 31 #define RAND_NUMBER_DIST_GAUSSIAN 1 32 #define RAND_NUMBER_DIST_UNIFORM 2 33 34 /* Used to indicate unspecified mutation rate for a param, will result in global mutn rate being applied*/ 35 #define PARAM_NUM_UNSPEC_MUTN_RATE -1.0 29 36 30 37 /* … … 93 100 94 101 /* 95 * NUMBER PARAMETERS have additional min/max values asconstraints.102 * NUMBER PARAMETERS have additional constraints. 96 103 */ 97 104 typedef struct RpOptimParamNumber { … … 99 106 double min; /* optimization constraint: min value */ 100 107 double max; /* optimization constraint: max value */ 108 double mutnrate; /* independently sets mutation rate for each parameter*/ 109 int randdist; /* gaussian or uniform distribution*/ 110 int strictmin; /* whether a strict min is to be applied for gauss. rand. numbers*/ 111 int strictmax; /* whether a strict max is to be applied for gauss. rand. numbers*/ 112 double stddev; /* std deviaton for gaussian profile*/ 113 double mean; /* mean for gaussian profile*/ 101 114 } RpOptimParamNumber; 102 115 -
trunk/packages/optimizer/src/rp_optimizer_tcl.c
r1062 r1070 47 47 * ---------------------------------------------------------------------- 48 48 */ 49 50 RpCustomTclOptionGet RpOption_GetRandDist; 51 RpCustomTclOptionParse RpOption_ParseRandDist; 52 RpTclOptionType RpOption_RandDist = { 53 "pga_randdist", RpOption_ParseRandDist,RpOption_GetRandDist,NULL 54 }; 49 55 RpTclOption rpOptimNumberOpts[] = { 50 56 {"-min", RP_OPTION_DOUBLE, Rp_Offset(RpOptimParamNumber,min)}, 51 57 {"-max", RP_OPTION_DOUBLE, Rp_Offset(RpOptimParamNumber,max)}, 58 {"-mutnrate",RP_OPTION_DOUBLE, Rp_Offset(RpOptimParamNumber,mutnrate)}, 59 {"-randdist",&RpOption_RandDist,Rp_Offset(RpOptimParamNumber,randdist)}, 60 {"-strictmin",RP_OPTION_BOOLEAN,Rp_Offset(RpOptimParamNumber,strictmin)}, 61 {"-strictmax",RP_OPTION_BOOLEAN,Rp_Offset(RpOptimParamNumber,strictmax)}, 62 {"-stddev",RP_OPTION_DOUBLE,Rp_Offset(RpOptimParamNumber,stddev)}, 63 {"-mean",RP_OPTION_DOUBLE,Rp_Offset(RpOptimParamNumber,mean)}, 52 64 {NULL, NULL, 0} 53 65 }; … … 884 896 return result; 885 897 } 898 899 /* 900 * ====================================================================== 901 * OPTION: -randdist <=> RAND_NUMBER_DIST_GAUSSIAN / RAND_NUMBER_DIST_UNIFORM 902 * ====================================================================== 903 */ 904 int 905 RpOption_ParseRandDist(interp, valObj, cdata, offset) 906 Tcl_Interp *interp; /* interpreter handling this request */ 907 Tcl_Obj *valObj; /* set option to this new value */ 908 ClientData cdata; /* save in this data structure */ 909 int offset; /* save at this offset in cdata */ 910 { 911 int *ptr = (int*)(cdata+offset); 912 char *val = Tcl_GetStringFromObj(valObj, (int*)NULL); 913 if (strcmp(val,"gaussian") == 0) { 914 *ptr = RAND_NUMBER_DIST_GAUSSIAN; 915 } 916 else if (strcmp(val,"uniform") == 0) { 917 *ptr = RAND_NUMBER_DIST_UNIFORM; 918 } 919 else { 920 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), 921 "bad value \"", val, "\": should be gaussian or uniform", 922 (char*)NULL); 923 return TCL_ERROR; 924 } 925 return TCL_OK; 926 } 927 928 int 929 RpOption_GetRandDist(interp, cdata, offset) 930 Tcl_Interp *interp; /* interpreter handling this request */ 931 ClientData cdata; /* get from this data structure */ 932 int offset; /* get from this offset in cdata */ 933 { 934 int *ptr = (int*)(cdata+offset); 935 switch (*ptr) { 936 case RAND_NUMBER_DIST_GAUSSIAN: 937 Tcl_SetResult(interp, "gaussian", TCL_STATIC); 938 break; 939 case RAND_NUMBER_DIST_UNIFORM: 940 Tcl_SetResult(interp, "uniform", TCL_STATIC); 941 break; 942 default: 943 Tcl_SetResult(interp, "???", TCL_STATIC); 944 break; 945 } 946 return TCL_OK; 947 }
Note: See TracChangeset
for help on using the changeset viewer.