-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoDTSPAlgorithms_old.m
131 lines (107 loc) · 3.7 KB
/
doDTSPAlgorithms_old.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function [ ] = runDTSPAlgorithms_old( V, x, options)
%RUNPTASALGORITHMS Find the length of the shortest Dubins path
% Plots solutions from various PTAS algorithms: nearest neighbor (greedy
% point-to-point) in MATLAB and C++, alternating algorithm in C++, nearest
% neighbor with line fitting (greedy line to line).
%
% Parameters:
% V An n-by-2 matrix of vertices to visit.
% x Starting heading.
% options Options for the scenario.
%
% David Goodman
% 2015.09.12
USE_CPP_SOLVER = 1; % C++ solvers
%% ================ Dependencies ===============
% Find all dependencies
% Add lib and class folders
addpath('lib','class');
% Add graph theory toolbox
if exist('grBase') ~= 2
if exist('lib/GrTheory') ~= 7
error('Could not find the GrTheory folder.');
end
addpath('lib/GrTheory');
end
% Add Dubins plot tool
if exist('dubins') ~= 3
if exist('lib/DubinsPlot') ~= 7
error('Could not find the DubinsPlot folder.');
end
addpath('lib/DubinsPlot');
if exist('dubins') ~= 3
error('Could not find compiled dubins mex file.');
end
end
% TODO add checks for c++ solvers
%% ================== Solver =================
startPosition = V(1,:);
startHeading = x; % [rad]
subplotDim = [2 2];
sortrows(V,[-2 1]); % sort by y descending, then by x ascending
[n, ~] = size(V);
if strcmp(options.Debug, 'on')
fprintf('## Testing scenario with %d waypoints...\n\n', n);
end
% Initial Graph
%figure('units','normalized','outerposition',[0 0 1 1])
%fig = plotWaypointScenario(V, [], subplotDim, 1, titleStr, options);
fig = gcf;
uicontrol('Style', 'text',...
'String', sprintf('Point to Point Solutions'),...
'Units','normalized',...
'FontSize',15,...
'BackgroundColor', 'white',...
'Position', [0.345 0.965 0.29 0.037]);
% ============== Nearest Neighbor (greedy PTP) ======================
% Run solvers
% Call MATLAB solver
fprintf('Running Nearest Neighbor (MATLAB) Greedy PTP solver...\n');
tic;
[E, X, Cost] = solveGreedyPointToPoint(V,startHeading,options);
elapsedTime = toc;
c = Cost;
vertexOrder = getVertexOrder(E);
if strcmp(options.Debug,'on')
fprintf(['Found nearest neighbor solution with a total cost of %.2f in %.2f seconds.\n\n'],...
c, elapsedTime);
end
titleStr = sprintf('Nearest Neighbor MATLAB (cost = %.2f)',c);
hAx = plotWaypointScenario(V, E, subplotDim, 2, titleStr, options);
plotWaypointDubins(hAx, V, E, X, options);
% Call CPP solver
if USE_CPP_SOLVER
fprintf('Running Nearest Neighbor (CPP) solver...\n');
tic;
[E, X, Cost] = solveNearestNeighborDTSP(V,startHeading,options);
%[E, X, Cost] = solveAlternatingDTSP(V,startHeading,options)
elapsedTime = toc;
c = Cost;
vertexOrder = getVertexOrder(E);
if strcmp(options.Debug,'on')
fprintf(['Found Nearest Neighbor CPP solution with a total cost of %.2f in %.2f seconds.\n\n'],...
c, elapsedTime);
end
titleStr = sprintf('Nearest Neighbor CPP (cost = %.2f)',c);
hAx = plotWaypointScenario(V, E, subplotDim, 3, titleStr, options);
plotWaypointDubins(hAx, V, E, X, options);
end
% ============== Alternating Algorithm (DTSP) ======================
% Call CPP solver
if USE_CPP_SOLVER
fprintf('Running Alternating Algorithm (CPP) solver...\n');
tic;
[E, X, Cost] = solveAlternatingDTSP(V,startHeading,options);
%[E, X, Cost] = solveAlternatingDTSP(V,startHeading,options)
elapsedTime = toc;
c = Cost;
vertexOrder = getVertexOrder(E);
if strcmp(options.Debug,'on')
fprintf(['Found Alternating Algorithm CPP solution with a total cost of %.2f in %.2f seconds.\n\n'],...
c, elapsedTime);
end
titleStr = sprintf('Alternating CPP (cost = %.2f)',c);
hAx = plotWaypointScenario(V, E, subplotDim, 4, titleStr, options);
plotWaypointDubins(hAx, V, E, X, options);
end
end