Ortools pickup delivery routing

From xuansenpa1/skillrevise · View interactive version

Ortools pickup delivery routing is an AI agent skill from the GitHub repository xuansenpa1/skillrevise. Model OR-Tools paired pickup-and-delivery routing problems, including PDPTW, dial-a-ride, paratransit, patient transport, and courier jobs with same-vehicle pairing, precedence, capacity, optional connected service, rider time windows, and vehicle shifts.

How to use this skill

Skill file

Pickup And Delivery Routing

Use this skill when each job has related pickup and dropoff or delivery events that must be served together. Apply it after the base routing model has a time dimension and before solving.

Implementation Flow

  1. Create one pickup node and one dropoff node per job.
  2. Add same-vehicle and precedence constraints for every pair.
  3. Apply the customer-facing time windows and service-time convention from the problem statement.
  4. Choose optional-service handling: independent pairs, or connected request sets.
  5. After solving, postprocess connected sets, rebuild route sequences if needed, and re-audit feasibility before counting served jobs or writing output.

Pair Structure

Time Windows And Service Times

Pair Constraint Skeleton

Use this pattern after adding the time dimension. It enforces the paired-service relationship for each job. Add optional-service logic separately after deciding whether jobs are independent pairs or members of connected request sets.

solver = routing.solver()

for job in jobs:
    pickup_index = manager.NodeToIndex(job.pickup_node)
    dropoff_index = manager.NodeToIndex(job.dropoff_node)

    routing.AddPickupAndDelivery(pickup_index, dropoff_index)
    solver.Add(routing.VehicleVar(pickup_index) == routing.VehicleVar(dropoff_index))
    solver.Add(time_dimension.CumulVar(pickup_index) <= time_dimension.CumulVar(dropoff_index))

    if job.dropoff_window is not None:
        low, high = job.dropoff_window
        time_dimension.CumulVar(dropoff_index).SetRange(low, high)
    if job.pickup_window is not None:
        low, high = job.pickup_window
        time_dimension.CumulVar(pickup_index).SetRange(low, high)

Optional And Connected Service

The complete modeling shape for connected optional pickup-delivery service is:

solver = routing.solver()

for job in jobs:
    pickup_index = manager.NodeToIndex(job.pickup_node)
    dropoff_index = manager.NodeToIndex(job.dropoff_node)
    routing.AddPickupAndDelivery(pickup_index, dropoff_index)
    solver.Add(routing.VehicleVar(pickup_index) == routing.VehicleVar(dropoff_index))
    solver.Add(time_dimension.CumulVar(pickup_index) <= time_dimension.CumulVar(dropoff_index))

for request_set in connected_request_sets:
    pickup_indices = [manager.NodeToIndex(job.pickup_node) for job in request_set]
    routing.AddDisjunction(
        pickup_indices,
        len(request_set) * request_penalty,
        len(request_set),
    )

for job in jobs:
    routing.AddDisjunction([manager.NodeToIndex(job.dropoff_node)], 0)

Postprocessing Connected Sets

For all-or-none grouped service, perform the following post-processing steps after extracting raw route order:

  1. Identify connected groups whose pickup and dropoff nodes all appear in the extracted routes.
  2. Keep only service stops belonging to complete groups.
  3. Rebuild each remaining route by reconnecting kept stops in their original order between the route's original start and end depot.
  4. Recompute arrival times, service starts, departures, and load along the rebuilt route.
  5. Reject the rebuilt route if a shortcut arc is invalid, a service window is missed, the vehicle cannot return to the depot in time, or final load is nonzero.
def complete_group_job_ids(groups, visited_nodes):
    complete = set()
    for group in groups:
        # group jobs expose job_id, pickup_node, and dropoff_node.
        if all(job.pickup_node in visited_nodes and job.dropoff_node in visited_nodes for job in group):
            complete.update(str(job.job_id) for job in group)
    return complete


def kept_service_nodes(route, complete_job_ids):
    return [
        stop.node
        for stop in route.stops
        if stop.kind in {"pickup", "dropoff"} and str(stop.job_id) in complete_job_ids
    ]

After filtering, audit the rebuilt sequence from scratch; do not reuse arrival times, loads, shortcut feasibility, depot-return feasibility, or served counts from the unfiltered route.

Final Checks

Ride Rules

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.