进化算法程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

进化算法作业1全局优化问题(1)422221614121144311.24minxxxxxxxxf..ts55ix,2,1i此问题的全局最优值min1.0316f。一.程序(1)主函数:main.mclearall;clc;popsize=60;%种群规模chromlength=34;%二进制编码,编码精度为0.0001,所以串长l为17pc=0.7;%杂交概率pm=0.1;%变异概率t=0;%进化代数初始为0pop=initpop(popsize,chromlength);%随机产生初始种群whilet500%迭代次数t=t+1;[objvalue]=calobjvalue(pop);%计算目标函数值fitvalue=calfitvalue(objvalue);%计算群体中每个个体的适应度[bestindividual,bestfit]=best(pop,fitvalue);%求出群体中适应度最大的个体及其适应度值x11=decodechrom(bestindividual,1,14);%将二进制数转换为十进制数x22=decodechrom(bestindividual,15,14);x1(t)=-5+10*x11/(pow2(14)-1);%将二值域中的数转换为变量域的数x2(t)=-5+10*x22/(pow2(14)-1);y(t)=4*x1(t)^2-2.1*x1(t)^4+1/3*x1(t)^6+x1(t)*x2(t)-4*x2(t)^2+4*x2(t)^4;%计算最佳个体的目标函数值[newpop1]=selection(pop,fitvalue);%选择算子[newpop2]=crossover(newpop1,pc);%交叉算子[newpop3]=mutation(newpop2,pm);%变异算子objvalue1=calobjvalue(newpop3(1,:));ifobjvalue1y(t)newpop3(1,:)=bestindividual;%保留最佳个体endpop=newpop3;%产生新种群endy;%每代的最佳目标函数值x1;%每代的最佳目标函数值对应的自变量x2;[gy,k]=min(y)%gy为全局最优值,k为最优值对应的进化代数gx1=x1(k)%全局最优值对应的自变量gx2=x2(k)plot(y)%最优值收敛曲线title('收敛性曲线');xlabel('进化代数');ylabel('函数值');axis([0,500,-1.5,1.5]);(2)初始种群:initpop.mfunctionpop=initpop(popsize,chromlength)pop=round(rand(popsize,chromlength));%rand随机产生[0,1]区间的一个小数,rand四舍五入取整end(3)计算目标函数值::calobjvalue.mfunction[objvalue]=calobjvalue(pop)temp1=decodechrom(pop,1,14);temp2=decodechrom(pop,15,14);x1=-5+(10*temp1)/(pow2(14)-1);%将二值域中的数转化为变量域中的数x2=-5+(10*temp2)/(pow2(14)-1);objvalue=4*x1.^2-2.1*x1.^4+1/3*x1.^6+x1.*x2-4*x2.^2+4*x2.^4;%计算目标函数enda.二进制转换为十进制:decodechrom.mfunctiontemp=decodechrom(pop,spoint,length)pop1=pop(:,spoint:spoint+length-1);%按变量个数分组转换,spoint为起始点,length为一个变量的长度temp=decodebinary(pop1);endb.求二进制串对应的十进制数:decodebinary.mfunctiontemp=decodebinary(pop)[px,py]=size(pop);%求pop行数和列数fori=1:pypop1(:,i)=2.^(py-i).*pop(:,i);endtemp=sum(pop1,2);%每一行求和end(4)计算个体适应度:calfitvalue.mfunctionfitvalue=calfitvalue(objvalue)fitvalue=1./(1+exp(objvalue));end(5)种群中最大适应度个体及其值:best.mfunction[bestindividual,bestfit]=best(pop,fitvalue)[px,py]=size(pop);bestindividual=pop(1,:);bestfit=fitvalue(1);fori=2:px;iffitvaluebestfitbestindividual=pop(i,:);best=fitvalue(i);endendend(6)选择算子:selection.mfunction[newpop1]=selection(pop,fitvalue)totalfit=sum(fitvalue);%适应度和ps=fitvalue./totalfit;%单个个体被选择的概率pss=cumsum(ps);%前几项累积和[px,py]=size(pop);ms=sort(rand(px,1));%随机产生px个0,1之间的数,并按升序排列fitin=1;newin=1;whilenewin=pxif(ms(newin)pss(fitin))newpop1(newin,:)=pop(fitin,:);newin=newin+1;elsefitin=fitin+1;endendend(7)交叉算子:crossover.mfunction[newpop2]=crossover(pop,pc)[px,py]=size(pop);newpop2=ones(size(pop));fori=1:2:px-1ifrandpccpoint=round(rand*py);%随机产生一个交叉位newpop2(i,:)=[pop(i,1:cpoint),pop(i+1,cpoint+1:py)];%交换相邻两个个体交叉位之后的基因newpop2(i+1,:)=[pop(i+1,1:cpoint),pop(i,cpoint+1:py)];elsenwepop2(i,:)=pop(i,:);newpop2(i+1,:)=pop(i+1,:);endendend(8)变异算子:mutation.mfunction[newpop3]=mutation(pop,pm)[px,py]=size(pop);newpop3=pop;fori=1:pxif(randpm)mpoint=round(rand*py);%随机产生一个变异位ifmpoint=0mpoint=1;endif(newpop3(i,mpoint)==0)%变为等为基因newpop3(i,mpoint)=1;elsenewpop3(i,mpoint)=0;endendendend二.独立运行程序30次的结果x10.1590-0.0900-0.0888-0.0894-0.08090.07720.08760.1175-0.1578-0.0778x2-0.70160.71260.71020.64210.7120-0.6247-0.7023-0.71380.70290.7090y-1.0115-1.0316-1.0316-0.9751-1.0313-0.9763-1.0308-1.0287-1.0125-1.0310x10.0900-0.0015-0.1566-0.0888-0.0882-0.00890.10040.0766-0.0900-0.0876x2-0.7126-0.70650.71690.71260.7029-0.6931-0.7029-0.71320.71510.7102y-1.0316-0.9989-0.10147-1.0316-1.0309-0.9922-1.0303-1.0309-1.0316-1.0316x1-0.9190.07780.09060.09060.09310.09060.0906-0.0925-0.0705-0.0919x20.7126-0.7114-0.7138-0.7126-0.7816-0.7132-0.70960.71630.69860.7126y-1.0316-1.0331-1.0316-1.0316-0.9891-1.0316-1.0315-1.0315-1.0288-1.0316最好目标函数值:-1.0316最差目标函数值:-0.9751平均目标函数值:-0.9914标准方差:0.0286三.最好的一次结果最好解:x1=0.0919x2=-0.7126最好值:-1.0316运行结果及收敛性曲线如下图:运行结果收敛性曲线(2)10cos81110)6541.5(min12121222xxxxxf..ts55ix,2,1i此问题的全局最优值min0.398f。一.程序(1)主函数:main.mclearall;clcpopsize=40;%种群规模chromlength=28;%二进制编码,编码精度为0.001,所以串长l为14pc=0.8;%杂交概率pm=0.2;%变异概率t=0;pop=initpop(popsize,chromlength);%随机产生初始种群whilet500%迭代次数t=t+1;[objvalue]=calobjvalue(pop);%计算目标函数值fitvalue=calfitvalue(objvalue);%计算群体中每个个体的适应度[bestindividual,bestfit]=best(pop,fitvalue);%求出群体中适应度最大的个体及其适应度值x11=decodechrom(bestindividual,1,14);%将二进制数转换为十进制数x22=decodechrom(bestindividual,15,14);x1(t)=-5+10*x11/(pow2(14)-1);%将二值域中的数转换为变量域的数x2(t)=-5+10*x22/(pow2(14)-1);y(t)=(x2(t)-5.1/(4*pi*pi).*x1(t).^2+5/pi.*x1(t)-6).^2+10.*(1-1/(8*pi)).*cos(x1(t))+10;%计算最佳个体的目标函数值[newpop1]=selection(pop,fitvalue);%选择算子[newpop2]=crossover(newpop1,pc);%杂交算子[newpop3]=mutation(newpop2,pm);%变异算子objvalue1=calobjvalue(newpop3(1,:));ifobjvalue1y(t)newpop3(1,:)=bestindividual;%保留最佳个体endpop=newpop3;%产生新种群endy;%每代的最佳目标函数值x1;%每代的最佳目标函数值对应的自变量x2;[gy,k]=min(y);%全局最优值gy=vpa(gy,3)%设置输出精度gx1=x1(k);%全局最优值对应的自变量x1=vpa(gx1,4)gx2=x2(k);x2=vpa(gx2,4)plot(y)%最优值收敛曲线title('收敛性曲线');xlabel('进化代数');ylabel('函数值');axis([0,500,0.2,1.5]);(2)初始种群:initpop.mfunctionpop=initpop(popsize,chromlength)pop=round(rand(popsize,chromlength));%rand随机产生[0,1]区间的一个小数,rand四舍

1 / 20
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功