function [Shortest_Route,Shortest_Length]=anttsp(city,iter_max,m,Alpha,Beta,Rho,Q)
n=size(city,1)
d=zeros(n,n)
d=squareform(pdist(city))
Eta=1./d
Tau=ones(n,n)
Tabu=zeros(m,n)
nC=1
R_best=zeros(iter_max,n)
L_best=inf.*ones(iter_max,1)
while nC<=iter_max
route=[]
for i=1:ceil(m/n)
route=[route,randperm(n)]
end
Tabu(:,1)=(route(1,1:m))'
for j=2:n
for i=1:m
visited=Tabu(i,1:(j-1))
J=zeros(1,(n-j+1))
P=J
Jc=1
for k=1:n
if isempty(find(visited==k, 1))
J(Jc)=k
Jc=Jc+1
end
end
for k=1:length(J)
P(k)=(Tau(visited(end),J(k))^Alpha)*(Eta(visited(end),J(k))^Beta)
end
P=P/(sum(P))
Pcum=cumsum(P)
Select=find(Pcum>=rand)
if isempty(Select)%是不是一定能保证Select不为空
Tabu(i,j)=round(1+(n-1)*rand)
else
next_visit=J(Select(1))
Tabu(i,j)=next_visit
end
end
end
if nC>=2
Tabu(1,:)=R_best(nC-1,:)
end
L=zeros(m,1)
for i=1:m
R=Tabu(i,:)
for j=1:(n-1)
L(i)=L(i)+d(R(j),R(j+1))
end
L(i)=L(i)+d(R(1),R(n))
end
L_best(nC)=min(L)
pos=find(L==L_best(nC))
R_best(nC,:)=Tabu(pos(1),:)
nC=nC+1
Delta_Tau=zeros(n,n)
for i=1:m
for j=1:(n-1)
Delta_Tau(Tabu(i,j),Tabu(i,j+1))=Delta_Tau(Tabu(i,j),Tabu(i,j+1))+Q/L(i)
end
Delta_Tau(Tabu(i,n),Tabu(i,1))=Delta_Tau(Tabu(i,n),Tabu(i,1))+Q/L(i)
end
Tau=(1-Rho).*Tau+Delta_Tau
Tabu=zeros(m,n)
end
Pos=find(L_best==min(L_best))
Shortest_Route=R_best(Pos(1),:)
Shortest_Length=L_best(Pos(1))
end
function [Shortest_Route,Shortest_Length]=anttsp(city,iter_max,m,Alpha,Beta,Rho,Q)n=size(city,1)
d=zeros(n,n)
d=squareform(pdist(city))
Eta=1./d
Tau=ones(n,n)
Tabu=zeros(m,n)
nC=1
R_best=zeros(iter_max,n)
L_best=inf.*ones(iter_max,1)
while nC<=iter_max
route=[]
for i=1:ceil(m/n)
route=[route,randperm(n)]
end
Tabu(:,1)=(route(1,1:m))'
for j=2:n
for i=1:m
visited=Tabu(i,1:(j-1))
J=zeros(1,(n-j+1))
P=J
Jc=1
for k=1:n
if isempty(find(visited==k, 1))
J(Jc)=k
Jc=Jc+1
end
end
for k=1:length(J)
P(k)=(Tau(visited(end),J(k))^Alpha)*(Eta(visited(end),J(k))^Beta)
end
P=P/(sum(P))
Pcum=cumsum(P)
Select=find(Pcum>=rand)
if isempty(Select)%是不是一定能保证Select不为空
Tabu(i,j)=round(1+(n-1)*rand)
else
next_visit=J(Select(1))
Tabu(i,j)=next_visit
end
end
end
if nC>=2
Tabu(1,:)=R_best(nC-1,:)
end
L=zeros(m,1)
for i=1:m
R=Tabu(i,:)
for j=1:(n-1)
L(i)=L(i)+d(R(j),R(j+1))
end
L(i)=L(i)+d(R(1),R(n))
end
L_best(nC)=min(L)
pos=find(L==L_best(nC))
R_best(nC,:)=Tabu(pos(1),:)
nC=nC+1
Delta_Tau=zeros(n,n)
for i=1:m
for j=1:(n-1)
Delta_Tau(Tabu(i,j),Tabu(i,j+1))=Delta_Tau(Tabu(i,j),Tabu(i,j+1))+Q/L(i)
end
Delta_Tau(Tabu(i,n),Tabu(i,1))=Delta_Tau(Tabu(i,n),Tabu(i,1))+Q/L(i)
end
Tau=(1-Rho).*Tau+Delta_Tau
Tabu=zeros(m,n)
end
Pos=find(L_best==min(L_best))
Shortest_Route=R_best(Pos(1),:)
Shortest_Length=L_best(Pos(1))
end
TSP问题遗传算法通用Matlab程序程序一:主程序
%TSP问题(又名:旅行商问题,货郎担问题)遗传算法通用matlab程序 %D是距离矩阵,n为种群个数 %参数a是中国31个城市的坐标
%C为停止代数,遗传到第 C代时程序停止,C的具体取值视问题的规模和耗费的时间而定 %m为适应值归一化淘汰加速指数,最好取为1,2,3,4,不宜太大
%alpha为淘汰保护指数,可取为0~1之间任意小数,取1时关闭保护功能,建议取0.8~1.0之间的值
%R为最短路径,Rlength为路径长度
function [R,Rlength]=geneticTSP(D,a,n,C,m,alpha) [N,NN]=size(D)
farm=zeros(n,N)%用于存储种群 for i=1:n
farm(i,:)=randperm(N)%随机生成初始种群 end
R=farm(1,:)subplot(1,3,1)
scatter(a(:,1),a(:,2),'x') pause(1)
subplot(1,3,2) plotaiwa(a,R) pause(1)
farm(1,:)=R
len=zeros(n,1)%存储路径长度
fitness=zeros(n,1)%存储归一化适应值 counter=0
while counterfor i=1:n
len(i,1)=myLength(D,farm(i,:))%计算路径长度 end
maxlen=max(len)minlen=min(len)
fitness=fit(len,m,maxlen,minlen)%计算归一化适应值 rr=find(len==minlen)
R=farm(rr(1,1),:)%更新最短路径
FARM=farm%优胜劣汰,nn记录了复制的个数 nn=0
for i=1:n
if fitness(i,1)>=alpha*rand nn=nn+1
FARM(nn,:)=farm(i,:)end
end
FARM=FARM(1:nn,:)
[aa,bb]=size(FARM)%交叉和变异 while aaif nn<=2 nnper=randperm(2)else
nnper=randperm(nn)end
A=FARM(nnper(1),:)B=FARM(nnper(2),:)[A,B]=intercross(A,B)FARM=[FARMAB][aa,bb]=size(FARM)end
if aa>n
FARM=FARM(1:n,:)%保持种群规模为n end
farm=FARMclear FARM
counter=counter+1end
Rlength=myLength(D,R)subplot(1,3,3) plotaiwa(a,R)
程序二:计算邻接矩阵
%输入参数a是中国31个城市的坐标 %输出参数D是无向图的赋权邻接矩阵 function D=ff01(a) [c,d]=size(a)D=zeros(c,c)for i=1:c
for j=i:c
bb=(a(i,1)-a(j,1)).^2+(a(i,2)-a(j,2)).^2D(i,j)=bb^(0.5)D(j,i)=D(i,j)end end
程序三:计算归一化适应值 %计算归一化适应值的子程序
function fitness=fit(len,m,maxlen,minlen) fitness=len
for i=1:length(len)
fitness(i,1)=(1-((len(i,1)-minlen)/(maxlen-minlen+0.0001))).^mend
程序四:交叉和变异的子程序
%交叉算法采用的是由Goldberg和Lingle于1985年提出的PMX(部分匹配交叉) function [a,b]=intercross(a,b) L=length(a)
if L<=10%确定交叉宽度 W=9
elseif ((L/10)-floor(L/10))>=rand&&L>10 W=ceil(L/10)+8else
W=floor(L/10)+8end
p=unidrnd(L-W+1)%随机选择交叉范围,从p到p+W for i=1:W%交叉
x=find(a==b(1,p+i-1))y=find(b==a(1,p+i-1))
[a(1,p+i-1),b(1,p+i-1)]=exchange(a(1,p+i-1),b(1,p+i-1))[a(1,x),b(1,y)]=exchange(a(1,x),b(1,y)) end
function [x,y]=exchange(x,y) temp=xx=yy=temp
程序五: 计算路径的子程序
%该路径长度是一个闭合的路径的长度 function len=myLength(D,p) [N,NN]=size(D)
len=D(p(1,N),p(1,1))for i=1:(N-1)
len=len+D(p(1,i),p(1,i+1))end
程序六:用于绘制路径示意图的程序 function plotaiwa(a,R)
scatter(a(:,1),a(:,2),'x') hold on
plot([a(R(1),1),a(R(31),1)],[a(R(1),2),a(R(31),2)]) hold on
for i=2:length(R) x0=a(R(i-1),1)y0=a(R(i-1),2)x1=a(R(i),1)y1=a(R(i),2)xx=[x0,x1]
yy=[y0,y1]
plot(xx,yy)
hold on
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)