Changeset 1067


Ignore:
Timestamp:
Jun 25, 2008 11:19:20 PM (14 years ago)
Author:
liveletlive
Message:

added more options to configure PGAPack.
These include: mutation rate, crossover rate, stoppage criteria
and random number seed (for reproducibility of runs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/optimizer/src/plugin_pgapack.c

    r1062 r1067  
    2222    int popSize;         /* population size <=> PGASetPopSize() */
    2323    int popRepl;         /* replacement <=> PGASetPopReplacementType() */
     24    int stpcriteria;     /*stoppage criteria <=> PGASetStoppingRuleType()*/
     25    int randnumseed;  /*Random Number Seed <=> PGASetRandomSeed()*/
     26    double mutnrate;     /*Mutation Rate <=> PGASetMutationProb()*/
     27    double crossovrate;  /*Crossover Rate <=> PGASetCrossoverProb();*/
    2428} PgapackData;
    2529
     30RpCustomTclOptionGet RpOption_GetStpCriteria;
     31RpCustomTclOptionParse RpOption_ParseStpCriteria;
     32RpTclOptionType RpOption_StpCriteria = {
     33        "pga_stpcriteria", RpOption_ParseStpCriteria,RpOption_GetStpCriteria,NULL
     34};
    2635
    2736RpCustomTclOptionParse RpOption_ParseOper;
     
    3645    "pga_poprepl", RpOption_ParsePopRepl, RpOption_GetPopRepl, NULL
    3746};
     47
    3848
    3949typedef struct PgapackRuntimeDataTable{
     
    4959  {"-poprepl", &RpOption_PopRepl, Rp_Offset(PgapackData,popRepl)},
    5060  {"-popsize", RP_OPTION_INT, Rp_Offset(PgapackData,popSize)},
     61  {"-mutnrate",RP_OPTION_DOUBLE,Rp_Offset(PgapackData,mutnrate)},
     62  {"-crossovrate",RP_OPTION_DOUBLE,Rp_Offset(PgapackData,crossovrate)},
     63  {"-randnumseed",RP_OPTION_INT,Rp_Offset(PgapackData,randnumseed)},
     64  {"-stpcriteria",&RpOption_StpCriteria,Rp_Offset(PgapackData,stpcriteria)},
    5165  {NULL, NULL, 0}
    5266};
     
    90104    dataPtr->popRepl = PGA_POPREPL_BEST;
    91105    dataPtr->popSize = 200;
     106    dataPtr->crossovrate = 0.85;
     107    dataPtr->mutnrate = 0.05; /*by default in PGAPack 1/stringlength*/
     108    dataPtr->randnumseed = 1; /*should be a number greater than one, PGAPack requires it*/
     109    dataPtr->stpcriteria = PGA_STOP_NOCHANGE;
    92110    return (ClientData)dataPtr;
    93111}
     
    125143    PGASetPopSize(ctx, dataPtr->popSize);
    126144    PGASetPopReplaceType(ctx, dataPtr->popRepl);
     145    PGASetStoppingRuleType(ctx, dataPtr->stpcriteria);
     146    PGASetMutationProb(ctx,dataPtr->mutnrate);
     147    PGASetCrossoverProb(ctx,dataPtr->crossovrate);
     148    PGASetRandomSeed(ctx,dataPtr->randnumseed);
    127149    PGASetCrossoverType(ctx, PGA_CROSSOVER_UNIFORM);
    128150
    129     /* stop if any of these are true */
    130     PGASetStoppingRuleType(ctx, PGA_STOP_MAXITER);
    131     PGASetStoppingRuleType(ctx, PGA_STOP_NOCHANGE);
    132     PGASetStoppingRuleType(ctx, PGA_STOP_TOOSIMILAR);
    133151
    134152    PGASetUserFunction(ctx, PGA_USERFUNCTION_CREATESTRING, PgapCreateString);
     
    647665        Tcl_SetResult(interp, "random-norepl", TCL_STATIC);
    648666        break;
     667    default:
     668        Tcl_SetResult(interp, "???", TCL_STATIC);
     669        break;
     670    }
     671    return TCL_OK;
     672}
     673
     674/*
     675 * ======================================================================
     676 *  OPTION:  -stpcriteria <=> PGA_STOP_MAXITER / PGA_STOP_NOCHANGE / PGA_STOP_TOOSIMILAR
     677 * ======================================================================
     678 */
     679int
     680RpOption_ParseStpCriteria(interp, valObj, cdata, offset)
     681    Tcl_Interp *interp;  /* interpreter handling this request */
     682    Tcl_Obj *valObj;     /* set option to this new value */
     683    ClientData cdata;    /* save in this data structure */
     684    int offset;          /* save at this offset in cdata */
     685{
     686    int *ptr = (int*)(cdata+offset);
     687    char *val = Tcl_GetStringFromObj(valObj, (int*)NULL);
     688    if (strcmp(val,"maxiter") == 0) {
     689        *ptr = PGA_STOP_MAXITER;
     690    }
     691    else if (strcmp(val,"nochange") == 0) {
     692        *ptr = PGA_STOP_NOCHANGE;
     693    }
     694    else if (strcmp(val,"toosimilar") == 0){
     695        *ptr = PGA_STOP_TOOSIMILAR;
     696    }
     697    else {
     698        Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
     699            "bad value \"", val, "\": should be maxiter, nochange or toosimilar",
     700            (char*)NULL);
     701        return TCL_ERROR;
     702    }
     703    return TCL_OK;
     704}
     705
     706int
     707RpOption_GetStpCriteria(interp, cdata, offset)
     708    Tcl_Interp *interp;  /* interpreter handling this request */
     709    ClientData cdata;    /* get from this data structure */
     710    int offset;          /* get from this offset in cdata */
     711{
     712    int *ptr = (int*)(cdata+offset);
     713    switch (*ptr) {
     714    case PGA_STOP_MAXITER:
     715        Tcl_SetResult(interp, "maxiter", TCL_STATIC);
     716        break;
     717    case PGA_STOP_NOCHANGE:
     718        Tcl_SetResult(interp, "nochange", TCL_STATIC);
     719        break;
     720    case PGA_STOP_TOOSIMILAR:
     721        Tcl_SetResult(interp, "toosimilar", TCL_STATIC);
     722        break;
    649723    default:
    650724        Tcl_SetResult(interp, "???", TCL_STATIC);
Note: See TracChangeset for help on using the changeset viewer.