Update tools.py
Browse files
tools.py
CHANGED
|
@@ -6,7 +6,7 @@ import time
|
|
| 6 |
import folium
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
from api import geocode_address_nominatim,get_route_osrm,get_alternative_routes_osrm, get_detailed_route_with_instructions
|
| 10 |
|
| 11 |
def estimate_traffic_from_time() -> float:
|
| 12 |
"""
|
|
@@ -61,6 +61,34 @@ REAL_VEHICLES = [
|
|
| 61 |
},
|
| 62 |
]
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def format_route_instructions(route_data: Dict) -> str:
|
| 65 |
"""
|
| 66 |
Format route instructions in readable format
|
|
@@ -120,7 +148,7 @@ def find_optimal_route(origin_coords: Tuple[float, float],
|
|
| 120 |
if not detailed_routes:
|
| 121 |
return None
|
| 122 |
|
| 123 |
-
traffic_factor =
|
| 124 |
|
| 125 |
scored_routes = []
|
| 126 |
for route_data in detailed_routes:
|
|
@@ -278,7 +306,7 @@ def real_route_planner(origin: str, destination: str) -> str:
|
|
| 278 |
|
| 279 |
return result
|
| 280 |
|
| 281 |
-
def real_cost_optimizer(distance_km: float, weight_kg: float, duration_min: float) -> str:
|
| 282 |
"""
|
| 283 |
Calculate real-world delivery costs
|
| 284 |
"""
|
|
@@ -298,7 +326,7 @@ def real_cost_optimizer(distance_km: float, weight_kg: float, duration_min: floa
|
|
| 298 |
|
| 299 |
base_fee = 150
|
| 300 |
|
| 301 |
-
traffic_multiplier =
|
| 302 |
|
| 303 |
capacity_usage = weight_kg / vehicle["capacity_kg"]
|
| 304 |
capacity_multiplier = 1.15 if capacity_usage > 0.8 else 1.0
|
|
@@ -351,7 +379,7 @@ def real_traffic_analyzer(origin: str, destination: str) -> str:
|
|
| 351 |
return "No route found for traffic analysis"
|
| 352 |
|
| 353 |
current_hour = datetime.now().hour
|
| 354 |
-
traffic_factor =
|
| 355 |
|
| 356 |
if traffic_factor >= 1.5:
|
| 357 |
traffic_level = "Heavy"
|
|
|
|
| 6 |
import folium
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
from api import geocode_address_nominatim,get_route_osrm,get_alternative_routes_osrm, get_detailed_route_with_instructions,get_realtime_traffic
|
| 10 |
|
| 11 |
def estimate_traffic_from_time() -> float:
|
| 12 |
"""
|
|
|
|
| 61 |
},
|
| 62 |
]
|
| 63 |
|
| 64 |
+
def calculate_traffic(origin_coords: Tuple[float, float], dest_coords: Tuple[float, float]):
|
| 65 |
+
"""Analyze the traffic using the realtime data"""
|
| 66 |
+
traffic_data = get_realtime_traffic(origin_coords, dest_coords)
|
| 67 |
+
|
| 68 |
+
print("Traffic Data:",traffic_data)
|
| 69 |
+
|
| 70 |
+
if not traffic_data:
|
| 71 |
+
print("sing fallback: estimate_traffic_from_time()")
|
| 72 |
+
return estimate_traffic_from_time()
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
base_time = traffic_data["noTrafficTravelTimeInSeconds"]
|
| 76 |
+
current_time = traffic_data["travelTimeInSeconds"]
|
| 77 |
+
delay = traffic_data["trafficDelayInSeconds"]
|
| 78 |
+
|
| 79 |
+
if base_time == 0:
|
| 80 |
+
return 1.0
|
| 81 |
+
|
| 82 |
+
traffic_factor = current_time / base_time
|
| 83 |
+
|
| 84 |
+
traffic_factor = max(0.8, min(traffic_factor, 2.0))
|
| 85 |
+
return round(traffic_factor, 2)
|
| 86 |
+
|
| 87 |
+
except KeyError:
|
| 88 |
+
print("Missing fields in traffic data, fallback to estimate_traffic_from_time()")
|
| 89 |
+
return estimate_traffic_from_time()
|
| 90 |
+
|
| 91 |
+
|
| 92 |
def format_route_instructions(route_data: Dict) -> str:
|
| 93 |
"""
|
| 94 |
Format route instructions in readable format
|
|
|
|
| 148 |
if not detailed_routes:
|
| 149 |
return None
|
| 150 |
|
| 151 |
+
traffic_factor = calculate_traffic(origin_coords,dest_coords)
|
| 152 |
|
| 153 |
scored_routes = []
|
| 154 |
for route_data in detailed_routes:
|
|
|
|
| 306 |
|
| 307 |
return result
|
| 308 |
|
| 309 |
+
def real_cost_optimizer(origin_coords:float,dest_coords:float,distance_km: float, weight_kg: float, duration_min: float) -> str:
|
| 310 |
"""
|
| 311 |
Calculate real-world delivery costs
|
| 312 |
"""
|
|
|
|
| 326 |
|
| 327 |
base_fee = 150
|
| 328 |
|
| 329 |
+
traffic_multiplier = calculate_traffic(origin_coords,dest_coords)
|
| 330 |
|
| 331 |
capacity_usage = weight_kg / vehicle["capacity_kg"]
|
| 332 |
capacity_multiplier = 1.15 if capacity_usage > 0.8 else 1.0
|
|
|
|
| 379 |
return "No route found for traffic analysis"
|
| 380 |
|
| 381 |
current_hour = datetime.now().hour
|
| 382 |
+
traffic_factor = calculate_traffic(origin_coords,dest_coords)
|
| 383 |
|
| 384 |
if traffic_factor >= 1.5:
|
| 385 |
traffic_level = "Heavy"
|