def draw_route_graph(data: dict, routing: pywrapcp.RoutingModel, assignment: pywrapcp.Assignment, filename: str = 'route.png', prog='sfdp'): """ Draw a route graph based on the solution of the problem. """ weights = data['weights'] demands = data['demands'] time_windows = data['time_windows'] graph = pgv.AGraph(directed=True) def _node(index: int) -> str: if index == 0: return f'{index}\nDepot' return f'{index}\nDemand: {demands[index]}\nRange: {time_windows[index]}' for vehicle_id in range(data['num_vehicles']): index = routing.Start(vehicle_id) while not routing.IsEnd(index): node_index = routing.IndexToNode(index) next_index = assignment.Value(routing.NextVar(index)) next_node_index = routing.IndexToNode(next_index) weight = weights[node_index][next_node_index] graph.add_edge( _node(node_index), _node(next_node_index), weight=weight, label=weight, ) index = next_index graph.draw(filename, prog=prog) print(f'The route graph has been saved to {filename}.')
def node_properties( routing: pywrapcp.RoutingModel, assignment: pywrapcp.Assignment, capacity_dimension: pywrapcp.RoutingDimension, time_dimension: pywrapcp.RoutingDimension, index: int, ) -> tuple: """ Get a node's properties on the index. """ node_index = routing.IndexToNode(index) load = assignment.Value(capacity_dimension.CumulVar(index)) time_var = time_dimension.CumulVar(index) time_min, time_max = assignment.Min(time_var), assignment.Max(time_var) return (node_index, load, time_min, time_max)