From 23a922af516b7149e0b79ecd5b35226813d957c1 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 19 Feb 2025 21:57:12 +0000 Subject: [PATCH] [CT421]: Assignment 1 map.py --- .../CT421/assignments/assignment1/code/map.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 year4/semester2/CT421/assignments/assignment1/code/map.py diff --git a/year4/semester2/CT421/assignments/assignment1/code/map.py b/year4/semester2/CT421/assignments/assignment1/code/map.py new file mode 100755 index 00000000..031131f8 --- /dev/null +++ b/year4/semester2/CT421/assignments/assignment1/code/map.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 + +import matplotlib.pyplot as plt +from salesman import graph_from_file + +def plot_tour(graph, tour): + cities = {city["name"]: (city["x"], city["y"]) for city in graph["cities"]} + + plt.figure(figsize=(8, 6)) + + for city, (x, y) in cities.items(): + plt.scatter(x, y, color='blue') + plt.text(x, y, str(city), fontsize=12, verticalalignment='bottom', horizontalalignment='right') + + for i in range(len(tour)): + city1 = tour[i] + city2 = tour[(i + 1) % len(tour)] # Connects last city to first + x_values = [cities[city1][0], cities[city2][0]] + y_values = [cities[city1][1], cities[city2][1]] + plt.plot(x_values, y_values, 'r-') + + plt.xlabel("X Coordinate") + plt.ylabel("Y Coordinate") + plt.title("Tour Path") + plt.grid() + plt.show() + +# Example usage +graph = graph_from_file("../materials/berlin52.tsp") +tour = [16, 50, 44, 34, 46, 48, 24, 38, 40, 37, 5, 15, 4, 6, 25, 51, 12, 28, 27, 26, 14, 13, 52, 11, 33, 43, 45, 19, 10, 9, 8, 41, 17, 31, 3, 18, 32, 36, 39, 35, 49, 1, 22, 23, 20, 30, 42, 2, 7, 21, 29, 47] +plot_tour(graph, tour)