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

📄 改进遗传算法-郭涛算法做最优化问题很管用.txt

📁 改进遗传算法-郭涛算法做最优化问题很管用,算法的基本思想是 先任意产生n个随机数
💻 TXT
字号:
主  题:  改进遗传算法-郭涛算法做最优化问题很管用 
作  者:  yfwf ()  
等  级:    
信 誉 值:  99 
所属论坛:  C/C++ 标准库 / 其它C++程序库 
问题点数:  0 
回复次数:  8 
发表时间:  2003-05-11 21:16:28Z 
   
 
   

改进遗传算法-郭涛算法做最优化问题很管用,算法的基本思想是
先任意产生n个随机数,然后从n个数里随机选择m个数,再有这m个
数合成一个新数,将这个新数同n个数中间适应值函数值的最差的比较,
如果好的话就取代最差的那个,如果它比最好的还要好的话,则把最好的
也取代。如果比最差的坏,则重新合成一个新数。依次循环下去。
程序的奇妙之处是GA_crossover()函数,产生的新数确实比较好,看看
那位大侠能改进一下,产生比这跟好的数。

下面是源代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>

#define GANVARS 2  //函数维数
#define GA_popsize 50  //种群数 n
#define GA_selectsize 8 //种群中挑出来的个数m
#define GA_maxgen 2000 //最大循环次数

void GA_init(void);
int better(int,int);
void GA(void);
void  GA_crossover(void);
void GA_elist(void);
int rabdi(int);
double randval(double,double);
double GA_evaluate(int);

int GA_worstIndex;
int GA_bestIndex;
double GA_pop[GA_popsize+1][GANVARS];
double lowBound[GANVARS],upperBound[GANVARS];
double GA_fitness[GA_popsize+1];
double MateParam[GA_selectsize];
FILE   *fp;

void GA_init(void)
{
int i,j;

/*lowBound[0]=-3.0;
upperBound[0]=12.1;
lowBound[1]=4.1;
upperBound[1]=5.8;*/
lowBound[0]=-100;
upperBound[0]=100;
lowBound[1]=-100;
upperBound[1]=100;
srand(GetTickCount());
for(i=0;i<GANVARS;i++)
{
	for(j=0;j<GA_popsize;j++)
	{
		GA_pop[j][i]=randval(lowBound[i],upperBound[i]);
	}
}
}



/************************************************/
double randval(double low,double high)
{
	double  val;
	val=((double)(rand()%1000)/1000.0)*(high-low)+low;
	return (val);
}


  
int rabdi( int Nmem)
{
	int newval;
	newval=(int)(((rand()%1000)/1000.0)*Nmem);
	return(newval);

}


/********************************************************/
void  GA_crossover()
{
    int i,j,temp,MatePool[GA_popsize];
    int validChild;
    double mbl2,mbl3,t;
    double sumA,tmpMin,tmpMax,MaxA,MinA;
    MinA=-0.5;
    MaxA=1.5;
    int oldrd;
    validChild=0;
    for(i=0;i<GA_popsize;i++)
         MatePool[i]=i;
    for(;validChild==0;)
	{
		for(i=0;i<GA_selectsize;i++)
		{
			oldrd=rabdi(GA_popsize-i);
		    j=i+oldrd;
		    temp=MatePool[i];
		    MatePool[i]=MatePool[j];
		    MatePool[j]=temp;
			//MatePool[i]=rabdi(GA_popsize);
	    }
	    sumA=0.0;
	    for(i=0;i<=GA_selectsize-2;i++)
		{
			double mt1;
		    mt1=GA_selectsize-i+1;
		    tmpMin=1-sumA-MaxA*mt1;
		    tmpMax=1-sumA-MinA*mt1;
		    if(tmpMin<MinA)
			     tmpMin=MinA;
		    if(tmpMax>MaxA)
			     tmpMax=MaxA;
		    MateParam[i]=randval(tmpMin,tmpMax);
		    sumA=sumA+MateParam[i];
		}
	    MateParam[GA_selectsize-1]=1-sumA;
	    validChild=1;
	    for(i=0;i<GANVARS;i++)
		{
			GA_pop[GA_popsize][i]=0;
			for(j=0;j<GA_selectsize;j++)
			{  GA_pop[GA_popsize][i]+=GA_pop[MatePool[j]][i]*MateParam[j]; }		
	        t=GA_pop[GA_popsize][i];
            mbl2=lowBound[i];
            mbl3=upperBound[i];
            if(t<mbl2||t>mbl3)
			{
			     validChild=0;
			      break;
			}
		}
	}
}


