Navya-Sree's picture
Create utils.py
1d67264 verified
# utils.py
import numpy as np
import pandas as pd
import config
def generate_mock_locations(num_nodes):
"""
Generates random coordinates centered around NYC.
Real apps would fetch this from a Database or API.
"""
np.random.seed(42) # For reproducibility
# Generate points around NYC (Lat: 40.75, Lon: -74.00)
locations = np.random.rand(num_nodes, 2) * 0.1 + [config.NYC_LAT - 0.05, config.NYC_LON - 0.05]
return locations
def format_route_data(solution, routing, manager, locations, fleet_size):
"""
Extracts route data from the OR-Tools solution object
and formats it for the Pydeck map visualization.
"""
routes_for_map = []
for vehicle_id in range(fleet_size):
index = routing.Start(vehicle_id)
route_coords = []
while not routing.IsEnd(index):
node_index = manager.IndexToNode(index)
route_coords.append(locations[node_index])
index = solution.Value(routing.NextVar(index))
# Add the last node (End position) if needed, or loop back
# For this demo, we just map the segments
if len(route_coords) > 1:
df = pd.DataFrame(route_coords, columns=['lat', 'lon'])
df['next_lat'] = df['lat'].shift(-1)
df['next_lon'] = df['lon'].shift(-1)
# Remove the last row which has NaN for 'next'
df = df.dropna()
# Assign a random color for this vehicle route
color = [
int(np.random.choice(range(50, 255))),
int(np.random.choice(range(50, 255))),
150
]
routes_for_map.append((df, color))
return routes_for_map