-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp_discrete_enn.cpp
181 lines (163 loc) · 6.5 KB
/
tsp_discrete_enn.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
// -*- C++ -*-
#include "tsp_discrete_enn.hpp"
#include <chrono>
#include <filesystem>
using TimeMilliS_t = std::chrono::milliseconds;
using TimeMicroS_t = std::chrono::microseconds;
using TimeUnit_t = TimeMilliS_t;
using TimePoint_t = std::chrono::steady_clock::time_point;
const std::string& time_unit{ "ms" };
const std::string& Data_Dir{ "Data/ALL_tsp" };
namespace stdfs = std::filesystem;
stdfs::path Data_Path{ stdfs::current_path() / stdfs::path(Data_Dir) };
const std::string& Data_Filename_berlin{ "berlin52.tsp" };
std::string Data_Filename{ "" };
stdfs::path Data_FilePath;
int main(int argc, char** argv)
{
const auto args_count{ static_cast<std::size_t>(argc) };
std::vector<std::string> args(args_count - 1);
for (std::size_t idx{ 1 }; idx < args_count; ++idx) {
args[idx - 1] = std::string(argv[idx]);
#if (DEBUG_PRINT > 1)
std::cout << "#" << idx << " flag " << args[idx - 1] << '\n';
#endif
}
// -------------------------------------------
// Seed and initialize rng
// -------------------------------------------
std::random_device::result_type seed{ std::random_device{}() };
std::default_random_engine rng{ seed };
// -------------------------------------------
// Create data file
// -------------------------------------------
if (utils::vectContains(std::string{ "--input" }, args)) {
const auto it = std::find_if(args.begin(), args.end(), utils::MatchItem<std::string>{ "--input" });
Data_Filename = *(it + 1);
}
if (Data_Filename.empty()) {
Data_Filename = Data_Filename_berlin;
}
Data_FilePath = Data_Path / stdfs::path(Data_Filename);
// -------------------------------------------
// Parse cities
// -------------------------------------------
Cities_t cities;
parseCities(cities, Data_FilePath.string());
std::shuffle(cities.begin(), cities.end(), rng);
const int num_cities = cities.size();
// -------------------------------------------
// Calculate layer details
// -------------------------------------------
int layers{ 0 };
int layers_val{ 1 };
while (true) {
++layers;
layers_val *= 4;
if (layers_val >= num_cities)
break;
}
std::printf(
"[Info]: Total number of layers expected %d (power of 4 : %d) for number of cities %d.\n",
layers, layers_val, num_cities);
// -------------------------------------------
// Create and setup Discrete ENN Solver
// -------------------------------------------
DiscreteENN_TSP enn_tsp;
// -------------------------------------------
// Construct Stack
// -------------------------------------------
createStack(cities, enn_tsp.stack(), layers);
std::printf("[Info]: Total number of layers created %d.\n", layers);
// -------------------------------------------
// Initialize Path
// -------------------------------------------
enn_tsp.initializePath();
if (enn_tsp.path().size() == static_cast<std::size_t>(num_cities)) {
std::printf(
"[Info]: Algorithm complete. Only %d number of cities provided.",
num_cities);
return EXIT_SUCCESS;
}
// -------------------------------------------
// Construct Path
// -------------------------------------------
enn_tsp.constructPath();
{
#if (TSP_DEBUG_PRINT > 0)
std::cout << ("\n[Debug] (main): validatePath\n");
#endif
NodeExp_t<bool> erased = enn_tsp.validatePath();
if (erased.err()) {
std::cerr << "[Error] (main): validatePath failed\n";
return EXIT_FAILURE;
}
#if (TSP_DEBUG_PRINT > 0)
std::cout << ("\n[Debug] (main): validatePath updateCostAll\n");
#endif
const auto [idx_fail, err] = enn_tsp.updateCostAll();
if (err) {
std::cerr << "[Error] (main): updateCostAll failed at index "
<< idx_fail << '\n';
return EXIT_FAILURE;
}
}
// -------------------------------------------
// Run Discrete ENN
// -------------------------------------------
std::cout << ("\n[Info] (main): Run Discrete ENN Algorithm\n");
TimePoint_t start_time = std::chrono::steady_clock::now();
const bool success = enn_tsp.run(rng);
TimePoint_t end_time = std::chrono::steady_clock::now();
if (not success) {
std::cerr << "[Error] (main): Discrete ENN run failed.\n";
return EXIT_FAILURE;
}
auto delta = std::chrono::duration_cast<TimeUnit_t>(end_time - start_time);
const auto duration = delta.count();
std::cout << "\n" + utils::Line_Str + "\n";
std::cout << "[Info] (main): Algorithm finished in " << duration
<< time_unit + "\n";
std::cout << utils::Line_Str + "\n";
assert("[Error] (main): path size not equal to stack size" &&
(enn_tsp.path().size() == enn_tsp.stack().size()));
assert("[Error] (main): path size not equal to number of cities" &&
(enn_tsp.path().size() == static_cast<std::size_t>(num_cities)));
const std::size_t num_nodes = enn_tsp.path().size();
Path_t& path = enn_tsp.path();
for (std::size_t idx{ 0 }; idx != num_nodes; ++idx) {
const auto [valid, err] = enn_tsp.validateNode(path[idx]);
if (err) {
std::cerr
<< "[Error] (main): Algoirthm has not found the optimal path\n";
return EXIT_FAILURE;
}
}
NodeExp_t<bool> erased = enn_tsp.validatePath();
if (erased.err()) {
std::cerr << "[Error] (main): final validatePath failed\n";
return EXIT_FAILURE;
}
if (erased.has_value()) {
std::cerr << "[Error] (main): final validatePath removed node(s)\n";
return EXIT_FAILURE;
}
// -------------------------------------------
// Show results
// -------------------------------------------
std::cout << "[Info]: Print results\n";
Value_t dist{ 0.0 };
for (std::size_t idx{ 0 }; idx != num_nodes; ++idx) {
// path[idx]->print();
const auto idx_next{ static_cast<std::size_t>(enn_tsp.properIndex(idx + 1)) };
dist += getDistance(*path[idx], *path[idx_next]);
}
std::cout << "\n" + utils::Line_Str + "\n";
std::cout << "[Info]: Total distance is : " << dist << '\n';
std::cout << utils::Line_Str + "\n";
if (not utils::vectContains(std::string{ "--batch" }, args)) {
const bool show_coords{ utils::vectContains(std::string{"--show-coords"}, args) };
drawPath(path, enn_tsp.stack(), show_coords);
}
return 0;
}