📄 matlab 数值优化遗传算法程序.txt
字号:
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);
y1=find(r<pCross);
y2=find(r>=pCross);
len=length(y1);
if len==1|(len>2&mod(len,2)==1)%如果用来进行交叉的染色体的条数为奇数,将其调整为偶数
y2(length(y2)+1)=y1(len);
y1(len)=[];
end
i=0;
if length(y1)>=2
if opts(1)==1%浮点编码交叉
while(i<=length(y1)-2)
NewPop(y1(i+1),:)=OldPop(y1(i+1),:);
NewPop(y1(i+2),:)=OldPop(y1(i+2),:);
if opts(2)==0&n>1%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),Points(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),CrossPoints)=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(10^v(1)*OldPop(y1(i+1),CrossPoints))+...
10^v(1)*OldPop(y1(i+2),CrossPoints)-floor(10^v(1)*OldPop(y1(i+2),CrossPoints)))/10^v(1);
NewPop(y1(i+2),CrossPoints)=(floor(10^v(2)*OldPop(y1(i+2),CrossPoints))+...
10^v(2)*OldPop(y1(i+1),CrossPoints)-floor(10^v(2)*OldPop(y1(i+1),CrossPoints)))/10^v(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),:)]=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)],xPoints(1):xPoints(2));
%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);
i=1;
if opts(1)==1%浮点变异
maxfit=max(fit);
upfit=maxfit+0.05*abs(maxfit);
if opts(2)==1|opts(2)==3
while(i<=L)%自适应变异(自增或自减)
Point=unidrnd(n);
T=(1-fit(MutIn(i))/upfit)^2;
q=abs(1-rand^T);
%if q>1%按严格数学推理来说,这段程序是不能缺少的
% 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;
end
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)*10^v),10);
NewPop(MutIn(i),Point)=OldPop(MutIn(i),Point)-(q-unidrnd(9))/10^v;
i=i+1;
end
else
while(i<=L)
Point=unidrnd(n);
if round(rand)
NewPop(MutIn(i),Point)=OldPop(MutIn(i),Point)*(1-rand);
else
NewPop(MutIn(i),Point)=OldPop(MutIn(i),Point)+(1-OldPop(MutIn(i),Point))*rand;
end
i=i+1;
end
end
elseif opts(1)==0%二进制串变异
if L>=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
NewPop(MutIn(i),k(j))=1;
end
end
i=i+1;
end
end
else%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)-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=rand(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=OldPop;
r=rand(1,m);
PopIn=find(r<=pInversion);
len=length(PopIn);
i=1;
if len>=1
while(i<=len)
d=sort(unidrnd(n,1,2));
NewPop(PopIn(i),d(1):d(2))=OldPop(PopIn(i),d(2):-1:d(1));
i=i+1;
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -