遗传算法求解fxxcosx2的最大值.docx
《遗传算法求解fxxcosx2的最大值.docx》由会员分享,可在线阅读,更多相关《遗传算法求解fxxcosx2的最大值.docx(25页珍藏版)》请在冰豆网上搜索。
![遗传算法求解fxxcosx2的最大值.docx](https://file1.bdocx.com/fileroot1/2023-7/7/41bfa2af-e728-41c4-8540-343924756646/41bfa2af-e728-41c4-8540-3439247566461.gif)
遗传算法求解fxxcosx2的最大值
遗传算法求解f(x)=xcosx+2的最大值
其中在尺度变换部分应用到了类似模拟退火算法部分,所有变量均使用汉语拼音很好懂
//中国电子科技集团公司
//第一研究室
//呼文韬
//
//随机初始种群
//编码方式为格雷码
//选择方法为随机遍历
//采用了精英保存策略
//采用了自适应的交叉率和变异率
//采用了与模拟退火算法相结合的尺度变换
//采用了均匀交叉法
#include
#include
#include
#include
#include
#include
#include
#defineIM163
#defineIM299
#defineAM(1.0/IM1)
#defineIMM1(IM1-1)
#defineIA140014
#defineIA240692
#defineIQ153668
#defineIQ252774
#defineIR112211
#defineIR23791
#defineNTAB32
#defineNDIV(1+IMM1/NTAB)
#defineEPS1.2e-7
#defineRNMX(1.0-EPS)
#definezhizhenjuli0.005
#definePI3.
#defineT0100000//温度要取得很高才行。
#definezhongqunshu1200
#definezuobianjie-2000
#defineyoubianjie2000
unsignedintseed=0;//seed为种子,要设为全局变量
voidmysrand(longinti)//初始化种子
{
seed=-i;
}
longa[1];
//doublehundun;
//doublec=4;
//设置全局变量
structindividual
{
unsigned*chrom;//染色体;
doublegeti;//变量值
doubleshiyingdu;//目标函数的值;
doublefitness;//变换后的适应度值;
};
individual*zuiyougeti;//精英保存策略
intzhongqunshu;//种群大小
individual*nowpop;//当前代
individual*newpop;//新一代
doublesumfitness;//当代的总适应度fitness
doublesumshiyingdu;//当代的总适应度shiyingdu
doublemaxfitness;//最大适应度
doubleavefitness;//平均适应度
doublemaxshiyingdu;//最大适应度
doubleavgshiyingdu;//平均适应度
floatpc;//交叉概率
floatpm;//变异概率
intlchrom;//染色体长度
intmaxgen;//最大遗传代数
intgen;//遗传代数
//函数
intflipc(double,double);//判断是否交叉
intflipm(double);//判断是否变异
intrnd(intlow,inthigh);//产生low与high之间的任意数
voidinitialize();//遗传算法初始化
voidpreselectfitness();//计算sumfiness,avefitness,maxfitness
voidgeneration();
doublesuijibianli();//产生随机遍历指针
intfuzhi(float);//选择要复制的个体
voidcrossover(individual,individual,individual&,individual&);//交叉
voidbianyi(individual&);//变异
voidmubiaohanshu(individual&);//计算适应度
voidchidubianhuan(individual&);//对shiyingdu进行尺度变换赋给fitness
doubleran1(long*);//随机数初始
voidbianma(doublebianliang,unsigned*p);//编码
doubleyima(unsigned*p);
voidguanjiancanshujisuan();//计算shiyingdu,根据shiyingdu计算sumshiyingdu,对shiyingdu进行尺度变换变成fitness,根据fitness计算sumfitness,avefitness,maxfitness
voidjingyingbaoliu();
voidglp(intn,ints,int*,int(*)[1],float(*)[1]);//glp生成函数
BOOLExist(intVal,intNum,int*Array);//判断一个数在前面是否出现过
intcmpfitness(constvoid*p1,constvoid*p2)
{
floati=((individual*)p1)->shiyingdu;//现在是按照"适应度"排序,改成"个体"的话就是按照"个体"排序
floatj=((individual*)p2)->shiyingdu;
returni-1:
(i==j?
0:
1);//现在是按升序牌排列,将1和-1互换后就是按降序排列
}
voidmain()
{
initialize();
cout<geti<<""<shiyingdu<for(gen=1;gen{generation();
}
jingyingbaoliu();
cout<:
fixed)<geti<<""<:
fixed)<shiyingdu)<delete[]newpop;
delete[]nowpop;
delete[]zuiyougeti;
system("pause");
}
voidinitialize()
{
intq[zhongqunshu1][1],s=1;
floatxx[zhongqunshu1][1];//生成的glp用x储存
inth[1]={1};//生成向量
zuiyougeti=newindividual;//最优个体的生成
zhongqunshu=200;//种群数量
nowpop=newindividual[zhongqunshu1];//当代
newpop=newindividual[zhongqunshu1];//新一代
maxgen=150;//最大代数
gen=0;//起始代
lchrom=22;//基因数量的初始化
mysrand(time(0));//随机数种子
a[0]=seed;//随机数种子
//对最优个体的初始化
zuiyougeti->geti=0;
zuiyougeti->fitness=0;
zuiyougeti->shiyingdu=0;
//
glp(zhongqunshu,s,h,q,xx);
//for(inti=0;i//{
//for(intj=0;j
//{
//nowpop[i].geti=zuobianjie+(youbianjie-zuobianjie)*xx[i][j];
//}
//}
for(inti=0;i{
nowpop[i].geti=zuobianjie+(youbianjie-(zuobianjie))*ran1(a);
}
//nowpop[0].geti=999;//////////////////////////
guanjiancanshujisuan();
jingyingbaoliu();//精英保留的实现
guanjiancanshujisuan();//计算shiyingdu,根据shiyingdu计算sumshiyingdu,对shiyingdu进行尺度变换变成fitness,根据fitness计算sumfitness,avefitness,maxfitness
}
voidjingyingbaoliu()//精英保留的实现
{
individual*zuiyougetiguodu;
zuiyougetiguodu=newindividual[zhongqunshu1];//建立一个过渡数组
for(inti=0;izuiyougetiguodu[i]=nowpop[i];
qsort(zuiyougetiguodu,zhongqunshu1,sizeof(individual),&cmpfitness);//按fitness升序排序
//cout<<"zuiyougetiguodu适应度:
"<//cout<<"zuiyougeti适应度:
"<shiyingdu<//system("pause");
if(zuiyougetiguodu[zhongqunshu-1].shiyingdu>zuiyougeti->shiyingdu)
{
*zuiyougeti=zuiyougetiguodu[zhongqunshu1-1];//如果最优个体的fitness比当代最大的fitness小则用当代的代替之
//cout<<"zuiyougetiguodu个体:
"<//cout<<"zuiyougeti个体:
"<geti<}
else
nowpop[rnd(0,(zhongqunshu1-1))]=*zuiyougeti;//否则的话从当代中随机挑选一个用最优个体代替之
delete[]zuiyougetiguodu;//释放过渡数组
}
voidguanjiancanshujisuan()//计算shiyingdu,根据shiyingdu计算sumshiyingdu,对shiyingdu进行尺度变换变成fitness,根据fitness计算sumfitness,avefitness,maxfitness
{
for(inti=0;imubiaohanshu(nowpop[i]);
for(i=0;ichidubianhuan(nowpop[i]);
preselectfitness();//根据fitness计算sumfitness,avefitness,maxfitness
}
voidmubiaohanshu(individual&bianliang)//计算shiyingdu
{
bianliang.shiyingdu=(bianliang.geti*cos(bianliang.geti)+2.0);//目标函数
}
voidchidubianhuan(individual&bianliang)//对shiyingdu进行尺度变换变成fitness
{
doubleT;//退火温度
T=T0*(pow(0.99,(gen+1-1)));
doublesum=0;
for(intj=0;jsum+=exp(nowpop[j].shiyingdu/T);
bianliang.fitness=exp(bianliang.shiyingdu/T)/sum;//算出fitness
}
voidpreselectfitness()//根据fitness计算sumfitness,avefitness,maxfitness
{
intj;
sumfitness=0;
for(j=0;jsumfitness+=nowpop[j].fitness;
individual*guodu;
guodu=newindividual[zhongqunshu1];
for(j=0;jguodu[j]=nowpop[j];
qsort(guodu,zhongqunshu1,sizeof(individual),&cmpfitness);
maxfitness=guodu[zhongqunshu1-1].fitness;
avefitness=sumfitness/zhongqunshu1;
delete[]guodu;
}
voidgeneration()
{
individualfuqin1,fuqin2,*pipeiguodu,*pipeichi;
int*peiduishuzu;//用来存放产生的随机配对
pipeiguodu=newindividual[zhongqunshu1];
pipeichi=newindividual[zhongqunshu1];
peiduishuzu=newint[zhongqunshu1];
intmember1,member2,j=0,fuzhijishu=0,i=0,temp=0,tt=0;
floatzhizhen;
//随机遍历的实现
for(zhizhen=suijibianli();zhizhen<1;(zhizhen=zhizhen+zhizhenjuli))//设定指针1/66
{
pipeichi[fuzhijishu]=nowpop[fuzhi(zhizhen)];
fuzhijishu++;
}
//交叉与变异的实现
//交叉
for(i=0;i{
peiduishuzu[i]=-1;
}
for(i=0;i{
temp=rnd(0,zhongqunshu1-1);//产生值在0-zhongqunshu1-1的随机数
while(Exist(temp,i,peiduishuzu))//判断产生的随机数是否已经产生过,如果是,则再产生一个随机数
{
temp=rnd(0,zhongqunshu1-1);
}
//如果没有的话,则把产生的随机数放在peiduishuzu中
*(peiduishuzu+i)=temp;
}
for(i=0;i{
fuqin1=pipeichi[peiduishuzu[i]];
fuqin2=pipeichi[peiduishuzu[i+1]];
crossover(fuqin1,fuqin2,newpop[i],newpop[i+1]);
}
for(j=0;j{
//if(newpop[j].geti<-1000)
//cout<<"个体数值小于下界了";
nowpop[j].geti=newpop[j].geti;
}
//
guanjiancanshujisuan();
//变异的实现
for(j=0;j{
bianyi(nowpop[j]);
}
//
guanjiancanshujisuan();
//精英保留的实现
jingyingbaoliu();
//
guanjiancanshujisuan();
delete[]peiduishuzu;
delete[]pipeichi;
delete[]pipeiguodu;
}
voidcrossover(individualparent1,individualparent2,individual&child1,individual&child2)//交叉
{
intj;
unsigned*panduan;
panduan=newunsigned[lchrom];
parent1.chrom=newunsigned[lchrom];
parent2.chrom=newunsigned[lchrom];
child1.chrom=newunsigned[lchrom];
child2.chrom=newunsigned[lchrom];
//cout<<"jiaocha"<bianma(parent1.geti,parent1.chrom);
bianma(parent2.geti,parent2.chrom);
if(flipc(parent1.fitness,parent2.fitness))
{
for(j=0;jpanduan[j]=rnd(0,1);
//for(j=0;j//{
//cout<//}
//cout<//system("pause");////////////////
for(j=0;j{
if(panduan[j]==1)
child1.chrom[j]=parent1.chrom[j];
else
child1.chrom[j]=parent2.chrom[j];
}
for(j=0;j{
if(panduan[j]==0)
child2.chrom[j]=parent1.chrom[j];
else
child2.chrom[j]=parent2.chrom[j];
}
//for(j=0;j//{
//cout<//}
//cout<//system("pause");////////////////
child1.geti=yima(child1.chrom);
child2.geti=yima(child2.chrom);
delete[]child2.chrom;
delete[]child1.chrom;
delete[]parent2.chrom;
delete[]parent1.chrom;
delete[]panduan;
}
else
{
for(j=0;j{
child1.chrom[j]=parent1.chrom[j];
child2.chrom[j]=parent2.chrom[j];
}
child1.geti=yima(child1.chrom);
child2.geti=yima(child2.chrom);
delete[]child2.chrom;
delete[]child1.chrom;
delete[]parent2.chrom;
delete[]parent1.chrom;
delete[]panduan;
}
}
voidbianyi(individual&child)//变异
{
child.chrom=newunsigned[lchrom];
//cout<<"变异"<bianma(child.geti,child.chrom);
for(inti=0;iif(flipm(child.fitness))
{
if(child.chrom[i]=0)
child.chrom[i]=1;
else
child.chrom[i]=0;
}
child.geti=yima(child.chrom);
delete[]child.chrom;
}
voidbianma(doublebianliang,unsigned*p)//编码
{
unsigned*q;
unsigned*gray;
q=newunsigned[lchrom];
gray=newunsigned[lchrom];
intx=0;
inti=0,j=0;
if(bianliang{
cout<<"bianliang:
"<system("pause");
}
//cout<//system("pause");
x=(bianliang-(zuobianjie))*((pow(2,lchrom)-1)/(youbianjie-(zuobianjie)));
//co