Fast 2-Opt Travelling Salesman Problem (TSP)

バージョン 1.0.9 (2.12 KB) 作成者: Moreno, M.
Fast and simple TSP algorithm using 2-opt heuristics for symmetric and non-symmetric problems.
ダウンロード: 30
更新 2024/10/19

ライセンスの表示

Description
tsp2opt finds a solution to the symmetric and non-symmetric Travelling Salesman Problem (TSP) using the 2-opt heuristic algorithm. It attempts to find the shortest (or longest) possible route that visits each city exactly once.
The function can handle multidimensional coordinates and includes options for setting the number of iterations and cycles. By providing an arbitrary last argument, the function can be instructed to find the maximum length path instead of the shortest.
If the start or endpoints had to be preserved, the function includes the comments for its implementation.
Syntax
[path, dist] = tsp2opt(x)
[path, dist] = tsp2opt(x, iter)
[path, dist] = tsp2opt(x, iter, ~)
Inputs
  • x: An N-by-M matrix representing the coordinates of N-nodes (cities) in M-dimensions.
  • iter (optional): Iteration count, specified as a scalar or a vector. If scalar, it represents the number of iterations. If vector, the first element specifies the number of attempts. If (any) negative, the non-symmetric TSP is solved. Default: max(10, ceil(n/10))
  • N/A (optional): If passed, flag to query for the longest possible route. Default: none
Outputs
  • path: A vector containing the indices of the nodes in the order they are visited in the optimized path.
  • dist: The total distance of the computed path.
Examples
% Initialize input data
col = [0, 114, 189; 217, 83, 25; 237, 177, 32; 126, 47, 142; ...
119, 172, 48; 77, 190, 238; 162, 20, 47] / 255;
x = rand(100,2);
% EXAMPLE 1: Validate different input types
rng('default'), [path1, dist1] = tsp2opt(x);
rng('default'), [path2, dist2] = tsp2opt(pdist2(x,x));
if isequal(path1, path2)
fprintf('SUCCESS: different input types match.\n')
else
fprintf('ERROR: different inputs do not match.\n')
end
% EXAMPLE 2: Optimized search with cycles/iter.
[path3, dist3] = tsp2opt(x, 5);
[path4, dist4] = tsp2opt(x, [10,100]);
figure, plot(x(path3,1), x(path3,2), 'o:')
hold on, plot(x(path4,1), x(path4,2),'LineWidth',1)
title 'Optimized TSP search'
% EXAMPLE 3: Color sorting for contrast
figure, tiledlayout(3,1), nexttile(1)
imshow(reshape(col, 1, [], 3), 'InitialMagnification', 'fit')
title 'Original color array'
idx = tsp2opt(col); idx = idx(1:end-1); nexttile(2)
imshow(reshape(col(idx,:), 1, [], 3), 'InitialMagnification', 'fit')
title 'Minimum contrast sorting'
idx = tsp2opt(col, [], 'long'); idx = idx(1:end-1); nexttile(3)
imshow(reshape(col(idx,:), 1, [], 3), 'InitialMagnification', 'fit')
title 'Maximum contrast sorting'
% EXAMPLE 4: different types of TSP
idx = tsp2opt(x(1:10,:), 10); figure, plot(x(idx,1), x(idx,2), 'o-')
idx = tsp2opt(x(1:10,:), -10); hold on, plot(x(idx,1) + 1, x(idx,2), 'o-')
title 'Symmetric and non-symmetric problem', pbaspect([2,1,1])

引用

Moreno, M. (2024). Fast 2-Opt Travelling Salesman Problem (TSP) (https://www.mathworks.com/matlabcentral/fileexchange/169161-fast-2-opt-travelling-salesman-problem-tsp), MATLAB Central File Exchange. に取得済み.

MATLAB リリースの互換性
作成: R2024a
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linux
タグ タグを追加

Community Treasure Hunt

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

Start Hunting!
バージョン 公開済み リリース ノート
1.0.9

Final version: optimal 2-opt implementation given an iteration number

1.0.8

Problem-based default iteration count
Symmetric and non-symmetric problems

1.0.7

- Added non-symmetric problem (TSP2OPT1)
- Added fixed-ends non-symmetric problem (TSP2OPT2)

1.0.6

Fully-optimized code via test functions.

1.0.5

Separated first segment from the FOR loop to accelerate the calculation.
Changed function name to TSP2OPT.
Updated thumbnail.

1.0.4

Corrected a small error in the final distance calculation

1.0.3

Increased speed of the function and grouped inputs more intuitively.

1.0.2

Logo change.

1.0.1

Includes 'worst' possible solution upon an additional input parameter call

1.0.0