Ignore:
Timestamp:
Jun 24, 2008, 6:13:41 PM (16 years ago)
Author:
liveletlive
Message:

Committing changes related to command <name> samples ?number?

File:
1 edited

Legend:

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

    r986 r1062  
    2424} PgapackData;
    2525
     26
    2627RpCustomTclOptionParse RpOption_ParseOper;
    2728RpCustomTclOptionGet RpOption_GetOper;
     
    3536    "pga_poprepl", RpOption_ParsePopRepl, RpOption_GetPopRepl, NULL
    3637};
     38
     39typedef struct PgapackRuntimeDataTable{
     40        double **data;                          /*Actual data per sample, like values of the genes, fitness of a sample, etc*/
     41        int num_of_rows;                        /*Number of rows alloced..should be constant for a run*/
     42        int no_of_samples_evaled;       /*Number of samples evaluated so far*/
     43        int no_of_columns;                                      /*Number of columns allocated to the data table so far*/
     44}PgapackRuntimeDataTable;
    3745
    3846RpTclOption PgapackOptions[] = {
     
    5866static RpOptimEnv* PgapGetEnvForContext _ANSI_ARGS_((PGAContext *ctx));
    5967static void PgapUnlinkContext2Env _ANSI_ARGS_((PGAContext *ctx));
    60 
    61 
     68void PGARuntimeDataTableInit _ANSI_ARGS_((RpOptimEnv *envPtr));
     69void PGARuntimeDataTableDeInit();
     70void GetSampleInformation _ANSI_ARGS_((char *buffer, int sampleNumber));
     71void PGARuntimeDataTableSetSampleValue _ANSI_ARGS_((RpOptimParam *chrom, double fitness));
     72static PgapackRuntimeDataTable table;
    6273/*
    6374 * ----------------------------------------------------------------------
     
    144155    PGARun(ctx, PgapEvaluate);
    145156    PGADestroy(ctx);
    146 
    147157    PgapUnlinkContext2Env(ctx);
    148158
     
    168178    int p;            /* sample #p being run */
    169179    int pop;          /* identifier for this population */
     180   
    170181{
    171182    double fit = 0.0;
     
    173184    RpOptimParam *paramPtr;
    174185    RpOptimStatus status;
    175 
    176186    envPtr = PgapGetEnvForContext(ctx);
    177187    paramPtr = (RpOptimParam*)PGAGetIndividual(ctx, p, pop)->chrom;
    178 
    179188    status = (*envPtr->evalProc)(envPtr, paramPtr, envPtr->numParams, &fit);
    180 
     189       
    181190    if (pgapack_abort) {
    182191        fprintf(stderr, "==WARNING: run aborted!");
     
    188197        PgapPrintString(ctx, stderr, p, pop);
    189198    }
    190 
     199       
     200        /*populate the table with this sample*/
     201        PGARuntimeDataTableSetSampleValue(paramPtr,fit);
    191202    return fit;
    192203}
     
    518529}
    519530
     531
     532
    520533/*
    521534 * ----------------------------------------------------------------------
     
    707720    }
    708721}
     722/*---------------------------------------------------------------------------------
     723 * PGARuntimeDTInit(): It initializes the runtime data table.
     724 * The table is organized slightly counter-intuitively
     725 * Instead of a
     726 *  param1|param2|param3  |param4...
     727 *      val11 |val12 |val13   |val14...
     728 *      val12 |val22 |val23   |val24....
     729 * orientation, it is organized as
     730 *      param1|val11|val12
     731 *      param2|val21|val22
     732 *      param3|val31|val32
     733 *      param4|val41|val42
     734 * Reallocating for additional columns is easier than reallocating additional rows and then
     735 * reallocating for columns
     736 * --------------------------------------------------------------------------------
     737 */
     738
     739void PGARuntimeDataTableInit(envPtr)
     740RpOptimEnv *envPtr;
     741{   
     742        int i;
     743        if(envPtr != NULL){
     744                table.num_of_rows = (envPtr->numParams)+1;
     745                table.data = malloc((table.num_of_rows)*sizeof(double*));
     746                if(table.data == NULL){
     747                        panic("\nAllocation for Runtime Data Table failed\n");
     748                }
     749                for(i=0;i<table.num_of_rows;i++){
     750                        table.data[i] = malloc(PGAPACK_RUNTIME_TABLE_DEFAULT_SIZE*sizeof(double));
     751                        if(table.data[i] == NULL){
     752                                panic("\nAllocation for Runtime Data Table failed\n");
     753                        }                       
     754                }
     755                table.no_of_samples_evaled = 0;
     756                table.no_of_columns = PGAPACK_RUNTIME_TABLE_DEFAULT_SIZE;
     757               
     758        }else{
     759                panic("\nError: NULL Environment variable OR Table pointer passed to Data Table Init\n");
     760        }
     761}
     762
     763void PGARuntimeDataTableDeInit()
     764{       
     765        int i;
     766        if((&table) == NULL){
     767                panic("Error: Table not present, therefore cannot free memory..");
     768        }
     769        for(i=0;i<table.num_of_rows;i++){
     770                free(table.data[i]);
     771        }
     772        free(table.data);
     773}
     774
     775void PGARuntimeDataTableSetSampleValue(chrom,fitness)
     776RpOptimParam *chrom;
     777double fitness;
     778{
     779        int i;
     780        printf("\nSetting sample value.......................\n");
     781        if(chrom!=NULL && (&table)!=NULL){
     782                (table.no_of_samples_evaled)+=1;
     783                if((table.no_of_samples_evaled) > table.no_of_columns){
     784                        /* then Reallocate space for more columns)*/
     785                        (table.no_of_columns)+=(table.no_of_columns);
     786                                //TODO GTG: Delete printing stuff
     787                        for(i=0;i<(table.num_of_rows);i++){
     788                                table.data[i] = realloc(table.data[i],table.no_of_columns);
     789                                if(table.data[i]==NULL){
     790                                        panic("\nError: Could not Reallocate more space for the table");
     791                                }                               
     792                        }
     793                }else{
     794                        if(chrom->type == RP_OPTIMPARAM_NUMBER){
     795                                for(i=0;i<(table.num_of_rows);i++){
     796                                        if(i==0){
     797                                                table.data[i][(table.no_of_samples_evaled)-1] = fitness;
     798                                                printf("\nSample Number %d:- Fitness: %lf\t",table.no_of_samples_evaled,fitness);
     799                                        }else{
     800                                                table.data[i][(table.no_of_samples_evaled)-1] = chrom[i-1].value.dval;
     801                                                printf("Param %d %lf\t",i,table.data[i][(table.no_of_samples_evaled)-1]);
     802                                        }
     803                }
     804                        }else{
     805                                panic("\n Chromosome value is RP_OPTIMPARAM_STRING\n");
     806                                //GTG TODO: find out what happens in this case. Will we be better off handling Tcl_objects?
     807                        }       
     808                }
     809        }else{
     810                panic("\nError:Either Chromosome, or table passed to PGARuntimeDataTableSetSampleValue() is NULL\n");
     811        }
     812       
     813}
     814
     815void GetSampleInformation(buffer,sampleNumber)
     816        char *buffer;
     817        int sampleNumber;
     818{
     819        int i;
     820        char tempBuff[50];
     821        printf("\nFetching sample information.........................\n");
     822        if((&table) == NULL){
     823                panic("Table uninitialized");
     824        }
     825        if(sampleNumber<=0){
     826                sprintf(buffer,"\nNumber of Samples Evaluated so far: %d\n",(table.no_of_samples_evaled)+1);
     827                return;
     828        }
     829        if(((table.num_of_rows)-1)*10>SINGLE_SAMPLE_DATA_BUFFER_DEFAULT_SIZE){
     830                buffer = realloc(buffer,50+25*(table.num_of_rows));
     831                //resizing the buffer, keeping 50 for display related jazz, around 12-15 characs for param names
     832                //and 10 characs for Fl.pt. display of the value
     833                if(buffer == NULL){
     834                        panic("\nError: Could not reallocate space for sample data buffer");
     835                }
     836        }
     837        for(i=0;i<(table.num_of_rows);i++){
     838                if(i==0){
     839                        sprintf(buffer,"\nSample Number %d ----> Fitness: %lf  ",sampleNumber,table.data[i][sampleNumber-1]);
     840                }else{
     841                        sprintf(tempBuff,"Param %d: %lf  ",i,table.data[i][sampleNumber-1]);
     842                        strcat(buffer,tempBuff);
     843                }
     844        }
     845        strcat(buffer,"\n");
     846}
Note: See TracChangeset for help on using the changeset viewer.