How do I plot the shortest route??
조회 수: 2 (최근 30일)
이전 댓글 표시
Here's my code that finds the shortest route and its distance:
function [D_min,route_min,Dist] = TSP_JAS(coord)
tic
N =5;
coordx = round(1000*rand(N,1))-500; % x coordinate of cities
coordy = round(1000*rand(N,1))-500; % y coordinates of cities
coord = [coordx coordy]; % x-y coordinates of N cities
%%Question 1
[m n]=size(coord);
n=m;
D = zeros(m,n);
for j = 1:m
for k = 1:n
D(j,k) = sqrt((coord(j,1)-coord(k,1))^2 + (coord(j,2)-coord(k,2))^2);
end
end
D
%%Question 2
R = perms(1:N);
r=factorial(N);
g=1;
while g<2
city1 = R(g,1);
if city1~=1
R(g,:)=[];
g=0;
end
g=g+1;
end
R(:,N)=1
%%Question 3
s=factorial((N-1));
Dist=zeros(s,1);
for h=1:s
for q=1:N-1
kk=R(h,N-(N-q));
q=q+1;
ww=R(h,N-(N-q));
u=D(kk,ww);
end
Dist(h)=u;
end
Dist
%%Question 4
D_min= min(Dist)
route_min=zeros(1,N);
for f=1:s
o=find(Dist==D_min);
y=min(o);
end
route_min(1,:)=R(y,:)
Grateful for any help you can offer.
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Traveling Salesman (TSP)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!