Routing - Dispatch Map
Problem: assign pallet deliveries to trucks from multiple depots while respecting truck capacity and route length.
Show code
import {
DefaultRoutingSearchParameters,
FirstSolutionStrategy,
initRouting,
RoutingIndexManager,
RoutingModel,
setWorkerBridgeEnabled,
} from 'or-tools-wasm/routing';
setWorkerBridgeEnabled(true);
await initRouting();
const starts = [0, 1, 2, 3];
const manager = new RoutingIndexManager(stops.length, starts.length, starts, starts);
const routing = new RoutingModel(manager);
const transit = routing.RegisterTransitCallback((from, to) => {
const a = stops[manager.IndexToNode(from)];
const b = stops[manager.IndexToNode(to)];
return Math.round(Math.hypot(a.x - b.x, a.y - b.y));
});
routing.SetArcCostEvaluatorOfAllVehicles(transit);
const demand = routing.RegisterUnaryTransitCallback((from) =>
stops[manager.IndexToNode(from)].demand,
);
routing.AddDimensionWithVehicleCapacity(demand, 0, vehicleCapacities, true, 'load');
routing.AddDimension(transit, 0, routeDistanceCap, true, 'distance');
deliveryNodes.forEach((node) => {
routing.AddDisjunction([manager.NodeToIndex(node)], dropPenaltyFor(node));
});
const params = DefaultRoutingSearchParameters();
params.firstSolutionStrategy = FirstSolutionStrategy.PATH_CHEAPEST_ARC;
const assignment = await routing.SolveWithParameters(params);
- Several trucks start and end at separate depots.
- Each delivery has a random pallet demand and priority.
- Delivery border color shows priority: gray is standard, amber is express, and red is critical.
- Each truck has the same pallet capacity and a route distance cap.
- Drop penalties allow impossible or inefficient deliveries to be left unserved, with critical stops penalized most.
- Before solving, the map shows generated depots and deliveries; after solving, each color is one truck route.
Routes
Run the solver to animate dispatch routes.
Status / Response: