]> Skullheadx's Git Forge - The-Traveling-Salesman-Problem.git/commitdiff
change name of calculate dist func
authorSkullheadx <704277@pdsb.net>
Wed, 28 Dec 2022 00:20:29 +0000 (19:20 -0500)
committerSkullheadx <704277@pdsb.net>
Wed, 28 Dec 2022 00:20:29 +0000 (19:20 -0500)
brute_force.py
graph.py

index 6800e3c4a8e2215b5ec8c6d0cea3f013f3bed46e..97244b96f31bbf0e79422c1db4471d967c306ffd 100644 (file)
@@ -1,5 +1,5 @@
 from queue import Queue
-from graph import calculate_distance
+from graph import calculate_route
 
 
 def brute_force(graph: list) -> list:
@@ -23,7 +23,7 @@ def brute_force(graph: list) -> list:
     shortest_distance = None
     shortest_route = []
     for route in routes:
-        distance = calculate_distance(route)
+        distance = calculate_route(route)
         if shortest_distance is None or distance < shortest_distance:
             shortest_distance = distance
             shortest_route = route
index 06a47054caaadce8b00a01bf724f16e132bc092b..d30f652ac5e702271964039158bb8aca81e16436 100644 (file)
--- a/graph.py
+++ b/graph.py
@@ -26,27 +26,25 @@ def create(path: str, width: int, height: int, nodes: int):
     return graph, filename
 
 
-def distance(x1: int, x2: int, y1: int, y2: int) -> float:
-    return pow(pow(x1 - x2, 2) + pow(y1 - y2, 2), 0.5)
+def distance(town1: tuple, town2: tuple) -> float:
+    return pow(pow(town1[0] - town2[0], 2) + pow(town1[1] - town2[1], 2), 0.5)
 
 
 def get_distances(graph: list) -> dict:
     distances = dict()
-    for i in graph:
-        distances[i] = dict()
-        x1, y1 = i
-        for j in graph:
-            x2, y2 = j
-            distances[i][j] = distance(x1, x2, y1, y2)
+    for town1 in graph:
+        distances[town1] = dict()
+        for town2 in graph:
+            distances[town1][town2] = distance(town1, town2)
     return distances
 
 
-def calculate_distance(route: list) -> float:
-    x1, y1 = route[0]
-    x2, y2 = route[-1]
-    d = distance(x1, x2, y1, y2)
+def calculate_route(route: list) -> float:
+    town1 = route[0]
+    town2 = route[-1]
+    d = distance(town1, town2)
     for i, node in enumerate(route[:-1]):
-        x2, y2 = route[i + 1]
-        d += distance(x1, x2, y1, y2)
-        x1, y1 = x2, y2
+        town2 = route[i + 1]
+        d += distance(town1, town2)
+        town1 = town2
     return d