完整可以运行的数值优化遗传算法源代码
%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):xPoints(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)))=[];
end
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));%随机生成掩码
crossposition=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(holdposition);%掩码为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*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);