2022年遗传算法Matlab源代码 .pdf

上传人:H****o 文档编号:33662334 上传时间:2022-08-12 格式:PDF 页数:10 大小:73.62KB
返回 下载 相关 举报
2022年遗传算法Matlab源代码 .pdf_第1页
第1页 / 共10页
2022年遗传算法Matlab源代码 .pdf_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《2022年遗传算法Matlab源代码 .pdf》由会员分享,可在线阅读,更多相关《2022年遗传算法Matlab源代码 .pdf(10页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、function X,MaxFval,BestPop,Trace=fga(FUN,bounds,MaxEranum,PopSize,options,pCross,pMutation,pInversion) % X,MaxFval,BestPop,Trace=fga(FUN,bounds,MaxEranum,PopSize,options,pCross,pMutation,pInversion) % Finds a maximum of a function of several variables. % fga solves problems of the form: % max F(X) s

2、ubject to: LB = X = UB (LB=bounds(:,1),UB=bounds(:,2) % X - 最优个体对应自变量值% MaxFval - 最优个体对应函数值% BestPop - 最优的群体即为最优的染色体群% Trace - 每代最佳个体所对应的目标函数值% FUN - 目标函数% bounds - 自变量范围% MaxEranum - 种群的代数 ,取 50-500(默认 200) % PopSize - 每一代种群的规模;此可取50-200(默认 100) % pCross - 交叉概率 ,一般取 0.5-0.85 之间较好 (默认 0.8) % pMutati

3、on - 初始变异概率,一般取 0.05-0.2 之间较好 (默认 0.1) % pInversion - 倒位概率 ,一般取 0.050.3 之间较好 (默认 0.2) % options - 1*2 矩阵 ,options(1)=0 二进制编码 (默认 0),option(1)=0 十进制编码 ,option(2) 设定求解精度(默认 1e-4) T1=clock; %检验初始参数if nargin0) error(数据输入错误 ,请重新输入 :); end % 定义全局变量global m n NewPop children1 children2 VarNum % 初始化种群和变量pre

4、cision = options(2); bits = ceil(log2(bounds(:,2)-bounds(:,1) ./ precision);%由设定精度划分区间VarNum = size(bounds,1); Pop = InitPop(PopSize,bounds,bits,options);% 初始化种群m,n = size(Pop); fit = zeros(1,m); NewPop = zeros(m,n); children1 = zeros(1,n); children2 = zeros(1,n); pm0 = pMutation; BestPop = zeros(Ma

5、xEranum,n);% 分配初始解空间BestPop,Trace Trace = zeros(1,MaxEranum); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 10 页 - - - - - - - - - Lb = ones(PopSize,1)*bounds(:,1); Ub = ones(PopSize,1)*bounds(:,2); %二进制编码采用多点交叉和均匀交叉,并逐步增大均匀交叉概率%浮点编码采用离散交叉(前期)、算术交叉(中期) 、AEA 重组

6、(后期)OptsCrossOver = ones(1,MaxEranum)*options(1);. round(unidrnd(2*(MaxEranum-1:MaxEranum)/MaxEranum); %浮点编码时采用两种自适应变异和一种随机变异(自适应变异发生概率为随机变异发生的2 倍)OptsMutation = ones(1,MaxEranum)*options(1);unidrnd(5,1,MaxEranum); if options(1)=3 D=zeros(n); CityPosition=bounds; D = sqrt(CityPosition(:, ones(1,n) -

7、 CityPosition(:, ones(1,n).2 +. (CityPosition(:,2*ones(1,n) - CityPosition(:,2*ones(1,n).2 ); end %= % 进化主程序% %= eranum = 1; H=waitbar(0,Please wait.); while(eranum0,1 ,Pop-(Pop-Lb)./(Ub-Lb) end switch round(unifrnd(0,eranum/MaxEranum)%进化前期尽量使用实行锦标赛选择,后期逐步增大非线性排名选择case 0 selectpop=TournamentSelect(P

8、op,fit,bits);% 锦标赛选择case 1 selectpop=NonlinearRankSelect(Pop,fit,bits);%非线性排名选择end 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 10 页 - - - - - - - - - CrossOverPop=CrossOver(selectpop,pCross,OptsCrossOver(eranum,:);% 交叉MutationPop=Mutation(CrossOverPop,fit,pM

9、utation,VarNum,OptsMutation(eranum,:); %变异InversionPop=Inversion(MutationPop,pInversion);%倒位%更新种群if options(1)=1 Pop=Lb+InversionPop.*(Ub-Lb);%还原 Pop else Pop=InversionPop; end pMutation=pm0+(eranum3)*(pCross/2-pm0)/(eranum4); %逐步增大变异率至1/2 交叉率percent=num2str(round(100*eranum/MaxEranum); waitbar(eran

10、um/MaxEranum,H,Evolution complete ,percent,%); eranum=eranum+1; end close(H); % 格式化输出进化结果和解的变化情况t=1:MaxEranum; plot(t,Trace,t,Meanfit); legend(解的变化 ,种群的变化 ); title( 函数优化的遗传算法); xlabel(进化世代数 ); ylabel(每一代最优适应度); MaxFval,MaxFvalIn=max(Trace); if options(1)=1|options(1)=3 X=BestPop(MaxFvalIn,:); elseif

11、 options(1)=0 X=b2f(BestPop(MaxFvalIn,:),bounds,bits); end hold on; plot(MaxFvalIn,MaxFval,*); text(MaxFvalIn+5,MaxFval,FMAX= num2str(MaxFval); str1=sprintf( Best generation:n %dnn Best X:n %snn MaxFvaln %fn,. MaxFvalIn,num2str(X),MaxFval); disp(str1); % -计时T2=clock; elapsed_time=T2-T1; if elapsed_t

12、ime(6)0 elapsed_time(6)=elapsed_time(6)+60; elapsed_time(5)=elapsed_time(5)-1; end if elapsed_time(5)1 时,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); end initpop(popsize,:)=ones(1,len);%The whole one encoding individual else for i=1:popsize initpop(i

13、,:)=randperm(numVars);% 为 Tsp问题初始化种群end end % - 二进制串解码- function fval = b2f(bval,bounds,bits) % fval - 表征各变量的十进制数% bval - 表征各变量的二进制编码串% bounds - 各变量的取值范围% bits - 各变量的二进制编码长度scale=(bounds(:,2)-bounds(:,1)./(2.bits-1); %The range of the variables numV=size(bounds,1); cs=0 cumsum(bits); for i=1:numV a=

14、bval(cs(i)+1):cs(i+1); fval(i)=sum(2.(size(a,2)-1:-1:0).*a)*scale(i)+bounds(i,1); end % - 选择操作- 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 10 页 - - - - - - - - - % 采用基于轮盘赌法的非线性排名选择% 各个体成员按适应值从大到小分配选择概率:% P(i)=(q/1-(1-q)n)*(1-q)i, 其中P(0)P(1).P(n), sum(P(i)=1

15、 function NewPop=NonlinearRankSelect(OldPop,fit,bits) global m n NewPop fit=fit; selectprob=fit/sum(fit);% 计算各个体相对适应度(0,1) q=max(selectprob);% 选择最优的概率x=zeros(m,2); x(:,1)=m:-1:1; y x(:,2)=sort(selectprob); r=q/(1-(1-q)m);% 标准分布基值newfit(x(:,2)=r*(1-q).(x(:,1)-1);%生成选择概率newfit=0 cumsum(newfit);%计算各选择概

16、率之和rNums=rand(m,1); newIn=1; while(newInnewfit),:); newIn=newIn+1; end % - 锦标赛选择 (含精英选择 ) - function NewPop=TournamentSelect(OldPop,fit,bits) global m n NewPop num=floor(m./2.(1:10); num(find(num=0)=; L=length(num); a=sum(num); b=m-a; PopIn=1; while(PopIn1 NewPop(sum(num)+1):(sum(num)+b-1),:)=OldPop

17、(unidrnd(m,1,b-1),:); end GlobalMaxfit,I=max(fit);%保留每一代中最佳个体NewPop(end,:)=OldPop(I,:); % - 交叉操作- function NewPop=CrossOver(OldPop,pCross,opts) global m n NewPop r=rand(1,m); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 10 页 - - - - - - - - - y1=find(r=pCross

18、); len=length(y1); if len=1|(len2&mod(len,2)=1)%如果用来进行交叉的染色体的条数为奇数,将其调整为偶数y2(length(y2)+1)=y1(len); y1(len)=; end i=0; if length(y1)=2 if opts(1)=1% 浮点编码交叉while(i1%discret crossover Points=sort(unidrnd(n,1,2); NewPop(y1(i+1),Points(1):Points(2)=OldPop(y1(i+2),Points(1):Points(2); NewPop(y1(i+2),Poin

19、ts(1):Points(2)=OldPop(y1(i+1),Points(1):Points(2); elseif opts(2)=1%arithmetical crossover Points=round(unifrnd(0,pCross,1,n); CrossPoints=find(Points=1); r=rand(1,length(CrossPoints); NewPop(y1(i+1),CrossPoints)=r.*OldPop(y1(i+1),CrossPoints)+(1-r).*OldPop(y1(i+2),CrossPoints); NewPop(y1(i+2),Cros

20、sPoints)=r.*OldPop(y1(i+2),CrossPoints)+(1-r).*OldPop(y1(i+1),CrossPoints); else %AEA recombination Points=round(unifrnd(0,pCross,1,n); CrossPoints=find(Points=1); v=unidrnd(4,1,2); NewPop(y1(i+1),CrossPoints)=(floor(10v(1)*OldPop(y1(i+1),CrossPoints)+. 10v(1)*OldPop(y1(i+2),CrossPoints)-floor(10v(1

21、)*OldPop(y1(i+2),CrossPoints)/10v(1); NewPop(y1(i+2),CrossPoints)=(floor(10v(2)*OldPop(y1(i+2),CrossPoints)+. 10v(2)*OldPop(y1(i+1),CrossPoints)-floor(10v(2)*OldPop(y1(i+1),CrossPoints)/10v(2); end i=i+2; end elseif opts(1)=0% 二进制编码交叉while(i=length(y1)-2) if opts(2)=0 NewPop(y1(i+1),:),NewPop(y1(i+2

22、),:)=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),:); end i=i+2; end else %Tsp问题次序杂交for i=0:2:length(y1)-2 xPoints=sort(unidrnd(n,1,2); NewPop(y1(i+1) y1(i+2),xPoints(1):xPoints(2)=OldPop(y1(i+2) y1(i+1)

23、,xPoints(1):xPoints(2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 10 页 - - - - - - - - - %NewPop(y1(i+2),xPoints(1):xPoints(2)=OldPop(y1(i+1),xPoints(1):xPoints(2); temp=OldPop(y1(i+1),xPoints(2)+1:n) OldPop(y1(i+1),1:xPoints(2); for del1i=xPoints(1):xPoin

24、ts(2) temp(find(temp=OldPop(y1(i+2),del1i)=; end NewPop(y1(i+1),(xPoints(2)+1):n)=temp(1:(n-xPoints(2); NewPop(y1(i+1),1:(xPoints(1)-1)=temp(n-xPoints(2)+1):end); temp=OldPop(y1(i+2),xPoints(2)+1:n) OldPop(y1(i+2),1:xPoints(2); for del2i=xPoints(1):xPoints(2) temp(find(temp=OldPop(y1(i+1),del2i)=; e

25、nd NewPop(y1(i+2),(xPoints(2)+1):n)=temp(1:(n-xPoints(2); NewPop(y1(i+2),1:(xPoints(1)-1)=temp(n-xPoints(2)+1):end); end end end NewPop(y2,:)=OldPop(y2,:); % -二进制串均匀交叉算子function children1,children2=EqualCrossOver(parent1,parent2) global n children1 children2 hidecode=round(rand(1,n);% 随机生成掩码crosspos

26、ition=find(hidecode=1); holdposition=find(hidecode=0); children1(crossposition)=parent1(crossposition);% 掩码为 1,父 1 为子 1 提供基因children1(holdposition)=parent2(holdposition);%掩码为 0,父 2 为子 1 提供基因children2(crossposition)=parent2(crossposition);% 掩码为 1,父 2 为子 2 提供基因children2(holdposition)=parent1(holdposit

27、ion);%掩码为 0,父 1 为子 2 提供基因% -二进制串多点交叉算子function Children1,Children2=MultiPointCross(Parent1,Parent2)%交叉点数由变量数决定global n Children1 Children2 VarNum Children1=Parent1; Children2=Parent2; Points=sort(unidrnd(n,1,2*VarNum); for i=1:VarNum Children1(Points(2*i-1):Points(2*i)=Parent2(Points(2*i-1):Points(2

28、*i); Children2(Points(2*i-1):Points(2*i)=Parent1(Points(2*i-1):Points(2*i); end % - 变异操作- function NewPop=Mutation(OldPop,fit,pMutation,VarNum,opts) global m n NewPop NewPop=OldPop; r=rand(1,m); MutIn=find(r=pMutation); L=length(MutIn); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理

29、 - - - - - - - 第 7 页,共 10 页 - - - - - - - - - i=1; if opts(1)=1% 浮点变异maxfit=max(fit); upfit=maxfit+0.05*abs(maxfit); if opts(2)=1|opts(2)=3 while(i1% 按严格数学推理来说,这段程序是不能缺少的% q=1 %end p=OldPop(MutIn(i),Point)*(1-q); if unidrnd(2)=1 NewPop(MutIn(i),Point)=p+q; else NewPop(MutIn(i),Point)=p; end i=i+1; e

30、nd elseif opts(2)=2|opts(2)=4%AEA变异(任意变量的某一位变异)while(i=L) Point=unidrnd(n); T=(1-abs(upfit-fit(MutIn(i)/upfit)2; v=1+unidrnd(1+ceil(10*T); %v=1+unidrnd(5+ceil(10*eranum/MaxEranum); q=mod(floor(OldPop(MutIn(i),Point)*10v),10); NewPop(MutIn(i),Point)=OldPop(MutIn(i),Point)-(q-unidrnd(9)/10v; i=i+1; en

31、d else while(i=1 while i=L k=unidrnd(n,1,VarNum); % 设置变异点数 (=变量数 ) for j=1:length(k) if NewPop(MutIn(i),k(j)=1 NewPop(MutIn(i),k(j)=0; else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 10 页 - - - - - - - - - NewPop(MutIn(i),k(j)=1; end end i=i+1; end end else

32、%Tsp 变异if opts(2)=1|opts(2)=2|opts(2)=3|opts(2)=4 numMut=ceil(pMutation*m); r=unidrnd(m,numMut,2); LocalMinfit,In=min(fit(r),2); SelectIn=r(In-1)*numMut+1:numMut); while(i=numMut) mPoints=sort(unidrnd(n,1,2); if mPoints(1)=mPoints(2) NewPop(SelectIn(i),1:mPoints(1)-1)=OldPop(SelectIn(i),1:mPoints(1)

33、-1); NewPop(SelectIn(i),mPoints(1):mPoints(2)-1)=OldPop(SelectIn(i),mPoints(1)+1:mPoints(2); NewPop(SelectIn(i),mPoints(2)=OldPop(SelectIn(i),mPoints(1); NewPop(SelectIn(i),mPoints(2)+1:n)=OldPop(SelectIn(i),mPoints(2)+1:n); else NewPop(SelectIn(i),:)=OldPop(SelectIn(i),:); end i=i+1; end else r=ran

34、d(1,m); MutIn=find(r=pMutation); L=length(MutIn); while i=L mPoints=sort(unidrnd(n,1,2); rIn=randperm(mPoints(2)-mPoints(1)+1); NewPop(MutIn(i),mPoints(1):mPoints(2)=OldPop(MutIn(i),mPoints(1)+rIn-1); i=i+1; end end end % - 倒位操作- function NewPop=Inversion(OldPop,pInversion) global m n NewPop NewPop=

35、OldPop; r=rand(1,m); PopIn=find(r=1 while(i=len) d=sort(unidrnd(n,1,2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 10 页 - - - - - - - - - NewPop(PopIn(i),d(1):d(2)=OldPop(PopIn(i),d(2):-1:d(1); i=i+1; end end 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 10 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术总结

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com