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

📄 support.cc

📁 c++编写的并行拉马克遗传算法的程序。实现分析对接程序
💻 CC
📖 第 1 页 / 共 2 页
字号:
//  These are the member functions for the support classes./*** $Log$*/#include "eval.h"#ifdef sgi    #include <stdio.h>    #include "support.h"    #include "stateLibrary.h"    #include "structs.h"#else    extern "C"    {	#include <stdio.h>	#include "support.h"	#include "stateLibrary.h"	#include "structs.h"    }#endifextern FILE *logFile;extern class Eval evaluate;Population::Population(Population &original): lhb(original.lhb), size(original.size){   register int i;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/Population::Population(Population &original)\n");#endif /* DEBUG */   heap = new Individual[size];   for (i=0; i<size; i++) {      heap[i] = original.heap[i];      heap[i].age = 0L; // gmm, 1998-07-14   }}/*  Heap Functions:  In this case, the heap condition means the maximal     element wrt fitness (i.e. the best) is at the top of the heap.  lhb     is the index of the last element to be inserted into the heap.  Some     the standard functions on the heap can be accomplished in the following     manner:       Root = size - 1  (Note: that the root is fixed)       LeftChild(I) = 2I - size       RightChild(I) = 2I - size - 1       Parent(I) = (I + size + 1)/2    It is important to notice that the heap condition is maintained from    lhb to size-1 *in reverse order*.*/void Population::swap(Individual &individual1, Individual &individual2){   Individual temp;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/void Population::swap(Individual &individual1, Individual &individual2)\n");#endif /* DEBUG */   temp = individual1;   individual1 = individual2;   individual2 = temp;}/*  This routine assumes that the heap condition is satisfied    between lhb and size-1 and that the new individual is in    position lhb-1.*/void Population::SiftUp(void){   int i, parent;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/void Population::SiftUp(void)\n");#endif /* DEBUG */   i = lhb-1;   while (((parent=(i+size+1)/2)<size)&&(heap[parent].value(Normal_Eval)>heap[i].value(Normal_Eval))) {      swap(heap[parent], heap[i]);      i = parent;   }   lhb--;}/*  This routine assumes that the heap condition is satisfied    between lhb & size-2 initially, and that the individual at size-1    needs to be accomodated.*/void Population::SiftDown(void){   int i, child;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/void Population::SiftDown(void)\n");#endif /* DEBUG */   i = size-1;   while ((child=2*i-size)>=lhb) {      if (child-1>=lhb) {         if (heap[child-1].value(Normal_Eval)<heap[child].value(Normal_Eval)) {            child--;         }      }      /*  Now child holds the index of the best child of i  */      if (heap[i].value(Normal_Eval)<heap[child].value(Normal_Eval)) {         break;      } else {         swap(heap[child], heap[i]);         i = child;      }   }}void Population::msort(int m){   register int i;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/void Population::msort(int m=%d)\n",m);#endif /* DEBUG */   //  First make a heap of the whole array, i.e lhb = 0 & uhb = size   lhb = size-1;   while (lhb>0) {      SiftUp();   }   //  Now place the m best members at the beginning of the array   for (i=0; i<m; i++) {      swap(heap[i], heap[size-1]);      lhb++;      SiftDown();   }      //  Assert: heap[0..m-1] sorted}//void Population::print(ostream &output, int num)//{   //register int i;//#ifdef DEBUG   //(void)fprintf(logFile, "support.cc/void Population::print(ostream &output, int num=%d)\n",num);//#endif /* DEBUG */   //(void)fprintf(logFile, "The top %d individuals in the population:/n", num);   //for (i=0; i<num; i++) {      //(void)fprintf(logFile,"%lf\n", heap[i].value(Normal_Eval));   //}//}void Population::print(FILE *output, int num) {   register int i;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/void Population::print(FILE *output, int num=%d)\n",num);#endif /* DEBUG */   (void)fprintf( output, "The top %d individuals in the population:\n\n", num);   for (i=0; i<num; i++) {      (void)fprintf( output, "(%d):\t %8.2f\n", i+1, heap[i].value(Normal_Eval));   }}void Population::printPopulationAsStates(FILE *output, int num, int ntor) {   register int i;#ifdef DEBUG2   register int j;#endif /* DEBUG2 */   double thisValue;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/void Population::printPopulationAsStates(FILE *output, int num=%d, int ntor=%d)\n",num,ntor);#endif /* DEBUG */   (void)fprintf( output, "The top %d individuals in the population:\n\n", num);   for (i=0; i<num; i++) {      thisValue = heap[i].value(Normal_Eval);      (void)fprintf( output, "(%d):\nEnergy= %8.2le\n", i+1, thisValue);      heap[i].printIndividualsState(output, ntor);#ifdef DEBUG2      if (!finite(thisValue) || ISNAN(thisValue)) {//debug	  // Convert state to coords and print it out...//debug	  cnv_state_to_coords(heap[i].state(ntor), heap[i].mol->vt,  heap[i].mol->tlist,  ntor, heap[i].mol->crdpdb,  heap[i].mol->crd,  heap[i].mol->natom);//debug	  for (j=0; j<heap[i].mol->natom; j++) {//debug	    (void)fprintf( logFile, "ATOM  %5d  C   RES     1    %8.3f%8.3f%8.3f %+8.2f %+6.2f\n", i+1, heap[i].mol->crd[i][X], heap[i].mol->crd[i][Y], heap[i].mol->crd[i][Z], 0., 0.); //debug	  }/*j*///debug      }// thisValue is either infinite or not-a-number.//debug#endif /* DEBUG2 */   }// i}Genotype::Genotype(unsigned int init_number_of_vectors, Representation **init_rep_vector): number_of_vectors(init_number_of_vectors), rep_vector(init_rep_vector),  modified(0){   register int i, j, k;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/Genotype::Genotype(unsigned int init_number_of_vectors=%d, Representation **init_rep_vector)\n",init_number_of_vectors);#endif /* DEBUG */   number_of_genes = 0;   for (i=0; i<number_of_vectors; i++) {      number_of_genes += rep_vector[i]->number_of_points();   }    i=0;   lookup = new Lookup[number_of_genes];   for (j=0; j<number_of_vectors; j++) {      for (k=0; k<rep_vector[j]->number_of_points(); k++) {         lookup[i].vector = j;         lookup[i].index = k;         i++;      }   }}Genotype::Genotype(const Genotype &original){   register int i;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/Genotype::Genotype(Genotype &original)\n");#endif /* DEBUG */   number_of_genes = original.number_of_genes;   number_of_vectors = original.number_of_vectors;   modified = original.modified;   if (original.rep_vector!=NULL) {      rep_vector = new Representation*[number_of_vectors];      lookup = new Lookup[number_of_genes];   } else {      rep_vector = NULL;      lookup = NULL;   }   for (i=0; i<number_of_vectors; i++) {      rep_vector[i] = original.rep_vector[i]->clone();   }   for (i=0; i<number_of_genes; i++) {      lookup[i] = original.lookup[i];   }}Genotype::~Genotype(void){   register int i;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/Genotype::~Genotype(void)\n");#endif /* DEBUG */   if (rep_vector!=NULL) {      for (i=0; i<number_of_vectors; i++) {         delete rep_vector[i];      }      delete [] rep_vector;      delete [] lookup;   }}Genotype &Genotype::operator=(const Genotype &original){   register int i;#ifdef DEBUG   (void)fprintf(logFile, "support.cc/Genotype &Genotype::operator=(const Genotype &original)\n");#endif /* DEBUG */   if (rep_vector!=NULL) {      for (i=0; i<number_of_vectors; i++) {         delete rep_vector[i];      }      delete [] rep_vector;      delete [] lookup;   }   number_of_vectors = original.number_of_vectors;   number_of_genes = original.number_of_genes;//   modified = original.modified;   modified = 1;   if (original.rep_vector!=NULL) {      rep_vector = new Representation *[number_of_vectors];      lookup = new Lookup[number_of_genes];   } else {      rep_vector = NULL;      lookup = NULL;   }   for (i=0; i<number_of_vectors; i++) {      rep_vector[i] = original.rep_vector[i]->clone();   }   for (i=0; i<number_of_genes; i++) {      lookup[i] = original.lookup[i];   }   return(*this);}void Genotype::write(Element value, int gene_number){#ifdef DEBUG   (void)fprintf(logFile, "support.cc/void Genotype::write(Element value, int gene_number=%d)\n",gene_number);#endif /* DEBUG */   modified = 1;   rep_vector[lookup[gene_number].vector]->write(value, lookup[gene_number].index);}void Genotype::write(unsigned char value, int gene_number){

⌨️ 快捷键说明

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