From 91cd4e91305f4dce07a5d604d57d09b593450d0d Mon Sep 17 00:00:00 2001 From: SciPhy Date: Thu, 25 Apr 2024 05:24:57 +0200 Subject: [PATCH] feat: add unit_fail_run.py script and start debug intersection --- Data/ALL_tsp/test5.tsp | 9 +++-- DiscreteENN_TSP_table.csv | 2 +- DiscreteENN_TSP_table.txt | 2 +- draw_lines.py | 42 ++++++++++++++++------- tsp_discrete_enn.hpp | 35 ++++++++++++++----- until_fail_run.py | 72 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 28 deletions(-) create mode 100644 until_fail_run.py diff --git a/Data/ALL_tsp/test5.tsp b/Data/ALL_tsp/test5.tsp index 27ddc13..477bbf2 100644 --- a/Data/ALL_tsp/test5.tsp +++ b/Data/ALL_tsp/test5.tsp @@ -3,10 +3,9 @@ TYPE: TSP DIMENSION: 5 EDGE_WEIGHT_TYPE: EUC_2D NODE_COORD_SECTION -1 24.0 30.0 -2 26.0 30.0 -3 23.0 37.0 -4 27.0 28.0 -5 25.0 35.0 +1 418.920013 200.149994 +2 176.919998 328.149994 +3 582.419983 321.149994 +4 155.419998 150.649994 EOF diff --git a/DiscreteENN_TSP_table.csv b/DiscreteENN_TSP_table.csv index 1fbd111..6275d8b 100644 --- a/DiscreteENN_TSP_table.csv +++ b/DiscreteENN_TSP_table.csv @@ -1,2 +1,2 @@ name points error time(us) time per city(us) distance optimal_distance -tsp225,225,7.540000,64888.000000,288.390015,4211.290039,3916.000000 +tsp225,225,1.960000,376685.000000,1674.160034,3992.860107,3916.000000 diff --git a/DiscreteENN_TSP_table.txt b/DiscreteENN_TSP_table.txt index d0aff4c..ed5b580 100644 --- a/DiscreteENN_TSP_table.txt +++ b/DiscreteENN_TSP_table.txt @@ -1,2 +1,2 @@ name points error time(us) time per city(us) distance optimal_distance -tsp225 225 7.540000 64888.000000 288.390015 4211.290039 3916.000000 +tsp225 225 1.960000 376685.000000 1674.160034 3992.860107 3916.000000 diff --git a/draw_lines.py b/draw_lines.py index 98289a9..3df7544 100644 --- a/draw_lines.py +++ b/draw_lines.py @@ -1,22 +1,40 @@ import matplotlib.pyplot as plt -# List of pairs of coordinates, where each pair is a line segment -# Each element is ((x1, y1), (x2, y2)) -line_segments = [ - ((6850.000000, 11700.000000), (6665.000000, 11710.000000)), - ((6665.000000, 11860.000000), (6775.000000, 11650.000000)), +points = [ + (254.919998, 271.649994), (418.920013, 200.149994), + (582.419983, 321.149994), (155.419998, 150.649994), +] +points_ref2 = [ + (418.920013, 200.149994), (176.919998, 328.149994), + (582.419983, 321.149994), (155.419998, 150.649994), +] +points = [ + (418.920013, 200.149994), (176.919998, 328.149994), + (582.419983, 321.149994), (155.419998, 150.649994), ] -# Create a plot + plt.figure(figsize=(8, 6)) -# Plot each line segment -for segment in line_segments: - start_point, end_point = segment - x_values = [start_point[0], end_point[0]] - y_values = [start_point[1], end_point[1]] +point1, point, point2, point3 = points + +x_values = [point1[0], point[0]] +y_values = [point1[1], point[1]] +plt.plot(x_values, y_values, marker='o', color="red", linestyle='--', markersize=5) # 'o' for circle markers + +x_values = [point[0], point2[0]] +y_values = [point[1], point2[1]] +plt.plot(x_values, y_values, marker='o', color="red", linestyle='-', markersize=5) # 'o' for circle markers + +x_values = [point2[0], point3[0]] +y_values = [point2[1], point3[1]] +plt.plot(x_values, y_values, marker='o', color="violet", linestyle='-', markersize=5) # 'o' for circle markers + +plt.plot(point[0], point[1], marker='x', color="green", markersize=10) # 'o' for circle markers - plt.plot(x_values, y_values, marker='o', linestyle='-', markersize=5) # 'o' for circle markers +x_values = [point1[0], point2[0]] +y_values = [point1[1], point2[1]] +plt.plot(x_values, y_values, marker='o', color="blue", linestyle='--', markersize=5) # 'o' for circle markers # Add titles and labels plt.title('Line Segments') diff --git a/tsp_discrete_enn.hpp b/tsp_discrete_enn.hpp index f97434f..4d57cf3 100644 --- a/tsp_discrete_enn.hpp +++ b/tsp_discrete_enn.hpp @@ -84,6 +84,8 @@ typedef std::map CityLayers_t; typedef std::optional NodeOpt_t; template using NodeExp_t = utils::Expected; +void drawPath(const Path_t& path, const Cities_t& stack, bool show_coords); + template <> inline bool utils::MatchItem::operator()(const City& city) { return (city.id == m_item.id); @@ -958,13 +960,13 @@ class DiscreteENN_TSP std::to_string(intersect0) + ", " + std::to_string(intersect_next) + ")", "checkIntersectPath"); - utils::printErr("node 0"); + utils::printInfo("node 0"); m_path[0]->print(); - utils::printErr("node last"); + utils::printInfo("node last"); (m_path.back())->print(); - utils::printErr("node " + std::to_string(intersect0)); + utils::printInfo("node " + std::to_string(intersect0)); m_path[static_cast(intersect0)]->print(); - utils::printErr("node " + std::to_string(intersect_next)); + utils::printInfo("node " + std::to_string(intersect_next)); m_path[intersect_next]->print(); return true; } @@ -972,19 +974,19 @@ class DiscreteENN_TSP int intersect{ checkIntersectEdge(idx, idx + 1) }; if (intersect != -1) { const std::size_t intersect_next{ properIndex(intersect + 1) }; - utils::printErr("Found an intersection for edge (" + + utils::printInfo("Found an intersection for edge (" + std::to_string(idx) + ", " + std::to_string(idx + 1) + ") and edge (" + std::to_string(intersect) + ", " + std::to_string(intersect_next) + ")", "checkIntersectPath"); - utils::printErr("node " + std::to_string(idx)); + utils::printInfo("node " + std::to_string(idx)); m_path[idx]->print(); - utils::printErr("node " + std::to_string(idx + 1)); + utils::printInfo("node " + std::to_string(idx + 1)); m_path[idx + 1]->print(); - utils::printErr("node " + std::to_string(intersect)); + utils::printInfo("node " + std::to_string(intersect)); m_path[static_cast(intersect)]->print(); - utils::printErr("node " + std::to_string(intersect_next)); + utils::printInfo("node " + std::to_string(intersect_next)); m_path[intersect_next]->print(); return true; } @@ -1072,6 +1074,11 @@ class DiscreteENN_TSP "run"); return false; } + if (checkIntersectPath()) { + utils::printErr("intersection after adding node at " + std::to_string(idx_added) + " current path size " + std::to_string(m_path.size()), "run"); + drawPath(m_path, m_stack, false); + // return false; + } const utils::Expected nodes = getNeigbhours(it); if (nodes.err()) { @@ -1085,12 +1092,22 @@ class DiscreteENN_TSP const auto [node_prev, node_next] = nodes.value(); const auto it_erased1 = removeIntersection(node_prev, it, node_next); + if (checkIntersectPath()) { + utils::printErr("intersection after removeIntersection from adding node at " + std::to_string(idx_added) + " current path size " + std::to_string(m_path.size()), "run"); + drawPath(m_path, m_stack, false); + // return false; + } const auto it_erased2 = validatePath(); if (it_erased2.err()) { utils::printErr("validatePath failed", "run"); return false; } + if (checkIntersectPath()) { + utils::printErr("intersection after validatePath", "run"); + drawPath(m_path, m_stack, false); + return false; + } if (m_fromScratch) { const int idx_rand{ distrib(gen) }; it = it_begin + idx_rand; diff --git a/until_fail_run.py b/until_fail_run.py new file mode 100644 index 0000000..e1bd0dd --- /dev/null +++ b/until_fail_run.py @@ -0,0 +1,72 @@ +#!/usr/bin/python3 + +import os +import sys +import time +import argparse +import subprocess + +COMMAND = "" + +def runUntilFailure(cmd, args): + count = 0 + while True: + process = subprocess.Popen( + [cmd] + args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + + stdout, stderr = process.communicate() + + # exit_code = p.wait() + exit_code = process.returncode + + if exit_code != 0: + print(f"command: {cmd} failed.") + print("Exit code: ", process.returncode) + print("Standard Output:\n", stdout.decode()) + print("Error Output:\n", stderr.decode()) + break + else: + count += 1 + # print(f"Run {count} successful") + print(f"\rRun: {count} successful", end='', flush=True) + time.sleep(1) + + print(f"Completed runs: {count}") + +def main(): + # parser = argparse.ArgumentParser(description="Run a command repeatedly until it fails.") + + # parser.add_argument("command_str", + # type=str, + # help="The command that should be run repeatedly.") + # parser.add_argument("command_args", nargs='*', help="Additional arguments to pass to the command.") + + # args = parser.parse_args() + + # command = args.command_str + # command_args = args.command_args + + # if not command: + # print(f"[Error]: Not command argument provided.\nExiting") + # exit(1) + + if len(sys.argv) < 2: + print(f"[Error]: Not command argument provided.\nExiting") + sys.exit(1) + + command = sys.argv[1] + + command_args = sys.argv[2:] + + if command[0:2] == "./": + command = os.path.abspath(command) + + print() + print() + runUntilFailure(command, command_args) + +if __name__ == "__main__": + main()