How do I plot the shortest route??

2 ビュー (過去 30 日間)
Jacob Savona
Jacob Savona 2015 年 3 月 3 日
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 件)

カテゴリ

Help Center および File ExchangeTraveling Salesman (TSP) についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by