-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_algorithms.py
209 lines (170 loc) · 9.22 KB
/
path_algorithms.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
import heapq
from utils import minutesToTime, timeToMinutes
cost_index = 0
PREV_INDEX = 1
DEP_TIME_INDEX = 2
ARR_TIME_INDEX = 3
LINE_INDEX = 4
def dijkstra(graph, start, end, start_time):
start_time_minutes = timeToMinutes(start_time)
queue = [(0, start, None, start_time_minutes, start_time_minutes, None)] # Initialize with total cost, current stop, etc.
visited = {}
while queue:
cost, current, prev, dep_time, arr_time, line = heapq.heappop(queue)
if current in visited:
continue
visited[current] = (cost, prev, dep_time, arr_time, line)
if current == end:
break
for next_stop, lines in graph.get(current, {}).items():
if next_stop not in graph:
continue
for line, trips in lines.items():
for trip in trips:
if trip['departure_time'] >= arr_time:
wait_time = trip['departure_time'] - arr_time
travel_time = trip['arrival_time'] - trip['departure_time']
new_cost = cost + wait_time + travel_time
if next_stop not in visited or new_cost < visited[next_stop][0]:
heapq.heappush(queue, (new_cost, next_stop, current, trip['departure_time'], trip['arrival_time'], line))
# Path reconstruction
path = []
current = end
while current:
if current in visited:
node = visited[current] # node structure: (cost, prev, dep_time, arr_time, line)
path.append((node[PREV_INDEX], current, node[ARR_TIME_INDEX], node[DEP_TIME_INDEX], node[LINE_INDEX]))
current = node[PREV_INDEX]
else:
break
path.reverse()
return path
def astar(graph, start, end, start_time, distance_function, stop_coordinates):
start_time_minutes = timeToMinutes(start_time)
end_lat, end_lon = float(stop_coordinates[end]['start_stop_lat']), float(stop_coordinates[end]['start_stop_lon']) # Destination coordinates
queue = [(0, start, None, start_time_minutes, start_time_minutes, None, 0)] # Initial heuristic is 0
visited = {}
while queue:
cost, current, prev, dep_time, arr_time, line, _ = heapq.heappop(queue)
if current in visited:
continue
visited[current] = (cost, prev, dep_time, arr_time, line)
if current not in graph:
continue
if current == end:
break
current_lat, current_lon = float(stop_coordinates[current]['start_stop_lat']), float(stop_coordinates[current]['start_stop_lon']) # Current coordinates
heuristic = distance_function(current_lat, current_lon, end_lat, end_lon) # Calculate heuristic
for next_stop, lines in graph.get(current, {}).items():
if next_stop not in graph:
continue
next_lat, next_lon = float(stop_coordinates[next_stop]['start_stop_lat']), float(stop_coordinates[next_stop]['start_stop_lon'])
# Calculate heuristic for the next_stop towards the end
next_stop_heuristic = distance_function(next_lat, next_lon, end_lat, end_lon)*10
for line, trips in lines.items():
for trip in trips:
if trip['departure_time'] >= arr_time:
wait_time = trip['departure_time'] - arr_time
travel_time = trip['arrival_time'] - trip['departure_time']
new_cost = cost + wait_time + travel_time
total_cost = new_cost + next_stop_heuristic
heapq.heappush(queue, (total_cost, next_stop, current, trip['departure_time'], trip['arrival_time'], line, next_stop_heuristic))
# Path reconstruction
path = []
current = end
while current:
if current in visited:
node = visited[current] # node structure: (cost, prev, dep_time, arr_time, line)
path.append((node[PREV_INDEX], current, node[ARR_TIME_INDEX], node[DEP_TIME_INDEX], node[LINE_INDEX])) # Adjusted indices
current = node[PREV_INDEX]
else:
break
path.reverse()
return path
def astar_improved(graph, start, end, start_time, distance_function, stop_coordinates):
start_time_minutes = timeToMinutes(start_time)
end_lat, end_lon = float(stop_coordinates[end]['start_stop_lat']), float(stop_coordinates[end]['start_stop_lon']) # Destination coordinates
queue = [(0, start, None, start_time_minutes, start_time_minutes, None, 0)] # Initial heuristic is 0
visited = {}
while queue:
cost, current, prev, dep_time, arr_time, line, _ = heapq.heappop(queue)
if current in visited:
continue
visited[current] = (cost, prev, dep_time, arr_time, line)
if current == end:
break
current_lat, current_lon = float(stop_coordinates[current]['start_stop_lat']), float(stop_coordinates[current]['start_stop_lon']) # Current coordinates
heuristic = distance_function(current_lat, current_lon, end_lat, end_lon) # Calculate heuristic
for next_stop, lines in graph.get(current, {}).items():
if next_stop not in graph:
continue
next_lat, next_lon = float(stop_coordinates[next_stop]['start_stop_lat']), float(stop_coordinates[next_stop]['start_stop_lon'])
# Calculate heuristic for the next_stop towards the end
next_stop_heuristic = distance_function(next_lat, next_lon, end_lat, end_lon)*100
for line, trips in lines.items():
for trip in trips:
if trip['departure_time'] >= arr_time:
wait_time = trip['departure_time'] - arr_time
travel_time = trip['arrival_time'] - trip['departure_time']
new_cost = cost + wait_time + travel_time
# Use next_stop's heuristic for total cost calculation
total_cost = new_cost + next_stop_heuristic
heapq.heappush(queue, (total_cost, next_stop, current, trip['departure_time'], trip['arrival_time'], line, next_stop_heuristic))
# Path reconstruction
path = []
current = end
while current:
if current in visited:
node = visited[current] # node structure: (cost, prev, dep_time, arr_time, line)
path.append((node[PREV_INDEX], current, node[ARR_TIME_INDEX], node[DEP_TIME_INDEX], node[LINE_INDEX])) # Adjusted indices
current = node[PREV_INDEX]
else:
break
path.reverse()
return path
def astar_for_lanes(graph, start, end, start_time, distance_function, stop_coordinates):
start_time_minutes = timeToMinutes(start_time)
end_lat, end_lon = float(stop_coordinates[end]['start_stop_lat']), float(stop_coordinates[end]['start_stop_lon'])
queue = [(0, 0, start, None, start_time_minutes, start_time_minutes, None)] # (line_changes, total_travel_time, current, ...)
visited = {}
while queue:
# Extract line_changes and total_travel_time from the queue
line_changes, total_travel_time, current, prev, dep_time, arr_time, prev_line = heapq.heappop(queue)
if current in visited:
continue
visited[current] = (line_changes, total_travel_time, prev, dep_time, arr_time, prev_line)
if current not in graph:
continue
if current == end:
break
current_lat, current_lon = float(stop_coordinates[current]['start_stop_lat']), float(stop_coordinates[current]['start_stop_lon'])
heuristic = distance_function(current_lat, current_lon, end_lat, end_lon) # Used for priority but does not affect cost
for next_stop, lines in graph.get(current, {}).items():
if next_stop not in graph:
continue
next_lat, next_lon = float(stop_coordinates[next_stop]['start_stop_lat']), float(stop_coordinates[next_stop]['start_stop_lon'])
for next_line, trips in lines.items():
for trip in trips:
if trip['departure_time'] >= arr_time:
wait_time = trip['departure_time'] - arr_time
travel_time = trip['arrival_time'] - trip['departure_time']
# Determine if changing lines
if prev_line is None or prev_line == next_line:
new_line_changes = line_changes
else:
new_line_changes = line_changes + 1
new_total_travel_time = total_travel_time + wait_time + travel_time
# Push new state to the queue with updated costs
heapq.heappush(queue, (new_line_changes, new_total_travel_time, next_stop, current, trip['departure_time'], trip['arrival_time'], next_line))
# Path reconstruction
path = []
current = end
while current:
if current in visited:
node = visited[current]
path.append((node[PREV_INDEX+1], current, node[ARR_TIME_INDEX+1], node[DEP_TIME_INDEX+1], node[LINE_INDEX+1])) # Adjusted indices
current = node[PREV_INDEX+1]
else:
break
path.reverse()
return path