Skip to content

Commit

Permalink
fix: hold debugging final city loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Akhil-CM committed Apr 25, 2024
1 parent bdec96a commit 5693bf6
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 33 deletions.
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
pr2392,2392,8.560000,15416018.000000,6444.819824,410374.000000,378032.000000
pcb442,442,8.330000,101219.000000,229.000000,55006.988281,50778.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
pr2392 2392 8.560000 15416018.000000 6444.819824 410374.000000 378032.000000
pcb442 442 8.330000 101219.000000 229.000000 55006.988281 50778.000000
15 changes: 15 additions & 0 deletions calc_cost.py
Original file line number Diff line number Diff line change
@@ -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)}")
58 changes: 58 additions & 0 deletions runs_loop.py
Original file line number Diff line number Diff line change
@@ -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()

74 changes: 44 additions & 30 deletions tsp_discrete_enn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1006,9 +1006,9 @@ class DiscreteENN_TSP
"run");
return false;
}
pos = stackBack();
continue;
}
// TimePoint_t start_time = std::chrono::steady_clock::now();
const int idx_added = findBestInsertion(pos);
if (idx_added == -1) {
utils::printErr("findBestInsertion failed at index " +
Expand All @@ -1028,15 +1028,22 @@ class DiscreteENN_TSP
"run");
return false;
}
// if (print_pos) {
// utils::printInfo("Adding node for pos " + std::to_string(pos) +
// " at " + std::to_string(idx_added) +
// " path size " +
// std::to_string(m_path.size()));
// }
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[idx_prev]].print();
m_cities[m_path[idx_added]].print();
m_cities[m_path[idx_next]].print();
}
// if (checkIntersectPath()) {
// utils::printErr("intersection after adding node at " +
// std::to_string(idx_added) +
// " for city " + std::to_string(m_path[idx_added]) +
// " current path size " +
// std::to_string(m_path.size()),
// "run");
Expand All @@ -1056,36 +1063,42 @@ class DiscreteENN_TSP
// drawPath(m_path, m_cities, false);
// // return false;
// }
// if (print_pos and it_erased1.has_value()) {
// utils::printInfo("Adding node for pos " + std::to_string(pos) +
// " at " + std::to_string(idx_added) +
// " and removing intersection node " +
// std::to_string(*it_erased1) + " path size " +
// std::to_string(m_path.size()));
// }
if (print_pos and it_erased1.has_value()) {
utils::printInfo("After adding node for city " + std::to_string(pos) +
" at " + std::to_string(idx_added) +
" and removing intersection city " +
std::to_string(*it_erased1) + " path size " +
std::to_string(m_path.size()));
m_cities[*it_erased1].print();
}

const auto it_erased2 = validatePath();
if (it_erased2.err()) {
utils::printErr("validatePath failed", "run");
return false;
}
// if (print_pos and it_erased2.has_value()) {
// utils::printInfo("Adding node for pos " + std::to_string(pos) +
// " at " + std::to_string(idx_added) +
// " and removing validation node " +
// std::to_string(it_erased2.value()) +
// " path size " + std::to_string(m_path.size()));
// }
if (print_pos and it_erased2.has_value()) {
utils::printInfo("After adding node for pos " + std::to_string(pos) +
" at " + std::to_string(idx_added) +
" and removing validation city " +
std::to_string(it_erased2.value()) +
" path size " + std::to_string(m_path.size()));
m_cities[it_erased2.value()].print();
}
// if (checkIntersectPath()) {
// utils::printErr("intersection after validatePath", "run");
// utils::printErr(
// "intersection after validatePath from adding node at " +
// std::to_string(idx_added) + " current path size " +
// std::to_string(m_path.size()),
// "run");
// drawPath(m_path, m_cities, false);
// return false;
// // return false;
// }
// if (m_fromScratch) {
// // const int idx_rand{ distrib(gen) };
// pos = stackAt(idx_rand);
// continue;
// }
if (m_fromScratch) {
// const int idx_rand{ distrib(gen) };
// pos = stackAt(idx_rand);
continue;
}

// if (it_erased1.has_value() or it_erased2.has_value()) {
// distrib.param(distrib_t::param_type(0, m_stack.size() - 1));
Expand Down Expand Up @@ -1119,7 +1132,6 @@ class DiscreteENN_TSP
std::to_string(m_path.size()) + "/" +
std::to_string(num_cities),
"run");
std::cout << std::endl;
// if (flip) {
// distrib.param(distrib_t::param_type(0, m_path.size() - 1));
// } else {
Expand All @@ -1129,8 +1141,10 @@ class DiscreteENN_TSP
// flip = not flip;
distrib.param(distrib_t::param_type(0, stack_size - 1));
const int idx_rand{ distrib(gen) };
// const int idx_rand = stack_size - 2;
// const int idx_rand = 0;
utils::printInfo(
"New starting point " + std::to_string(idx_rand), "run");
"New starting point " + std::to_string(idx_rand) + " with city " + std::to_string(m_stack[idx_rand]), "run");
pos = stackAt(idx_rand);
continue;
#if (TSP_DEBUG_PRINT > 0)
Expand Down
2 changes: 1 addition & 1 deletion until_fail_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5693bf6

Please sign in to comment.