/********************************************************/
double GA_evaluate(int GA_mem)
{
	int i;
    double x[10],Mplace;
	for (i=0;i<GANVARS;i++)
	  x[i]=GA_pop[GA_mem][i];
     Mplace=pow(x[0]*x[0]+x[1]-11,2)+pow(x[0]+x[1]*x[1]-7,2);
		 //-21.5-x[0]*sin(4*atan(1)*4*x[0])-x[1]*sin(20*atan(1)*4*x[1]);
		 //
		 //100*(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+(1-x[0])*(1-x[0]);
		 //2*x[0]*x[0]-1.05*pow(x[0],4)+pow(x[0],6)/6.0-x[0]*x[1]+x[1]*x[1];
		 
     // Mplace=-(20+x[0]*cos(x[1])+x[1]*sin(x[0]));
	 //Mplace=(1.5-x[1]*(1-x[2]))*(1.5-x[1]*(1-x[2]))+(2.25-x[1]*(1-x[2]*x[2]))*
	      //(2.25-x[1]*(1-x[2]*x[2]))+(2.625-x[1]*(1-x[2]*x[2]*x[2]))*(2.625-x[1]*(1-x[2]*x[2]*x[2]));
	return( Mplace);
}

/*****************************************************/


int better(int GA_mem1,int GA_mem2)
{ 
	double  t1,t2;
    t1=GA_evaluate(GA_mem1);
    t2=GA_evaluate(GA_mem2);	 
	if(t1<=t2)
	  return(1);
    else
	  return(0);
}

/*****************************************************/


void GA()
{
	int i,bl;
    int gen=1;
    double mt1,mt2;
    double GA_temp;
    GA_init();
    for(i=0;i<GA_popsize;i++)
   {
	   fprintf(fp,"\n %d     %8.4f",i,GA_pop[i][0]);   
   }
    GA_elist();
    mt1=GA_fitness[GA_worstIndex];
    mt2=GA_fitness[GA_bestIndex];
    if((fabs(mt1-mt2))>0.00000001)   bl=1;
    else    bl=0;
    while(bl&&gen<=GA_maxgen)
	{
		GA_crossover();
        GA_fitness[GA_popsize]=GA_evaluate(GA_popsize);
        GA_temp=GA_fitness[GA_popsize];
        if(better(GA_popsize,GA_worstIndex))
		{
			for(i=0;i<GANVARS;i++)
				GA_pop[GA_worstIndex][i]=GA_pop[GA_popsize][i];
	   	    GA_fitness[GA_worstIndex]=GA_temp;
	        if(better(GA_popsize,GA_bestIndex))
			{   GA_bestIndex=GA_worstIndex;   }
	        GA_worstIndex=0;
	        for(i=1;i<GA_popsize;i++)
			{
				if(better(GA_worstIndex,i))
				{  GA_worstIndex=i;   }
			}
            mt1=GA_evaluate(GA_worstIndex);
            mt2=GA_evaluate(GA_bestIndex);
            if((fabs(mt1-mt2))>0.00000001)
                bl=1;
            else 
                bl=0;    
    		printf("\ngen= %d\n",gen);	   
			fprintf(fp,"\n %d     %8.4f",gen,GA_fitness[GA_bestIndex]);
			gen++;
            if(gen>GA_maxgen)
	           break;
		}
	}
}

 

/******************************************/
void GA_elist()
{
	double pt;
    int i;	
//  int k;
    GA_worstIndex=0;
    GA_bestIndex=0;

    for(i=0;i<GA_popsize;i++)
    {
	   pt=GA_evaluate(i);
       GA_fitness[i]=pt;
       if(GA_fitness[i]>GA_fitness[GA_worstIndex])
	   {
		   GA_worstIndex=i;

	   }
       if(GA_fitness[i]<GA_fitness[GA_bestIndex])
	   {
		   GA_bestIndex=i;
	   }
    }
/*	GA_fitness[GA_popsize]=GA_fitness[GA_bestIndex];
	for(i=0;i<GANVARS;i++)
		GA_pop[GA_popsize][i]=GA_pop[GA_bestIndex][i];*/
} 

void main(void)
{

   int i;
   if((fp=fopen("guotaoout.txt","w"))==NULL)
   {
	   printf("\nCannot open input file\n");
       exit(1);
   }
   GA();
   for(i=0;i<GA_popsize;i++)
   {
	   fprintf(fp,"\n %d     %8.4f",i,GA_pop[i][0]);   
   }
   for(i=0;i<GANVARS;i++)
   {
	   fprintf(fp,"\n %d     %8.4f",i,GA_pop[GA_bestIndex][i]);
//     fprintf(fp,"\t%d\t         %f\n", i+1,  pDoc->mLx[di][i]);
   }
   fprintf(fp,"\n\n  Best  fitness=%8.4f\n",GA_fitness[GA_bestIndex]);
   fclose(fp);
}
 

⌨️ 快捷键说明

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