Skip to content

Commit

Permalink
feat: add unit_fail_run.py script and start debug intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
Akhil-CM committed Apr 25, 2024
1 parent fc87eef commit 91cd4e9
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 28 deletions.
9 changes: 4 additions & 5 deletions Data/ALL_tsp/test5.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion DiscreteENN_TSP_table.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion DiscreteENN_TSP_table.txt
Original file line number Diff line number Diff line change
@@ -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
42 changes: 30 additions & 12 deletions draw_lines.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
35 changes: 26 additions & 9 deletions tsp_discrete_enn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ typedef std::map<int, Cities_t> CityLayers_t;
typedef std::optional<Node_t> NodeOpt_t;
template <typename T> using NodeExp_t = utils::Expected<Node_t, T>;

void drawPath(const Path_t& path, const Cities_t& stack, bool show_coords);

template <> inline bool utils::MatchItem<City>::operator()(const City& city)
{
return (city.id == m_item.id);
Expand Down Expand Up @@ -958,33 +960,33 @@ 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<std::size_t>(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;
}
for (std::size_t idx{ 0 }; idx != (num_nodes - 1); ++idx) {
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<std::size_t>(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;
}
Expand Down Expand Up @@ -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()) {
Expand All @@ -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;
Expand Down
72 changes: 72 additions & 0 deletions until_fail_run.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 91cd4e9

Please sign in to comment.