Skip to content

Commit

Permalink
fix: floating point errors in updateCost, findBestInsertion and valid…
Browse files Browse the repository at this point in the history
…ateNode
  • Loading branch information
Akhil-CM committed Apr 26, 2024
1 parent 5693bf6 commit 616a286
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 96 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
pcb442,442,8.330000,101219.000000,229.000000,55006.988281,50778.000000
tsp225,225,4.780000,16217.000000,72.080002,4103.149902,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
pcb442 442 8.330000 101219.000000 229.000000 55006.988281 50778.000000
tsp225 225 4.780000 16217.000000 72.080002 4103.149902 3916.000000
47 changes: 47 additions & 0 deletions draw_intersection.py
Original file line number Diff line number Diff line change
@@ -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()

56 changes: 25 additions & 31 deletions draw_lines.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
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),
]

def plotPoints(points, color):
points_num = len(points)
for idx in range(points_num - 1):
point1 = points[idx]
if idx == points_num - 1:
point2 = points[0]
else:
point2 = points[idx + 1]
if not point1 or not point2:
continue
x_values = [point1[0], point2[0]]
y_values = [point1[1], point2[1]]
plt.plot(x_values, y_values, marker='o', color=color, linestyle='--', markersize=5) # 'o' for circle markers

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
points = [
(850.000000, 700.000000), (900.000000, 600.000000),
(800.000000, 600.000000), (850.000000, 520.000000),
(750.000000, 490.000000)
]
plotPoints(points, "red")

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 = [
(850.000000, 700.000000), (800.000000, 600.000000),
(900.000000, 600.000000), (850.000000, 520.000000),
(750.000000, 490.000000)
]
plotPoints(points, "blue")

# Add titles and labels
plt.title('Line Segments')
Expand Down
23 changes: 13 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,12 @@ int runPipelineSingle(TSPInfo& info, const stdfs::path& data_path,
utils::printInfo("Running algorithm for " + data_path.string(),
"runPipelineSingle");

const std::string& filename{ data_path.stem().string() };
// -------------------------------------------
// Create and setup Discrete ENN Solver
// -------------------------------------------
DiscreteENN_TSP enn_tsp;
enn_tsp.name() = filename;
Cities_t& cities{ enn_tsp.cities() };
Indices_t& path = enn_tsp.path();

Expand Down Expand Up @@ -323,11 +325,13 @@ int runPipelineSingle(TSPInfo& info, const stdfs::path& data_path,
#if (TSP_DEBUG_PRINT > 0)
std::cout << ("\n[Debug] (main): validatePath updateCostAll\n");
#endif
const auto [idx_fail, err] = enn_tsp.updateCostAll();
if (err) {
std::cerr << "[Error] (main): updateCostAll failed at index "
<< idx_fail << '\n';
return 1;
if (erased.has_value() and path.size() > 2) {
const auto [idx_fail, err] = enn_tsp.updateCostAll();
if (err) {
std::cerr << "[Error] (main): updateCostAll failed at index "
<< idx_fail << '\n';
return 1;
}
}
}

Expand Down Expand Up @@ -364,21 +368,21 @@ int runPipelineSingle(TSPInfo& info, const stdfs::path& data_path,
if (erased.err()) {
std::cerr << "[Error] (main): final validatePath failed\n";
if (draw_failed) {
drawPath(path, cities, show_coords);
drawPath(path, cities, show_coords, filename);
}
return 1;
}
if (erased.has_value()) {
std::cerr << "[Error] (main): final validatePath removed node(s)\n";
if (draw_failed) {
drawPath(path, cities, show_coords);
drawPath(path, cities, show_coords, filename);
}
return 1;
}
if (enn_tsp.checkIntersectPath()) {
std::cerr << "[Error] (main): final checkIntersectPath failed\n";
if (draw_failed) {
drawPath(path, cities, show_coords);
drawPath(path, cities, show_coords, filename);
}
return 1;
}
Expand All @@ -393,7 +397,6 @@ int runPipelineSingle(TSPInfo& info, const stdfs::path& data_path,
const auto idx_next{ enn_tsp.properIndex(idx + 1) };
dist += getDistance(cities[path[idx]], cities[path[idx_next]]);
}
const std::string& filename{ data_path.stem().string() };
info.m_name = filename;
info.m_distance = dist;
info.m_points = num_cities;
Expand All @@ -407,7 +410,7 @@ int runPipelineSingle(TSPInfo& info, const stdfs::path& data_path,
std::cout << utils::Line_Str + "\n";

if (draw) {
drawPath(path, cities, show_coords);
drawPath(path, cities, show_coords, filename);
}
utils::printInfo("Finished algorithm for " + data_path.string(),
"runPipelineSingle");
Expand Down
Loading

0 comments on commit 616a286

Please sign in to comment.