-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute_optimizer.py
More file actions
340 lines (273 loc) · 11.6 KB
/
Copy pathroute_optimizer.py
File metadata and controls
340 lines (273 loc) · 11.6 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
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
import json
import heapq
import numpy as np
from typing import List, Dict, Tuple, Optional
from dataclasses import dataclass
from math import radians, cos, sin, asin, sqrt
@dataclass
class Route:
path: List[str]
total_distance_km: float
total_risk: float
avg_risk: float
estimated_time_hours: float
route_score: float
class RouteOptimizer:
def __init__(self, graph_state_file: str):
with open(graph_state_file, 'r') as f:
self.graph_state = json.load(f)
self.nodes = {
node["node_id"]: {
"latitude": node["latitude"],
"longitude": node["longitude"],
"overall_risk": node["overall_risk"]
}
for node in self.graph_state["nodes"]
}
self.adjacency = {node_id: [] for node_id in self.nodes.keys()}
for edge in self.graph_state["edges"]:
self.adjacency[edge["source"]].append({
"target": edge["target"],
"distance_km": edge["distance_km"],
"trade_volume": edge.get("trade_volume", 1000000)
})
self.adjacency[edge["target"]].append({
"target": edge["source"],
"distance_km": edge["distance_km"],
"trade_volume": edge.get("trade_volume", 1000000)
})
print(f"Route Optimizer initialized with {len(self.nodes)} nodes")
def haversine_distance(self, lat1: float, lon1: float,
lat2: float, lon2: float) -> float:
lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
dlat = lat2 - lat1
dlon = lon2 - lon1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371
return c * r
def calculate_heuristic(self, current: str, goal: str) -> float:
curr_node = self.nodes[current]
goal_node = self.nodes[goal]
distance = self.haversine_distance(
curr_node["latitude"], curr_node["longitude"],
goal_node["latitude"], goal_node["longitude"]
)
avg_risk = (curr_node["overall_risk"] + goal_node["overall_risk"]) / 2
return distance * (1 + avg_risk)
def calculate_edge_cost(self,
source: str,
target: str,
edge_info: Dict,
weights: Dict[str, float]) -> float:
distance = edge_info["distance_km"]
trade_volume = edge_info["trade_volume"]
source_risk = self.nodes[source]["overall_risk"]
target_risk = self.nodes[target]["overall_risk"]
avg_risk = (source_risk + target_risk) / 2
distance_cost = distance * weights["distance"]
risk_cost = avg_risk * distance * weights["risk"]
trade_factor = 1.0 / (1.0 + trade_volume / 1_000_000)
trade_cost = distance * trade_factor * weights["trade"]
total_cost = distance_cost + risk_cost + trade_cost
return total_cost
def find_optimal_route(self,
source: str,
destination: str,
weights: Optional[Dict[str, float]] = None) -> Optional[Route]:
if weights is None:
weights = {
"risk": 1.0,
"distance": 0.3,
"trade": 0.2
}
if source not in self.nodes or destination not in self.nodes:
print(f"Invalid source or destination")
return None
open_set = []
heapq.heappush(open_set, (0, source))
came_from = {}
g_score = {node_id: float('inf') for node_id in self.nodes}
g_score[source] = 0
f_score = {node_id: float('inf') for node_id in self.nodes}
f_score[source] = self.calculate_heuristic(source, destination)
distance_to = {source: 0.0}
risk_to = {source: 0.0}
while open_set:
_, current = heapq.heappop(open_set)
if current == destination:
path = []
node = current
while node in came_from:
path.append(node)
node = came_from[node]
path.append(source)
path.reverse()
total_distance = distance_to[destination]
total_risk = risk_to[destination]
avg_risk = total_risk / len(path)
estimated_time = total_distance / 37.0
route_score = g_score[destination]
route = Route(
path=path,
total_distance_km=total_distance,
total_risk=total_risk,
avg_risk=avg_risk,
estimated_time_hours=estimated_time,
route_score=route_score
)
return route
for neighbor_info in self.adjacency[current]:
neighbor = neighbor_info["target"]
edge_cost = self.calculate_edge_cost(
current, neighbor, neighbor_info, weights
)
tentative_g_score = g_score[current] + edge_cost
if tentative_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = tentative_g_score
f_score[neighbor] = tentative_g_score + self.calculate_heuristic(
neighbor, destination
)
distance_to[neighbor] = distance_to[current] + neighbor_info["distance_km"]
node_risk = self.nodes[neighbor]["overall_risk"]
risk_to[neighbor] = risk_to[current] + node_risk
if neighbor not in [item[1] for item in open_set]:
heapq.heappush(open_set, (f_score[neighbor], neighbor))
return None
def find_k_best_routes(self,
source: str,
destination: str,
k: int = 3,
diversity_penalty: float = 0.3) -> List[Route]:
routes = []
weight_configs = [
{"risk": 1.0, "distance": 0.3, "trade": 0.2},
{"risk": 0.5, "distance": 0.8, "trade": 0.2},
{"risk": 0.7, "distance": 0.4, "trade": 0.5},
{"risk": 1.2, "distance": 0.2, "trade": 0.1},
{"risk": 0.3, "distance": 1.0, "trade": 0.3},
]
candidate_paths = self._enumerate_simple_paths(source, destination)
unique_routes: Dict[tuple, Route] = {}
for weights in weight_configs:
for path in candidate_paths:
route = self._build_route_from_path(path, weights)
if route is None:
continue
key = tuple(route.path)
existing = unique_routes.get(key)
if existing is None or route.route_score < existing.route_score:
unique_routes[key] = route
for route in sorted(unique_routes.values(), key=lambda item: item.route_score):
is_diverse = True
for existing_route in routes:
overlap = len(set(route.path) & set(existing_route.path))
if overlap / len(route.path) > 0.8:
is_diverse = False
break
if is_diverse:
routes.append(route)
if len(routes) >= k:
break
return routes[:k]
def _enumerate_simple_paths(
self,
source: str,
destination: str,
max_depth: Optional[int] = None,
) -> List[List[str]]:
if source not in self.nodes or destination not in self.nodes:
return []
if max_depth is None:
max_depth = len(self.nodes) - 1
paths: List[List[str]] = []
def dfs(current: str, path: List[str], visited: set):
if len(path) - 1 > max_depth:
return
if current == destination:
paths.append(path[:])
return
for neighbor_info in sorted(
self.adjacency[current],
key=lambda item: item["distance_km"],
):
neighbor = neighbor_info["target"]
if neighbor in visited:
continue
visited.add(neighbor)
path.append(neighbor)
dfs(neighbor, path, visited)
path.pop()
visited.remove(neighbor)
dfs(source, [source], {source})
return paths
def _build_route_from_path(
self,
path: List[str],
weights: Dict[str, float],
) -> Optional[Route]:
if len(path) < 2:
return None
total_distance = 0.0
total_cost = 0.0
total_risk = self.nodes[path[0]]["overall_risk"]
for source, target in zip(path, path[1:]):
edge_info = next(
(edge for edge in self.adjacency[source] if edge["target"] == target),
None,
)
if edge_info is None:
return None
total_distance += edge_info["distance_km"]
total_cost += self.calculate_edge_cost(source, target, edge_info, weights)
total_risk += self.nodes[target]["overall_risk"]
avg_risk = total_risk / len(path)
estimated_time = total_distance / 37.0
return Route(
path=path,
total_distance_km=total_distance,
total_risk=total_risk,
avg_risk=avg_risk,
estimated_time_hours=estimated_time,
route_score=total_cost,
)
def analyze_route(self, route: Route) -> Dict:
analysis = {
"route_summary": {
"path": " → ".join(route.path),
"total_distance_km": round(route.total_distance_km, 2),
"avg_risk": round(route.avg_risk, 3),
"estimated_time_days": round(route.estimated_time_hours / 24, 1),
"route_score": round(route.route_score, 2)
},
"node_risks": [],
"high_risk_segments": [],
"bottlenecks": []
}
for node_id in route.path:
node = self.nodes[node_id]
analysis["node_risks"].append({
"node": node_id,
"risk": round(node["overall_risk"], 3)
})
if node["overall_risk"] > 0.7:
analysis["high_risk_segments"].append({
"node": node_id,
"risk": round(node["overall_risk"], 3),
"warning": "High risk - consider alternative"
})
for i, node_id in enumerate(route.path[1:-1], 1):
num_alternatives = len([
n for n in self.adjacency[node_id]
if n["target"] in route.path[i-1:i+2]
])
if num_alternatives <= 2:
analysis["bottlenecks"].append({
"node": node_id,
"alternatives": num_alternatives,
"warning": "Limited alternative routes"
})
return analysis
if __name__ == "__main__":
print("Run api_server.py and call /api/analyze_route to use live route optimization.")