📄 ls.cc
字号:
/******************************************************************** These are the methods of the local searchers rsh 9/95 Modifications to the class heirarchy made 2/21/96 rsh********************************************************************/#include "ls.h"extern class Eval evaluate;extern FILE *logFile;// This function adds array1 + array2 to all the reals in the representationPhenotype genPh(const Phenotype &original, float *array1, float *array2){ RepType genetype; register int i, index = 0; Phenotype retval(original);#ifdef DEBUG (void)fprintf(logFile, "ls.cc/Phenotype genPh(const Phenotype &original, float *array1, float *array2)\n");#endif /* DEBUG */ for (i=0; i<retval.num_pts(); i++) { genetype = retval.gtype(i); if ((genetype == T_RealV)||(genetype == T_CRealV)) { retval.write(retval.gread(i).real + array1[index] + array2[index], i); index++; } } return(retval);}// What Solis & Wetts does is add random deviates to every// real number in the Phenotype.void Solis_Wets::SW(Phenotype &vector){ register int i, j, num_successes = 0, num_failures = 0; register float temp_rho = rho;#ifdef DEBUG (void)fprintf(logFile, "ls.cc/void Solis_Wets::SW(Phenotype &vector)\n");#endif /* DEBUG */ Phenotype newPh; // Reset bias for (i=0; i<size; i++) { bias[i] = 0.0; } for (i=0; i<max_its; i++) { // Generate deviates for (j=0; j<size; j++) { deviates[j] = gen_deviates(temp_rho); } newPh = genPh(vector, deviates, bias); // Evaluate if (newPh.evaluate(Normal_Eval) < vector.evaluate(Normal_Eval)) { num_successes++; num_failures = 0; vector = newPh; for (j=0; j<size; j++) { bias[j] = 0.20*bias[j] + 0.40*deviates[j]; } } else // We need to check if the opposite deviates do any good { for (j=0; j<vector.num_pts(); j++) { deviates[j] *= -1.0; } newPh = genPh(vector, deviates, bias); if (newPh.evaluate(Normal_Eval) < vector.evaluate(Normal_Eval)) { num_successes++; num_failures = 0; vector = newPh; for (j=0; j<size; j++) { bias[j] -= 0.40*deviates[j]; } } else { num_failures++; num_successes = 0; for (j=0; j<size; j++) { bias[j] *= 0.50; } } } // Check to see if we need to expand or contract if (num_successes>=max_successes) {// rho *= expansion; temp_rho *= expansion; num_successes = num_failures = 0; } else if (num_failures>=max_failures) {// rho *= contraction; temp_rho *= contraction; num_successes = num_failures = 0; } // if (rho<lower_bound_on_rho) if (temp_rho<lower_bound_on_rho) break; }}// This is pseudo-Solis & Wets in that it adds random deviates to every dimension// of the current solution, but the variances vary across dimensions.void Pseudo_Solis_Wets::SW(Phenotype &vector){ register int i, j, num_successes = 0, num_failures = 0; Phenotype newPh;#ifdef DEBUG (void)fprintf(logFile, "ls.cc/void Pseudo_Solis_Wets::SW(Phenotype &vector)\n");#endif /* DEBUG */ // Initialize the temp_rho's for (i=0; i<size; i++) { temp_rho[i] = rho[i]; }// fprintf(stderr,"SW:%f,%f,%d",expansion,contraction,max_its); // Reset bias for (i=0; i<size; i++) { bias[i] = 0.0; } for (i=0; i<max_its; i++) { // Generate deviates for (j=0; j<size; j++) { deviates[j] = gen_deviates(temp_rho[j]); } newPh = genPh(vector, deviates, bias); // Evaluate if (newPh.evaluate(Normal_Eval) < vector.evaluate(Normal_Eval)) { num_successes++; num_failures = 0; vector = newPh; for (j=0; j<size; j++) { bias[j] = 0.20*bias[j] + 0.40*deviates[j]; } } else // We need to check if the opposite deviates do any good { for (j=0; j<vector.num_pts(); j++) { deviates[j] *= -1.0; } newPh = genPh(vector, deviates, bias); if (newPh.evaluate(Normal_Eval) < vector.evaluate(Normal_Eval)) { num_successes++; num_failures = 0; vector = newPh; for (j=0; j<size; j++) { bias[j] -= 0.40*deviates[j]; } } else { num_failures++; num_successes = 0; for (j=0; j<size; j++) { bias[j] *= 0.50; } } } // Check to see if we need to expand or contract if (num_successes>=max_successes) { for(j=0; j<size; j++) {// rho[j] *= expansion; temp_rho[j] *= expansion; } num_successes = num_failures = 0; } else if (num_failures>=max_failures) { for(j=0; j<size; j++) {// rho[j] *= contraction; temp_rho[j] *= contraction; } num_successes = num_failures = 0; } for(j=0; j<size; j++) { // if (rho[j]<lower_bound_on_rho[j]) if (temp_rho[j]<lower_bound_on_rho[j]) break; } }}int Solis_Wets_Base::search(Individual &solution){#ifdef DEBUG (void)fprintf(logFile, "ls.cc/int Solis_Wets_Base::search(Individual &solution)\n");#endif /* DEBUG */// fprintf(stderr,"search_f_b:%f\n",search_frequency); // if (ranf()<search_frequency) { // if (ranf()<1) { // fprintf(stderr,"search_f:%f\n",search_frequency); SW(solution.phenotyp); //solution.inverse_mapping(); // } return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -