-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathutil.py
56 lines (41 loc) · 1.36 KB
/
util.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
55
56
import math
import random
import matplotlib.pyplot as plt
class City:
def __init__(self, x, y):
self.x = x
self.y = y
def distance(self, city):
return math.hypot(self.x - city.x, self.y - city.y)
def __repr__(self):
return f"({self.x}, {self.y})"
def read_cities(size):
cities = []
with open(f'test_data/cities_{size}.data', 'r') as handle:
lines = handle.readlines()
for line in lines:
x, y = map(float, line.split())
cities.append(City(x, y))
return cities
def write_cities_and_return_them(size):
cities = generate_cities(size)
with open(f'test_data/cities_{size}.data', 'w+') as handle:
for city in cities:
handle.write(f'{city.x} {city.y}\n')
return cities
def generate_cities(size):
return [City(x=int(random.random() * 1000), y=int(random.random() * 1000)) for _ in range(size)]
def path_cost(route):
return sum([city.distance(route[index - 1]) for index, city in enumerate(route)])
def visualize_tsp(title, cities):
fig = plt.figure()
fig.suptitle(title)
x_list, y_list = [], []
for city in cities:
x_list.append(city.x)
y_list.append(city.y)
x_list.append(cities[0].x)
y_list.append(cities[0].y)
plt.plot(x_list, y_list, 'ro')
plt.plot(x_list, y_list, 'g')
plt.show(block=True)