-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.cpp
183 lines (155 loc) · 5.37 KB
/
App.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include "App.h"
App::App() {
for(int i = 0; i < NUMBER_GRAPHS; i++){
graphs.push_back(new Graph(i));
}
}
void waitForKey() {
cout << "\nPress ENTER to continue ";
cin.ignore();
cin.get();
}
int getOptionFromUser() {
int choice;
cin >> choice;
return choice;
}
void App::run() {
while (true) {
cout << "\n\n=========== SELECT A STRATEGY TO SOLVE T.S.P. ===========\n";
cout << "1. Backtracking Algorithm\n";
cout << "2. Triangular Approximation Heuristics\n";
cout << "3. Nearest Insertion Heuristics\n";
cout << "4. Quit\n";
cout << "Enter your choice: ";
int choice = getOptionFromUser();
switch (choice) {
case 1:
menuOption1();
break;
case 2:
menuOption2();
break;
case 3:
menuOption3();
break;
case 4:
std::cout << "\nGoodbye!\n";
return;
default:
std::cout << "Invalid choice. Please try again.\n";
}
}
}
Graph* App::getGraphFromUser() const {
cout << "\n\n=========== SELECT A GRAPH ===========\n";
cout << "0. Go back\n";
cout << "1. shipping.csv (toy)\n";
cout << "2. stadiums.csv (toy)\n";
cout << "3. tourism.csv (toy)\n";
cout << "4. graph1 (real world)\n";
cout << "5. graph2 (real world)\n";
cout << "6. graph3 (real world)\n";
cout << "7. edges_25.csv (extra)\n";
cout << "8. edges_50.csv (extra)\n";
cout << "9. edges_75.csv (extra)\n";
cout << "10. edges_100.csv (extra)\n";
cout << "11. edges_200.csv (extra)\n";
cout << "12. edges_300.csv (extra)\n";
cout << "13. edges_400.csv (extra)\n";
cout << "14. edges_500.csv (extra)\n";
cout << "15. edges_600.csv (extra)\n";
cout << "16. edges_700.csv (extra)\n";
cout << "17. edges_800.csv (extra)\n";
cout << "18. edges_900.csv (extra)\n";
cout << "Enter your choice: ";
int graphToUse = getOptionFromUser();
if (graphToUse <= 0 || graphToUse > NUMBER_GRAPHS) {
cout << "Invalid graph choice.\n";
return nullptr;
}
Graph *graph = graphs[graphToUse-1];
if(!graph->isLoaded()){
auto startTime = std::chrono::high_resolution_clock::now();
graph->load();
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
cout << "Time spent loading: " << duration.count() << " milliseconds\n\n";
}
return graph;
}
void App::menuOption1() {
Graph *graph = getGraphFromUser();
if(graph == nullptr){
return;
}
cout << "Running backtracking algorithm...\n";
auto startTime = std::chrono::high_resolution_clock::now();
auto result = graph->TSP_Backtracking();
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
cout << "\n>>> RESULT: ";
if(graph->getNumberNodes() <= 100){
cout << "\nThe shortest path is: ";
for (unsigned int i : result.second) {
cout << i << " ";
}
}
cout << "\nThe distance is: " << result.first << endl;
cout << "Time spent: " << duration.count() << " milliseconds" << endl;
waitForKey();
}
void App::menuOption2() {
Graph *graph = getGraphFromUser();
if(graph == nullptr){
return;
}
cout << "Running triangular approximation heuristics...\n";
auto startTime = std::chrono::high_resolution_clock::now();
auto result = graph->TSP_TriangularApproximation();
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
if(result.first == -1){
cout << "\n>>> It was not possible to find a path\n";
waitForKey();
return;
}
cout << "\n>>> RESULT: ";
if(graph->getNumberNodes() <= 100){
cout << "\nThe shortest path is: ";
for (unsigned int i : result.second) {
cout << i << " ";
}
}
cout << "\nThe distance is: " << result.first << endl;
cout << "Time spent: " << duration.count() << " milliseconds" << endl;
waitForKey();
}
void App::menuOption3() {
Graph *graph = getGraphFromUser();
if(graph == nullptr){
return;
}
cout << "Running insertion heuristics...\n";
auto startTime = std::chrono::high_resolution_clock::now();
auto result = graph->TSP_NearestInsertion();
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
if(result.first == -1){
cout << "\n>>> It was not possible to find a path\n";
waitForKey();
return;
}
cout << "\n>>> RESULT: ";
//cout << "\nThe path contains the right number of nodes? " << (result.second.size() == graph->getNumberNodes()+1 ? "Yes" : "No");
if(graph->getNumberNodes() <= 100){
cout << "\nThe shortest path is: ";
for (unsigned int i : result.second) {
cout << i << " ";
}
}
cout << "\nThe distance is: " << result.first << endl;
cout << "Time spent: " << duration.count() << " milliseconds" << endl;
waitForKey();
}