]> Skullheadx's Git Forge - The-Traveling-Salesman-Problem.git/commitdiff
two opt improvement attempt 1
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Tue, 10 Jan 2023 13:49:16 +0000 (08:49 -0500)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Tue, 10 Jan 2023 13:49:16 +0000 (08:49 -0500)
main.py
tour_improvements/two_opt.py [new file with mode: 0644]

diff --git a/main.py b/main.py
index b62478e20c63f1e01b10aec283167a7b8acdf4f0..19cf63a85f395c001c2a0bf3a4326f400f8ed9bc 100644 (file)
--- a/main.py
+++ b/main.py
@@ -1,14 +1,15 @@
-from graph import create, read, print_info, find_MST, find_lower_bound, find_one_tree
+from graph import create, read, print_info, find_MST, find_lower_bound, find_one_tree, linker, calculate_route
 from display import Display
 from heuristics.Christofides import christofides
 from heuristics.nearest_neighbor import nearest_neighbor
 from heuristics.greedy import greedy
 from tour_improvements.random_swapping import random_swapping
+from tour_improvements.two_opt import two_opt
 from time import perf_counter
 import os
 
 GRAPH_PATH = "graphs/"
-CREATE_NEW_GRAPHS = False
+CREATE_NEW_GRAPHS = True
 
 
 def main():
@@ -23,22 +24,26 @@ def main():
                     print("The file does not exist")
 
     if CREATE_NEW_GRAPHS:
-        graph, filename = create(GRAPH_PATH, 640, 640, 100)
+        graph, filename = create(GRAPH_PATH, 640, 640, 25)
     else:
         filename = "graph1.txt"
         graph = read(GRAPH_PATH, filename)
 
     route_time_start = perf_counter()
     # route = brute_force(graph)  # 10 nodes in 85.042 seconds. Optimal = 2,262.29
-    route = nearest_neighbor(graph)  # 100 nodes in 0.5762094999663532 seconds. Distance = 6,270.568142156188
-    route = greedy(graph)  # 100 nodes in 0.1383088999427855 seconds. Distance = 5,523.211501332208 OTLB: 4,
+    route = nearest_neighbor(graph)  # 100 nodes in 0.5762094999663532 seconds. Distance = 6,270.568142156188
+    route = greedy(graph)  # 100 nodes in 0.1383088999427855 seconds. Distance = 5,523.211501332208 OTLB: 4,
     # 344.881943246125 Approx. 27.119944188995277%
     # route = christofides(graph)
     route_time_end = perf_counter()
 
+    route = linker(route)
+    print("Old Route Cost:", calculate_route(route))
     improvement_time_start = perf_counter()
 
-    r2 = random_swapping(route,5000)
+    # r2 = random_swapping(route,1000)
+    r2 = two_opt(route)
+
 
     improvement_time_end = perf_counter()
 
@@ -46,9 +51,9 @@ def main():
     # MST_distance, MST = find_MST(graph)
     # print("MST_DISTANCE:", MST_distance)
     one_tree_time_start = perf_counter()
-    lower_bound, one_tree = find_one_tree(graph)
+    lower_bound, one_tree = find_one_tree(graph)
     removed_vertex = graph[0]
-    lower_bound, one_tree, removed_vertex = find_lower_bound(graph)
+    lower_bound, one_tree, removed_vertex = find_lower_bound(graph)
     one_tree_time_end = perf_counter()
 
     print_info(r2, route_time_end - route_time_start, "NN Heuristic + Random swapping", lower_bound,
diff --git a/tour_improvements/two_opt.py b/tour_improvements/two_opt.py
new file mode 100644 (file)
index 0000000..358fd53
--- /dev/null
@@ -0,0 +1,23 @@
+from graph import calculate_route, delinker, linker
+from queue import Queue
+from itertools import combinations
+
+
+def two_opt(route:list) ->list:
+    d = calculate_route(route)
+    r = delinker(route)
+    c = combinations(r,2)
+    for edge1,edge2 in c:
+        s1,e1 = edge1
+        s2, e2 = edge2
+
+        print(edge1,edge2)
+        temp = r[:]
+        temp.remove(edge1)
+        temp.remove(edge2)
+        temp.append((s1, e2))
+        temp.append((s2, e1))
+        new_d = calculate_route(temp, mode="points")
+        if new_d < d:
+            r = temp[:]
+    return linker(r)
\ No newline at end of file