MATLAB Answers

Error using matlab.gra​phics.char​t.primitiv​e.GraphPlo​t/highligh​t>checkSub​graph

31 views (last 30 days)
I am trying to solve a modified version of Travelling Salesman Problem based on the example here https://www.mathworks.com/help/optim/examples/travelling-salesman-problem.html . I solved it using MATLAB-Gurobi interface. I got problem in visualizing the results. I keep getting the error messages below:
"Error using matlab.graphics.chart.primitive.GraphPlot/highlight>checkSubgraph
G must have the same nodes and a subset of the edges of the plotted graph.
Error in matlab.graphics.chart.primitive.GraphPlot/highlight
Error in TSPGurobi (line 186)
highlight(hGraph,Gsol,'LineStyle','-')"
I use the same code from the example to visualize the results, why did I get error messages?
Results: Node 1, 2, 5, 6 are visited and connected.
I once plotted the results using the same code, but it no longer works. I don't know why. Here are my codes for the visulization part. I directly put the results of the TSP as data with vector name of edge and node. Any help is appreciated! Thanks!
I use 2020a, 2019b and 2019a, same error.
% Generate random stops inside a crude polygonal representation of the continental U.S.
load('usborder.mat','x','y','xx','yy');
rng(3,'twister') % Makes a plot with stops in Maine & Florida, and is reproducible
nStops =7; % You can use any number, but the problem size scales as N^2
stopsLon = zeros(nStops,1); % Allocate x-coordinates of nStops
stopsLat = stopsLon; % Allocate y-coordinates
n = 1;
while (n <= nStops)
xp = rand*1.5;
yp = rand;
if inpolygon(xp,yp,x,y) % Test if inside the border
stopsLon(n) = xp;
stopsLat(n) = yp;
n = n+1;
end
end
% Generate all the trips, meaning all pairs of stops.
idxs = nchoosek(1:nStops,2);
%Represent the problem as a graph. Create a graph where the stops are nodes and the trips are edges.
G = graph(idxs(:,1),idxs(:,2));
%Display the stops using a graph plot. Plot the nodes without the graph edges.
num=1:nStops;
label=string(num);
figure
hGraph = plot(G,'XData',stopsLon,'YData',stopsLat,'LineStyle','none','NodeLabel',{});
labelnode(hGraph,[1:nStops],label(1:nStops))
hold on
% Draw the outside border
plot(x,y,'r-')
hold off
% I solve the problem and get the results below
edge=[1;0;0; 0;1;0; 0;0;1; 0;0;0; 0;0;0; 0;0;0; 1;0;0 ];
node=[1;1;0;0;1;1;0];
% Create a new graph with the solution trips as edges.
% To do so, round the solution in case some values are not exactly integers, and convert the resulting values to logical.
ledge= logical(round(edge));
Gsol = graph(idxs(ledge,1),idxs(ledge,2));
%Overlay the new graph on the existing plot and highlight its edges.
%hold on
highlight(hGraph,Gsol,'LineStyle','-')
title('Solution with Subtours')

Accepted Answer

Christine Tobler
Christine Tobler on 6 Jul 2020
Try using
Gsol = graph(idxs(ledge,1),idxs(ledge,2), [], numnodes(G));
so that G and Gsol both have the same number of nodes. This wasn't necessary in the doc example, but might be needed with modified code.
  3 Comments
Christine Tobler
Christine Tobler on 10 Jul 2020
If G and Gsol don't have the same number of nodes, the highlight command should always error. However, in the plot above, Gsol includes an edge that uses node 6, so the number of nodes of Gsol would be 6, same as that of G itself.
In the previous code, Gsol will have fewer nodes than G if it doesn't include an edge that touches the node with maximal index.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!