I have a few points in space:
A 0 0
B 0 1
C 0 2
D 0 4
E 1 2
F 1 3
G 2 0
H 2 4
I 3 4
J 4 4
K 5 0
L 6 1
M 7 2
N 7 4
O 4 2
P 8 3
Q 8 4 ;
I also have a solution for the Traveling Salesman Problem, essentially the edges which have to be connected.
A B 1
A G 1
B C 1
C E 1
D F 1
D H 1
E F 1
G O 1
H I 1
I J 1
J N 1
K L 1
K O 1
L M 1
M P 1
N Q 1
P Q 1
I could plot the nodes but I am not sure how to specify the edges.
Use the MATLOG toolbox from Dr. Michael Kay (NC State). Free download from link (backup link).
Take a look at the pplot()
documentation (link here). Specifically you can plot given the Arc List, denoted IJ
in the documentation (see code below).
% Examples:
XY = [2 2; 3 1; 4 3; 1 4]; % Points
pplot(XY,'r.')
pplot(XY,num2cell(1:4))
IJ = [1 2; 1 3; 1 4]; % Arc List
h1 = pplot(IJ,XY,'g-');
IJlab = {'(1,2)','(1,3)','(1,4)'};
h2 = pplot(IJ,IJlab,XY);
delete([h1; h2])
loc = {[1 4 3 2 1]}; % Loc Seq Vector
h3 = pplot(loc,XY,'g-');
loclab = {'(1,4)','(4,3)','(3,2)','(2,1)'};
h4 = pplot(loc,loclab,XY);
set(h3,'color','b')
This was able to quickly produce your desired plot.
Update
Example using tspnneighbor()
and tsp2opt()
from the MATLOG function list & documentation.
This approach uses a location vector which is just the node indices in sequence, e.g. loc = [3 5 6 1 2 4 3]
.
% Add MATLOG directory to path
path(path,'C:\Users\username\mypathtoMATLOG')
% DATA
XY_NCcity = uscity('XY',strcmp('NC',uscity('ST'))); % Get NC city [lon, lat] coordinates
C = dists(XY_NCcity,XY_NCcity,'mi'); % Get distance matrix
% ANIMATION
% Closest Unvisited City Algorithm (Nearest Neighbor)
makemap(XY_NCcity)
h = pplot(XY_NCcity,'r.')
[loc,TC,bestvtx] = tspnneighbor(C,527,h) % Start CUCA from node 527 (Raleigh)
Below is the output using cities in North Carolina. Further examination of the tspnneighbor( )
function shows it uses MATLOG's pplot
function.
For completeness, there is also a two-opt improvement feature, tsp2opt()
, which can improve the solution.
% OPTIONAL: ANIMATED TWO-OPT IMPROVEMENT
% Demonstrates what a "more optimal" tour would look like
% Final result is not guaranteed to be optimal
[loc,TC] = tsp2opt(loc,C,[],[],[],h); % Two-opt heuristic improvement