TSP问题的matlab程序(类似GA)
但又不是。。。。。
注意淘汰处理
觉得 有点启发。。呵呵
///////////////////
%test
ap3=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71;58,69;54,62;51,67;37,84;41,94;
2,99;7,64;22,60;25,62;18,54;4,50;13,40;18,40;24,42;25,38;41,26;45,21;44,35;58,35;62,32];
Map1=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71];
Map2=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71;58,69;54,62;51,67;37,84;41,94;
2,99;7,64;22,60;25,62;18,54];
MaxIter=100;
SizeScale=1000;
ChangeRate=0.05;
[time,opt,fval]=TspGa(Map1,MaxIter,SizeScale,ChangeRate)
function [time,opt,fval]=TspGa( Map,MaxIter,SizeScale,ChangeRate )
%This is function
%Input:
%Output:
t=cputime;
%MaxIter=100;
%SizeScale=100;
n=max(size(Map));
%compute the roadmatrix
DistMatrix=zeros(n,n);
for i=1:n
for j=1:n
DistMatrix(i,j)=distance(Map(i,:),Map(j,:));
end
end
%to inital the road( oder the cites )
Road=ones(SizeScale,n);
for i=1:SizeScale
Road(i,:)=randperm(n);
end
%
Dist=zeros(SizeScale,1);
for i=1:SizeScale
for j=1:(n-1)
Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));
end
end
[MinRoad,II]=min(Dist);
for iter=1:MaxIter
flag=0;
%SumDist=sum(Dist);
MinRoad=min(Dist);
MaxRoad=max(Dist);
for i=1:SizeScale
fit=fitness(MinRoad,MaxRoad,Dist(i));
if fit==1
flag=flag+1;
%get new road
Road(i,:)=Road(II,:);
ChangeLocation=randint(1,2,[1 n]);
if ChangeLocation(1)==ChangeLocation(2)
continue;
end
temp=Road(i,ChangeLocation(1));
Road(i,ChangeLocation(1))=Road(i,ChangeLocation(2));
Road(i,ChangeLocation(2))=temp;
Dist(i)=0;
for j=1:(n-1)
Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));
end
end
end
%change
%bianyi
Numchange=int16(ChangeRate*SizeScale);
IDchange=randint(1,Numchange,[1,SizeScale]);
step=3;
for i=1:Numchange
if IDchange(i)==II
continue;
end
L=1;
while (L+step)< n
temp=Road(IDchange(i),L);
Road(IDchange(i),L)=Road(IDchange(i),L+step);
Road(IDchange(i),L+step)=temp;
L=L+step;
end
Dist(i)=0;
for j=1:(n-1)
Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));
end
end
[MinRoad,II]=min(Dist);
% disp('this Iter the Opt is');
% Road(II,:)
% MinRoad
flag
%Dist
end
plot(Map(:,1),Map(:,2),'*');
hold on
XX=Map(Road(II,:),1);
XX=[XX;Map(Road(II,1),1)];
YY=Map(Road(II,:),2);
YY=[YY;Map(Road(II,1),2)];
plot(XX,YY);
time=cputime-t;
opt=Road(II,:);
fval=MinRoad;
function d=distance(A,B)
%this function to get the distance of two point A,B
d=sqrt((A(1)-B(1))^2+(A(2)-B(2))^2);
function f=fitness(fmin,fmax,froad)
%this function to computer the fitness this road
%this the f is 0 or 1 froad more less the f more posible 1
t=(froad-fmin)/(fmax-fmin);
f=rand<((1-t));
但又不是。。。。。
注意淘汰处理
觉得 有点启发。。呵呵
///////////////////
%test
ap3=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71;58,69;54,62;51,67;37,84;41,94;
2,99;7,64;22,60;25,62;18,54;4,50;13,40;18,40;24,42;25,38;41,26;45,21;44,35;58,35;62,32];
Map1=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71];
Map2=[82,7;91,38;83,46;71,44;64,60;68,58;83,69;87,76;74,78;71,71;58,69;54,62;51,67;37,84;41,94;
2,99;7,64;22,60;25,62;18,54];
MaxIter=100;
SizeScale=1000;
ChangeRate=0.05;
[time,opt,fval]=TspGa(Map1,MaxIter,SizeScale,ChangeRate)
function [time,opt,fval]=TspGa( Map,MaxIter,SizeScale,ChangeRate )
%This is function
%Input:
%Output:
t=cputime;
%MaxIter=100;
%SizeScale=100;
n=max(size(Map));
%compute the roadmatrix
DistMatrix=zeros(n,n);
for i=1:n
for j=1:n
DistMatrix(i,j)=distance(Map(i,:),Map(j,:));
end
end
%to inital the road( oder the cites )
Road=ones(SizeScale,n);
for i=1:SizeScale
Road(i,:)=randperm(n);
end
%
Dist=zeros(SizeScale,1);
for i=1:SizeScale
for j=1:(n-1)
Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));
end
end
[MinRoad,II]=min(Dist);
for iter=1:MaxIter
flag=0;
%SumDist=sum(Dist);
MinRoad=min(Dist);
MaxRoad=max(Dist);
for i=1:SizeScale
fit=fitness(MinRoad,MaxRoad,Dist(i));
if fit==1
flag=flag+1;
%get new road
Road(i,:)=Road(II,:);
ChangeLocation=randint(1,2,[1 n]);
if ChangeLocation(1)==ChangeLocation(2)
continue;
end
temp=Road(i,ChangeLocation(1));
Road(i,ChangeLocation(1))=Road(i,ChangeLocation(2));
Road(i,ChangeLocation(2))=temp;
Dist(i)=0;
for j=1:(n-1)
Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));
end
end
end
%change
%bianyi
Numchange=int16(ChangeRate*SizeScale);
IDchange=randint(1,Numchange,[1,SizeScale]);
step=3;
for i=1:Numchange
if IDchange(i)==II
continue;
end
L=1;
while (L+step)< n
temp=Road(IDchange(i),L);
Road(IDchange(i),L)=Road(IDchange(i),L+step);
Road(IDchange(i),L+step)=temp;
L=L+step;
end
Dist(i)=0;
for j=1:(n-1)
Dist(i)=Dist(i)+DistMatrix(Road(i,j),Road(i,j+1));
end
end
[MinRoad,II]=min(Dist);
% disp('this Iter the Opt is');
% Road(II,:)
% MinRoad
flag
%Dist
end
plot(Map(:,1),Map(:,2),'*');
hold on
XX=Map(Road(II,:),1);
XX=[XX;Map(Road(II,1),1)];
YY=Map(Road(II,:),2);
YY=[YY;Map(Road(II,1),2)];
plot(XX,YY);
time=cputime-t;
opt=Road(II,:);
fval=MinRoad;
function d=distance(A,B)
%this function to get the distance of two point A,B
d=sqrt((A(1)-B(1))^2+(A(2)-B(2))^2);
function f=fitness(fmin,fmax,froad)
%this function to computer the fitness this road
%this the f is 0 or 1 froad more less the f more posible 1
t=(froad-fmin)/(fmax-fmin);
f=rand<((1-t));