/** * mate randomly the rest of population after elitism */ void mate (ga_struct * population, ga_struct * beta_population) { int esize = POPSIZE * ELITRATE; // elitism of top elements in array elitism (population, beta_population, esize); // mate the rest of shitty population xD int m = esize, i1 = -1, i2 = -1, pos = -1, tsize = ELEMENTS * 4 + 1; for (; m < POPSIZE; m++) { pos = rand () % tsize; i1 = rand () % POPSIZE; i2 = rand () % POPSIZE; int i = 0; for (; i < pos; i++) { beta_population[m].gen[i] = population[i1].gen[i]; } for (i = pos; i < tsize; i++) { beta_population[m].gen[i] = population[i2].gen[i]; } if (rand () < MUTATION) { mutate (&beta_population[m]); } } }
void EvolutionaryAlgorithm::selectSurvivors(void) { elitism(); tournament.clear(); int needeSurviorsLeft = populationSize - survivors.size(); for (int tour = 0; tour < needeSurviorsLeft; tour++) { for(int participant = 0; participant < tournamentSize; participant++) { //add an individual that is not already surviving and is not too old to live on to the tournament; int selectedIndividual = rand() % currentPopulation.size(); while (currentPopulation[selectedIndividual].getAlreadySurvived() || currentPopulation[selectedIndividual].getGoingToDie()) { selectedIndividual = rand() % currentPopulation.size(); } tournament.push_back(¤tPopulation[selectedIndividual]); } sort(tournament.begin(), tournament.end(), Individual::individualPointerLessThan); survivors.push_back(tournament[0]->incrementAge()); tournament[0]->setAsSurvivor(); tournament.clear(); } }
// Main algorithm. void MOGA::compute() { if ( num_bits_ == 0 ) return; double p; // Build initial population. initialization(); // Do generations. for ( int i = 0; i < num_generations_; ++i ) { while ( (int)population_.size() < 2 * num_individuals_ ) { // Select a pair of individuals. std::pair<int, int> s( selection() ); p = (double)std::rand() / (double)RAND_MAX; if ( p <= Pc_ ) { // Cross them. std::pair<individual, individual> children( crossover( population_[s.first], population_[s.second] ) ); p = (double)std::rand() / (double)RAND_MAX; if ( p <= Pm_ ) { // Mutate children. mutation( children.first ); mutation( children.second ); } // Repair children. repair( children.first ); repair( children.second ); population_.push_back( children.first ); population_.push_back( children.second ); } else { population_.push_back( population_[s.first] ); population_.push_back( population_[s.second] ); } } // Keep the best individuals. elitism(); print(); } // LOCAL SEARCH solutions_.clear(); if ( Argument::local_search ) { LocalSearch local( data_, cust_, fac_ ); local.pipe_fp_ = pipe_fp_; for ( unsigned int i = 0; i < population_.size(); ++i ) { local.search( population_[i] ); solutions_.merge( local.solutions_ ); } } // FINISHED - KEEP BEST VALUES // Keep only "first rank" solutions. for ( unsigned int i = 0; i < population_.size(); ++i ) { #if STRICT_CHECK if ( population_[i].rank == 1 && is_feasible( population_[i] ) && population_[i].is_feasible() ) #else if ( population_[i].rank == 1 && population_[i].is_feasible() ) #endif { #if STRICT_CHECK // Check objective is_valid( population_[i] ); #endif Solution sol( getNbObjective() ); for ( int k = 0; k < getNbObjective(); ++k ) { sol.setObj( k, population_[i].obj[k] ); } solutions_.push_back( sol ); } } }
int main(int argc, char **argv){ int rc,num_processors,rank; rc=MPI_Init(&argc,&argv); if (rc != MPI_SUCCESS) { printf("Error starting MPI program. Terminating\n"); MPI_Abort(MPI_COMM_WORLD, rc); } MPI_Comm_size(MPI_COMM_WORLD,&num_processors); MPI_Comm_rank(MPI_COMM_WORLD,&rank); //**************************************************************************************// // reading input information by all cores // //**************************************************************************************// FILE *fpenergy=fopen("./energy","w"); // Print the energy evolution FILE *fprestart=fopen("./restart","w"); //print the generation and popsize coordinates FILE *fpoptim=fopen("./optim.xyz","wb"); FILE *fp=fopen("data.txt","r"); time_t current_time; double seconds,start,end,seconds_new,seconds_old,seconds_total; struct timespec now,tmstart; char* c_time_string,c_time_final; int flag_converge=0; int i=0; srand(time(NULL)+rank); clock_gettime(CLOCK_REALTIME, &tmstart); printf("hello from processor %ld\n",rank); start = (double)clock() /(double) CLOCKS_PER_SEC; seconds_old= (double)(tmstart.tv_sec+tmstart.tv_nsec*1e-9); current_time = time(NULL); c_time_string = ctime(¤t_time); printf("Current time is %s\n", c_time_string); printf("hello from processor %ld\n",rank); char skip[10]; FILE *fp_input2 = fopen("ga_dftb_input1.1","r"); int NSTEP=0; int step=0; double ee_mate=0 ; // the energy cretiria for accepting children cluster. the larger, the less restrict. can be 0.1 0 or -0.1 double ELITRATE=0.2; int POPSIZE=0; int min_step=0; int NEWPOPSIZE=POPSIZE; double delte=0.00001 ; //0.00001; int ptnatm,runatm,cnatm; double boxl=0; double Mu=0.2; double dptc; int glob=0,globconvg=20; double globe[30]; int Temp; int flag_res=0; double cell[9]; int esize=0; int num_cores_child; fscanf(fp_input2,"%d %d %d %s\n", &ptnatm, &runatm,&cnatm,&skip); fscanf(fp_input2,"%d %s\n ", &POPSIZE,skip); fscanf(fp_input2,"%d %s\n", &NSTEP,skip); fscanf(fp_input2,"%d %s\n", &globconvg,skip); fscanf(fp_input2,"%lf %s\n",&ELITRATE,skip); fscanf(fp_input2,"%lf %s\n",&delte,skip); fscanf(fp_input2,"%d %s\n",&Temp,skip); fscanf(fp_input2,"%d %s\n",&min_step,skip); fscanf(fp_input2,"%lf %s\n",&ee_mate,skip); fscanf(fp_input2,"%lf %s\n",&dptc,skip); fscanf(fp_input2,"%lf %s\n",&boxl,skip); fscanf(fp_input2,"%d %s\n",&flag_res,skip); fscanf(fp_input2,"%d %s\n",&num_cores_child,skip); int ii=0; for (ii=0;ii<3;ii++){ fscanf(fp_input2,"%lf %lf %lf\n", cell+ii*3+0,cell+ii*3+1,cell+ii*3+2); //**************************************************************************************// // Above are the information that all processors need to know // //**************************************************************************************// if(rank==0){ } printf("********************JOB started*****************************\n"); printf("********************JOB started*****************************\n"); printf("********************JOB started*****************************\n\n\n"); printf("Attention!! The dftb excutable file must be in ~/bin and must be named 'dftb+' !!\n"); printf("Attention!! The dftb excutable file must be in ~/bin and must be named 'dftb+' !!\n"); printf("Attention!! The dftb excutable file must be in ~/bin and must be named 'dftb+' !!\n"); printf("Attention!! The dftb excutable file must be in ~/bin and must be named 'dftb+' !!\n"); printf("Attention!! The dftb excutable file must be in ~/bin and must be named 'dftb+' !!\n"); printf("Number of atoms %d %d %d\n", ptnatm,runatm,cnatm); if(ptnatm+runatm>=Max_Atom || cnatm>=CMax_Atom) exitp("Number of atoms exceed allowed Max!"); printf("POPSIZE %d\n",POPSIZE); printf("NSTEP %d\n",NSTEP); printf("globconvg %d\n",globconvg); printf("ELITRATE %lf\n",ELITRATE); printf("delte %lf\n",delte); printf("Temprature %d\n",Temp); printf("minimiz step %d\n",min_step); printf("ee_mate %lf\n",ee_mate); printf("dptc %lf\n",dptc); printf("intial boxl %lf\n",boxl); printf("reading from pt_coord? %d\n",flag_res); printf("Total number of processsors required %d\n",num_processors); printf("Number of processors for each child %d\n",num_cores_child); printf("\n\n\n******** end reading input information ***********************\n\n\n"); } if(POPSIZE!=num_processors/num_cores_child){ printf("Error!,POPSZIE=%d num_processrs=%d num_cores_child=%d num_processrs/num_cores_child=%d",\ POPSIZE,num_processors,num_cores_child,num_processors/num_cores_child); MPI_Abort(MPI_COMM_WORLD,0); } ga_struct *population = malloc(sizeof(ga_struct)*POPSIZE); ga_struct *beta_population = malloc(sizeof(ga_struct)*POPSIZE); //****************************************************************************************// //define new mpi structure for data transfer between processors // //****************************************************************************************// int count=4; int length[4]={1,3*Max_Atom,3*CMax_Atom,1}; MPI_Aint offset[4]={offsetof(ga_struct,fitness),offsetof(ga_struct,gen),offsetof(ga_struct,cgen),offsetof(ga_struct,ep)} ; MPI_Datatype type[4]={MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE,MPI_DOUBLE}; MPI_Datatype popDatatype; MPI_Type_struct(count,length,offset,type,&popDatatype); MPI_Type_commit(&popDatatype); //****************************************************************************************// //****************************************************************************************// //Initialization the population for all processors // //****************************************************************************************// if(rank==0){ printf("Total number of processors is %d\n",num_processors); printf("Total number of population is %d\n",POPSIZE); printf ("For each candidate, there are %d processors to be used\n",num_processors/POPSIZE); init_population(ptnatm+runatm,cnatm,POPSIZE,population,beta_population,boxl,flag_res); printf("argc=%d\n",argc); // int i=0,j=0; // int esize=POPSIZE*ELITRATE; cal_pop_energy(POPSIZE,population,ptnatm,runatm,cnatm,cell,argc, argv); /// for (i=0;i<POPSIZE;i++){ //////// center(ptnatm+runatm,population[i].gen); /// write_coord(fpoptim,ptnatm,runatm,cnatm,population[i].gen,population[i].cgen,population[i].ep); // shift(ptnatm+runatm, population[i].gen,dptc); /// } // for (i=0;i<POPSIZE;i++) // write_coord(fpoptim,ptnatm,runatm,cnatm,population[i].gen,population[i].cgen,population[i].ep); fflush(fpoptim); } char command[30]; char filename[30]; sprintf(command,"mkdir core%d",rank); system(command); sprintf(command,"cp dftb_in.hsd *.coord *.skf core%d",rank); system(command); sprintf(filename,"core%d",rank); chdir(filename); system("pwd"); //****************************************************************************************// // Loop started // //****************************************************************************************// for (step=0;step<NSTEP;step++){ //*********************************************************************************// // master core preparation // //*********************************************************************************// if(rank==0){ cal_pop_energy(POPSIZE,population,ptnatm,runatm,cnatm,cell, argc, argv); printf("\n\n\n\n***********************************************\n"); printf( "***********************************************\n"); printf( "***********************************************\n"); printf("Gen= %d starting optimization..................\n\n",step); qsort (population, POPSIZE, sizeof(ga_struct),sort_func); normal_fitness(POPSIZE,population); for(i=0;i<POPSIZE;i++){ fprintf(fpenergy,"%d num %d %lf\n",step, i,population[i].ep); fflush(fpenergy); } printf("fabs %lf\n",fabs(population[0].ep-population[POPSIZE-1].ep)); if(fabs(population[0].ep-population[POPSIZE-1].ep)<delte){ fprintf(fpenergy,"%d %lf %lf global minimum \n",step,population[0].ep,population[0].fitness); globe[glob]=population[POPSIZE-1].ep; glob=glob+1; } if(glob>20){ if(fabs(globe[glob]-globe[glob-20])<delte){ fprintf(fpenergy,"%d %lf %lf final global minimum \n",step,population[0].ep,population[0].fitness); flag_converge=1; } } ///// PPreserve the first esize parentes from the previous generation. copy from population to beta_population esize=POPSIZE*ELITRATE; if (esize<1){esize=1;} elitism(esize,ptnatm+runatm, cnatm,population,beta_population); for (i=1;i<num_processors;i++) MPI_Send(population,POPSIZE,popDatatype,i,0,MPI_COMM_WORLD); //send coordinates and energy information to other ith processors }else MPI_Recv(population,POPSIZE,popDatatype,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE); //*********************************************************************************// // master core preparation ended // //*********************************************************************************// //*********************************************************************************// // other cores mating start // //*********************************************************************************// //receive coordinates and energy information from 0(master) core MPI_Bcast(&flag_converge, 1, MPI_INT,0,MPI_COMM_WORLD); // printf("rank=%d,xyz=%lf\n",rank,population[0].gen[0][0]); if(flag_converge ==1) {MPI_Finalize(); exit(0);} // for (i=0;i<POPSIZE;i++) // write_coord(fpoptim,ptnatm,runatm,cnatm,population[i].gen,population[i].cgen,population[i].ep); fflush(fpoptim); // printf("rank=%d,xyz=%lf\n",rank,population[0].gen[0][0]); // Generate the rest part of beta_generation by mating process if(rank!=0&&rank<POPSIZE) mate(ptnatm,runatm,cnatm,cell,esize,POPSIZE,population,beta_population,Temp,ee_mate,dptc,min_step,argc, argv); MPI_Barrier(MPI_COMM_WORLD); if(rank!=0&&rank<POPSIZE) MPI_Send(&beta_population[0],1,popDatatype,0,1,MPI_COMM_WORLD); //send the information of first children(optimized) from other processors to master core //*********************************************************************************// // other cores mating ended // //*********************************************************************************// if(rank==0){ for (i=1;i<POPSIZE;i++) MPI_Recv(&population[i],1,popDatatype,i,1,MPI_COMM_WORLD,MPI_STATUS_IGNORE); //recieve coordinates and energy information to other ith processors if (ptnatm!=0&&runatm!=0) mutate_perm (ptnatm,runatm, Mu, POPSIZE, beta_population); fprestart=fopen("./restart","w"); for (i=0;i<POPSIZE;i++){ write_coord(fpoptim,ptnatm,runatm,cnatm,population[i].gen,population[i].cgen,population[i].ep); write_coord(fprestart,ptnatm,runatm,cnatm,population[i].gen,population[i].cgen,population[i].ep); fflush(fpoptim); fflush(fprestart); } fclose(fprestart); clock_gettime(CLOCK_REALTIME, &now); seconds_new = (double)((now.tv_sec+now.tv_nsec*1e-9)); seconds=seconds_new-seconds_old; seconds_total = (double)((now.tv_sec+now.tv_nsec*1e-9) - (double)(tmstart.tv_sec+tmstart.tv_nsec*1e-9)); printf("\nWall time for this generation is %lf s\n",seconds); printf("\nWall time totally %lf s\n",seconds_total); seconds_old=seconds_new; } } //********************************************************************************* //************************************loop ended*********************************** //********************************************************************************* if(rank==0){ printf("\n********************JOB FINISHED*****************************\n"); fclose(fpenergy); fclose(fpoptim); ///////// time information current_time = time(NULL); c_time_string = ctime(¤t_time); printf("Current time is %s\n", c_time_string); // measure elapsed wall time clock_gettime(CLOCK_REALTIME, &now); seconds = (double)((now.tv_sec+now.tv_nsec*1e-9) - (double)(tmstart.tv_sec+tmstart.tv_nsec*1e-9)); printf("wall time %fs\n", seconds); // measure CPU time end = (double)clock() / (double) CLOCKS_PER_SEC; printf("cpu time %fs\n", end - start); printf("\n********************JOB FINISHED*****************************\n"); free(population); free(beta_population); } }