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

📄 nsga2.c

📁 CODE OF NSGA,I hope that it will help you,thank you~
💻 C
📖 第 1 页 / 共 2 页
字号:
  fprintf(rep_ptr,"Statistics at Generation 0 ->\n");  fprintf(rep_ptr,"--------------------------------------------------\n");    /********************************************************************/  /*----------------------GENERATION STARTS HERE----------------------*/  for (i = 0;i < gener;i++)    {      printf("Generation = %d\n",i+1);      old_pop_ptr = &(oldpop);      mate_pop_ptr = &(matepop);      fprintf(rep_ptr,"Population at generation no. -->%d\n",i+1);      fprintf(gen_ptr,"#Generation No. -->%d\n",i+1);      fprintf(gen_ptr,"#Variable_vector  Fitness_vector Constraint_violation Overall_penalty\n");            /*--------SELECT----------------*/      nselect(old_pop_ptr ,mate_pop_ptr );            new_pop_ptr = &(newpop);      mate_pop_ptr = &(matepop);            /*CROSSOVER----------------------------*/            if (nchrom > 0) 	{	  	  if(optype == 1)	    {	      crossover(new_pop_ptr ,mate_pop_ptr );	      /*Binary Cross-over*/	    }	  	  if(optype == 2)	    {	      unicross(new_pop_ptr ,mate_pop_ptr );	      /*Binary Uniform Cross-over*/	    }	}      if (nvar > 0) 	realcross(new_pop_ptr ,mate_pop_ptr );      /*Real Cross-over*/                  /*------MUTATION-------------------*/      new_pop_ptr = &(newpop);            if (nchrom > 0)	mutate(new_pop_ptr );      /*Binary Mutation */            if (nvar > 0)	real_mutate(new_pop_ptr );      /*Real Mutation*/            new_pop_ptr = &(newpop);            /*-------DECODING----------*/      if(nchrom > 0)	decode(new_pop_ptr );      /*Decoding for binary strings*/            /*----------FUNCTION EVALUATION-----------*/      new_pop_ptr = &(newpop);      func(new_pop_ptr );            /*-------------------SELECTION KEEPING FRONTS ALIVE--------------*/      old_pop_ptr = &(oldpop);      new_pop_ptr = &(newpop);      mate_pop_ptr = &(matepop);            /*Elitism And Sharing Implemented*/      keepalive(old_pop_ptr ,new_pop_ptr ,mate_pop_ptr,i+1);                  mate_pop_ptr = &(matepop);      if(nchrom > 0)	decode(mate_pop_ptr );            mate_pop_ptr = &(matepop);      /*------------------REPORT PRINTING--------------------------------*/        report(i ,old_pop_ptr ,mate_pop_ptr ,rep_ptr ,gen_ptr, lastit );            /*==================================================================*/            /*----------------Rank Ratio Calculation------------------------*/      new_pop_ptr = &(matepop);      old_pop_ptr = &(oldpop);            /*Finding the greater maxrank among the two populations*/            if(old_pop_ptr->maxrank > new_pop_ptr->maxrank)	maxrank1 = old_pop_ptr->maxrank;      else 	maxrank1 = new_pop_ptr->maxrank;      fprintf(rep2_ptr,"--------RANK AT GENERATION %d--------------\n",i+1);      fprintf(rep2_ptr,"Rank old ranks   new ranks     rankratio\n");      for(j = 0;j < maxrank1 ; j++)	{ 	  /*Sum of the no of individuals at any rank in old population 	    and the new populaion*/	  	  tot = (old_pop_ptr->rankno[j])+ (new_pop_ptr->rankno[j]);	  	  /*Finding the rank ratio for new population at this rank*/	  	  new_pop_ptr->rankrat[j] = (new_pop_ptr->rankno[j])/tot;	  	  /*Printing this rank ratio to a file called ranks.dat*/	  	  fprintf(rep2_ptr," %d\t  %d\t\t %d\t %f\n",j+1,old_pop_ptr->rankno[j],new_pop_ptr->rankno[j],new_pop_ptr->rankrat[j]);	  	}            fprintf(rep2_ptr,"-----------------Rank Ratio-------------------\n");      /*==================================================================*/            /*=======Copying the new population to old population======*/            old_pop_ptr = &(oldpop);      new_pop_ptr = &(matepop);      for(j = 0;j < popsize;j++)	{	  old_pop_ptr->ind_ptr = &(old_pop_ptr->ind[j]);	  new_pop_ptr->ind_ptr = &(new_pop_ptr->ind[j]);	  if(nchrom > 0)	    {	      /*For Binary GA copying of the chromosome*/	      	      for(l = 0;l < chrom;l++)		old_pop_ptr->ind_ptr->genes[l]=new_pop_ptr->ind_ptr->genes[l];	      	      for(l = 0;l < nchrom;l++)		old_pop_ptr->ind_ptr->xbin[l] = new_pop_ptr->ind_ptr->xbin[l];	    }	  if(nvar > 0)	    {	      /*For Real Coded GA copying of the chromosomes*/	      for(l = 0;l < nvar;l++)		old_pop_ptr->ind_ptr->xreal[l] = new_pop_ptr->ind_ptr->xreal[l];	    }	  	  /*Copying the fitness vector */	  	  for(l = 0 ; l < nfunc ;l++)	    old_pop_ptr->ind_ptr->fitness[l] = new_pop_ptr->ind_ptr->fitness[l];	  	  /*Copying the dummy fitness*/	  old_pop_ptr->ind_ptr->cub_len = new_pop_ptr->ind_ptr->cub_len;	  	  /*Copying the rank of the individuals*/	  old_pop_ptr->ind_ptr->rank = new_pop_ptr->ind_ptr->rank;	  	  /*Copying the error and constraints of the individual*/	  	  old_pop_ptr->ind_ptr->error = new_pop_ptr->ind_ptr->error;	  for(l = 0;l < ncons;l++)	    {	      old_pop_ptr->ind_ptr->constr[l] = new_pop_ptr->ind_ptr->constr[l];	    }	  	  /*Copying the flag of the individuals*/	  old_pop_ptr->ind_ptr->flag = new_pop_ptr->ind_ptr->flag;	}   // end of j            maxrank1 = new_pop_ptr->maxrank ;	        /*Copying the array having the record of the individual 	at different ranks */      for(l = 0;l < popsize;l++)	{	  old_pop_ptr->rankno[l] = new_pop_ptr->rankno[l];	}            /*Copying the maxrank */      old_pop_ptr->maxrank = new_pop_ptr->maxrank;            /*Printing the fitness record for last generation in a file last*/      if(i == gener-1)       	{  // for the last generation 	  old_pop_ptr = &(matepop);	  for(f = 0;f < popsize ; f++) // for printing	    {	      old_pop_ptr->ind_ptr = &(old_pop_ptr->ind[f]);	      	      if ((old_pop_ptr->ind_ptr->error <= 0.0) && (old_pop_ptr->ind_ptr->rank == 1))  // for all feasible solutions and non-dominated solutions		{		  for(l = 0;l < nfunc;l++)		    fprintf(end_ptr,"%f\t",old_pop_ptr->ind_ptr->fitness[l]);		  for(l = 0;l < ncons;l++)		    {		      fprintf(end_ptr,"%f\t",old_pop_ptr->ind_ptr->constr[l]);		    }		  if (ncons > 0)		    fprintf(end_ptr,"%f\t",old_pop_ptr->ind_ptr->error);		  fprintf(end_ptr,"\n");		  		  if (nvar > 0)		    {		      for(l = 0;l < nvar ;l++)			{			  fprintf(g_var,"%f\t",old_pop_ptr->ind_ptr->xreal[l]);			}		      fprintf(g_var,"  ");		    }		  		  if(nchrom > 0)		    {		      for(l = 0;l < nchrom;l++)			{			  fprintf(g_var,"%f\t",old_pop_ptr->ind_ptr->xbin[l]);			}		    }		  fprintf(g_var,"\n");		}  // feasibility check	    } // end of f (printing)	  	} // for the last generation    }  // end of i   /*                   Generation Loop Ends                                */  /************************************************************************/    fprintf(rep_ptr,"NO. OF CROSSOVER = %d\n",ncross);  fprintf(rep_ptr,"NO. OF MUTATION = %d\n",nmut);  fprintf(rep_ptr,"------------------------------------------------------------\n");  fprintf(rep_ptr,"---------------------------------Thanks---------------------\n");  fprintf(rep_ptr,"-------------------------------------------------------------\n");  printf("NOW YOU CAN LOOK IN THE FILE OUTPUT2.DAT\n");    /*Closing the files*/  fclose(rep_ptr);  fclose(gen_ptr);  fclose(rep2_ptr);  fclose(end_ptr);  fclose(g_var);  fclose(lastit);}

⌨️ 快捷键说明

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