完整可以运行的数值优化遗传算法源代码
% 采用基于轮盘赌法的非线性排名选择
% 各个体成员按适应值从大到小分配选择概率:
% P(i)=(q/1-(1-q)^n)*(1-q)^i, 其中 P(0)>P(1)>...>P(n), sum(P(i))=1
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)];%计算各选择概率之和
rNums=rand(m,1);
newIn=1;
while(newIn<=m)
NewPop(newIn,:)=OldPop(length(find(rNums(newIn)>newfit)),:);
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(PopIn<=L)
r=unidrnd(m,num(PopIn),2^PopIn);
[LocalMaxfit,In]=max(fit(r),[],2);
SelectIn=r((In-1)*num(PopIn)+[1:num(PopIn)]');
NewPop(sum(num(1:PopIn))-num(PopIn)+1:sum(num(1:PopIn)),:)=OldPop(SelectIn,:);
PopIn=PopIn+1;
r=[];In=[];LocalMaxfit=[];
end
if b>1
NewPop((sum(num)+1):(sum(num)+b-1),:)=OldPop(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);
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库遗传算法Matlab源代码(5)在线全文阅读。
相关推荐: