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

📄 传统遗传算法.cpp

📁 传统遗传算法程序。开发环境为visual C
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            newpopulation[i] = population[0];      
      else
            {
            for (j = 0; j < POPSIZE;j++)      
                  if (p >= population[j].cfitness && 
                              p<population[j+1].cfitness)
                        newpopulation[i] = population[j+1];
            }
      }
/* once a new population is created, copy it back */

for (i = 0; i < POPSIZE; i++)
      population[i] = newpopulation[i];      
}

/***************************************************************/
/* Crossover selection: selects two parents that take part in  */
/* the crossover. Implements a single point crossover          */
/***************************************************************/

void crossover(void)
{
int i, mem, one;
int first  =  0; /* count of the number of members chosen */
double x;

for (mem = 0; mem < POPSIZE; ++mem)
      {
      x = rand()%1000/1000.0;
	  one=0;
	  //PXOVER=recompu1(one,mem);

      if (x < PXOVER)
            {
            ++first;
            if (first % 2 == 0)
                  Xover(one, mem);
            else
                  one = mem;
            }
      }
}
/**************************************************************/
/* Crossover: performs crossover of the two selected parents. */
/**************************************************************/

void Xover(int one, int two)
{
int i;
int point; /* crossover point */

/* select crossover point */
if(NVARS > 1)
   {
   if(NVARS == 2)
         point = 1;
   else
         point = (rand() % (NVARS - 1)) + 1;

   for (i = 0; i < point; i++)
        swap(&population[one].gene[i], &population[two].gene[i]);

   }
}

/*************************************************************/
/* Swap: A swap procedure that helps in swapping 2 variables */
/*************************************************************/

void swap(double *x, double *y)
{
double temp;

temp = *x;
*x = *y;
*y = temp;

}

/**************************************************************/
/* Mutation: Random uniform mutation. A variable selected for */
/* mutation is replaced by a random value between lower and   */
/* upper bounds of this variable                              */
/**************************************************************/

void mutate(void)
{
int i, j;
double lbound, hbound;
double x;
for (i = 0; i < POPSIZE; i++)
      for (j = 0; j < NVARS; j++)
            {
            x = rand()%1000/1000.0;
			//PMUTATION=recompu2(i);
            if (x < PMUTATION)
                  {
                  /* find the bounds on the variable to be mutated */
                  lbound = population[i].lower[j];
                  hbound = population[i].upper[j];  
                  population[i].gene[j] = randval(lbound, hbound);
                  }
            }
}

/***************************************************************/
/* Report function: Reports progress of the simulation. Data   */
/* dumped into the  output file are separated by commas        */
/***************************************************************/

void report(void)
{
int i;
double best_val;            /* best population fitness */
double avg;                 /* avg population fitness */
double stddev;              /* std. deviation of population fitness */
double sum_square;          /* sum of square for std. calc */
double square_sum;          /* square of sum for std. calc */
double sum;                 /* total population fitness */

sum = 0.0;
sum_square = 0.0;

for (i = 0; i < POPSIZE; i++)
      {
      sum += population[i].fitness;
      sum_square += population[i].fitness * population[i].fitness;
      }

avg = sum/(double)POPSIZE;
square_sum = avg * avg * POPSIZE;
stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));
best_val = population[POPSIZE].fitness;

/*fprintf(galog, "\n%5d,      %6.9f, %6.9f, %6.9f ", generation, 
                                      best_val, avg, stddev);*/
}

/*************************************************************/
/* recomputate the pxover by the real time environment         */
/*************************************************************/

double recompu1(int a,int b)
{
	int i;
	double k1=1.0,k3=1.0,ff,fmax;
	double sum=0,avg;
	for (i = 0; i < POPSIZE; i++)
	{
		sum += population[i].fitness;
		//sum_square += population[i].fitness * population[i].fitness;
	}
	avg = sum/(double)POPSIZE;
	if(population[a].fitness>population[b].fitness)ff=population[a].fitness;
	else ff=population[b].fitness;
	fmax=population[POPSIZE].fitness;
	if(ff>=avg)return (fmax-ff)*k1/(fmax-avg);
	else return k3;
}

/*************************************************************/
/* recomputate the pmutaion by the real time envirment       */
/*************************************************************/

double recompu2(int a)
{
	int i;
	double k2=0.5,k4=0.5,ff,fmax;
	double sum=0,avg;
	for (i = 0; i < POPSIZE; i++)
	{
		sum += population[i].fitness;
		//sum_square += population[i].fitness * population[i].fitness;
	}
	avg = sum/(double)POPSIZE;
	ff=population[a].fitness;
	fmax=population[POPSIZE].fitness;
	if(ff>=avg)return (fmax-ff)*k2/(fmax-avg);
	else return k4;
}



/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then          */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied                                     */
/**************************************************************/

void main(void)
{
int i,j;

int gent;
double tmp;

if ((galog = fopen("galog.txt","w"))==NULL)
      {
      exit(1);
      }

	
//for(PXOVER=0.1;PXOVER<=0.9;PXOVER+=0.1)
	//for(PMUTATION=0.01;PMUTATION<=0.1;PMUTATION+=0.01)
	//{
		//gen=1000;
		//best1=0;
	long t1,t2;
		for(i=0;i<runtime;i++)  //计算100次
		{
			memset(population,0,sizeof(population));//将数组初始化 此处为置为0

			t1=clock();

			initialize();

			generation = 0;

			gent=0;
			tmp=-1;

			/*fprintf(galog, "\n generation  best  average  standard \n");
			fprintf(galog, " number      value fitness  deviation \n");*/

			
			evaluate();
			keep_the_best();
			while(generation<MAXGENS)
			{
				  generation++;
				  select();
				  
				  /*if(generation<0.382*600)
				  {
					  PXOVER=1;
					  PMUTATION=0.1;
				  }
				  else if(generation<0.618*600)
				  {
					  PXOVER=0.75;
					  PMUTATION=1/80;
				  }
				  else 
				  {
					  PXOVER=0.5;
					  PMUTATION=0.1/80;
				  }*/


				  crossover();
				  mutate();
				  report();
				  evaluate();
				  elitist();
				  if(population[POPSIZE].fitness>tmp)
				  {
					  tmp=population[POPSIZE].fitness;  //收敛时的适应值
					  gen=generation;   //进化到收敛的代数
				  }
			}
			/*fprintf(galog,"\n\n Simulation completed\n");
			fprintf(galog,"\n Best member: \n");*/

			/*for (i = 0; i < NVARS; i++)
			   {
			   fprintf (galog,"\n var(%d) = %3.9f",i,population[POPSIZE].gene[i]);
			   }*/
			//gen+=gent;
			//best1+=tmp;
			//if(gen>gent)gen=gent;
			//if(best1<tmp)best1=tmp;
		//}
		t2=clock();
		fprintf(galog,"%d\t%.9f\t%f\t%d\n",gen,tmp,(180.05-tmp)/180.05,t2-t1);//(180.05-tmp)/180.05为误差的计算,t1-t2表示所用的时间
		
		printf("Success\n");
	}
	
}

⌨️ 快捷键说明

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