📄 gs.cc
字号:
// Flip it tempvar.bit = 1 - tempvar.bit; // write it mutant.write(tempvar, gene_number); break; case CauchyDev: //((double *)gene)[point] = ((double *)gene)[point] + rcauchy(alpha, beta); // Read the real //tempvar.real = *((double *)mutant.gread(gene_number)); tempvar = mutant.gread(gene_number); // Add deviate tempvar.real += rcauchy(alpha, beta); // Write it //mutant.write((void *)(&tempvar.real), gene_number); mutant.write(tempvar, gene_number); break; case IUniformSub: //((int *)gene)[point] = ignuin(low, high); // Generate the new integer tempvar.integer = ignuin(low, high); // Write it //mutant.write((void *)(&tempvar.integer), gene_number); mutant.write(tempvar, gene_number); break; default: (void)fprintf(logFile,"gs.cc/Unrecognized mutation Mode!\n"); break; }}void Genetic_Algorithm::mutation(Population &pure){ int num_mutations, individual, gene_number;#ifdef DEBUG (void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::mutation(Population &pure)\n");#endif /* DEBUG */ num_mutations = check_table(ranf()); // Note we don't check to see if we mutate the same gene twice. // So, effectively we are lowering the mutation rate, etc... // Might want to check out Bentley's chapter on selection. for (; num_mutations>0; num_mutations--) { individual = ignlgi()%pure.num_individuals(); gene_number = ignlgi()%pure[individual].genotyp.num_genes(); mutate(pure[individual].genotyp, gene_number); pure[individual].age = 0L; }}void Genetic_Algorithm::crossover(Population &original){ int i, starting_point, temp_index, temp_ordering; #ifdef DEBUG (void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover(Population &original)\n");#endif /* DEBUG */ // Permute ordering for (i=0; i<original.num_individuals(); i++) { temp_ordering = ordering[i]; temp_index = ignlgi()%original.num_individuals(); ordering[i] = ordering[temp_index]; assert(ordering[i] < original.num_individuals());//debug ordering[temp_index] = temp_ordering; assert(ordering[temp_index] < original.num_individuals());//debug } // How does Goldberg implement crossover? for (i=0; i<original.num_individuals()-1; i=i+2) { if (ranf()<c_rate) { switch(c_mode) { case TwoPt: starting_point = ignuin(0, original[i].genotyp.num_genes()-1); crossover_2pt(original[ordering[i]].genotyp, original[ordering[i+1]].genotyp, starting_point, starting_point+ignuin(0, original[i].genotyp.num_genes()-starting_point-1)); original[ordering[i]].age = 0L; original[ordering[i+1]].age = 0L; break; case OnePt: starting_point = ignlgi()%original[i].genotyp.num_genes(); // We can accomplish one point crossover by using the 2pt crossover operator crossover_2pt(original[ordering[i]].genotyp, original[ordering[i+1]].genotyp, starting_point, original[ordering[i]].genotyp.num_genes()-1); original[ordering[i]].age = 0L; original[ordering[i+1]].age = 0L; break; case Uniform: (void)fprintf(logFile,"gs.cc/This crossover mode is unimplemented!\n"); break; default: (void)fprintf(logFile,"gs.cc/Unrecognized crossover mode!\n"); } } }}/* Assumes that 0<=pt1<pt2<=number_of_pts * There are four cases to consider:- * (1) the copied area is contained entirely within the gene * (2) the gene is contained entirely within the copied area * (3) the copied area is partially contained within the gene * (4) there's no intersection between the copied area and the gene */void Genetic_Algorithm::crossover_2pt(Genotype &father, Genotype &mother, unsigned int pt1, unsigned int pt2){ int i; Element temp;#ifdef DEBUG (void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::crossover_2pt(Genotype"); (void)fprintf(logFile, "&father, Genotype &mother, unsigned int pt1, unsigned int pt2)\n"); (void)fprintf(logFile,"gs.cc/Trying to crossover from %d to %d \n", pt1,pt2);#endif /* DEBUG */ for (i=pt1; i<=pt2; i++) {#ifdef DEBUG //(void)fprintf(logFile,"gs.cc/1::At pt %d father: %.3lf mother: %.3lf\n", //i, *((double *)father.gread(i)), *((double *)mother.gread(i)) );#endif /* DEBUG */ temp = father.gread(i); father.write(mother.gread(i), i); mother.write(temp, i);#ifdef DEBUG //(void)fprintf(logFile,"gs.cc/1::At pt %d father: %.3lf mother: %.3lf\n", //i, *((double *)father.gread(i)), *((double *)mother.gread(i)) );#endif /* DEBUG */ }}/* * Proportional Selection * * * We want to perform minimization on a function. To do so, we * take fitness values in a given generation and normalize them s.t. * they are non-negative, and such that smaller f's are given a higher * weighting. * If f in [a,b], then f' in [A,B] s.t. f(a) => f'(B) and f(b) => f'(A) is * * f' = (B-A) * (1 - (f-a)/(b-a)) + A * * = (B-A) * (b-f)/(b-a) + A * * Note that it suffices to map f into [0,1], giving * * f' = (b-f)/(b-a) * * Now the expected number of samples generated from a given point is * * N * f'_i / \sum f'_i = N * ((b-f_i)/(b-a)) / \sum ((b-f_i)/(b-a)) * * = N * (b-f_i) / (N*b - \sum f_i) * * Within a given generation, let 'b' be the maximal (worst) individual and * let 'a' be the minimal individual. * * Note: * (1) This calculation is invariant to the value of B, but _not_ * invariant to the value of A. * (2) This selection strategy works fine for functions bounded above, * but it won't necessarily work for functions which are unbounded * above. * * The 'b' parameter is represented as 'Worst' and is selected by the * scale_fitness method. If a value is greater than Worst, it is given a * value of zero for it's expectation. */void Genetic_Algorithm::selection_proportional(Population &original_pop, Individual *new_pop){ register int i=0; int temp_ordering, temp_index, start_index = 0;#ifdef DEBUG2 float debug_ranf; int allzero = 1;//debug Molecule *individualMol;//debug#endif#ifdef CHECK_ISNAN int allEnergiesEqual = 1; double diffwa = 0.0, invdiffwa = 0.0, firstEnergy = 0.0;#endif#ifdef DEBUG (void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::"); (void)fprintf(logFile, "selection_proportional(Population &original_pop, Individual *new_pop)\n");#endif /* DEBUG */#ifdef DEBUG2 (void)fprintf(logFile, "gs.cc/At the start of sel_prop: sel_prop_count= %d, start_index= %d\n\n",sel_prop_count, start_index); //debug original_pop.printPopulationAsStates(logFile, original_pop.num_individuals(), global_ntor);//debug#endif#ifdef CHECK_ISNAN /* * This is the new code to check for the NaN case that could * arise; this will preempt any fatal errors. */ // Calculate expected number of children for each individual assert(finite(worst)); assert(finite(avg)); assert(!ISNAN(worst)); assert(!ISNAN(avg)); diffwa = worst - avg; assert(finite(diffwa)); assert(!ISNAN(diffwa)); if (diffwa != 0.0) { // added by gmm, 4-JUN-1997 invdiffwa = 1.0 / diffwa; if (ISNAN(invdiffwa)) { (void)fprintf(logFile,"WARNING! While doing proportional selection, not-a-number was detected (NaN).\n"); (void)fprintf(logFile,"All members of the population will be arbitrarily allocated 1 child each.\n\n"); for (i=0; i < original_pop.num_individuals(); i++) { alloc[i] = 1.0; // arbitrary } // Assume run has converged: converged = 1; (void)fprintf(stderr,"WARNING! The population appears to have converged, so this run will shortly terminate.\n"); } else { assert(finite(invdiffwa)); assert(finite(worst)); assert(finite(original_pop.num_individuals())); assert(!ISNAN(invdiffwa)); assert(!ISNAN(worst)); assert(!ISNAN(original_pop.num_individuals())); for (i=0; i < original_pop.num_individuals(); i++) { alloc[i] = (worst - original_pop[i].value(e_mode)) * invdiffwa;#ifdef DEBUG2 (void)fprintf(logFile,"gs.cc:allocLoop: worst= %.3f\toriginal_pop[%d].value(e_mode)= %.3f\talloc[%d]= %.3e\tinvdiffwa= %.3e\n",worst, i, original_pop[i].value(e_mode), i, alloc[i], invdiffwa);//debug if (!finite(original_pop[i].value(e_mode) || ISNAN(original_pop[i].value(e_mode))) ) { individualMol = original_pop[i].getMol(); (void) writeMolAsPDBQ( individualMol, logFile);//debug }#endif assert(finite(original_pop[i].value(e_mode))); assert(finite(alloc[i])); assert(!ISNAN(original_pop[i].value(e_mode))); assert(!ISNAN(alloc[i])); }// for i }// endif (ISNAN(invdiffwa)) } else { // diffwa = 0.0, so worst = avg // This implies the population may have converged. converged = 1; // Write warning message to both stderr and logFile... (void)fprintf(stderr,"WARNING! While doing proportional selection, worst (%6.2le) = avg (%6.2le).\n", worst, avg); (void)fprintf(stderr," This would cause a division-by-zero error.\n"); (void)fprintf(stderr," All members of the population will be arbitrarily allocated 1 child each.\n"); (void)fprintf(stderr,"WARNING! The population appears to have converged, so this run will shortly terminate.\n\n"); (void)fprintf(logFile,"WARNING! While doing proportional selection, worst (%6.2le) = avg (%6.2le).\n", worst, avg); (void)fprintf(logFile," This would cause a division-by-zero error.\n"); (void)fprintf(logFile," All members of the population will be arbitrarily allocated 1 child each.\n"); (void)fprintf(logFile,"WARNING! The population appears to have converged, so this run will shortly terminate.\n\n"); alloc[0] = 1.0; // Added by gmm, 2-APR-1997 firstEnergy = original_pop[0].value(e_mode); allEnergiesEqual = 1; for (i=1; i < original_pop.num_individuals(); i++) { alloc[i] = 1.0; // Added by gmm, 2-APR-1997 allEnergiesEqual = allEnergiesEqual && (firstEnergy == original_pop[i].value(e_mode)); } if (allEnergiesEqual) { (void)fprintf(logFile," All individuals in the population have the same fitness (%6.2le)\n", firstEnergy); } else { (void)fprintf(logFile," Here are the fitness values of the population:\n\n"); for (i=0; i < original_pop.num_individuals(); i++) { (void)fprintf(logFile,"%3d = %6.2le, ", i+1, original_pop[i].value(e_mode) ); } } (void)fprintf(logFile,"\n"); } // diffwa = 0.0, so worst = avg /* * Commented out because this writes too many * errors out -- (worst-avg) is constant inside loop, * so can be brought outside for-loop. * for (i=0; i<original_pop.num_individuals(); i++) { * if ((worst - avg) != 0.0) { // added by gmm, 4-JUN-1997 * // In function minimization, the max energy is the worst * alloc[i] = (worst - original_pop[i].value(e_mode))/(worst - avg); * } else { * (void)fprintf(logFile,"gs.cc/WARNING! While doing proportional selection, * worst (%6.2le) and avg (%6.2le) were found equal, which would cause a * division-by-zero error; this was just prevented, for individual %d\n", * worst, avg, i); // Added by gmm, 4-JUN-1997 * alloc[i] = 1.0; // Added by gmm, 2-APR-1997 * } * } */#else /* * This is how the code used to be, before the worst=avg problem * was observed. */ // Calculate expected number of children for each individual for (i=0; i < original_pop.num_individuals(); i++) { // In our case of function minimization, the max individual is the worst alloc[i] = (worst - original_pop[i].value(e_mode))/(worst - avg); }#endif /* not CHECK_ISNAN */#ifdef DEBUG2 allzero = 1; //debug int J;//debug
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -