Ignore:
Timestamp:
Oct 29, 2008 12:16:26 PM (14 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.