Changeset 1062 for trunk/packages/optimizer/src/plugin_pgapack.c
- Timestamp:
- Jun 24, 2008, 6:13:41 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/optimizer/src/plugin_pgapack.c
r986 r1062 24 24 } PgapackData; 25 25 26 26 27 RpCustomTclOptionParse RpOption_ParseOper; 27 28 RpCustomTclOptionGet RpOption_GetOper; … … 35 36 "pga_poprepl", RpOption_ParsePopRepl, RpOption_GetPopRepl, NULL 36 37 }; 38 39 typedef 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; 37 45 38 46 RpTclOption PgapackOptions[] = { … … 58 66 static RpOptimEnv* PgapGetEnvForContext _ANSI_ARGS_((PGAContext *ctx)); 59 67 static void PgapUnlinkContext2Env _ANSI_ARGS_((PGAContext *ctx)); 60 61 68 void PGARuntimeDataTableInit _ANSI_ARGS_((RpOptimEnv *envPtr)); 69 void PGARuntimeDataTableDeInit(); 70 void GetSampleInformation _ANSI_ARGS_((char *buffer, int sampleNumber)); 71 void PGARuntimeDataTableSetSampleValue _ANSI_ARGS_((RpOptimParam *chrom, double fitness)); 72 static PgapackRuntimeDataTable table; 62 73 /* 63 74 * ---------------------------------------------------------------------- … … 144 155 PGARun(ctx, PgapEvaluate); 145 156 PGADestroy(ctx); 146 147 157 PgapUnlinkContext2Env(ctx); 148 158 … … 168 178 int p; /* sample #p being run */ 169 179 int pop; /* identifier for this population */ 180 170 181 { 171 182 double fit = 0.0; … … 173 184 RpOptimParam *paramPtr; 174 185 RpOptimStatus status; 175 176 186 envPtr = PgapGetEnvForContext(ctx); 177 187 paramPtr = (RpOptimParam*)PGAGetIndividual(ctx, p, pop)->chrom; 178 179 188 status = (*envPtr->evalProc)(envPtr, paramPtr, envPtr->numParams, &fit); 180 189 181 190 if (pgapack_abort) { 182 191 fprintf(stderr, "==WARNING: run aborted!"); … … 188 197 PgapPrintString(ctx, stderr, p, pop); 189 198 } 190 199 200 /*populate the table with this sample*/ 201 PGARuntimeDataTableSetSampleValue(paramPtr,fit); 191 202 return fit; 192 203 } … … 518 529 } 519 530 531 532 520 533 /* 521 534 * ---------------------------------------------------------------------- … … 707 720 } 708 721 } 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 739 void PGARuntimeDataTableInit(envPtr) 740 RpOptimEnv *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 763 void 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 775 void PGARuntimeDataTableSetSampleValue(chrom,fitness) 776 RpOptimParam *chrom; 777 double 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 815 void 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.