VRPTW Routing › Example
Machine Learning from ScratchAll chapters VRPTW Routing › ExampleOne section · Sections in this chapter4 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Now let's see how the mathematical formulation works in practice by solving a small, concrete example. This hands-on walkthrough will help you understand how the constraints interact and how a valid solution satisfies all requirements simultaneously.
Problem Setup: A Simple Delivery Scenario
Imagine you run a small delivery service with:
- 1 depot (your warehouse, location 0)
- 4 customers (locations 1-4) needing deliveries
- 2 vehicles, each with capacity 10 units
Each customer has specific requirements: a demand (how much they need), a time window (when they can receive delivery), and travel times between locations. Let's examine the data:
Example VRPTW Problem:
Distance Matrix (travel times between locations):
Depot C1 C2 C3 C4
Depot 0 2 3 4 5
C1 2 0 2 3 4
C2 3 2 0 2 3
C3 4 3 2 0 2
C4 5 4 3 2 0
Time Windows [earliest, latest]:
Earliest Latest
Depot 0 10
C1 1 4
C2 2 5
C3 3 6
C4 4 7
Customer Demands: [3, 2, 4, 3]
Vehicle Capacities: [10, 10]Key observations from the data:
- Customer 1 has the earliest time window (1-4) and is closest to the depot (travel time 2)
- Customer 4 has the latest time window (4-7) and is farthest from the depot (travel time 5)
- Total demand is 3+2+4+3 = 12 units, which exceeds either vehicle's capacity (10), so we need both vehicles
- The time windows overlap, creating flexibility in sequencing
Building a Solution: Step-by-Step Constraint Checking
Let's construct a feasible solution manually, checking each constraint as we go. This demonstrates how the mathematical formulation translates to practical routing decisions.
Step 1: Assigning Customers to Vehicles
We need to partition the 4 customers between 2 vehicles. Let's try:
- Vehicle 1: Customers 1 and 3 (demands: 3 + 4 = 7 units)
- Vehicle 2: Customers 2 and 4 (demands: 2 + 3 = 5 units)
Capacity check (Constraint 2):
- Vehicle 1: 7 ≤ 10
- Vehicle 2: 5 ≤ 10
Both assignments satisfy capacity constraints. Now we need to determine the sequence of visits for each vehicle.
Step 2: Sequencing Visits for Vehicle 1
Vehicle 1 must visit Customers 1 and 3. Let's consider the sequence: Depot → Customer 1 → Customer 3 → Depot
Time window check (Constraints 4 and 5):
Let's trace the route step by step, using our notation:
- Start at depot: (vehicle 1 starts at depot at time 0)
- Travel to C1: (travel time from depot to customer 1)
- Arrival at C1:
- Time window check: (Constraint 4 satisfied)
- Service at C1: (assume 0 service time for simplicity)
- Travel to C3: (travel time from customer 1 to customer 3)
- Arrival at C3:
- Time window check: (Constraint 4 satisfied)
- Precedence check: (Constraint 5 satisfied)
- Return to depot: (travel time from customer 3 to depot)
- Arrival at depot:
Flow conservation check (Constraint 3):
- Vehicle 1 enters C1: 1 arc (from depot)
- Vehicle 1 leaves C1: 1 arc (to C3)
- Vehicle 1 enters C3: 1 arc (from C1)
- Vehicle 1 leaves C3: 1 arc (to depot)
Route 1 is feasible! In terms of our decision variables:
- (vehicle 1 travels depot→C1)
- (vehicle 1 travels C1→C3)
- (vehicle 1 travels C3→depot)
- , (arrival times)
Step 3: Sequencing Visits for Vehicle 2
Vehicle 2 must visit Customers 2 and 4. Let's try: Depot → Customer 2 → Customer 4 → Depot
Time window check:
Tracing route 2 with our notation:
- Start at depot: (vehicle 2 starts at depot at time 0)
- Travel to C2: (travel time from depot to customer 2)
- Arrival at C2:
- Time window check: (Constraint 4 satisfied)
- Service at C2: (assume 0 service time)
- Travel to C4: (travel time from customer 2 to customer 4, from distance matrix row 2, column 4)
- Arrival at C4:
- Time window check: (Constraint 4 satisfied)
- Precedence check: (Constraint 5 satisfied)
- Return to depot: (travel time from customer 4 to depot)
- Arrival at depot:
Route 2 is also feasible! Decision variables:
- , ,
- ,
Step 4: Verifying All Constraints
Let's verify that our complete solution satisfies all constraints:
Constraint 1 (Each customer visited exactly once):
For each customer , we verify:
- Customer 1 ():
(Only , all others are 0)
- Customer 2 ():
(Only )
-
Customer 3 ():
(Only )
-
Customer 4 ():
(Only )
Constraint 2 (Capacity):
For vehicle 1 ():
Since and (and all other for vehicle 1):
For vehicle 2 ():
Since and (and all other for vehicle 2):
Constraint 3 (Flow conservation): Already verified in Steps 2 and 3
Constraint 4 (Time windows): Already verified in Steps 2 and 3
Constraint 5 (Temporal precedence): Already verified in Steps 2 and 3
Constraint 6 (Binary variables): All are 0 or 1
Solution Summary
Our complete solution:
Route 1 (Vehicle 1): Depot → Customer 1 → Customer 3 → Depot
- Arcs used: , ,
- Travel time calculation:
- Demand served:
- Arrival times: (within window ), (within window )
Route 2 (Vehicle 2): Depot → Customer 2 → Customer 4 → Depot
- Arcs used: , ,
- Travel time calculation:
- Demand served:
- Arrival times: (within window ), (within window )
Total travel time (objective value):
where (using travel time as cost).


The visualizations above show the complete worked example. The first figure displays the spatial routes with travel times annotated on each arc, customer demands (d), and time windows (TW). The second figure shows the timeline for how each vehicle progresses through time, with shaded regions representing time windows and vertical bars marking actual arrival times. Notice how all arrivals fall within their respective time windows, confirming the solution's feasibility.
Why This Solution Works
This example illustrates several key insights:
-
Capacity constraints force customer partitioning: Since total demand (12) exceeds individual vehicle capacity (10), we need to use both vehicles. The capacity constraint guides how we group customers.
-
Time windows influence sequencing: Customer 1's early window (1-4) makes it a natural first stop. Customer 4's later window (4-7) provides flexibility for later visits.
-
Travel times affect feasibility: The sequence C1→C3 works because travel time 3 allows arrival at C3 (time 5) within its window [3,6]. A different sequence might violate time windows.
-
Constraints are interdependent: We can't optimize routes independently. The capacity constraint affects which customers can be grouped, which affects sequencing options, which affects time window feasibility.
This small example demonstrates the complexity that emerges even with just 4 customers. Real-world problems with hundreds of customers require sophisticated algorithms to search this constraint space efficiently.