⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gs.cc

📁 c++编写的并行拉马克遗传算法的程序。实现分析对接程序
💻 CC
📖 第 1 页 / 共 3 页
字号:
/********************************************************************     These are the methods for Global_Search and its derivations.                                rsh 9/95*********************************************************************/#include <iostream.h>#include <math.h>#include "gs.h"#include "ranlib.h"#include "eval.h"#include "rep.h"#include "assert.h"#include "support.h"#ifdef sgi    #include <stdio.h>    #include <sys/types.h>      /*time_t time(time_t *tloc); */    #include <time.h>           /*time_t time(time_t *tloc); */    #include <sys/times.h>    #include <ieeefp.h>    #include "constants.h"    #include "autocomm.h"    #include "timesyshms.h"    #include "writePDBQ.h"#else    extern "C"    {        #include <stdio.h>        #include <sys/types.h>      /*time_t time(time_t *tloc); */        #include <time.h>           /*time_t time(time_t *tloc); */        #include <sys/times.h>        #include "constants.h"        #include "autocomm.h"        #include "timesyshms.h"        #include "writePDBQ.h"    }#endifextern FILE *logFile;extern class Eval evaluate;extern int sel_prop_count;//debugextern int global_ntor;//debugdouble worst_in_window(double *window, int size){   register int i;   double worst;   worst = window[0];#ifdef DEBUG2   (void)fprintf(logFile, "gs.cc/double worst_in_window(double *window, int size)_________________________\n");//debug#endif   for (i=1; i<size; i++) {#ifdef DEBUG2      (void)fprintf(logFile, "gs.cc/window[%d]= %.3f\tworst= %.3f\n", i, window[i], worst);//debug#endif      if (window[i]>worst) {         worst = window[i];#ifdef DEBUG2         (void)fprintf(logFile, "gs.cc/i= %d\t(window[i]>worst)\tUpdating: worst= %.3f\n", i, worst);//debug#endif      }   }// for i#ifdef DEBUG2   (void)fprintf(logFile, "gs.cc/Returning: worst= %.3f\n\n", worst);//debug#endif   return(worst);}double avg_in_window(double *window, int size){   register int i;   double mysum = 0.0, myavg = 0.0;#ifdef DEBUG2   (void)fprintf(logFile, "gs.cc/avg_in_window(double *window, int size)_________________________\n");//debug#endif   for (i=0; i<size; i++) {      mysum += window[i];#ifdef DEBUG2      (void)fprintf(logFile, "gs.cc/mysum= %.3f\twindow[%d]= %.3f\n",mysum, i, window[i]);//debug#endif   }   myavg = mysum / size;#ifdef DEBUG2   (void)fprintf(logFile, "gs.cc/Returning: myavg= %.3f\n\n",myavg);//debug#endif   return(myavg);}//  Also set avgdouble Genetic_Algorithm::worst_this_generation(Population &pop){   register int i;   double worstval, avgval;#ifdef DEBUG2   (void)fprintf(logFile, "gs.cc/worst_this_generation(Population &pop)_________________________\n");#endif#ifdef DEBUG   (void)fprintf(logFile, "gs.cc/double Genetic_Algorithm::worst_this_generation(Population &pop)\n");#endif /* DEBUG */   avgval = worstval = pop[0].value(Normal_Eval);#ifdef DEBUG2   (void)fprintf(logFile, "gs.cc/avgval= %.3f\tworstval= %.3f\n", avgval, worstval);#endif   for (i=1; i<pop.num_individuals(); i++) {      avgval += pop[i].value(Normal_Eval);#ifdef DEBUG2      (void)fprintf(logFile, "gs.cc/avgval= %.3f\tpop[%d].value(Normal_Eval)= %.3f\n", avgval, i, pop[i].value(Normal_Eval));#endif      if (pop[i].value(Normal_Eval)>worstval) {         worstval = pop[i].value(Normal_Eval);#ifdef DEBUG2         (void)fprintf(logFile, "gs.cc/(pop[i].value(Normal_Eval)>worstval): Updating: worstval= %.3f\n", worstval);#endif      }   }   avg = avgval/pop.num_individuals();#ifdef DEBUG2   (void)fprintf(logFile, "gs.cc/Returning: avg= %.3f, worstval= %.3f\n\n", avg, worstval);#endif   return(worstval);}//  This could be made inlineGenetic_Algorithm::Genetic_Algorithm(EvalMode init_e_mode,     Selection_Mode init_s_mode, Xover_Mode init_c_mode,    Worst_Mode init_w_mode,     int init_elitism, float init_c_rate, float init_m_rate,     int init_window_size, unsigned int init_max_generations):  e_mode(init_e_mode), s_mode(init_s_mode), c_mode(init_c_mode),     w_mode(init_w_mode), elitism(init_elitism),    c_rate(init_c_rate), m_rate(init_m_rate), window_size(init_window_size),     alpha(1.0), beta(0.0), tranStep(1.0), quatStep(0.52359878), torsStep(0.52359878), low(-100.0), high(100.0),      generations(0), max_generations(init_max_generations),    converged(0),    alloc(NULL), mutation_table(NULL),     ordering(NULL), m_table_size(0), worst(0), avg(0){#ifdef DEBUG   (void)fprintf(logFile, "gs.cc/Genetic_Algorithm::Genetic_Algorithm(EvalMode init_e_mode,...\n");#endif /* DEBUG */   worst_window = new double[window_size];}void Genetic_Algorithm::set_worst(Population &currentPop){   double temp = 0.0;#ifdef DEBUG   (void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::set_worst(Population &currentPop)\n");#endif /* DEBUG */   worst_window[generations%window_size] = worst_this_generation(currentPop);   switch(w_mode)   {      //  Assume for this case that there's a window_size of 1      case Ever:         if (generations!=0) {            if (temp>worst)               worst = worst_window[0];         } else {            worst = worst_window[0];         }         break;      case OfN:         if (generations>=window_size) {            worst = worst_in_window(worst_window, window_size);         } else {            worst = worst_in_window(worst_window, generations+1);         }         break;      case AverageOfN:         if (generations>=window_size) {            worst = avg_in_window(worst_window, window_size);         } else {            worst = avg_in_window(worst_window, generations+1);         }         break;      default:         (void)fprintf(logFile,"gs.cc/Unable to set the individual with the worst fitness!\n");   }}M_mode Genetic_Algorithm::m_type(RepType type){#ifdef DEBUG   (void)fprintf(logFile, "gs.cc/M_mode Genetic_Algorithm::m_type(RepType type)\n");#endif /* DEBUG */   switch(type)   {      case T_BitV:         return(BitFlip);      case T_RealV:      case T_CRealV:         return(CauchyDev);      case T_IntV:         return(IUniformSub);      default:         (void)fprintf(logFile,"gs.cc/Unrecognized Type (The allowed types are:  T_BitV, T_RealV, T_CRealV and T_IntV)!\n");         return(ERR);   }}void Genetic_Algorithm::make_table(int size, float prob){   register int i, j;   double L;#ifdef DEBUG   (void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::make_table(int size=%d, float prob=%f)\n",size, prob);#endif /* DEBUG */   m_table_size = size;   mutation_table = new float[size+1];   mutation_table[0] = pow(1-prob, size);   mutation_table[size] = 1;   i = 1;   while (i<=(int)size*prob) {      L = 0.0;      for (j=1; j<=i; j++) {         L += log(size+1-j) - log(j);      }      L += i*log(prob) + (size-i)*log(1-prob);      mutation_table[i] = mutation_table[i-1]+exp(L);      i++;   }   L = exp(L);   for (; i<size; i++) {      L = (L*prob*(size+1-i))/(i*(1-prob));      mutation_table[i] = mutation_table[i-1]+L;   }}int Genetic_Algorithm::check_table(float prob){   int low, high;#ifdef DEBUG   (void)fprintf(logFile, "gs.cc/int Genetic_Algorithm::check_table(float prob=%f)\n",prob);#endif /* DEBUG */   low = 0; high = m_table_size;   while (high-low>1) {      if (mutation_table[(high+low)/2]<prob) {         low = (high+low)/2;      } else if (mutation_table[(high+low)/2]>prob) {         high = (high+low)/2;      } else {         high = low = (high+low)/2;      }   }   return(low);}void Genetic_Algorithm::initialize(unsigned int pop_size, unsigned int num_poss_mutations){   register unsigned int i;#ifdef DEBUG   (void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::initialize(unsigned int pop_size=%d, ",pop_size);   (void)fprintf(logFile, "unsigned int num_poss_mutations=%d)\n",num_poss_mutations);#endif /* DEBUG */   if (alloc!=NULL) {      delete [] alloc;   }   if (ordering!=NULL) {      delete [] ordering;   }   if (mutation_table!=NULL) {      delete [] mutation_table;   }      alloc = new float[pop_size];   ordering = new unsigned int[pop_size];   for (i=0; i<pop_size; i++) {      ordering[i] = i;      assert(ordering[i] < pop_size);//debug      alloc[i] = 1.0; // changed by gmm, 12-sep-1997.   }   make_table(pop_size*num_poss_mutations, m_rate);}void Genetic_Algorithm::mutate(Genotype &mutant, int gene_number){   Element tempvar;#ifdef DEBUG   (void)fprintf(logFile, "gs.cc/void Genetic_Algorithm::mutate(Genotype &mutant, int gene_number=%d)\n",gene_number);#endif /* DEBUG */   switch(m_type(mutant.gtype(gene_number)))   {      case BitFlip:         //((unsigned char *)gene)[point] = 1 - ((unsigned char *)gene)[point];         //  Read the bit         tempvar = mutant.gread(gene_number);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -