Skip to content

Commit

Permalink
fix: cleanup prints and add stringFmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Akhil-CM committed Apr 29, 2024
1 parent 2582baa commit 9ae8aeb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 55 deletions.
18 changes: 17 additions & 1 deletion Output/DiscreteENN_TSP_table.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
name,points,error,time(us),time per city(us),distance,optimal_distance
pcb442,442,7.670000,17520.000000,39.639999,54672.000000,50778.000000
berlin52,52,7.660000,5706.000000,109.730003,8119.740234,7542.000000
st70,70,5.280000,13714.000000,195.910004,710.640015,675.000000
eil76,76,7.500000,21199.000000,278.929993,578.359985,538.000000
pr76,76,2.750000,15595.000000,205.199997,111134.906250,108159.000000
kroA100,100,3.100000,38926.000000,389.260010,21941.839844,21282.000000
kroC100,100,5.280000,38659.000000,386.589996,21844.609375,20749.000000
kroD100,100,9.290000,40232.000000,402.320007,23271.939453,21294.000000
rd100,100,6.780000,40496.000000,404.959991,8446.009766,7910.000000
eil101,101,8.240000,43959.000000,435.239990,680.840027,629.000000
lin105,105,6.590000,46708.000000,444.839996,15326.269531,14379.000000
ch130,130,5.240000,93645.000000,720.349976,6430.339844,6110.000000
ch150,150,7.720000,127938.000000,852.919983,7032.080078,6528.000000
tsp225,225,2.460000,578429.000000,2570.800049,4012.479980,3916.000000
a280,279,8.240000,1161661.000000,4163.660156,2791.620117,2579.000000
pcb442,442,9.060000,3614965.000000,8178.649902,55376.488281,50778.000000
pr1002,1002,7.630000,35489376.000000,35418.539062,278808.312500,259045.000000
pr2392,2392,9.870000,896665792.000000,374860.281250,415355.312500,378032.000000
18 changes: 17 additions & 1 deletion Output/DiscreteENN_TSP_table.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
name points error time(us) time per city(us) distance optimal_distance
pcb442 442 7.670000 17520.000000 39.639999 54672.000000 50778.000000
berlin52 52 7.660000 5706.000000 109.730003 8119.740234 7542.000000
st70 70 5.280000 13714.000000 195.910004 710.640015 675.000000
eil76 76 7.500000 21199.000000 278.929993 578.359985 538.000000
pr76 76 2.750000 15595.000000 205.199997 111134.906250 108159.000000
kroA100 100 3.100000 38926.000000 389.260010 21941.839844 21282.000000
kroC100 100 5.280000 38659.000000 386.589996 21844.609375 20749.000000
kroD100 100 9.290000 40232.000000 402.320007 23271.939453 21294.000000
rd100 100 6.780000 40496.000000 404.959991 8446.009766 7910.000000
eil101 101 8.240000 43959.000000 435.239990 680.840027 629.000000
lin105 105 6.590000 46708.000000 444.839996 15326.269531 14379.000000
ch130 130 5.240000 93645.000000 720.349976 6430.339844 6110.000000
ch150 150 7.720000 127938.000000 852.919983 7032.080078 6528.000000
tsp225 225 2.460000 578429.000000 2570.800049 4012.479980 3916.000000
a280 279 8.240000 1161661.000000 4163.660156 2791.620117 2579.000000
pcb442 442 9.060000 3614965.000000 8178.649902 55376.488281 50778.000000
pr1002 1002 7.630000 35489376.000000 35418.539062 278808.312500 259045.000000
pr2392 2392 9.870000 896665792.000000 374860.281250 415355.312500 378032.000000
80 changes: 39 additions & 41 deletions include/tsp_discrete_enn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

#define TSP_DRAW

#define TSP_DEBUG_PRINT 1
#define TSP_DEBUG_CHECK 0
#define TSP_DEBUG_PRINT 0
#define TSP_ERROR 0

#define TSP_TIME 2
Expand All @@ -23,7 +22,7 @@
#include <utility>
#include <chrono>

#if TSP_DEBUG_PRINT > 1
#if TSP_DEBUG_PRINT > 0
#include <thread>
#endif

Expand Down Expand Up @@ -59,7 +58,7 @@ constexpr Index_t Num_Nodes_Initial{ 10 };
constexpr bool Validation_Intersection{ true };
constexpr bool Intersection_Recursive{ true };

#if TSP_DEBUG_PRINT > 1
#if TSP_DEBUG_PRINT > 0
inline bool global_print{ false };
#endif

Expand Down Expand Up @@ -367,7 +366,7 @@ class DiscreteENN_TSP
const Value_t cost_current{ insertionCost(city, m_cities[pos],
m_cities[pos_next]) };
if (cost_current < cost) {
#if TSP_DEBUG_PRINT > 1
#if TSP_DEBUG_PRINT > 0
if (global_print) {
utils::printInfo("cost_current " +
std::to_string(cost_current) +
Expand Down Expand Up @@ -553,6 +552,7 @@ class DiscreteENN_TSP

IndexExp_t<bool> validatePath()
{
IndexOpt_t pos_erased{ std::nullopt };
Index_t num_nodes = m_path.size();
#if TSP_ERROR > 0
if (num_nodes < 3) {
Expand All @@ -563,10 +563,9 @@ class DiscreteENN_TSP
#if TSP_ERROR > 1
throw std::runtime_error{ "Invalid path size" };
#endif
return std::make_pair(-1, true);
return IndexExp_t<bool>{ pos_erased, true };
}
#endif
IndexOpt_t pos_erased{ std::nullopt };
for (Index_t idx{ 0 }; idx != num_nodes;) {
if (m_fromScratch) {
break;
Expand Down Expand Up @@ -717,7 +716,7 @@ class DiscreteENN_TSP
++idx;
continue;
}
#if TSP_DEBUG_PRINT > 1
#if TSP_DEBUG_PRINT > 0
if (global_print) {
const auto [start_tmp, end_tmp] = findEdge(pos_start, pos_end);
utils::printInfoFmt("hasIntersection true for the edges (%u, %u) and (%u, %u)", "removeIntersectionEdge", start_tmp, end_tmp, idx, idx_next);
Expand Down Expand Up @@ -886,7 +885,7 @@ class DiscreteENN_TSP
const City& city{ m_cities[pos] };
const Value_t cost{ insertionCost(city, city_start, city_end) };
if (cost < city.cost) {
#if TSP_DEBUG_PRINT > 1
#if TSP_DEBUG_PRINT > 0
if (global_print) {
const auto [start_tmp, end_tmp] = findEdge(pos_start, pos_end);
utils::printInfoFmt("validation failed for the node %u with edge (%u, %u)", "validateWithEdge", idx, start_tmp, end_tmp);
Expand Down Expand Up @@ -1119,7 +1118,7 @@ class DiscreteENN_TSP

bool run(std::default_random_engine& gen)
{
#if TSP_DEBUG_PRINT > 1
#if TSP_DEBUG_PRINT > 0
bool print_pos{ false };
int loop_count{ 0 };
int loop_check = 1e4;
Expand Down Expand Up @@ -1158,23 +1157,23 @@ class DiscreteENN_TSP
return false;
}

#if TSP_DEBUG_PRINT > 1
#if TSP_DEBUG_PRINT > 0
if (print_pos) {
const auto [idx_prev, idx_next] = getNeigbhours(idx_added);
utils::printInfo(
"Adding node for city " + std::to_string(pos) + " at " +
std::to_string(idx_added) + " with neighbours (" +
std::to_string(idx_prev) + ", " + std::to_string(idx_next) +
")" + " having cities (" +
std::to_string(m_path[idx_prev]) + ", " +
std::to_string(m_path[idx_next]) + ")" + " path size " +
std::to_string(m_path.size()));
m_cities[m_path[properIndex(int(idx_prev) - 1)]].print();
m_cities[m_path[idx_prev]].print();
m_cities[m_path[idx_added]].print();
m_cities[m_path[idx_next]].print();
m_cities[m_path[properIndex(idx_next + 1)]].print();
if (loop_count > loop_check) {
const auto [idx_prev, idx_next] = getNeigbhours(idx_added);
utils::printInfo(
"Adding node for city " + std::to_string(pos) + " at " +
std::to_string(idx_added) + " with neighbours (" +
std::to_string(idx_prev) + ", " + std::to_string(idx_next) +
")" + " having cities (" +
std::to_string(m_path[idx_prev]) + ", " +
std::to_string(m_path[idx_next]) + ")" + " path size " +
std::to_string(m_path.size()));
m_cities[m_path[properIndex(int(idx_prev) - 1)]].print();
m_cities[m_path[idx_prev]].print();
m_cities[m_path[idx_added]].print();
m_cities[m_path[idx_next]].print();
m_cities[m_path[properIndex(idx_next + 1)]].print();
std::cout.flush();
std::cerr.flush();
std::this_thread::sleep_for(sleep_time);
Expand Down Expand Up @@ -1212,7 +1211,7 @@ class DiscreteENN_TSP
}
}

#if TSP_DEBUG_PRINT > 2
#if TSP_DEBUG_PRINT > 1
if (checkIntersectPath()) {
utils::printErr(
"intersection after removeIntersection from adding node at " +
Expand All @@ -1223,7 +1222,7 @@ class DiscreteENN_TSP
}
#endif

#if TSP_DEBUG_PRINT > 2
#if TSP_DEBUG_PRINT > 1
const auto it_erased = validatePath();
if (it_erased.err()) {
utils::printErr("validatePath failed", "run");
Expand All @@ -1243,7 +1242,7 @@ class DiscreteENN_TSP
}
#endif

#if TSP_DEBUG_PRINT > 1
#if TSP_DEBUG_PRINT > 0
if (print_pos and erased1.has_value()) {
if (loop_count > loop_check) {
global_print = true;
Expand All @@ -1270,26 +1269,25 @@ class DiscreteENN_TSP
break;
}
if (not pattern_hashes.insert(m_pattern).second) {
#if TSP_DEBUG_PRINT > 1
distrib.param(distrib_t::param_type(0, stack_size - 1));
const int idx_rand{ distrib(gen) };
// const int idx_rand = 0;
pos = stackPopAt(idx_rand);

#if TSP_DEBUG_PRINT > 0
print_pos = true;
utils::printInfo(
"Found repeating pattern. Randomize input node", "run");
utils::printInfo("Path progress " +
std::to_string(m_path.size()) + "/" +
std::to_string(num_cities),
"run");
#endif

distrib.param(distrib_t::param_type(0, stack_size - 1));
const int idx_rand{ distrib(gen) };
// const int idx_rand = 0;
pos = stackPopAt(idx_rand);

#if TSP_DEBUG_PRINT > 1
utils::printInfo("New starting point " +
std::to_string(idx_rand) + " with city " +
std::to_string(pos),
"run");
// utils::printInfo("New starting point " +
// std::to_string(idx_rand) + " with city " +
// std::to_string(pos),
// "run");
utils::printInfoFmt("New starting point %i with city %u",
"run", idx_rand, pos);
#endif
continue;
}
Expand Down
47 changes: 35 additions & 12 deletions include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,23 @@ inline std::string subtituteStr(std::string text,
return text;
}

inline void printInfo(const std::string& msg, const std::string& fname = "")
template<typename... Args>
inline std::string stringFmt(const std::string& fmt, Args... args)
{
const int size = std::snprintf(nullptr, 0, fmt.c_str(), args...);
if (size < 0) {
throw std::runtime_error{ "[Error] (stringFmt): during formatting with:\n" + fmt };
}
auto buf = std::make_unique<char[]>(size + 1);
std::snprintf(buf.get(), size + 1, fmt.c_str(), args...);
std::string result{buf.get(), static_cast<std::string::size_type>(size)};
return result;
}

template<typename... Args>
inline void printInfoFmt(const std::string& fmt, const std::string& fname, Args... args)
{
std::string msg{ stringFmt(fmt, args...) };
bool split_newlines = (msg.find('\n') != std::string::npos);
std::string prefix{ "[Info]: " };
std::cout << Line_Str << '\n';
Expand All @@ -88,30 +103,24 @@ inline void printInfo(const std::string& msg, const std::string& fname = "")
std::cout << Line_Str << '\n';
}

inline void printErr(const std::string& msg, const std::string& fname = "")
template<typename... Args>
inline void printErrFmt(const std::string& fmt, const std::string& fname, Args... args)
{
std::string msg{ stringFmt(fmt, args...) };
bool split_newlines = (msg.find('\n') != std::string::npos);
std::string prefix{ "[Error]: " };
std::cerr << Line_Str << '\n';
if (not fname.empty()) {
prefix = "\n[Error] (" + fname + "): ";
}
std::cerr << prefix;
std::cerr << (split_newlines ? subtituteStr(msg, "\n", prefix) : msg)
std::cerr << (split_newlines ? subtituteStr(msg, "\n", "\n" + prefix) : msg)
<< '\n';
std::cerr << Line_Str << '\n';
}

template<typename... Args>
inline void printInfoFmt(const std::string& fmt, const std::string& fname, Args... args)
inline void printInfo(const std::string& msg, const std::string& fname = "")
{
const std::size_t msg_len{ fmt.length() * 10 };
std::string msg(msg_len, 'a');
const int cutoff = std::snprintf(msg.data(), msg.length(), fmt.c_str(), args...);
if (cutoff < 0 or cutoff >= msg_len) {
utils::printErr("Coud not produce formatted string. cutoff : " + std::to_string(cutoff), "printInfoFmt");
}
msg = msg.substr(0, cutoff);
bool split_newlines = (msg.find('\n') != std::string::npos);
std::string prefix{ "[Info]: " };
std::cout << Line_Str << '\n';
Expand All @@ -124,6 +133,20 @@ inline void printInfoFmt(const std::string& fmt, const std::string& fname, Args.
std::cout << Line_Str << '\n';
}

inline void printErr(const std::string& msg, const std::string& fname = "")
{
bool split_newlines = (msg.find('\n') != std::string::npos);
std::string prefix{ "[Error]: " };
std::cerr << Line_Str << '\n';
if (not fname.empty()) {
prefix = "\n[Error] (" + fname + "): ";
}
std::cerr << prefix;
std::cerr << (split_newlines ? subtituteStr(msg, "\n", "\n" + prefix) : msg)
<< '\n';
std::cerr << Line_Str << '\n';
}

template <typename F,
typename std::enable_if<std::is_convertible_v<F, stdfs::path>>::type* =
nullptr>
Expand Down

0 comments on commit 9ae8aeb

Please sign in to comment.