遗传算法matlab程序实例

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

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

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

资源描述

遗传算法程序�一�:说明:fga.m为遗传算法的主程序;采用二进制Gray编码,采用基于轮盘赌法的非线性排名选择,均匀交叉,变异操作,而且还引入了倒位操作!function[BestPop,Trace]=fga(FUN,LB,UB,eranum,popsize,pCross,pMutation,pInversion,options)%[BestPop,Trace]=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation)%Findsamaximumofafunctionofseveralvariables.%fmaxgasolvesproblemsoftheform:%maxF(X)subjectto:LB=X=UB%BestPop-最优的群体即为最优的染色体群%Trace-最佳染色体所对应的目标函数值%FUN-目标函数%LB-自变量下限%UB-自变量上限%eranum-种群的代数,取100--1000(默认200)%popsize-每一代种群的规模�此可取50--200(默认100)%pcross-交叉概率,一般取0.5--0.85之间较好(默认0.8)%pmutation-初始变异概率,一般取0.05-0.2之间较好(默认0.1)%pInversion-倒位概率,一般取0.05�0.3之间较好(默认0.2)%options-1*2矩阵,options(1)=0二进制编码(默认0),option(1)~=0十进制编%码,option(2)设定求解精度(默认1e-4)%%------------------------------------------------------------------------T1=clock;ifnargin3,error('FMAXGArequiresatleastthreeinputarguments');endifnargin==3,eranum=200;popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[01e-4];endifnargin==4,popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[01e-4];endifnargin==5,pCross=0.8;pMutation=0.1;pInversion=0.15;options=[01e-4];endifnargin==6,pMutation=0.1;pInversion=0.15;options=[01e-4];endifnargin==7,pInversion=0.15;options=[01e-4];endiffind((LB-UB)0)error('数据输入错误,请重新输入(LBUB):');ends=sprintf('程序运行需要约%.4f秒钟时间,请稍等......',(eranum*popsize/1000));disp(s);globalmnNewPopchildren1children2VarNumbounds=[LB;UB]';bits=[];VarNum=size(bounds,1);precision=options(2);%由求解精度确定二进制编码长度bits=ceil(log2((bounds(:,2)-bounds(:,1))'./precision));%由设定精度划分区间[Pop]=InitPopGray(popsize,bits);%初始化种群[m,n]=size(Pop);NewPop=zeros(m,n);children1=zeros(1,n);children2=zeros(1,n);pm0=pMutation;BestPop=zeros(eranum,n);%分配初始解空间BestPop,TraceTrace=zeros(eranum,length(bits)+1);i=1;whilei=eranumforj=1:mvalue(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%计算适应度end[MaxValue,Index]=max(value);BestPop(i,:)=Pop(Index,:);Trace(i,1)=MaxValue;Trace(i,(2:length(bits)+1))=b2f(BestPop(i,:),bounds,bits);[selectpop]=NonlinearRankSelect(FUN,Pop,bounds,bits);%非线性排名选择[CrossOverPop]=CrossOver(selectpop,pCross,round(unidrnd(eranum-i)/eranum));%采用多点交叉和均匀交叉�且逐步增大均匀交叉的概率%round(unidrnd(eranum-i)/eranum)[MutationPop]=Mutation(CrossOverPop,pMutation,VarNum);%变异[InversionPop]=Inversion(MutationPop,pInversion);%倒位Pop=InversionPop;%更新pMutation=pm0+(i^4)*(pCross/3-pm0)/(eranum^4);%随着种群向前进化�逐步增大变异率至1/2交叉率p(i)=pMutation;i=i+1;endt=1:eranum;plot(t,Trace(:,1)');title('函数优化的遗传算法');xlabel('进化世代数(eranum)');ylabel('每一代最优适应度(maxfitness)');[MaxFval,I]=max(Trace(:,1));X=Trace(I,(2:length(bits)+1));holdon;plot(I,MaxFval,'*');text(I+5,MaxFval,['FMAX='num2str(MaxFval)]);str1=sprintf('进化到%d代,自变量为%s时,得本次求解的最优值%f\n对应染色体是�%s',I,num2str(X),MaxFval,num2str(BestPop(I,:)));disp(str1);%figure(2);plot(t,p);%绘制变异值增大过程T2=clock;elapsed_time=T2-T1;ifelapsed_time(6)0elapsed_time(6)=elapsed_time(6)+60;elapsed_time(5)=elapsed_time(5)-1;endifelapsed_time(5)0elapsed_time(5)=elapsed_time(5)+60;elapsed_time(4)=elapsed_time(4)-1;end%像这种程序当然不考虑运行上小时啦str2=sprintf('程序运行耗时%d小时%d分钟%.4f秒',elapsed_time(4),elapsed_time(5),elapsed_time(6));disp(str2);%初始化种群%采用二进制Gray编码,其目的是为了克服二进制编码的Hamming悬崖缺点function[initpop]=InitPopGray(popsize,bits)len=sum(bits);initpop=zeros(popsize,len);%Thewholezeroencodingindividualfori=2:popsize-1pop=round(rand(1,len));pop=mod(([0pop]+[pop0]),2);%i=1时,b(1)=a(1);i1时,b(i)=mod(a(i-1)+a(i),2)%其中原二进制串:a(1)a(2)...a(n),Gray串:b(1)b(2)...b(n)initpop(i,:)=pop(1:end-1);endinitpop(popsize,:)=ones(1,len);%Thewholeoneencodingindividual%解码function[fval]=b2f(bval,bounds,bits)%fval-表征各变量的十进制数%bval-表征各变量的二进制编码串%bounds-各变量的取值范围%bits-各变量的二进制编码长度scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1);%TherangeofthevariablesnumV=size(bounds,1);cs=[0cumsum(bits)];fori=1:numVa=bval((cs(i)+1):cs(i+1));fval(i)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(i)+bounds(i,1);end%选择操作%采用基于轮盘赌法的非线性排名选择%各个体成员按适应值从大到小分配选择概率�%P(i)=(q/1-(1-q)^n)*(1-q)^i,其中P(0)P(1)...P(n),sum(P(i))=1function[selectpop]=NonlinearRankSelect(FUN,pop,bounds,bits)globalmnselectpop=zeros(m,n);fit=zeros(m,1);fori=1:mfit(i)=feval(FUN(1,:),(b2f(pop(i,:),bounds,bits)));%以函数值为适应值做排名依据endselectprob=fit/sum(fit);%计算各个体相对适应度(0,1)q=max(selectprob);%选择最优的概率x=zeros(m,2);x(:,1)=[m:-1:1]';[yx(:,2)]=sort(selectprob);r=q/(1-(1-q)^m);%标准分布基值newfit(x(:,2))=r*(1-q).^(x(:,1)-1);%生成选择概率newfit=cumsum(newfit);%计算各选择概率之和rNums=sort(rand(m,1));fitIn=1;newIn=1;whilenewIn=mifrNums(newIn)newfit(fitIn)selectpop(newIn,:)=pop(fitIn,:);newIn=newIn+1;elsefitIn=fitIn+1;endend%交叉操作function[NewPop]=CrossOver(OldPop,pCross,opts)%OldPop为父代种群�pcross为交叉概率globalmnNewPopr=rand(1,m);y1=find(rpCross);y2=find(r=pCross);len=length(y1);iflen2&mod(len,2)==1%如果用来进行交叉的染色体的条数为奇数�将其调整为偶数y2(length(y2)+1)=y1(len);y1(len)=[];endiflength(y1)=2fori=0:2:length(y1)-2ifopts==0[NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=EqualCrossOver(OldPop(y1(i+1),:),OldPop(y1(i+2),:));else[NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=MultiPointCross(OldPop(y1(i+1),:),OldPop(y1(i+2),:));endendendNewPop(y2,:)=OldPop(y2,:);%采用均匀交叉function[children1,children2]=EqualCrossOver(parent1,parent2)globalnchildren1children2hidecode=round(r

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

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

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

×
保存成功