-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_recursive.cpp
More file actions
349 lines (336 loc) · 11.2 KB
/
Copy pathmain_recursive.cpp
File metadata and controls
349 lines (336 loc) · 11.2 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <iostream>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <queue>
#include <chrono>
#include <vector>
#include <random>
#include <string>
#include <sstream>
#include <climits>
#include <iterator>
#include <algorithm>
#include <cctype>
#include <locale>
#include <cmath>
#include <tuple>
#include <unordered_set>
#include "structures.h"
using namespace std;
using namespace std::chrono;
vector<vector<string>> tasks;
vector<string> results;
string results_file_name = "";
int number_of_current_graph_vertices = 0;
adjacency_matrix current_graph_adjacency_matrix = adjacency_matrix();
int *path_array;
int ***solved_subproblems;
struct Result
{
string graph_name;
string path;
int path_weight;
double time;
int number_of_repeats;
Result(string graph_name, string path, int path_weight, double time, int number_of_repeats)
{
this->graph_name = graph_name;
this->path = path;
this->path_weight = path_weight;
this->time = time;
this->number_of_repeats = number_of_repeats;
}
string toString()
{
return (graph_name + "," + path + "," + to_string(path_weight) + "," + to_string(time) + "," + to_string(number_of_repeats));
}
};
void save_results(string results_file_name)
{
std::cout << "Saving results" << endl;
fstream fout;
fout.open(results_file_name, ios::out);
fout << "graph_name,path,path_weight,time,number_of_repeats" << endl;
for (long unsigned int i = 0; i < results.size(); i++)
{
fout << results[i] << endl;
}
fout.close();
std::cout << "Correctly saved " << results.size() << " results" << endl;
}
template <typename Out>
void split(const std::string &s, char delim, Out result)
{
std::istringstream iss(s);
std::string item;
while (std::getline(iss, item, delim))
{
*result++ = item;
}
}
std::vector<std::string> split(const std::string &s, char delim)
{
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
static inline void ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch)
{ return !std::isspace(ch); }));
}
static inline void rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch)
{ return !std::isspace(ch); })
.base(),
s.end());
}
bool load_data(string file_name)
{
std::cout << "Loading data from " << file_name << " file" << endl;
ifstream fin;
fin.open(file_name, ios::in);
if (!fin.good())
{
std::cout << "Data file " << file_name << " not exist" << endl;
fin.close();
return false;
}
string loaded_source, loaded_destination, loaded_weight;
string loaded_number_of_vertices;
getline(fin, loaded_number_of_vertices);
number_of_current_graph_vertices = stoi(loaded_number_of_vertices);
current_graph_adjacency_matrix = adjacency_matrix(number_of_current_graph_vertices);
for (int i = 0; i < number_of_current_graph_vertices; i++)
{
string loaded_line_of_matrix = "";
getline(fin, loaded_line_of_matrix);
ltrim(loaded_line_of_matrix);
rtrim(loaded_line_of_matrix);
vector<string> single_line = split(loaded_line_of_matrix, ' ');
std::vector<std::string>::iterator it = single_line.begin();
while (it != single_line.end())
{
if (it->length() == 0)
{
it = single_line.erase(it);
}
else
{
++it;
}
}
for (long unsigned int j = 0; j < single_line.size(); j++)
{
ltrim(single_line[j]);
rtrim(single_line[j]);
current_graph_adjacency_matrix.add_edge_dir(i, j, stoi(single_line[j]));
}
}
std::cout << "Loaded correctly graph with " << number_of_current_graph_vertices << " vertices" << endl
<< "Graph:" << endl;
current_graph_adjacency_matrix.print();
fin.close();
return true;
}
void load_config()
{
std::cout << "Loading config.ini" << endl;
ifstream fin;
fin.open("config.ini", ios::in);
if (!fin.good())
{
std::cout << "Config.ini not found" << endl;
fin.close();
return;
}
string loaded_line_of_task = "";
getline(fin, results_file_name);
while (getline(fin, loaded_line_of_task))
{
vector<string> single_line = split(loaded_line_of_task, ' ');
string graph_file_name, number_of_repeats, shortest_path_weight, shortest_path;
if (single_line.size() >= 4)
{
graph_file_name = single_line[0];
number_of_repeats = single_line[1];
shortest_path_weight = single_line[2];
for (long unsigned int i = 3; i < single_line.size(); i++)
{
shortest_path += single_line[i];
shortest_path += " ";
}
}
if (graph_file_name.size() == 0|| number_of_repeats.size() == 0 || shortest_path_weight.size() == 0 || shortest_path.size() == 0)
{
std::cout << "Cannot load this task: " << graph_file_name << " " << number_of_repeats << " " << shortest_path_weight << " " << shortest_path;
break;
}
vector<string> task;
task.push_back(graph_file_name);
task.push_back(number_of_repeats);
task.push_back(shortest_path_weight);
task.push_back(shortest_path);
tasks.push_back(task);
}
fin.close();
std::cout << "Config loaded correctly" << endl;
return;
}
unsigned int countSetBits(unsigned int n)
{
unsigned int count = 0;
while (n)
{
count += n & 1;
n >>= 1;
}
return count;
}
int fact(int x)
{
if (x == 1 || x == 0)
return 1;
return (x * fact(x - 1));
}
int *solve_combination(int finish, unsigned long long node_set)
{
if (solved_subproblems[finish][node_set][0] != INT_MAX)
{
return solved_subproblems[finish][node_set];
}
else if (node_set == 0)
{
solved_subproblems[finish][node_set][0] = current_graph_adjacency_matrix.matrix[0][finish];
solved_subproblems[finish][node_set][1] = 0;
return solved_subproblems[finish][node_set];
}
else
{
for (long unsigned int i = 0; i < sizeof(node_set) * 8; i++)
{
if (1 & (node_set >> i)) // node_set contains i?
{
unsigned long long current_node_set = node_set & (~(1 << i));
solved_subproblems[i + 1][current_node_set] = solve_combination(i + 1, current_node_set);
int cost = current_graph_adjacency_matrix.matrix[i + 1][finish] + solved_subproblems[i + 1][current_node_set][0];
if (cost < solved_subproblems[finish][node_set][0])
{
solved_subproblems[finish][node_set][0] = cost;
solved_subproblems[finish][node_set][1] = i + 1;
}
}
}
return solved_subproblems[finish][node_set]; // cost, parent
}
}
pair<vector<int>, int> TSP_held_karp()
{
vector<int> path;
int weight;
int number_of_combinations = pow(2, number_of_current_graph_vertices - 1);
solved_subproblems = new int **[number_of_current_graph_vertices];
for (int i = 0; i < number_of_current_graph_vertices; i++)
{
solved_subproblems[i] = new int *[number_of_combinations];
for (int j = 0; j < number_of_combinations; j++)
{
solved_subproblems[i][j] = new int[2]();
solved_subproblems[i][j][0] = INT_MAX;
}
}
unsigned long long node_set;
node_set = number_of_combinations - 1;
int *solve = solve_combination(0, node_set);
weight = solve[0];
path.push_back(0);
int *temp_solve = solve;
unsigned long long temp_node_set = node_set;
for (int i = 0; i < number_of_current_graph_vertices; i++)
{
path.push_back(temp_solve[1]);
temp_node_set = temp_node_set & (~(1 << (temp_solve[1] - 1)));
temp_solve = solve_combination(temp_solve[1], temp_node_set);
}
for (int i = 0; i < number_of_current_graph_vertices; i++)
{
for (int j = 0; j < number_of_combinations; j++)
{
delete solved_subproblems[i][j];
}
delete solved_subproblems[i];
}
delete solved_subproblems;
std::reverse(path.begin(), path.end());
return make_pair(path, weight);
}
int main()
{
load_config();
if (tasks.size() == 0)
{
std::cout << "No tasks found to be performed." << endl;
}
else
{
for (long unsigned int i = 0; i < tasks.size(); i++)
{
std::cout << endl
<< "##################################################" << endl
<< endl;
string graph_file_name = tasks[i][0];
int number_of_repeats = stoi(tasks[i][1]);
string shortest_path_weight = tasks[i][2];
string shortest_path = tasks[i][3];
if (!load_data(graph_file_name))
{
std::cout << "Cannot load graph from " << graph_file_name << " file." << endl;
}
std::cout << "Computing TSP in " << graph_file_name << " graph repeated " << number_of_repeats << " times" << endl;
if (number_of_current_graph_vertices < 1)
{
std::cout << "Cannot execute task. The array must to have at least 1 element.";
}
else if (number_of_repeats < 1)
{
std::cout << "Cannot execute task. The minimum number of repetitions is 1.";
}
else
{
pair<vector<int>, int> answer;
high_resolution_clock::time_point t_start = high_resolution_clock::now();
for (int j = 0; j < number_of_repeats; j++)
{
answer = TSP_held_karp();
}
high_resolution_clock::time_point t_end = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t_end - t_start);
int weight = answer.second;
string path = "";
std::vector<int>::iterator it = answer.first.begin();
while (it != answer.first.end())
{
path += to_string(*it);
path += " ";
it++;
}
ltrim(path);
rtrim(path);
cout << "Calculated shortest path: " << path << endl
<< "Defined shortest path: " << shortest_path << endl
<< "Calculated weight: " << weight << endl
<< "Defined weight: " << shortest_path_weight << endl
<< "Time: " << ((double)time_span.count() / (double)number_of_repeats) << " s" << endl;
Result result = Result(graph_file_name, path, weight, time_span.count(), number_of_repeats);
results.push_back(result.toString());
}
}
}
std::cout << endl;
save_results(results_file_name);
std::cout << "Press any key to continue...";
getchar();
return 0;
}