Ignore:
Timestamp:
Oct 29, 2008, 12:16:26 PM (16 years ago)
Author:
liveletlive
Message:

Changes made to include the following items:
1) Hard restart
2) SBX
3) Random population replacement proportion
4) Definitions and macros for the above functions in pgapack.h

Location:
trunk/packages/optimizer/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/optimizer/src/pgapack/pgapack/include/pgapack.h

    r1178 r1202  
    144144#define PGA_CROSSOVER_TWOPT     2    /* Two point crossover                */
    145145#define PGA_CROSSOVER_UNIFORM   3    /* Uniform   crossover                */
     146#define PGA_CROSSOVER_SBX               4    /* Simulated Binary crossover                 */
    146147
    147148/*****************************************
     
    278279    double FitnessCmaxValue; /* Cmax value used to convert minimizations  */
    279280    double restartAlleleProb;/* prob of changing an allele in a restart   */
     281    double randreplprop;     /* proportion of randomly generated individuals per generation post initialization*/
    280282    int restart;             /* whether to use the restart operator       */
    281283    int restartFreq;         /* frequency with which to restart           */
     
    715717int PGAGetRandomSeed(PGAContext *ctx);
    716718void PGASetRandomSeed(PGAContext *ctx, int seed);
     719double PGAGetRandReplProp(PGAContext *ctx);
     720void PGASetRandReplProp(PGAContext *ctx, double randreplprop);
    717721
    718722/*****************************************
  • trunk/packages/optimizer/src/pgapack/pgapack/source/cross.c

    r816 r1202  
    266266        case PGA_CROSSOVER_TWOPT:
    267267        case PGA_CROSSOVER_UNIFORM:
     268        case PGA_CROSSOVER_SBX:
    268269            ctx->ga.CrossoverType = crossover_type;
    269270            break;
  • trunk/packages/optimizer/src/pgapack/pgapack/source/duplcate.c

    r816 r1202  
    198198                   PGA_DOUBLE, (void *) &mr );
    199199
    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                   
    212215    }
    213216
  • trunk/packages/optimizer/src/pgapack/pgapack/source/parallel.c

    r986 r1202  
    127127        if (rank == 0) {
    128128            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);
    134132            } 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);
    137135            }
    138136        }
  • trunk/packages/optimizer/src/pgapack/pgapack/source/pga.c

    r986 r1202  
    200200{
    201201    int i, j, n, m1, m2;
    202     int popsize, numreplace;
    203     double pc;
     202    int popsize, numreplace, randnumreplace;
     203    double pc, randreplprop;
    204204
    205205    PGADebugEntered("PGARunMutationAndCrossover");
     
    207207    popsize = PGAGetPopSize(ctx);
    208208    numreplace = PGAGetNumReplaceValue(ctx);
     209    randreplprop = PGAGetRandReplProp(ctx);
     210    randnumreplace = (int)randreplprop*popsize;
    209211    /*** first, copy n best strings (sorted by fitness) to new pop ***/
    210212    PGASortPop( ctx, oldpop );
    211     n = popsize - numreplace;
     213    n = popsize - numreplace - randnumreplace;
    212214    for ( i=0; i < n; i++ ) {
    213215        j = PGAGetSortedPopIndex( ctx, i );
     
    216218    pc = PGAGetCrossoverProb(ctx);
    217219    /*** reproduce to create the rest of the new population ***/
    218     while ( n < popsize) {
     220    while ( n < (popsize- randnumreplace)) {
    219221        m1 = PGASelectNextIndex( ctx );
    220222        m2 = PGASelectNextIndex( ctx );
     
    230232              n++;
    231233
    232               if ( n < popsize ) {
     234              if ( n < (popsize-randnumreplace) ) {
    233235              /*** mutate and copy second string to new population ***/
    234236              PGAMutate ( ctx, PGA_TEMP2, newpop);
     
    242244            PGACopyIndividual ( ctx, m1, oldpop, n, newpop );
    243245            n++;
    244             if ( n < ctx->ga.PopSize ) {
     246            if ( n < (popsize-randnumreplace) ) {
    245247                PGACopyIndividual ( ctx, m2, oldpop, n, newpop );
    246248                n++;
     
    248250       }
    249251    }
     252   
     253    if(randnumreplace > 0){
     254            while(n < popsize){
     255                PGACreateIndividual(ctx,n,newpop,PGA_TRUE);
     256                n++;
     257            }
     258    }
    250259
    251260    PGADebugExited("PGARunMutationAndCrossover");
     
    276285{
    277286    int i, j, n, m1, m2;
    278     int popsize, numreplace;
    279     double pc;
     287    int popsize, numreplace, randnumreplace;
     288    double pc, randreplprop;
    280289
    281290    PGADebugEntered("PGARunMutationOrCrossover");
     
    283292    popsize = PGAGetPopSize(ctx);
    284293    numreplace = PGAGetNumReplaceValue(ctx);
     294    randreplprop = PGAGetRandReplProp(ctx);
     295    randnumreplace = (int)randreplprop*popsize;
    285296    /*** first, copy n best strings (sorted by fitness) to new pop ***/
    286297    PGASortPop( ctx, oldpop );
    287     n = popsize - numreplace;
     298    n = popsize - numreplace - randnumreplace ;
    288299    for ( i=0; i < n; i++ ) {
    289300        j = PGAGetSortedPopIndex( ctx, i );
     
    292303    pc = PGAGetCrossoverProb(ctx);
    293304    /*** reproduce to create the rest of the new population ***/
    294     while ( n < popsize ) {
     305    while ( n < (popsize-randnumreplace) ) {
    295306        m1 = PGASelectNextIndex( ctx );
    296307        m2 = PGASelectNextIndex( ctx );
     
    305316            n++;
    306317
    307             if ( n < popsize )
     318            if ( n < (popsize-randnumreplace) )
    308319            {
    309320                 /*** copy second string to new population ***/
     
    323334             n++;
    324335
    325              if ( n < popsize ) {
     336             if ( n < (popsize-randnumreplace) ) {
    326337                  PGACopyIndividual(ctx, m2, oldpop, PGA_TEMP2, newpop);
    327338                  PGAMutate ( ctx, PGA_TEMP2, newpop );
     
    333344        }
    334345    }
     346   
     347    if(randnumreplace > 0){
     348            while(n < popsize){
     349                PGACreateIndividual(ctx,n,newpop,PGA_TRUE);
     350                n++;
     351            }
     352    }
    335353
    336354    PGADebugExited("PGARunMutationOrCrossover");
  • trunk/packages/optimizer/src/pgapack/pgapack/source/random.c

    r816 r1202  
    403403    PGADebugExited("PGASetRandomSeed");
    404404}
     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 ***************************************************************************/
     422void 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 ***************************************************************************/
     450double PGAGetRandReplProp(PGAContext *ctx){
     451        PGADebugEntered("PGAGetRandReplProp");
     452        PGADebugExited("PGAGetRandReplProp");
     453        return ctx->ga.randreplprop;
     454}
  • trunk/packages/optimizer/src/plugin_pgapack.c

    r1166 r1202  
    2828    double mutnValue;     /*use this value while mutating*/
    2929    double crossovrate;  /*Crossover Rate <=> PGASetCrossoverProb();*/
     30    int crossovtype;    /*Crossover Type <=> UNIFORM/SBX (SBX Defined from Deb and Kumar 1995)*/
    3031    int allowdup;        /*Allow duplicate strings in the population or not*/
    3132    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*/
    3235} PgapackData;
    3336
     
    4245RpTclOptionType RpOption_Oper = {
    4346    "pga_operation", RpOption_ParseOper, RpOption_GetOper, NULL
     47};
     48
     49RpCustomTclOptionParse RpOption_ParseCrossovType;
     50RpCustomTclOptionGet RpOption_GetCrossovType;
     51RpTclOptionType RpOption_CrossovType = {
     52        "pga_crossovtype", RpOption_ParseCrossovType,RpOption_GetCrossovType,NULL
    4453};
    4554
     
    6776  {"-mutnValue",RP_OPTION_DOUBLE,Rp_Offset(PgapackData,mutnValue)},
    6877  {"-crossovrate",RP_OPTION_DOUBLE,Rp_Offset(PgapackData,crossovrate)},
     78  {"-crossovtype",&RpOption_CrossovType,Rp_Offset(PgapackData,crossovtype)},
    6979  {"-randnumseed",RP_OPTION_INT,Rp_Offset(PgapackData,randnumseed)},
    7080  {"-stpcriteria",&RpOption_StpCriteria,Rp_Offset(PgapackData,stpcriteria)},
    7181  {"-allowdup",RP_OPTION_BOOLEAN,Rp_Offset(PgapackData,allowdup)},
    7282  {"-mutnandcrossover",RP_OPTION_BOOLEAN,Rp_Offset(PgapackData,mutnandcrossover)},
     83  {"-randReplProp",RP_OPTION_DOUBLE,Rp_Offset(PgapackData,randReplProp)},
    7384  {NULL, NULL, 0}
    7485};
     
    111122    dataPtr->maxRuns = 10000;
    112123    dataPtr->popRepl = PGA_POPREPL_BEST;
     124    dataPtr->crossovtype = PGA_CROSSOVER_UNIFORM;
    113125    dataPtr->popSize = 200;
    114126    dataPtr->numReplPerPop = (dataPtr->popSize)/10; /*10% replaced by default, change to whatever value you need*/
     
    120132    dataPtr->allowdup = PGA_FALSE; /*Do not allow duplicate strings by default*/
    121133    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*/
    122135    return (ClientData)dataPtr;
    123136}
     
    162175    PGASetCrossoverProb(ctx,dataPtr->crossovrate);
    163176    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));
    166179    PGASetMutationAndCrossoverFlag(ctx,dataPtr->mutnandcrossover);
    167180    PGASetNumReplaceValue(ctx,dataPtr->numReplPerPop);
     181    PGASetRandReplProp(ctx,dataPtr->randReplProp);
    168182
    169183
     
    461475    RpOptimParam *parent1, *parent2, *child1, *child2;
    462476    double pu;
     477    PgapackData *dataPtr;
     478    /*declare variables for SBX*/
     479    double ui,beta,eta,powVal;
    463480
    464481    envPtr = PgapGetEnvForContext(ctx);
     
    467484    child1  = (RpOptimParam*)PGAGetIndividual(ctx, c1, pop2)->chrom;
    468485    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++) {
    473491        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 {
    474522            /* child inherits from parent */
    475523            memcpy(&child1[n], &parent1[n], sizeof(RpOptimParam));
    476524            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));
    481525        }
    482526    }
     527                       
     528               
    483529}
    484530
     
    675721    return TCL_OK;
    676722}
     723
     724
     725/*
     726 * ======================================================================
     727 *  OPTION:  -crossovtype <=> PGA_CROSSOVER_UNIFORM / PGA_CROSSOVER_SBX
     728 * ======================================================================
     729 */
     730int
     731RpOption_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
     754int
     755RpOption_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
    677777
    678778/*
Note: See TracChangeset for help on using the changeset viewer.