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

📄 call_glss.cc

📁 c++编写的并行拉马克遗传算法的程序。实现分析对接程序
💻 CC
字号:
/********************************************************************     Call_glss:  Invokes a GA-LS hybrid to try and solve the                 docking problem.                                rsh 9/95********************************************************************//*** $Log$*/#include <iostream.h>#include "gs.h"#include "ls.h"#include "support.h"#include "eval.h"#include "hybrids.h"#include "mpi.h"#ifdef sgi                        // CC on the sgi doesn't invoke cc on *.c files   #include "constants.h"   #include "structs.h"   extern FILE *logFile;#else   extern "C"   {      #include "constants.h"      #include "structs.h"      extern FILE *logFile;   }#endifint global_ntor;Eval evaluate;Phenotype genPh_cs(const Phenotype &original,float *array1){   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(array1[index] , i);         index++;      }   }   return(retval);}Representation **generate_R(int num_torsions, double xlo, double xhi, double ylo, double yhi, double zlo, double zhi){   Representation **retval;   retval = new Representation *[5];   retval[0] = new RealVector(1, xlo, xhi);   retval[1] = new RealVector(1, ylo, yhi);   retval[2] = new RealVector(1, zlo, zhi);   retval[3] = new RealVector(3);   retval[4] = new RealVector(num_torsions+1);   return(retval);}Genotype generate_Gtype(int num_torsions, double xlo, double xhi, double ylo, double yhi, double zlo, double zhi){   Genotype temp(5, generate_R(num_torsions, xlo, xhi, ylo, yhi, zlo, zhi));   return(temp);}Phenotype generate_Ptype(int num_torsions, double xlo, double xhi, double ylo, double yhi, double zlo, double zhi){   Phenotype temp(5, generate_R(num_torsions, xlo, xhi, ylo, yhi, zlo, zhi));   return(temp);}Individual random_ind(int num_torsions, double xlo, double xhi, double ylo, double yhi, double zlo, double zhi){   Genotype temp_Gtype;   Phenotype temp_Ptype;   temp_Gtype = generate_Gtype(num_torsions, xlo, xhi, ylo, yhi, zlo, zhi);   temp_Ptype = generate_Ptype(num_torsions, xlo, xhi, ylo, yhi, zlo, zhi);   Individual temp(temp_Gtype, temp_Ptype);   return(temp);}State call_glss(Global_Search *global_method, Local_Search *local_method,                 State now, unsigned int num_evals, unsigned int pop_size,                 float xlo, float xhi, float ylo,                 float yhi, float zlo, float zhi,                int outlev, Molecule *mol,int np,int myid){    register int i,j;    int num_iterations = 0, num_loops = 0, allEnergiesEqual = 1, numTries = 0,step=pop_size/np,cs_flag=0,rand_low,rand_hig,v_length;    int i_sum,i_sum_all,*i_sum_array,*i_sub_num_array,*i_displs,sub_num[100],*sub,*i_j_displs;    int break_flag=0,eval_sub=0,eval_all=0,*eval_array;    double firstEnergy = 0.0,search_frequency=0.06;    global_method->reset();    local_method->reset();    evaluate.reset();        sub=new int[np];    i_j_displs=new int[np];    i_sum_array=new int[np];    i_displs=new int[np];    eval_array=new int[np];    i_sub_num_array=new int[100];     Job job_test;   MPI_Datatype mystruct_job;   int blocklens_job[3],base_job;   MPI_Aint indices_job[3];   MPI_Datatype old_types_job[3];   blocklens_job[0] = MAX_TORS;   blocklens_job[1] = 1;   blocklens_job[2] = 1;   old_types_job[0] = MPI_DOUBLE;   old_types_job[1] = MPI_DOUBLE;   old_types_job[2] = MPI_INT; MPI_Address( job_test.vector, &indices_job[0] ); MPI_Address( &job_test.value, &indices_job[1] ); MPI_Address( &job_test.popnum, &indices_job[2] ); base_job = indices_job[0]; for(i=0; i<3; i++) indices_job[i] -= base_job; MPI_Type_struct( 3, blocklens_job, indices_job, old_types_job, &mystruct_job ); MPI_Type_commit( &mystruct_job );       (void)fprintf( logFile, "\nCreating a population of %u individuals.\n", pop_size);    Population thisPop(pop_size);    (void)fprintf( logFile, "\nAssigning a random translation, a random orientation and %d random torsions to each of the %u individuals.\n\n", now.ntor, pop_size);    global_ntor = now.ntor;//debug    do {        ++numTries;        // Create a population of pop_size random individuals...        for (i=0; i<pop_size; i++) {            thisPop[i] = random_ind( now.ntor, double(xlo), double(xhi), double(ylo), double(yhi), double(zlo), double(zhi));            thisPop[i].mol = mol;            thisPop[i].age = 0L;        }        // Now ensure that there is some variation in the energies...        firstEnergy = thisPop[0].value(Normal_Eval);        for (i=1; i<pop_size; i++) {             allEnergiesEqual = allEnergiesEqual && (thisPop[i].value(Normal_Eval) == firstEnergy);        }        if (allEnergiesEqual) {            (void)fprintf(logFile,"NOTE: All energies are equal in population; re-initializing. (Try Number %d)\n", numTries);        }    } while (allEnergiesEqual);    if (outlev > 2) { thisPop.printPopulationAsStates(logFile, pop_size, now.ntor); }    (void)fprintf( logFile, "Beginning Lamarckian Genetic Algorithm (LGA), with a maximum of %u\nenergy evaluations.\n\n", num_evals);    /*do {        if (outlev > 1) { (void)fprintf( logFile, "Global-local search iteration %d,  Starting global search.\n", ++num_loops); }        global_method->search(thisPop);        if (outlev > 1) { (void)fprintf( logFile, "\tEnding global search.\n"); }        if (outlev > 2) { thisPop.printPopulationAsStates(logFile, pop_size, now.ntor); }        if (outlev > 3) { minmeanmax( logFile, thisPop, ++num_iterations ); }        if (outlev > 1) { (void)fprintf( logFile, "\tStarting local search.\n"); }        for (i=0; i<pop_size; i++) {            local_method->search(thisPop[i]);        }        if (outlev > 1) { (void)fprintf( logFile, "\tEnding local search.\n"); }        if (outlev > 2) { thisPop.printPopulationAsStates(logFile, pop_size, now.ntor); }    } while ((evaluate.evals() < num_evals) && (!global_method->terminate()));*/    v_length=thisPop[0].phenotyp.number_of_points;      do{cs_flag++;       i_sum_all=0;       i_sum=0;       eval_all=0;                if (outlev > 1) { (void)fprintf( logFile, "Global-local search iteration %d,  Starting global search.\n", ++num_loops); }       // fprintf(stderr,"global_start");        if (myid==0)  {global_method->search(thisPop);}       // fprintf(stderr,"global_end");        if (outlev > 1) { (void)fprintf( logFile, "\tEnding global search.\n");  }        if (outlev > 2) { thisPop.printPopulationAsStates(logFile, pop_size, now.ntor); }        if (outlev > 3) { minmeanmax( logFile, thisPop, ++num_iterations ); }        if (outlev > 1) { (void)fprintf( logFile, "\tStarting local search.\n"); }                    if(myid==np-1)        {rand_low=myid*step;         rand_hig=(myid+1)*step+pop_size%np;         for(i=rand_low;i<rand_hig;i++)        if (ranf()<search_frequency){sub_num[i_sum]=i;i_sum++;}        }      else{          rand_low=myid*step;         rand_hig=(myid+1)*step;           for(i=rand_low;i<rand_hig;i++)        if (ranf()<search_frequency){sub_num[i_sum]=i;i_sum++;}         }            MPI_Barrier( MPI_COMM_WORLD );      MPI_Gather(&i_sum,1,MPI_INT,i_sum_array,1,MPI_INT,0,MPI_COMM_WORLD);            //MPI_Bcast(&test_num,1,MPI_INT,0,MPI_COMM_WORLD);      //fprintf(stderr,"myid_test:%d,%d\n",myid,test_num);      if(myid==0){      for (i=0;i<np;i++){      i_displs[i]=i_sum_all;      i_sum_all+=i_sum_array[i];      //fprintf(stderr,"myid:%d,%d,%d\n",myid,i_displs[i],i_sum_array[i]);       }      }      MPI_Bcast(&i_sum_all,1,MPI_INT,0,MPI_COMM_WORLD);      //fprintf(stderr,"myid20:%d,%d\n",myid,i_sum_all);      if(i_sum_all<2){      if(myid==0){      for(i=0;i<i_sum_all;i++)local_method->search(thisPop[i_sub_num_array[i]]);                 }        }else {//i_sub_num_array=(int *)malloc(i_sum_all*sizeof(int));      //fprintf(stderr,"myid2:%d,%d,%d\n",myid,i_sum,i_sum_all);      MPI_Gatherv(sub_num,i_sum,MPI_INT,i_sub_num_array,i_sum_array,i_displs,MPI_INT,0,MPI_COMM_WORLD);            Job *job,*job_root;      job=new Job[i_sum_all];      job_root=new Job[i_sum_all];           if(myid==0){                 for(i=0;i<np;i++)          {sub[i]=i_sum_all/np+((i_sum_all%np-i-1)>=0?1:0);          if(i==0)i_j_displs[i]=0;          else           i_j_displs[i]=i_j_displs[i-1]+sub[i-1];           // fprintf(stderr,"myid4:%d,%d,%d\n",myid,sub[i],i_j_displs[i]);          }       for(i=0;i<i_sum_all;i++)         {for (j=0;j<v_length;j++)          {job_root[i].vector[j]=thisPop[i_sub_num_array[i]].phenotyp.value_vector[thisPop[i_sub_num_array[i]].phenotyp.lookup[j].vector]->gene(thisPop[i_sub_num_array[i]].phenotyp.lookup[j].index).real;            }          job_root[i].value=thisPop[i_sub_num_array[i]].phenotyp.value;           job_root[i].popnum=i_sub_num_array[i];         }                 }       int sub_pop=i_sum_all/np+((i_sum_all%np-myid-1)>=0?1:0);       MPI_Scatterv(job_root, sub,i_j_displs , mystruct_job, job,sub_pop,mystruct_job, 0, MPI_COMM_WORLD);                                    if (myid==0){        for(i=0;i<sub_pop;i++)        {//fprintf(logFile,"myid7_rs!!!:%d\n",myid);        local_method->search(thisPop[job[i].popnum]);        //fprintf(stderr,"myid7_re!!!:%d\n",myid);        }                     }        else{        for(i=0;i<sub_pop;i++){         for(j=0;j<v_length;j++){        thisPop[job[i].popnum].phenotyp.value_vector[thisPop[job[i].popnum].phenotyp.lookup[j].vector]->write(job[i].vector[j],thisPop[job[i].popnum].phenotyp.lookup[j].index);        }        thisPop[job[i].popnum].phenotyp.value=job[i].value;        thisPop[job[i].popnum].phenotyp.evalflag=1;        local_method->search(thisPop[job[i].popnum]);        thisPop[job[i].popnum].genotyp.modified=1;         }        }       if (outlev > 1) { (void)fprintf( logFile, "\tEnding local search.\n"); }       if (outlev > 2) { thisPop.printPopulationAsStates(logFile, pop_size, now.ntor); }              // fprintf(logFile,"myid6:%d,%d,%d\n",myid,sub_pop,v_length);            for(i=0;i<sub_pop;i++)         {for (j=0;j<v_length;j++)          {job[i].vector[j]=thisPop[job[i].popnum].phenotyp.value_vector[thisPop[job[i].popnum].phenotyp.lookup[j].vector]->gene(thisPop[job[i].popnum].phenotyp.lookup[j].index).real;           }          job[i].value=thisPop[job[i].popnum].phenotyp.value;          eval_sub=evaluate.evals();           //job[i].popnum=i_sub_num_array[i];         }                 //fprintf(logFile,"myid8_ev!!!:%d\n",myid);                MPI_Gatherv(job,sub_pop,mystruct_job,job_root,sub,i_j_displs,mystruct_job,0,MPI_COMM_WORLD);         MPI_Gather(&eval_sub,1,MPI_INT,eval_array,1,MPI_INT,0,MPI_COMM_WORLD);                 if(myid==0){         for(i=0;i<i_sum_all;i++)         {         for(j=0;j<v_length;j++){        thisPop[job_root[i].popnum].phenotyp.value_vector[thisPop[job_root[i].popnum].phenotyp.lookup[j].vector]->write(job_root[i].vector[j],thisPop[job_root[i].popnum].phenotyp.lookup[j].index);        }        thisPop[job_root[i].popnum].phenotyp.value=job_root[i].value;        thisPop[job_root[i].popnum].inverse_mapping();         }                for(i=0;i<np;i++){   eval_all+=eval_array[i];}         }         }            if(myid==0){            if (!((eval_all< num_evals) && (!global_method->terminate()))){             break_flag=1;         }       }        MPI_Bcast( &break_flag, 1, MPI_INT, 0, MPI_COMM_WORLD );                     if (break_flag==1)break;           }while(1);          thisPop.msort(3);    return( thisPop[0].state(now.ntor) );}

⌨️ 快捷键说明

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