From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 9 Jan 2023 04:17:17 +0000 (-0500) Subject: random swapping X-Git-Url: http://git.skullheadx.com/sitemap.xml?a=commitdiff_plain;h=1efb405c495f675f58623f4296d54c0b35ca2372;p=The-Traveling-Salesman-Problem.git random swapping --- diff --git a/display.py b/display.py index b1f88b9..795b785 100644 --- a/display.py +++ b/display.py @@ -26,7 +26,7 @@ class Node: def __init__(self, position: tuple, number: int) -> None: self.position = pygame.Vector2(position) # self.text = self.font.render(str(number), True, self.text_colour) - self.coord_text = self.font.render(str((self.position)), True, (0, 0, 0)) + # self.coord_text = self.font.render(str((self.position)), True, (0, 0, 0)) def draw(self, surf: pygame.Surface) -> None: @@ -35,7 +35,7 @@ class Node: # surf.blit(self.text, self.text.get_rect(center=self.position)) - surf.blit(self.coord_text, self.coord_text.get_rect(center=self.position)) + # surf.blit(self.coord_text, self.coord_text.get_rect(center=self.position)) class Salesman: @@ -81,7 +81,7 @@ class Display: pygame.display.set_caption("Traveling Salesman Problem") font = pygame.font.SysFont("arial", 20) - def __init__(self, path: str, route: list, mst=None, one_tree=None, removed_vertex=None, mode="direct") -> None: + def __init__(self, path: str, route: list,improved_route=None, mst=None, one_tree=None, removed_vertex=None, mode="direct") -> None: with open(path, "r") as f: contents = f.read().split("\n") if contents[-1] == "": @@ -95,6 +95,8 @@ class Display: if mode == "points": self.route = linker(self.route) + self.route2 = improved_route + self.salesman = Salesman(self.route) self.mst = mst @@ -130,7 +132,9 @@ class Display: # self.screen.blit(text, text.get_rect(center=((start[0]+end[0])/2,(end[1]+start[1])/2))) if len(self.route) > 1: - pygame.draw.lines(self.screen, BLUE, True, self.route, 3) # Route + pygame.draw.lines(self.screen, BLUE, True, self.route, 12) # Route + if len(self.route) > 1: + pygame.draw.lines(self.screen, RED, True, self.route2, 3) # Route for i, node in enumerate(self.nodes): node.draw(self.screen) diff --git a/main.py b/main.py index ebe3868..b62478e 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,9 @@ -from graph import create, read, print_info, find_MST, find_lower_bound +from graph import create, read, print_info, find_MST, find_lower_bound, find_one_tree 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 time import perf_counter import os @@ -20,34 +23,41 @@ def main(): print("The file does not exist") if CREATE_NEW_GRAPHS: - graph, filename = create(GRAPH_PATH, 640, 640, 15) + graph, filename = create(GRAPH_PATH, 640, 640, 100) 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 = 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 = christofides(graph) route_time_end = perf_counter() - MST_distance, MST = find_MST(graph) - print("MST_DISTANCE:", MST_distance) + improvement_time_start = perf_counter() + + r2 = random_swapping(route,5000) + + improvement_time_end = perf_counter() + + + # 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) removed_vertex = graph[0] lower_bound, one_tree, removed_vertex = find_lower_bound(graph) one_tree_time_end = perf_counter() - print_info(route, route_time_end - route_time_start, "Christofides Algorithm", lower_bound, + print_info(r2, route_time_end - route_time_start, "NN Heuristic + Random swapping", lower_bound, one_tree_time_end - one_tree_time_start, r=3000, mode="direct") - display = Display(os.path.join(GRAPH_PATH, filename), route, mst=MST, one_tree=None, + display = Display(os.path.join(GRAPH_PATH, filename), route,improved_route=r2, mst=None, one_tree=None, removed_vertex=removed_vertex, mode="direct") display.show() - + # print("Done") if __name__ == "__main__": main() diff --git a/tour_improvements/random_swapping.py b/tour_improvements/random_swapping.py new file mode 100644 index 0000000..18a2313 --- /dev/null +++ b/tour_improvements/random_swapping.py @@ -0,0 +1,28 @@ +from graph import calculate_route +from random import sample + + +def random_swap(route: list) -> list: + r = route[:] + + a, b = sample(range(0, len(r) - 1), 2) + + temp = r[b] + r[b] = r[a] + r[a] = temp + + return r + + +def random_swapping(route: list, n: int) -> list: + d = calculate_route(route) + r = route[:] + + for _ in range(n): + + random_swap_route = random_swap(r) + + if calculate_route(random_swap_route) < d: + r = random_swap_route + + return r