-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectSubtours.m
33 lines (31 loc) · 1.58 KB
/
detectSubtours.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function subTours = detectSubtours(x,idxs)
% used to be part of matlab but removed in the latest versions so added here for
% *forward*-compatability - how often do you get to say that? :-/
% Returns a cell array of subtours. The first subtour is the first row of x, etc.
% Copyright 2014 The MathWorks, Inc.
x = round(x); % correct for not-exactly integers
r = find(x); % indices of the trips that exist in the solution
substuff = idxs(r,:); % the collection of node pairs in the solution
unvisited = ones(length(r),1); % keep track of places not yet visited
curr = 1; % subtour we are evaluating
startour = find(unvisited,1); % first unvisited trip
while ~isempty(startour)
home = substuff(startour,1); % starting point of subtour
nextpt = substuff(startour,2); % next point of tour
visited = nextpt; unvisited(startour) = 0; % update unvisited points
while nextpt ~= home
% Find the other trips that starts at nextpt
[srow,scol] = find(substuff == nextpt);
% Find just the new trip
trow = srow(srow ~= startour);
scol = 3-scol(trow == srow); % turn 1 into 2 and 2 into 1
startour = trow; % the new place on the subtour
nextpt = substuff(startour,scol); % the point not where we came from
visited = [visited,nextpt]; % update nodes on the subtour
unvisited(startour) = 0; % update unvisited
end
subTours{curr} = visited; % store in cell array
curr = curr + 1; % next subtour
startour = find(unvisited,1); % first unvisited trip
end
end