-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw2.py
208 lines (152 loc) · 5.13 KB
/
hw2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# coding: utf-8
# In[615]:
import pandas as pd
import numpy as np
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mp
import geopandas as gpd
from matplotlib.collections import PatchCollection
from shapely.geometry import Point, Polygon, MultiPolygon
from descartes import PolygonPatch
from adjustText import adjust_text
from matplotlib import animation, rc
from IPython.display import HTML
from math import radians, cos, sin, asin, sqrt
rc('animation', html='html5')
plt.rcParams['figure.figsize'] = (10, 5)
# In[174]:
data = pd.read_csv("cities.csv")
data['Население'] = pd.to_numeric(data['Население'], errors='coerce')
data.sort_values("Население", ascending=False, inplace=True)
data = data.iloc[:30]
data.iloc[0, 6] = "Москва"
data.iloc[1, 6] = "Санкт-Петербург"
data = data[["Город", "Широта", "Долгота", "Население"]]
# In[595]:
def permute(order):
temp_order = order.copy()
indexes = np.arange(len(temp_order))
np.random.shuffle(indexes)
a, b = indexes[:2]
temp = temp_order[a]
temp_order[a] = temp_order[b]
temp_order[b] = temp
return temp_order
def haversine(lon1, lat1, lon2, lat2):
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371
return c * r
def dist(order, xs, ys):
x, y = xs[order[0]], ys[order[0]]
cum_dist = 0
for i in order[1:]:
cum_dist += haversine(x, y, xs[i], ys[i])
x, y = xs[i], ys[i]
return cum_dist
def p(order, xs, ys, T):
distance = dist(order, xs, ys)
return np.exp(- distance / T)
def sa(xs, ys, T, alpha, iterations):
order = np.arange(0, len(xs))
np.random.shuffle(order)
distance = dist(order, xs, ys)
orders = [order]
distances = [distance]
temperatures = [T]
for i in range(iterations):
new_order = permute(order)
thresh = np.random.uniform()
new_distance = dist(new_order, xs, ys)
d = (new_distance - distance) / distance;
p = 1 if d < 0 else np.exp( - d / T)
if p >= thresh:
order = new_order
distance = new_distance
T = T * alpha
distances.append(distance)
orders.append(order)
temperatures.append(T)
return orders, distances, temperatures
# In[601]:
paths, dists, temps = sa(xs, ys, 10000, 0.95, 10000)
# In[607]:
# fig, axs = plt.subplots()
# all_patches = []
# for i, r in regions_df.iterrows():
# p = r['geometry']
# if isinstance(p, MultiPolygon):
# mp = p
# patches = []
# for p in mp:
# patches.append(PolygonPatch(p, fc='blue', lw=1, alpha=0.2))
# all_patches.extend(patches)
# else:
# patches = [PolygonPatch(p, fc='blue', lw=1, alpha=0.2)]
# all_patches.extend(patches)
# axs.add_collection(PatchCollection(all_patches, match_original=True))
# import pyproj as proj
# ys = np.array(data['Широта'])
# xs = np.array(data['Долгота'])
# ms = 20 * np.log10(data['Население'])
# names = data['Город']
# texts = [plt.text(x, y, name) for (x, y, name) in zip(xs, ys, names)]
# plt.scatter(xs, ys, s = 20, c="r", alpha = 0.5)
# plt.plot(xs[paths[-1]], ys[paths[-1]])
# plt.xlim(15, 185)
# plt.ylim(40, 90)
# adjust_text(texts)
# plt.show()
# In[620]:
# First set up the figure, the axis, and the plot element we want to animate
ax = plt.subplot(211)
axT = plt.subplot(223)
axD = plt.subplot(224)
fig = plt.gcf()
ax.set_xlim((15, 185))
ax.set_ylim((40, 90))
ax.set_title("Salesman path")
axT.set_xlim((0, len(paths) + 1))
axT.set_ylim((0, np.max(temps)*1.1))
axT.set_title("Annealing temperature")
axD.set_xlim((0, len(paths) + 1))
axD.set_ylim((0, np.max(dists) + 200))
axD.set_title("Total distance")
line_path, = ax.plot([], [], c = "r")
line_temp, = ax.plot([], [], c = "blue")
line_dist, = ax.plot([], [], c = "blue")
pass
# initialization function: plot the background of each frame
def init():
ax.add_collection(PatchCollection(all_patches, match_original=True))
ys = data['Широта']
xx = data['Долгота']
names = data['Город']
ax.scatter(xs, ys, s = 30, alpha = 0.2, c="red")
line_path.set_data([], [])
return (line_path, line_temp, line_dist, )
# animation function. This is called sequentially
def animate(i):
ys = data['Широта']
xx = data['Долгота']
names = data['Город']
texts = [ax.text(x, y, name) for (x, y, name) in zip(xs, ys, names)]
o = paths[i]
x = xs[o]
y = ys[o]
_ = adjust_text(texts, x=x, y=y)
line_path.set_data(x, y)
line_temp.set_data(range(i), temps[:i])
line_dist.set_data(range(i), dists[:i])
return (line_path, line_temp, line_dist, )
# In[621]:
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=2, interval=200, blit=False)
# In[622]:
anim