-
Notifications
You must be signed in to change notification settings - Fork 0
/
city.py
359 lines (274 loc) · 14.8 KB
/
city.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
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
from dataclasses import dataclass
from collections import defaultdict as dd
from copy import copy
from Time import Time
from Vehicle import machine
@dataclass
class edge:
line : str
vehicle : str
value : float = float("inf")
class paths:
def __init__(self):
self.edges :list[edge] = []
def add_edge(self, path :edge):
if isinstance(path, edge):
self.edges.append(path)
def get_min_dist(self):
medge = edge("", "", float("inf"))
for item in self.edges:
if medge.value > item.value :
medge = item
return medge
def get_vehicle(self, line:str, name:str):
for item in self.edges:
if item.line == line and item.vehicle == name:
return item
return edge("", "")
@dataclass
class save_direction:
line : list[str]
vehicle :list[str]
stations: list[str]
value : float = float("inf")
minutes : int = 0
def __copy__(self):
return save_direction(copy(self.line), copy(self.vehicle), copy(self.stations), self.value, self.minutes)
class Tehran:
def __init__(self):
self.city_graph = dd(lambda : dd(paths))
self.nodes = dd(set[tuple])
self.lines = dd(list[str])
def read_from_file(self, *names):
for name in names:
try:
file = open(f"{name}.txt", 'r')
line = file.readline().replace(":\n" , "")
stat1 = ""
stat2 = ""
dist = 0
while True:
# read parts like line, station1, station2 and distance
stat1 = file.readline().replace("\n", "")
if stat1 == "" or line == "":
break
elif stat1 == "end" :
line = file.readline().replace(":\n" , "")
continue
stat2 = file.readline().replace("\n", "")
dist = int(file.readline().replace("\n", ""))
if line[0] == 'l':
e1 = edge(line, "Subway", dist)
e2 = edge(line, "Taxi", dist)
self.city_graph[stat1][stat2].add_edge(e1)
self.city_graph[stat1][stat2].add_edge(e2)
self.city_graph[stat2][stat1].add_edge(e1)
self.city_graph[stat2][stat1].add_edge(e2)
self.nodes[stat1].add((line, "Subway"))
self.nodes[stat1].add((line, "Taxi"))
self.nodes[stat2].add((line, "Subway"))
self.nodes[stat2].add((line, "Taxi"))
elif line[0] == 'b':
e1 = edge(line, "Bus", dist)
self.city_graph[stat1][stat2].add_edge(e1)
self.city_graph[stat2][stat1].add_edge(e1)
self.nodes[stat1].add((line, "Bus"))
self.nodes[stat2].add((line, "Bus"))
if(stat1 not in self.lines[line]):
self.lines[line].append(stat1)
if(stat2 not in self.lines[line]):
self.lines[line].append(stat2)
except:
raise ValueError(f"File with name {name}.txt is missing!")
def get_min(self, dj_table: dd, visited: set):
min_num = float("inf")
min_name = ""
for key, value in dj_table.items():
if value.value < min_num and (key not in visited):
min_num = value.value
min_name = key
return min_name
def find_shortest_path(self, src:str, dest:str):
if src in self.city_graph.keys() and dest in self.city_graph.keys():
visited_node : set[str] = set()
node_data = dd(lambda : save_direction([] , [] , []))
node_data[src].value = 0;
node_data[src].stations.append(src)
for i in range(len(self.city_graph)):
min_station = self.get_min(node_data, visited_node)
visited_node.add(min_station)
if min_station == dest:
break;
for key,value in self.city_graph[min_station].items():
if (key not in visited_node and node_data[min_station].value != float("inf")
and value.get_min_dist().value + node_data[min_station].value < node_data[key].value):
node_data[key].value = value.get_min_dist().value + node_data[min_station].value
node_data[key].line = node_data[min_station].line.copy()
node_data[key].line.append(value.get_min_dist().line)
node_data[key].stations = node_data[min_station].stations.copy()
node_data[key].stations.append(key)
node_data[key].vehicle = node_data[min_station].vehicle.copy()
node_data[key].vehicle.append("Subway or Taxi" if value.get_min_dist().line[0] == 'l' else "Bus")
return node_data[dest]
else:
raise ValueError("Station does not exist!")
def travel_line_time(self, line : str, vehicle:str, src:str, t1: Time, dj_table: dd[str, save_direction], visited: set[str]):
src_index = self.lines[line].index(src)
m1 = machine(vehicle)
start_time = copy(dj_table[src])
for i in range(src_index, len(self.lines[line])-1):
if self.lines[line][i+1] not in visited:
if i == src_index:
if len(start_time.line) == 0:
start_time.value += m1.get_in_time(t1 + start_time.value)
elif start_time.line[len(start_time.line) - 1] != line:
start_time.value += m1.get_in_time(t1 + start_time.value)
elif start_time.vehicle[len(start_time.vehicle) - 1] != vehicle:
start_time.value += m1.get_in_time(t1 + start_time.value)
start_time.value += (self.city_graph[self.lines[line][i]][self.lines[line][i+1]].get_vehicle(line, vehicle).value
* m1.get_pass_time(t1 + start_time.value))
start_time.stations.append(self.lines[line][i+1])
start_time.line.append(line)
start_time.vehicle.append(vehicle)
if start_time.value < dj_table[self.lines[line][i+1]].value:
dj_table[self.lines[line][i + 1]] = copy(start_time)
else:
break
start_time = copy(dj_table[src])
for i in range(src_index, 0, -1):
if self.lines[line][i-1] not in visited:
if i == src_index:
if len(start_time.line) == 0:
start_time.value += m1.get_in_time(t1 + start_time.value)
elif start_time.line[len(start_time.line) - 1] != line:
start_time.value += m1.get_in_time(t1 + start_time.value)
elif start_time.vehicle[len(start_time.vehicle) - 1] != vehicle:
start_time.value += m1.get_in_time(t1 + start_time.value)
start_time.value += (self.city_graph[self.lines[line][i]][self.lines[line][i - 1]].get_vehicle(line, vehicle).value
* m1.get_pass_time(t1 + start_time.value))
start_time.stations.append(self.lines[line][i - 1])
start_time.line.append(line)
start_time.vehicle.append(vehicle)
if start_time.value < dj_table[self.lines[line][i - 1]].value:
dj_table[self.lines[line][i - 1]] = copy(start_time)
else:
break
def find_best_time(self, src: str, dest: str, t1: Time):
if src in self.city_graph.keys() and dest in self.city_graph.keys():
visited_node : set[str] = set()
node_data = dd(lambda : save_direction([] , [] , []))
node_data[src].value = 0;
node_data[src].stations.append(src)
for i in range(len(self.city_graph)):
min_station = self.get_min(node_data, visited_node)
if min_station == dest:
break;
for pair in self.nodes[min_station]:
self.travel_line_time(pair[0], pair[1], min_station, t1, node_data, visited_node)
visited_node.add(min_station)
return node_data[dest]
else:
raise ValueError("Station does not exist!")
def get_arrive_time_sp(self, path: save_direction, t1: Time):
minutes = 0
for i in range(len(path.vehicle)):
vehicle = "Subway" if path.line[i][0] == 'l' else "Bus"
m1 = machine(vehicle)
if i == 0:
minutes += m1.get_in_time(t1 + minutes)
elif path.line[i] != path.line[i-1]:
minutes += m1.get_in_time(t1 + minutes)
elif path.vehicle[i] != path.vehicle[i-1]:
minutes += m1.get_in_time(t1 + minutes)
minutes += (self.city_graph[path.stations[i]][path.stations[i+1]].get_vehicle(path.line[i], vehicle).value
* m1.get_pass_time(t1 + minutes))
return minutes
def travel_line_cost(self, line: str, vehicle: str, src: str, t1: Time, dj_table: dd[str, save_direction], visited: set[str]):
src_index = self.lines[line].index(src)
m1 = machine(vehicle)
start_cost = copy(dj_table[src])
cost = 0
if vehicle == "Taxi":
get_in = 0
if len(start_cost.line) == 0:
get_in += m1.get_in_time(t1 + start_cost.minutes)
elif start_cost.line[len(start_cost.line) - 1] != line:
get_in += m1.get_in_time(t1 + start_cost.minutes)
elif start_cost.vehicle[len(start_cost.vehicle) - 1] != vehicle:
get_in += m1.get_in_time(t1 + start_cost.minutes)
start_cost.minutes += get_in
for i in range(src_index, len(self.lines[line]) - 1):
start_cost.value += (self.city_graph[self.lines[line][i]][self.lines[line][i + 1]].get_vehicle(line, vehicle).value
* m1.get_price(t1 + start_cost.minutes))
start_cost.minutes +=(self.city_graph[self.lines[line][i]][self.lines[line][i + 1]].get_vehicle(line, vehicle).value
* m1.get_pass_time(t1 + start_cost.minutes))
start_cost.line.append(line)
start_cost.vehicle.append(vehicle)
start_cost.stations.append(self.lines[line][i + 1])
if dj_table[self.lines[line][i + 1]].value > start_cost.value:
dj_table[self.lines[line][i + 1]] = copy(start_cost)
start_cost = copy(dj_table[src])
start_cost.minutes += get_in
for i in range(src_index, 0, -1):
start_cost.value += (self.city_graph[self.lines[line][i]][self.lines[line][i - 1]].get_vehicle(line, vehicle).value
* m1.get_price(t1 + start_cost.minutes))
start_cost.minutes +=(self.city_graph[self.lines[line][i]][self.lines[line][i - 1]].get_vehicle(line, vehicle).value
* m1.get_pass_time(t1 + start_cost.minutes))
start_cost.line.append(line)
start_cost.vehicle.append(vehicle)
start_cost.stations.append(self.lines[line][i - 1])
if dj_table[self.lines[line][i - 1]].value > start_cost.value:
dj_table[self.lines[line][i - 1]] = copy(start_cost)
else:
start_cost = copy(dj_table[src])
cost = 0
if len(start_cost.line) == 0:
cost += m1.get_price(t1 + start_cost.minutes)
elif start_cost.line[len(start_cost.line) - 1] != line:
cost += m1.get_price(t1 + start_cost.minutes)
elif start_cost.vehicle[len(start_cost.vehicle) - 1] != vehicle:
cost += m1.get_price(t1 + start_cost.minutes)
start_cost.value += cost
for i in range(src_index + 1, len(self.lines[line])):
start_cost.line.append(line)
start_cost.vehicle.append(vehicle)
start_cost.stations.append(self.lines[line][i])
if dj_table[self.lines[line][i]].value > start_cost.value:
dj_table[self.lines[line][i]] = copy(start_cost)
start_cost = copy(dj_table[src])
start_cost.value += cost
for i in range(src_index, 0, -1):
start_cost.line.append(line)
start_cost.vehicle.append(vehicle)
start_cost.stations.append(self.lines[line][i - 1])
if dj_table[self.lines[line][i - 1]].value > start_cost.value:
dj_table[self.lines[line][i - 1]] = copy(start_cost)
def find_best_cost(self, src: str, dest: str, t1: Time):
if src in self.city_graph.keys() and dest in self.city_graph.keys():
visited_node : set[str] = set()
node_data = dd(lambda : save_direction([] , [] , []))
node_data[src].value = 0;
node_data[src].stations.append(src)
for i in range(len(self.city_graph)):
min_station = self.get_min(node_data, visited_node)
if min_station == dest:
break;
for pair in self.nodes[min_station]:
self.travel_line_cost(pair[0], pair[1], min_station, t1, node_data, visited_node)
visited_node.add(min_station)
return node_data[dest]
else:
raise ValueError("Station does not exist!")
def get_arrive_time_bc(self, path: save_direction, t1: Time):
minutes = 0
for i in range(len(path.vehicle)):
m1 = machine(path.vehicle[i])
if i == 0:
minutes += m1.get_in_time(t1 + minutes)
elif path.line[i] != path.line[i-1]:
minutes += m1.get_in_time(t1 + minutes)
elif path.vehicle[i] != path.vehicle[i-1]:
minutes += m1.get_in_time(t1 + minutes)
minutes += (self.city_graph[path.stations[i]][path.stations[i+1]].get_vehicle(path.line[i], path.vehicle[i]).value
* m1.get_pass_time(t1 + minutes))
return minutes