www.gusucode.com > optim 案例源码 matlab代码程序 > optim/detectSubtours.m
function subTours = detectSubtours(x,idxs) % 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