-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcities.py
More file actions
281 lines (213 loc) · 7.38 KB
/
cities.py
File metadata and controls
281 lines (213 loc) · 7.38 KB
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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 26 10:13:53 2019
@author: Diana Jaganjac
"""
import random as rand
from sys import maxsize
import copy
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString
import math
def read_cities(file_name):
"""
Read in the cities from the given `file_name`, and return
them as a list of four-tuples:
[(state, city, latitude, longitude), ...]
Use this as your initial `road_map`, that is, the cycle
Alabama -> Alaska -> Arizona -> ... -> Wyoming -> Alabama.
"""
f = open(file_name)
lines = f.readlines()
result = []
for line in lines:
line = line.strip("\n")
ele = line.split("\t")
joinele = (ele[0]), (ele[1]), float(ele[2]), float(ele[3])
result.append(joinele)
return (result)
f.close
z = read_cities(r"city-data.txt")
#print(z)
def print_cities(road_map):
"""
Prints a list of cities, along with their locations.
Print only one or two digits after the decimal point.
"""
result = []
for ele in road_map:
rm = ((ele[1]), (round(ele[2],1)), (round(ele[3],1)))
result.append(rm)
return result
road_map = print_cities(z)
#print(road_map)
def compute_total_distance(road_map):
"""
Returns, as a floating point number, the sum of the distances of all
the connections in the `road_map`. Remember that it's a cycle, so that
(for example) in the initial `road_map`, Wyoming connects to Alabama...
"""
coords = []
for i in road_map:
coords.append(i[1])
coords.append(i[2])
x = [float(r) for r in coords[0::2]]
y = [float(r) for r in coords[1::2]]
xy = list(zip(x,y))
dist = 0
for r in range(len(xy)-1):
dist += math.sqrt((xy[r][0]-xy[r+1][0])**2 + (xy[r][1]-xy[r+1][1])**2)
return round(dist, 1)
#d = compute_total_distance(new_road_map)
#print(d)
def swap_cities(road_map, index1, index2):
"""
Take the city at location `index1` in the `road_map`, and the
city at location `index2`, swap their positions in the `road_map`,
compute the new total distance, and return the tuple
(new_road_map, new_total_distance)
Allow for the possibility that `index1=index2`,
and handle this case correctly.
"""
new_road_map = copy.deepcopy(road_map)
if index1 != index2:
position = new_road_map[index1], new_road_map[index2]
new_road_map[index2], new_road_map[index1] = position
if index1 == index2:
pass
new_total_distance = compute_total_distance(new_road_map)
return (new_road_map, new_total_distance)
#h = swap_cities(road_map, 0, 4)
#print(h)
def shift_cities(road_map):
"""
For every index i in the `road_map`, the city at the position i moves
to the position i+1. The city at the last position moves to the position
0. Return the new road map.
"""
road_map.insert(0, road_map.pop())
new_dist = compute_total_distance(road_map)
return (road_map, new_dist)
#g = shift_cities(new_road_map)
#print(g)
def find_best_cycle(road_map):
"""
Using a combination of `swap_cities` and `shift_cities`,
try `10000` swaps/shifts, and each time keep the best cycle found so far.
After `10000` swaps/shifts, return the best cycle found so far.
Use randomly generated indices for swapping.
"""
shortest_dist = maxsize
best_cycle = road_map
for n in range(0, 10000):
number1 = rand.randint(0,(len(road_map)-1))
number2 = rand.randint(0,(len(road_map)-1))
(cycle1, dist_1) = swap_cities(best_cycle, number1, number2)
if dist_1 < shortest_dist:
shortest_dist = dist_1
best_cycle = cycle1
(cycle2, dist_2) = shift_cities(best_cycle)
if dist_2 < shortest_dist:
shortest_dist = dist_2
best_cycle = cycle2
return best_cycle
s = find_best_cycle(road_map)
print(s)
#alist = []
#for i in range(10):
# s = find_best_cycle(road_map)
# alist.append(s)
#print(alist)
def distance_cities(road_map):
coords = []
ind = []
for i in road_map:
coords.append(i[1])
coords.append(i[2])
x = [float(r) for r in coords[0::2]]
y = [float(r) for r in coords[1::2]]
xy = list(zip(x,y))
for r in range(len(xy)-1):
dist = math.sqrt((xy[r][0]-xy[r+1][0])**2 + (xy[r][1]-xy[r+1][1])**2)
rounded = round(dist, 1)
ind.append(rounded)
return ind
#k = distance_cities(road_map)
#print(k)
def print_map(road_map):
"""
Prints, in an easily understandable format, the cities and
their connections, along with the cost for each connection
and the total cost.
"""
cities = find_best_cycle(road_map)
cost = distance_cities(cities)
for i in range(len(cities)-1):
indc = cost[i]
endvar = f"The cost from {cities[i][0]} to {cities[i+1][0]} is {indc}."
print(endvar)
total_cost = compute_total_distance(road_map)
total = f"The total cost is {total_cost}"
return total
#c = print_map(road_map)
#print(c)
def main(file_name):
"""
Reads in, and prints out, the city data, then creates the "best"
cycle and prints it out.
"""
if __name__ == "__main__": #keep this in
file = read_cities(file_name)
road_map = print_cities(file)
print()
result = find_best_cycle(road_map)
result.append(result[0])
total_cost = compute_total_distance(result)
return f"The best cycle is {result} with a total cost of {total_cost}"
final = main(r"city-data.txt")
print(final)
def visualise(road_map):
fp = (r"states.shp")
map_df = gpd.read_file(fp)
df = pd.read_csv(r"city-data.csv", header=0)
merged = map_df.set_index("STATE_NAME").join(df.set_index("State"))
data = merged.drop(["District of Columbia"])
geometry = [Point(xy) for xy in zip(data["Long"], data["Lat"])]
geo_df = gpd.GeoDataFrame(data, geometry = geometry)
dots = []
d = find_best_cycle(road_map)
d.append(d[0])
for i in d:
dots.append(i[1])
dots.append(i[2])
x = [float(r) for r in dots[0::2]]
y = [float(r) for r in dots[1::2]]
xy = list(zip(y,x))
n = []
count = 0
for i in xy:
count +=1
n.append(count)
n.remove(n[-1])
start = [Point(xy[0])]
start_df = gpd.GeoDataFrame(geometry = start)
line = [LineString(xy)]
line_df = gpd.GeoDataFrame(geometry = line)
line_df.to_file("line.shp")
route = (r"line.shp")
route_df = gpd.read_file(route)
route_df.crs = ({'init': 'epsg:4269'})
fig, ax = plt.subplots(figsize=(28, 12))
map_df.plot(ax=ax)
geo_df.plot(ax = ax, markersize = 10, color = "red", marker = "o", label = "US Capital Cities")
start_df.plot(ax = ax, marker = "*", color = "yellow", markersize = 20, label = "Starting Point")
route_df.plot(ax = ax, linewidth = 1, color = "orange", label = "Route Map")
for i, txt in enumerate(n):
ax.annotate(txt, (xy[i]), fontsize = 8)
plt.legend()
plt.title("Travelling Salesman Solution")
return ax
d = visualise(road_map)
print(d)