?? gtsf.txt
字號(hào):
改進(jìn)遺傳算法-郭濤算法
改進(jìn)遺傳算法-郭濤算法做最優(yōu)化問題很管用,算法的基本思想是
先任意產(chǎn)生n個(gè)隨機(jī)數(shù),然后從n個(gè)數(shù)里隨機(jī)選擇m個(gè)數(shù),再有這m個(gè)
數(shù)合成一個(gè)新數(shù),將這個(gè)新數(shù)同n個(gè)數(shù)中間適應(yīng)值函數(shù)值的最差的比較,
如果好的話就取代最差的那個(gè),如果它比最好的還要好的話,則把最好的
也取代。如果比最差的壞,則重新合成一個(gè)新數(shù)。依次循環(huán)下去。
程序的奇妙之處是GA_crossover()函數(shù),產(chǎn)生的新數(shù)確實(shí)比較好,看看
那位大俠能改進(jìn)一下,產(chǎn)生比這跟好的數(shù)。
下面是源代碼:
#i nclude <stdio.h>
#i nclude <stdlib.h>
#i nclude <math.h>
#i nclude <windows.h>
#define GANVARS 2 //函數(shù)維數(shù)
#define GA_popsize 50 //種群數(shù) n
#define GA_selectsize 8 //種群中挑出來的個(gè)數(shù)m
#define GA_maxgen 2000 //最大循環(huán)次數(shù)
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);
}
資料提供:http://topic.csdn.net/t/20030511/21/1769628.html
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -