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

📄 rep.cc

📁 c++编写的并行拉马克遗传算法的程序。实现分析对接程序
💻 CC
📖 第 1 页 / 共 2 页
字号:
/* rep.cc *//********************************************************************     The methods associated with the Representation class hierarchy.				rsh 9/95********************************************************************/#include <iostream.h>#include <stdio.h>#include <math.h>#include <limits.h>#include "rep.h"#include "ranlib.h"#include "structs.h"extern FILE *logFile;//  InitializationsFourByteLong IntVector::low = -INT_MAX/4;FourByteLong IntVector::high = INT_MAX/4;//float RealVector::low = -100.0;//float RealVector::high = 100.0;//  For now assume that normalize handles this constraint//float ConstrainedRealVector::low = -100.0;//float ConstrainedRealVector::high = 100.0;//float RealVector::low = -PI;//float RealVector::high = PI;float ConstrainedRealVector::low = -PI;float ConstrainedRealVector::high = PI;double ConstrainedRealVector::sum = 1.0;float BitVector::one_prob = 0.5;//  The member functions for the canonical base classes//  This constructor is used to generate the initial (random) instances//  of an integer vector.IntVector::IntVector(int number_of_els): Representation(number_of_els){   register int i;#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/IntVector::IntVector(int number_of_els=%d) \n",number_of_els);#endif /* DEBUG */   mytype = T_IntV;   vector = new FourByteLong[number_of_els];   for (i=0; i<number_of_els; i++) {      vector[i] = ignuin(low, high);   }}IntVector::IntVector(int num_els, FourByteLong init_low, FourByteLong init_high): Representation(num_els){   register int i;#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/IntVector::IntVector(int num_els=%d, FourByteLong init_low=%ld, FourByteLong init_high=%ld) \n",num_els,init_low,init_high);#endif /* DEBUG */   mytype = T_IntV;   vector = new FourByteLong[num_els];   for (i=0; i<num_els; i++) {      vector[i] = ignuin(init_low, init_high);   }}//  This constructor does an actual copy of the vector.//  We could make gains by doing reference counting, but//  that's for the future.IntVector::IntVector(const IntVector &original): Representation(original.number_of_pts){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/IntVector::IntVector(const IntVector &original) \n");#endif /* DEBUG */   mytype = T_IntV;   if (original.vector!=NULL) { vector = new FourByteLong[number_of_pts];   } else {      vector = NULL;   }   for (register int i=0; i<number_of_pts; i++) {      vector[i] = original.vector[i];   }}void IntVector::write(unsigned char value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void IntVector::write(unsigned char value=%c, int gene=%d) \n",value,gene);#endif /* DEBUG */   (void)fprintf(logFile,"Writing a Bit to an Int!\n"); // used to be "stderr"   (void)fprintf(logFile,"value= \"%c\", gene= %d\n", value, gene); // used to be "stderr"}void IntVector::write(FourByteLong value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void IntVector::write(FourByteLong value=%ld, int gene=%d)` \n",value,gene);#endif /* DEBUG */   if (value<low) {      vector[gene] = low;   } else if (value>high) {      vector[gene] = high;   } else {      vector[gene] = value;   }}void IntVector::write(double value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void IntVector::write(double value=%lf, int gene=%d) \n",value,gene);#endif /* DEBUG */   (void)fprintf(logFile,"Writing a Real to an Int!\n"); // used to be "stderr"   (void)fprintf(logFile,"value= %lf, gene= %d\n", value, gene); // used to be "stderr"}/*void IntVector::write(const void *value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void IntVector::write(const void *value, int gene=%d) \n",gene);#endif // * DEBUG * /   if (*((FourByteLong *)value)<low) {      (void)fprintf(logFile,"Writing out-of-bounds Int!\n"); // used to be "stderr"      vector[gene] = low;   } else if (*((FourByteLong *)value)>high) {      (void)fprintf(logFile,"Writing out-of-bounds Int!\n"); // used to be "stderr"      vector[gene] = high;   } else {      vector[gene] = *((FourByteLong *)value);   }}*/void IntVector::write(const Element value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void IntVector::write(const Element value, int gene=%d) \n",gene);#endif /* DEBUG */   if (value.integer<low) {      (void)fprintf(logFile,"Writing out-of-bounds Int!\n"); // used to be "stderr"      vector[gene] = low;   } else if (value.integer>high) {      (void)fprintf(logFile,"Writing out-of-bounds Int!\n"); // used to be "stderr"      vector[gene] = high;   } else {      vector[gene] = value.integer;   }}/*const void *IntVector::gene(unsigned int gene_number) const{#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/const void *IntVector::gene(unsigned int gene_number=%d) const \n",gene_number);#endif // * DEBUG * /   if (gene_number>=number_of_pts) {      (void)fprintf(logFile,"Trying to access an out-of-bounds gene!\n"); // used to be "stderr"      return(NULL);   } else {      return((void *)(&vector[gene_number]));   }}*/const Element IntVector::gene(unsigned int gene_number) const{   Element retval;#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/const Element IntVector::gene(unsigned int gene_number=%d) const \n",gene_number);#endif /* DEBUG */   if (gene_number>=number_of_pts) {      (void)fprintf(logFile,"Trying to access an out-of-bounds gene!\n"); // used to be "stderr"      retval.integer = 0;      return(retval);   } else {      retval.integer = vector[gene_number];      return(retval);  // typecast int as Element   }}const void *IntVector::internals(void) const{#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/const void *IntVector::internals(void) const \n");#endif /* DEBUG */   return((void *)(&vector[0]));}Representation &IntVector::operator=(const Representation &original){   register int i;   FourByteLong *array;#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/Representation &IntVector::operator=(const Representation &original) \n");#endif /* DEBUG */   array = (FourByteLong *)original.internals();   if (original.type()==T_IntV) {      number_of_pts = original.number_of_points();      if (vector!=NULL) {         delete [] vector;      }      if (array!=NULL) {         vector = new FourByteLong[number_of_pts];      } else {         vector = NULL;      }      for (i=0; i<number_of_pts; i++) {         vector[i] = array[i];      }   } else {      (void)fprintf(logFile,"Unable to invoke operator= because Representations don't match!\n"); // used to be "stderr"   }   return(*this);}//  This is the constructor used to initialize the (random)//  starting population.RealVector::RealVector(int num_els): Representation(num_els){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/RealVector::RealVector(int num_els=%d) \n",num_els);#endif /* DEBUG */   mytype = T_RealV;   low = REALV_LOW;   high = REALV_HIGH;   vector = new double[num_els];   for (; --num_els>=0;) {      vector[num_els] = double(genunf(low, high));   }}RealVector::RealVector(int num_els, double init_low, double init_high): Representation(num_els){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/RealVector::RealVector(int num_els=%d, double init_low=%lf, double init_high=%lf) \n",num_els,init_low,init_high);#endif /* DEBUG */   mytype = T_RealV;   low = init_low;   high = init_high;   vector = new double[num_els];   for (; --num_els>=0;) {      vector[num_els] = double(genunf(init_low, init_high));   }}//  Do a deep copy of the originalRealVector::RealVector(const RealVector &original): Representation(original.number_of_pts){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/RealVector::RealVector(const RealVector &original) \n");#endif /* DEBUG */   mytype = T_RealV;   low =  original.low;   high = original.high;   if (original.vector!=NULL) {      vector = new double[original.number_of_pts];   } else {      vector = NULL;   }   for (register int i=0; i<original.number_of_pts; i++) {      vector[i] = original.vector[i];#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/i=%d, original.number_of_pts=%d, vector[%d]= %.3f\n",i, original.number_of_pts, i, vector[i]);#endif /* DEBUG */   }}void RealVector::write(unsigned char value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void RealVector::write(unsigned char value=%c, int gene=%d) \n",value,gene);#endif /* DEBUG */   (void)fprintf(logFile,"Writing a Bit to a Real!\n"); // used to be "stderr"   (void)fprintf(logFile,"value= \"%c\", gene= %d\n", value, gene); // used to be "stderr"}void RealVector::write(FourByteLong value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void RealVector::write(FourByteLong value=%ld, int gene=%d) \n",value,gene);#endif /* DEBUG */   (void)fprintf(logFile,"Writing an Int to a Real!\n"); // used to be "stderr"   (void)fprintf(logFile,"value= %ld, gene= %d\n", value, gene); // used to be "stderr"}void RealVector::write(double value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void RealVector::write(double value=%lf, int gene=%d) \n",value,gene);#endif /* DEBUG */   if (value<low) {//      (void)fprintf(logFile,"Writing out of bounds Real!  value (%lf) too low (%lf)\n",value,low); // used to be "stderr"      vector[gene] = low;   } else if (value>high) {//      (void)fprintf(logFile,"Writing out of bounds Real!  value (%lf) too high (%lf)\n",value,high); // used to be "stderr"      vector[gene] = high;   } else {      vector[gene] = value;   }}/*void RealVector::write(const void *value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void RealVector::write(const void *value, int gene=%d) \n",gene);#endif // * DEBUG * /   if (*((double *)value)<low) {      vector[gene] = low;   } else if (*((double *)value)>high) {      vector[gene] = high;   } else {      vector[gene] = *((double *)value);   }}*/void RealVector::write(const Element value, int gene){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/void RealVector::write(const Element value, int gene=%d) \n",gene);#endif /* DEBUG */   if (value.real<low) {      vector[gene] = low;   } else if (value.real>high) {      vector[gene] = high;   } else {      vector[gene] = value.real;   }}/* *const void *RealVector::gene(unsigned int gene_number) const *{ * *#ifdef DEBUG     *(void)fprintf(logFile, "rep.cc/const void *RealVector::gene(unsigned int gene_number=%d) const \n",gene_number); *#endif // * DEBUG * / *    *if (gene_number>=number_of_pts) {       *(void)fprintf(logFile,"Trying to access out-of-bounds gene\n"); // used to be "stderr"       *return(NULL);    *} else {       *return((void *)(&vector[gene_number]));    *} *} */const Element RealVector::gene(unsigned int gene_number) const{#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/const Element RealVector::gene(unsigned int gene_number=%d) const \n",gene_number);#endif /* DEBUG */   Element retval;   if (gene_number>=number_of_pts) {      //(void)fprintf(logFile,"Trying to access out-of-bounds gene\n"); // used to be "stderr"      (void)fprintf(logFile,"Trying to access out-of-bounds gene:%d,%d\n",gene_number,number_of_pts);      retval.real = 0.0;      return(retval);   } else {      retval.real = vector[gene_number];      return(retval);   }}const void *RealVector::internals(void) const{#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/const void *RealVector::internals(void) const \n");#endif /* DEBUG */   return((void *)(&vector[0]));}Representation &RealVector::operator=(const Representation &original){#ifdef DEBUG    (void)fprintf(logFile, "rep.cc/Representation &RealVector::operator=(const Representation &original) \n");#endif /* DEBUG */   register int i;   double *array;   if (original.type()==T_RealV) {      low = ((const RealVector &)original).low;      high = ((const RealVector &)original).high;      array = (double *)original.internals();      number_of_pts = original.number_of_points();      if (vector!=NULL) {         delete [] vector;      }      if (array!=NULL) {         vector = new double[number_of_pts];      } else {

⌨️ 快捷键说明

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