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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import folium
import pandas as pd
import utils
# In[2]:
# Load the data
ListA = pd.read_csv('List A.csv')
ListB = pd.read_csv('List B.csv')
ListC = pd.read_csv('List C.csv')
ListD = pd.read_csv('List D.csv')
# In[3]:
# Create two centroids, one in the North End and one in the Seaport District
centroids = [[42.365, -71.054], [42.351, -71.045]]
northeastern_coordinate = "-71.09033,42.33976"
# In[4]:
# Combine the two lists and add a column to indicate the list
ListA['list'] = 'A'
ListB['list'] = 'B'
ListC['list'] = 'C'
ListD['list'] = 'D'
TotalList = pd.concat([ListA, ListB, ListC, ListD])
# In[5]:
# Remove all columns but name and gps
TotalList = TotalList[['name', 'gps', 'list']]
# In[6]:
# Convert the gps column to a list of lists for k-means
TotalList['gps'] = TotalList['gps'].apply(lambda x: x.strip('[]').split(','))
TotalList['gps'] = TotalList['gps'].apply(lambda x: [float(i) for i in x])
# In[7]:
display(TotalList)
# # 2 Routes
# ## Cluster and Minimize
# In[8]:
# Cluster and minimize the data
_, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate,
time_diff=0.25, max_time=24)
route_1_coordinates = routes[0]
route_2_coordinates = routes[1]
# ## Create JSON
# In[9]:
# Create a JSON request for the API
# This is the data we want to get from the API
route_1 = utils.list_to_string(route_1_coordinates)
route_2 = utils.list_to_string(route_2_coordinates)
# In[10]:
# Create a dataframe from the JSON
df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate)
df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate)
# In[11]:
# Add columns for the route number
df1['route'] = 1
df2['route'] = 2
# Concatenate the two dataframes
df = pd.concat([df1, df2], ignore_index=True)
# In[12]:
display(df)
# ## Map
# In[13]:
# Create a map
m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11)
# Add the points and lines for the two routes with different colors
colors = ['red', 'blue']
for route in df['route'].unique():
df_route = df[df['route'] == route]
folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m)
for i in range(len(df_route)):
folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to(
m)
# Display the map
m
# ## Results
# In[14]:
# Get the number of waypoints for each route
route_1_waypoints = len(route_1_coordinates)
route_2_waypoints = len(route_2_coordinates)
print("Route 1 has {} waypoints".format(route_1_waypoints))
print("Route 2 has {} waypoints".format(route_2_waypoints))
# In[15]:
trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]),
northeastern_coordinate)
print("The trip will take {} hours".format(trip_hrs_1))
trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]),
northeastern_coordinate)
print("The trip will take {} hours".format(trip_hrs_2))
# # 3 Routes
# In[16]:
# Cluster and minimize the data
# Add a third centroid in the Financial District
centroids.append([42.356, -71.055])
_, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, time_diff=0.3, max_time=24)
route_1_coordinates = routes[0]
route_2_coordinates = routes[1]
route_3_coordinates = routes[2]
# ## Create JSON
# In[17]:
# Create a JSON request for the API
# This is the data we want to get from the API
route_1 = utils.list_to_string(route_1_coordinates)
route_2 = utils.list_to_string(route_2_coordinates)
route_3 = utils.list_to_string(route_3_coordinates)
# In[18]:
# Create a dataframe from the JSON
df1 = utils.create_json_df(route_1, utils.list_to_string([centroids[0]]), northeastern_coordinate)
df2 = utils.create_json_df(route_2, utils.list_to_string([centroids[1]]), northeastern_coordinate)
df3 = utils.create_json_df(route_3, utils.list_to_string([centroids[2]]), northeastern_coordinate)
# In[19]:
# Add columns for the route number
df1['route'] = 1
df2['route'] = 2
df3['route'] = 3
# Concatenate the three dataframes
df = pd.concat([df1, df2, df3], ignore_index=True)
# In[20]:
display(df)
# ## Map
# In[21]:
# Create a map
m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11)
# Add the points and lines for the three routes with different colors
colors = ['red', 'blue', 'green']
for route in df['route'].unique():
df_route = df[df['route'] == route]
folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m)
for i in range(len(df_route)):
folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to(
m)
# Display the map
m
# ## Results
# In[22]:
# Get the number of waypoints for each route
route_1_waypoints = len(route_1_coordinates)
route_2_waypoints = len(route_2_coordinates)
route_3_waypoints = len(route_3_coordinates)
print("Route 1 has {} waypoints".format(route_1_waypoints))
print("Route 2 has {} waypoints".format(route_2_waypoints))
print("Route 3 has {} waypoints".format(route_3_waypoints))
# In[23]:
# Get the trip time for each route
trip_hrs_1 = utils.get_trip_time(route_1, route_1_waypoints, utils.list_to_string([centroids[0]]),
northeastern_coordinate)
print("The trip will take {} hours".format(trip_hrs_1))
trip_hrs_2 = utils.get_trip_time(route_2, route_2_waypoints, utils.list_to_string([centroids[1]]),
northeastern_coordinate)
print("The trip will take {} hours".format(trip_hrs_2))
trip_hrs_3 = utils.get_trip_time(route_3, route_3_waypoints, utils.list_to_string([centroids[2]]),
northeastern_coordinate)
print("The trip will take {} hours".format(trip_hrs_3))
# # 10 ROUTES (because I can)
# In[24]:
# Cluster and minimize the data
# Add seven more centroids around Boston with different latitudes and longitudes
for i in range(7):
centroids.append([42.365 + i * 0.01, -71.054 + i * 0.01])
_, routes = utils.cluster_and_optimize(TotalList, centroids, northeastern_coordinate, time_diff=0.5, max_time=24)
# ## Create JSON
# In[25]:
# Create a JSON request for the API
# This is the data we want to get from the API
route_strings = []
for route in routes:
route_strings.append(utils.list_to_string(route))
# In[26]:
# Create a dataframe from the JSON
dfs = []
for i in range(len(routes)):
dfs.append(utils.create_json_df(route_strings[i], utils.list_to_string([centroids[i]]), northeastern_coordinate))
# Concatenate the dataframes
df = pd.concat(dfs, ignore_index=True)
# In[30]:
# Add columns for the route number
for i in range(len(routes)):
df['route'].iloc[i * len(routes[i]):(i + 1) * len(routes[i])] = i + 1
# In[31]:
# Display the dataframe
display(df)
# ## Map
# In[37]:
# Create a map
m = folium.Map(location=[df['lon'].mean(), df['lat'].mean()], zoom_start=11)
# Add the points and lines for the three routes with different colors
colors = ['red', 'blue', 'green', 'orange', 'purple', 'pink', 'black', 'gray', 'brown', 'yellow']
for route in df['route'].unique():
df_route = df[df['route'] == route]
folium.PolyLine(df_route[['lon', 'lat']].values.tolist(), color=colors[route - 1]).add_to(m)
for i in range(len(df_route)):
folium.CircleMarker(df_route[['lon', 'lat']].iloc[i].values.tolist(), radius=3, color=colors[route - 1]).add_to(
m)
# Display the map
m
# ## Results
# In[36]:
# Get the number of waypoints for each route
route_waypoints = []
for route in routes:
route_waypoints.append(len(route))
for i in range(len(route_waypoints)):
print("Route {} has {} waypoints".format(i + 1, route_waypoints[i]))
# In[34]:
# Get the trip time for each route
trip_hrs = []
for i in range(len(routes)):
trip_hrs.append(utils.get_trip_time(route_strings[i], route_waypoints[i], utils.list_to_string([centroids[i]]),
northeastern_coordinate))
for i in range(len(trip_hrs)):
print("The trip will take {} hours".format(trip_hrs[i]))
# In[ ]:
|