From 4540b72b0716f4dadf37342ca19325240ff8a6bb Mon Sep 17 00:00:00 2001 From: SciPhy Date: Fri, 26 Apr 2024 00:14:59 +0200 Subject: [PATCH] feat: add new scripts runs_loop, calc_cost and draw_intersection --- DiscreteENN_TSP_table.csv | 2 +- DiscreteENN_TSP_table.txt | 2 +- calc_cost.py | 15 ++++++++++ draw_intersection.py | 47 +++++++++++++++++++++++++++++++ draw_lines.py | 37 +++++++------------------ runs_loop.py | 58 +++++++++++++++++++++++++++++++++++++++ tsp_discrete_enn.hpp | 30 ++++++++++---------- until_fail_run.py | 2 +- 8 files changed, 148 insertions(+), 45 deletions(-) create mode 100644 calc_cost.py create mode 100644 draw_intersection.py create mode 100644 runs_loop.py diff --git a/DiscreteENN_TSP_table.csv b/DiscreteENN_TSP_table.csv index 6275d8b..bbb05a2 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,1.960000,376685.000000,1674.160034,3992.860107,3916.000000 +pcb442,442,8.390000,89656.000000,202.839996,55039.710938,50778.000000 diff --git a/DiscreteENN_TSP_table.txt b/DiscreteENN_TSP_table.txt index ed5b580..40994fc 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 1.960000 376685.000000 1674.160034 3992.860107 3916.000000 +pcb442 442 8.390000 89656.000000 202.839996 55039.710938 50778.000000 diff --git a/calc_cost.py b/calc_cost.py new file mode 100644 index 0000000..ec85103 --- /dev/null +++ b/calc_cost.py @@ -0,0 +1,15 @@ +import math + +def getDistance(a, b): + x_sqr = (a[0] - b[0]) * (a[0] - b[0]) + y_sqr = (a[1] - b[1]) * (a[1] - b[1]) + return math.sqrt(x_sqr + y_sqr) + +def getCost(c, a, b): + return getDistance(c, a) + getDistance(c, b) - getDistance(a, b) + +if __name__ == "__main__": + point = (800.000000, 600.000000) + pointA = (900.000000, 600.000000) + pointB = (850.000000, 520.000000) + print(f"cost for ({pointA}, {point}, {pointB}):\n{getCost(point, pointA, pointB)}") diff --git a/draw_intersection.py b/draw_intersection.py new file mode 100644 index 0000000..3df7544 --- /dev/null +++ b/draw_intersection.py @@ -0,0 +1,47 @@ +import matplotlib.pyplot as plt + +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), +] + + +plt.figure(figsize=(8, 6)) + +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 + +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') +plt.xlabel('X coordinate') +plt.ylabel('Y coordinate') +plt.grid(True) # Optionally add a grid + +# Show the plot +plt.show() + diff --git a/draw_lines.py b/draw_lines.py index 3df7544..424c892 100644 --- a/draw_lines.py +++ b/draw_lines.py @@ -1,13 +1,5 @@ import matplotlib.pyplot as plt -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), @@ -16,25 +8,16 @@ plt.figure(figsize=(8, 6)) -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 - -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 +points_num = len(points) +for idx in range(points_num): + point1 = points[idx] + if idx == points_num - 1: + point2 = points[0] + else: + point2 = points[idx + 1] + x_values = [point1[0], point2[0]] + y_values = [point1[1], point2[1]] + plt.plot(x_values, y_values, marker='o', color="red", linestyle='--', markersize=5) # 'o' for circle markers # Add titles and labels plt.title('Line Segments') diff --git a/runs_loop.py b/runs_loop.py new file mode 100644 index 0000000..7bd36bd --- /dev/null +++ b/runs_loop.py @@ -0,0 +1,58 @@ +#!/usr/bin/python3 + +import os +import sys +import time +import subprocess + +COMMAND = "" + +def runCommand(cmd, args): + process = subprocess.Popen( + [cmd] + args, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT + ) + + # stdout, stderr = process.communicate() + + # # exit_code = p.wait() + # exit_code = process.returncode + + # # print("Standard Output:\n", stdout.decode()) + # # print("Error Output:\n", stderr.decode()) + # # print("Exit code: ", exit_code) + # time.sleep(1) + # os.system('clear') + + for line in process.stdout: + if line: + print(str(line.strip(), 'utf-8')) + time.sleep(1) + os.system('clear') + + exit_code = 0 + return exit_code + +def main(): + + 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() + while True: + if runCommand(command, command_args) != 0: + break; + +if __name__ == "__main__": + main() + diff --git a/tsp_discrete_enn.hpp b/tsp_discrete_enn.hpp index 4d57cf3..194a5f6 100644 --- a/tsp_discrete_enn.hpp +++ b/tsp_discrete_enn.hpp @@ -1074,11 +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; - } + // 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()) { @@ -1092,22 +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; - } + // 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 (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 index e1bd0dd..60f3ec9 100644 --- a/until_fail_run.py +++ b/until_fail_run.py @@ -24,7 +24,7 @@ def runUntilFailure(cmd, args): if exit_code != 0: print(f"command: {cmd} failed.") - print("Exit code: ", process.returncode) + print("Exit code: ", exit_code) print("Standard Output:\n", stdout.decode()) print("Error Output:\n", stderr.decode()) break