Changeset 1202 for trunk/packages/optimizer/src
- Timestamp:
- Oct 29, 2008, 12:16:26 PM (16 years ago)
- Location:
- trunk/packages/optimizer/src
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/optimizer/src/pgapack/pgapack/include/pgapack.h
r1178 r1202 144 144 #define PGA_CROSSOVER_TWOPT 2 /* Two point crossover */ 145 145 #define PGA_CROSSOVER_UNIFORM 3 /* Uniform crossover */ 146 #define PGA_CROSSOVER_SBX 4 /* Simulated Binary crossover */ 146 147 147 148 /***************************************** … … 278 279 double FitnessCmaxValue; /* Cmax value used to convert minimizations */ 279 280 double restartAlleleProb;/* prob of changing an allele in a restart */ 281 double randreplprop; /* proportion of randomly generated individuals per generation post initialization*/ 280 282 int restart; /* whether to use the restart operator */ 281 283 int restartFreq; /* frequency with which to restart */ … … 715 717 int PGAGetRandomSeed(PGAContext *ctx); 716 718 void PGASetRandomSeed(PGAContext *ctx, int seed); 719 double PGAGetRandReplProp(PGAContext *ctx); 720 void PGASetRandReplProp(PGAContext *ctx, double randreplprop); 717 721 718 722 /***************************************** -
trunk/packages/optimizer/src/pgapack/pgapack/source/cross.c
r816 r1202 266 266 case PGA_CROSSOVER_TWOPT: 267 267 case PGA_CROSSOVER_UNIFORM: 268 case PGA_CROSSOVER_SBX: 268 269 ctx->ga.CrossoverType = crossover_type; 269 270 break; -
trunk/packages/optimizer/src/pgapack/pgapack/source/duplcate.c
r816 r1202 198 198 PGA_DOUBLE, (void *) &mr ); 199 199 200 while (( changed == PGA_FALSE ) && (mr <= 1.0)) { 201 if (ctx->fops.Mutation) { 202 fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1; 203 nflips = (*ctx->fops.Mutation)(&ctx, &fp, &pop, &mr); 204 } else { 205 nflips = (*ctx->cops.Mutation)( ctx, p, pop, mr ); 206 } 207 208 if ( nflips > 0 ) 209 changed = PGA_TRUE; 210 else 211 mr = 1.1*mr; 200 /**TODO: GTG (Clear)I dont see why we need to wait for iterations to mutate?****/ 201 /**TODO: can't we directly set mr = 1??**/ 202 while (changed == PGA_FALSE) { 203 if (ctx->fops.Mutation) { 204 fp = ((p == PGA_TEMP1) || (p == PGA_TEMP2)) ? p : p+1; 205 nflips = (*ctx->fops.Mutation)(&ctx, &fp, &pop, &mr); 206 } else { 207 nflips = (*ctx->cops.Mutation)( ctx, p, pop, mr ); 208 } 209 210 if ( nflips > 0 ) 211 changed = PGA_TRUE; 212 else 213 mr = ( mr>1 ? 1 :1.1*mr); 214 212 215 } 213 216 -
trunk/packages/optimizer/src/pgapack/pgapack/source/parallel.c
r986 r1202 127 127 if (rank == 0) { 128 128 Restarted = PGA_FALSE; 129 if ((ctx->ga.restart == PGA_TRUE) && 130 (ctx->ga.ItersOfSame % ctx->ga.restartFreq == 0)) { 131 ctx->ga.ItersOfSame++; 132 Restarted = PGA_TRUE; 133 PGARestart(ctx, PGA_OLDPOP, PGA_NEWPOP); 129 if (PGARestartCondition(ctx)) { 130 Restarted = PGA_TRUE; 131 PGARestart(ctx, PGA_OLDPOP, PGA_NEWPOP); 134 132 } else { 135 PGASelect(ctx, PGA_OLDPOP);136 CreateNewGeneration(ctx, PGA_OLDPOP, PGA_NEWPOP);133 PGASelect(ctx, PGA_OLDPOP); 134 CreateNewGeneration(ctx, PGA_OLDPOP, PGA_NEWPOP); 137 135 } 138 136 } -
trunk/packages/optimizer/src/pgapack/pgapack/source/pga.c
r986 r1202 200 200 { 201 201 int i, j, n, m1, m2; 202 int popsize, numreplace ;203 double pc ;202 int popsize, numreplace, randnumreplace; 203 double pc, randreplprop; 204 204 205 205 PGADebugEntered("PGARunMutationAndCrossover"); … … 207 207 popsize = PGAGetPopSize(ctx); 208 208 numreplace = PGAGetNumReplaceValue(ctx); 209 randreplprop = PGAGetRandReplProp(ctx); 210 randnumreplace = (int)randreplprop*popsize; 209 211 /*** first, copy n best strings (sorted by fitness) to new pop ***/ 210 212 PGASortPop( ctx, oldpop ); 211 n = popsize - numreplace ;213 n = popsize - numreplace - randnumreplace; 212 214 for ( i=0; i < n; i++ ) { 213 215 j = PGAGetSortedPopIndex( ctx, i ); … … 216 218 pc = PGAGetCrossoverProb(ctx); 217 219 /*** reproduce to create the rest of the new population ***/ 218 while ( n < popsize) {220 while ( n < (popsize- randnumreplace)) { 219 221 m1 = PGASelectNextIndex( ctx ); 220 222 m2 = PGASelectNextIndex( ctx ); … … 230 232 n++; 231 233 232 if ( n < popsize) {234 if ( n < (popsize-randnumreplace) ) { 233 235 /*** mutate and copy second string to new population ***/ 234 236 PGAMutate ( ctx, PGA_TEMP2, newpop); … … 242 244 PGACopyIndividual ( ctx, m1, oldpop, n, newpop ); 243 245 n++; 244 if ( n < ctx->ga.PopSize) {246 if ( n < (popsize-randnumreplace) ) { 245 247 PGACopyIndividual ( ctx, m2, oldpop, n, newpop ); 246 248 n++; … … 248 250 } 249 251 } 252 253 if(randnumreplace > 0){ 254 while(n < popsize){ 255 PGACreateIndividual(ctx,n,newpop,PGA_TRUE); 256 n++; 257 } 258 } 250 259 251 260 PGADebugExited("PGARunMutationAndCrossover"); … … 276 285 { 277 286 int i, j, n, m1, m2; 278 int popsize, numreplace ;279 double pc ;287 int popsize, numreplace, randnumreplace; 288 double pc, randreplprop; 280 289 281 290 PGADebugEntered("PGARunMutationOrCrossover"); … … 283 292 popsize = PGAGetPopSize(ctx); 284 293 numreplace = PGAGetNumReplaceValue(ctx); 294 randreplprop = PGAGetRandReplProp(ctx); 295 randnumreplace = (int)randreplprop*popsize; 285 296 /*** first, copy n best strings (sorted by fitness) to new pop ***/ 286 297 PGASortPop( ctx, oldpop ); 287 n = popsize - numreplace ;298 n = popsize - numreplace - randnumreplace ; 288 299 for ( i=0; i < n; i++ ) { 289 300 j = PGAGetSortedPopIndex( ctx, i ); … … 292 303 pc = PGAGetCrossoverProb(ctx); 293 304 /*** reproduce to create the rest of the new population ***/ 294 while ( n < popsize) {305 while ( n < (popsize-randnumreplace) ) { 295 306 m1 = PGASelectNextIndex( ctx ); 296 307 m2 = PGASelectNextIndex( ctx ); … … 305 316 n++; 306 317 307 if ( n < popsize)318 if ( n < (popsize-randnumreplace) ) 308 319 { 309 320 /*** copy second string to new population ***/ … … 323 334 n++; 324 335 325 if ( n < popsize) {336 if ( n < (popsize-randnumreplace) ) { 326 337 PGACopyIndividual(ctx, m2, oldpop, PGA_TEMP2, newpop); 327 338 PGAMutate ( ctx, PGA_TEMP2, newpop ); … … 333 344 } 334 345 } 346 347 if(randnumreplace > 0){ 348 while(n < popsize){ 349 PGACreateIndividual(ctx,n,newpop,PGA_TRUE); 350 n++; 351 } 352 } 335 353 336 354 PGADebugExited("PGARunMutationOrCrossover"); -
trunk/packages/optimizer/src/pgapack/pgapack/source/random.c
r816 r1202 403 403 PGADebugExited("PGASetRandomSeed"); 404 404 } 405 406 /***************************************************************************** 407 Category: Generation/Utility 408 409 Inputs: 410 ctx - context variable 411 randreplaceprop - proportion of individuals to be replaced per generation randomly, post initialization 412 413 Outputs: 414 None 415 416 Example: 417 PGAContext *ctx; 418 . 419 . 420 PGASetRandReplProp(ctx, 0.05); 421 ***************************************************************************/ 422 void PGASetRandReplProp(PGAContext *ctx, double randreplprop){ 423 PGADebugEntered("PGASetRandReplProp"); 424 PGAFailIfSetUp("PGASetRandReplProp"); 425 if(randreplprop < 0 || randreplprop > 1){ 426 PGAError ( ctx, "PGASetRandReplProp: Invalid value set:", 427 PGA_FATAL, PGA_DOUBLE, (void *) &randreplprop); 428 }else{ 429 ctx->ga.randreplprop = randreplprop; 430 } 431 PGADebugExited("PGASetRandReplProp"); 432 } 433 434 /***************************************************************************** 435 Category: Generation/Utility 436 437 Inputs: 438 ctx - context variable 439 440 Outputs: 441 randreplprop - proportion of randomly generated individuals per population, post initialization 442 443 Example: 444 PGAContext *ctx; 445 double randreplprop; 446 . 447 . 448 randreplprop = PGAGetRandReplProp(ctx); 449 ***************************************************************************/ 450 double PGAGetRandReplProp(PGAContext *ctx){ 451 PGADebugEntered("PGAGetRandReplProp"); 452 PGADebugExited("PGAGetRandReplProp"); 453 return ctx->ga.randreplprop; 454 } -
trunk/packages/optimizer/src/plugin_pgapack.c
r1166 r1202 28 28 double mutnValue; /*use this value while mutating*/ 29 29 double crossovrate; /*Crossover Rate <=> PGASetCrossoverProb();*/ 30 int crossovtype; /*Crossover Type <=> UNIFORM/SBX (SBX Defined from Deb and Kumar 1995)*/ 30 31 int allowdup; /*Allow duplicate strings in the population or not*/ 31 32 int mutnandcrossover;/*By default strings that do not undergo crossover undergo mutation, this option allows strings to crossover and be mutated*/ 33 double randReplProp; /*By default, random replacement is off, therefore randReplaceProp is zero by default, */ 34 /*a nonzero replacement value causes random generation of individuals in later generations*/ 32 35 } PgapackData; 33 36 … … 42 45 RpTclOptionType RpOption_Oper = { 43 46 "pga_operation", RpOption_ParseOper, RpOption_GetOper, NULL 47 }; 48 49 RpCustomTclOptionParse RpOption_ParseCrossovType; 50 RpCustomTclOptionGet RpOption_GetCrossovType; 51 RpTclOptionType RpOption_CrossovType = { 52 "pga_crossovtype", RpOption_ParseCrossovType,RpOption_GetCrossovType,NULL 44 53 }; 45 54 … … 67 76 {"-mutnValue",RP_OPTION_DOUBLE,Rp_Offset(PgapackData,mutnValue)}, 68 77 {"-crossovrate",RP_OPTION_DOUBLE,Rp_Offset(PgapackData,crossovrate)}, 78 {"-crossovtype",&RpOption_CrossovType,Rp_Offset(PgapackData,crossovtype)}, 69 79 {"-randnumseed",RP_OPTION_INT,Rp_Offset(PgapackData,randnumseed)}, 70 80 {"-stpcriteria",&RpOption_StpCriteria,Rp_Offset(PgapackData,stpcriteria)}, 71 81 {"-allowdup",RP_OPTION_BOOLEAN,Rp_Offset(PgapackData,allowdup)}, 72 82 {"-mutnandcrossover",RP_OPTION_BOOLEAN,Rp_Offset(PgapackData,mutnandcrossover)}, 83 {"-randReplProp",RP_OPTION_DOUBLE,Rp_Offset(PgapackData,randReplProp)}, 73 84 {NULL, NULL, 0} 74 85 }; … … 111 122 dataPtr->maxRuns = 10000; 112 123 dataPtr->popRepl = PGA_POPREPL_BEST; 124 dataPtr->crossovtype = PGA_CROSSOVER_UNIFORM; 113 125 dataPtr->popSize = 200; 114 126 dataPtr->numReplPerPop = (dataPtr->popSize)/10; /*10% replaced by default, change to whatever value you need*/ … … 120 132 dataPtr->allowdup = PGA_FALSE; /*Do not allow duplicate strings by default*/ 121 133 dataPtr->mutnandcrossover = PGA_FALSE;/*do not allow mutation and crossover to take place on the same string by default*/ 134 dataPtr->randReplProp = 0; /*0 randomly generated individuals after initialization, per generation*/ 122 135 return (ClientData)dataPtr; 123 136 } … … 162 175 PGASetCrossoverProb(ctx,dataPtr->crossovrate); 163 176 PGASetRandomSeed(ctx,dataPtr->randnumseed); 164 PGASetCrossoverType(ctx, PGA_CROSSOVER_UNIFORM);165 PGASetNoDuplicatesFlag(ctx, dataPtr->allowdup);177 PGASetCrossoverType(ctx, dataPtr->crossovtype); 178 PGASetNoDuplicatesFlag(ctx,!(dataPtr->allowdup)); 166 179 PGASetMutationAndCrossoverFlag(ctx,dataPtr->mutnandcrossover); 167 180 PGASetNumReplaceValue(ctx,dataPtr->numReplPerPop); 181 PGASetRandReplProp(ctx,dataPtr->randReplProp); 168 182 169 183 … … 461 475 RpOptimParam *parent1, *parent2, *child1, *child2; 462 476 double pu; 477 PgapackData *dataPtr; 478 /*declare variables for SBX*/ 479 double ui,beta,eta,powVal; 463 480 464 481 envPtr = PgapGetEnvForContext(ctx); … … 467 484 child1 = (RpOptimParam*)PGAGetIndividual(ctx, c1, pop2)->chrom; 468 485 child2 = (RpOptimParam*)PGAGetIndividual(ctx, c2, pop2)->chrom; 469 470 pu = PGAGetUniformCrossoverProb(ctx); 471 472 for (n=0; n < envPtr->numParams; n++) { 486 487 pu = PGAGetCrossoverProb(ctx); 488 dataPtr =(PgapackData*)envPtr->pluginData; 489 490 for (n=0; n < envPtr->numParams; n++) { 473 491 if (PGARandomFlip(ctx, pu)) { 492 /* crossover */ 493 switch(dataPtr->crossovtype){ 494 case PGA_CROSSOVER_UNIFORM: 495 memcpy(&child1[n], &parent2[n], sizeof(RpOptimParam)); 496 memcpy(&child2[n], &parent1[n], sizeof(RpOptimParam)); 497 break; 498 case PGA_CROSSOVER_SBX: 499 /*Implement a Simulated Binary Crossover for Real Encoding*/ 500 /*From Deb and Agrawal, 1995; Deb and Kumar, 1995)*/ 501 switch(parent1[n].type){ 502 case RP_OPTIMPARAM_NUMBER: 503 ui = PGARandom01(ctx,0); 504 eta = 1.5;/*We can adjust eta later....keeping it 1.5 for now*/ 505 powVal = 1/(eta+1); 506 if(ui<=0.5){ 507 beta = pow(2*ui,powVal); 508 }else{ 509 beta = pow(0.5/(1-ui),powVal); 510 } 511 child1[n].value.dval = 0.5*((1+beta)*(parent1[n].value.dval) + (1-beta)*(parent2[n].value.dval)); 512 child2[n].value.dval = 0.5*((1-beta)*(parent1[n].value.dval) + (1+beta)*(parent2[n].value.dval)); 513 break; 514 default: 515 panic("Bad Optim Param Type in PgapCrossover()"); 516 } 517 break; 518 default: 519 panic("bad parameter type in PgapCrossover()"); 520 } 521 } else { 474 522 /* child inherits from parent */ 475 523 memcpy(&child1[n], &parent1[n], sizeof(RpOptimParam)); 476 524 memcpy(&child2[n], &parent2[n], sizeof(RpOptimParam)); 477 } else {478 /* crossover */479 memcpy(&child1[n], &parent2[n], sizeof(RpOptimParam));480 memcpy(&child2[n], &parent1[n], sizeof(RpOptimParam));481 525 } 482 526 } 527 528 483 529 } 484 530 … … 675 721 return TCL_OK; 676 722 } 723 724 725 /* 726 * ====================================================================== 727 * OPTION: -crossovtype <=> PGA_CROSSOVER_UNIFORM / PGA_CROSSOVER_SBX 728 * ====================================================================== 729 */ 730 int 731 RpOption_ParseCrossovType(interp, valObj, cdata, offset) 732 Tcl_Interp *interp; /* interpreter handling this request */ 733 Tcl_Obj *valObj; /* set option to this new value */ 734 ClientData cdata; /* save in this data structure */ 735 int offset; /* save at this offset in cdata */ 736 { 737 int *ptr = (int*)(cdata+offset); 738 char *val = Tcl_GetStringFromObj(valObj, (int*)NULL); 739 if (strcmp(val,"uniform") == 0) { 740 *ptr = PGA_CROSSOVER_UNIFORM; 741 } 742 else if (strcmp(val,"sbx") == 0) { 743 *ptr = PGA_CROSSOVER_SBX; 744 } 745 else { 746 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), 747 "bad value \"", val, "\": should be either 'uniform' or 'sbx'", 748 (char*)NULL); 749 return TCL_ERROR; 750 } 751 return TCL_OK; 752 } 753 754 int 755 RpOption_GetCrossovType(interp, cdata, offset) 756 Tcl_Interp *interp; /* interpreter handling this request */ 757 ClientData cdata; /* get from this data structure */ 758 int offset; /* get from this offset in cdata */ 759 { 760 int *ptr = (int*)(cdata+offset); 761 switch (*ptr) { 762 case PGA_CROSSOVER_UNIFORM: 763 Tcl_SetResult(interp, "uniform", TCL_STATIC); 764 break; 765 case PGA_CROSSOVER_SBX: 766 Tcl_SetResult(interp, "sbx", TCL_STATIC); 767 break; 768 default: 769 Tcl_SetResult(interp, "???", TCL_STATIC); 770 break; 771 } 772 return TCL_OK; 773 } 774 775 776 677 777 678 778 /*
Note: See TracChangeset
for help on using the changeset viewer.