-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgreedy_tsp.py
54 lines (45 loc) · 1.63 KB
/
greedy_tsp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import matplotlib.pyplot as plt
from util import City, read_cities, write_cities_and_return_them, generate_cities, path_cost
class Greedy:
def __init__(self, cities):
self.unvisited = cities[1:]
self.route = [cities[0]]
def run(self, plot):
if plot:
plt.ion()
plt.show(block=False)
self.init_plot()
while len(self.unvisited):
index, nearest_city = min(enumerate(self.unvisited),
key=lambda item: item[1].distance(self.route[-1]))
self.route.append(nearest_city)
del self.unvisited[index]
self.plot_interactive(False)
self.route.append(self.route[0])
self.plot_interactive(False)
self.route.pop()
return path_cost(self.route)
def plot_interactive(self, block):
x1, y1, x2, y2 = self.route[-2].x, self.route[-2].y, self.route[-1].x, self.route[-1].y
plt.plot([x1, x2], [y1, y2], 'ro')
plt.plot([x1, x2], [y1, y2], 'g')
plt.draw()
plt.pause(0.07)
plt.show(block=block)
def init_plot(self):
fig = plt.figure(0)
fig.suptitle('Greedy TSP')
x_list, y_list = [], []
for city in [*self.route, *self.unvisited]:
x_list.append(city.x)
y_list.append(city.y)
x_list.append(self.route[0].x)
y_list.append(self.route[0].y)
plt.plot(x_list, y_list, 'ro')
plt.show(block=False)
if __name__ == "__main__":
cities = read_cities(64)
greedy = Greedy(cities)
print(greedy.run(plot=True))
print(greedy.route)
plt.show(block=True)