File size: 1,736 Bytes
1d67264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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