VRPTW Routing › Formula
Machine Learning from ScratchAll chapters VRPTW Routing › FormulaOne section · Sections in this chapter2 / 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
Every optimization problem tells a story of choices and consequences. In VRPTW, the story begins with a simple question: How do we teach a computer to make the same decisions a skilled dispatcher makes instinctively? A human dispatcher juggles multiple concerns simultaneously: which truck goes where, what time to arrive, how much each vehicle can carry. To solve this problem computationally, we must translate this intuition into precise mathematical language.
This section develops the mathematical formulation step by step. For each component, we'll ask: Why do we need this? and What happens if we leave it out? By the end, you'll understand both what the formulas say and why each component is important for capturing the real-world complexity of vehicle routing.
The Dispatcher's Dilemma: Three Intertwined Decisions
Picture yourself as a logistics dispatcher on a Monday morning. You have a fleet of delivery trucks, a list of customers expecting packages, and a day's worth of time windows to honor. Your job is to answer three fundamental questions:
- Assignment: Which vehicle should visit which customers?
- Sequencing: In what order should each vehicle make its stops?
- Timing: When exactly should each vehicle arrive at each location?
At first glance, these might seem like separate problems. But consider what happens when you try to solve them independently:
- You assign Customer A to Truck 1 because it's nearby, but Truck 1 is already overloaded.
- You sequence Customer B before Customer C because B is closer to the depot, but Customer B's time window doesn't open until the afternoon.
- You schedule an arrival at 10:00 AM, but given travel times, the truck can't possibly get there before noon.
The fundamental insight is that these three decisions are deeply interconnected. The sequence of visits determines travel times, which determine arrival times, which must fit within time windows, which constrain which sequences are feasible, which affects how we assign customers to vehicles. It's a web of dependencies that we must capture mathematically.
The Language of Optimization: Parameters and Variables
Before we can express our dispatcher's decisions mathematically, we need to establish a precise vocabulary. In optimization, we distinguish between two types of quantities:
- Parameters: Fixed information about the problem that we cannot change
- Decision variables: The unknowns that the solver must determine
Think of parameters as the rules of the game, and decision variables as the moves we're trying to optimize.
Throughout this formulation, we use the following notation:
| Symbol | Type | Meaning |
|---|---|---|
| Set | All locations (0 = depot, others = customers) | |
| Set | Available vehicles | |
| Set | Possible direct connections (arcs) | |
| Parameter | Demand at customer | |
| Parameter | Capacity of vehicle | |
| Parameter | Travel time from location to | |
| Parameter | Time window at location | |
| Parameter | Service time at location | |
| Parameter | Cost of traveling from to | |
| Decision variable | 1 if vehicle travels from to , 0 otherwise | |
| Decision variable | Arrival time of vehicle at location | |
| Constant | Large value for Big-M constraints |
The Problem Parameters:
The world we're optimizing within is defined by several fixed quantities:
-
Locations (): The depot (node 0) is home base, where all vehicles start and must return. The remaining nodes represent customers awaiting service.
-
Fleet (): We have vehicles at our disposal. Each vehicle has a capacity representing the maximum total demand it can carry. A delivery van might hold 20 packages; a truck might have a 10-ton weight limit.
-
Network (): Between any two locations, there exists a potential route. The arc represents the possibility of traveling directly from to , with travel time and cost .
-
Customer Requirements: Each customer has a demand (how much they need), a time window (when they can receive service), and a service time (how long the delivery takes once the vehicle arrives).
The Decisions We Must Make (Variables):
Our mathematical model must determine two types of quantities:
-
Route structure (): For every possible arc and every vehicle , we must decide: does vehicle use this arc? The binary nature of this variable (it can only be 0 or 1) reflects the discrete reality of routing: a truck either takes a road or it doesn't.
-
Arrival schedule (): For every location and vehicle, we must determine the arrival time. Unlike routing decisions, timing is continuous: a vehicle might arrive at 10:15 or 10:16 or any time in between.
These two variable types encode the spatial dimension (where do vehicles go?) and the temporal dimension (when do they get there?) of our solution. The constraints we'll develop ensure these two dimensions remain consistent with each other and with real-world requirements.
The Objective: What Are We Trying to Achieve?
With our vocabulary established, we can now state what we're trying to accomplish. In most routing problems, efficiency means minimizing total cost, whether that's travel time, distance, fuel consumption, or some combination.
Reading the formula: This expression says: "For every vehicle and every possible arc , if that vehicle uses that arc (), add the arc's cost to our total. If the arc isn't used (), add nothing."
The binary variable acts as a switch: it "turns on" the cost only when the arc is part of a route. Summing over all vehicles and all arcs gives us the total operational cost of our solution.
What does "cost" mean in practice?
This formulation is flexible. The cost could represent:
- Travel time (minimize total driving hours)
- Distance (minimize fuel consumption)
- Monetary cost (including tolls, fuel, driver wages)
- Environmental impact (minimize carbon emissions)
The mathematical structure remains identical regardless of which metric we optimize.
The Constraints: Rules That Solutions Must Obey
The objective function tells us what we want to achieve, but without constraints, the solver would trivially "solve" the problem by not sending any vehicles anywhere: zero cost! Constraints encode the rules that separate valid solutions from invalid ones.
Each constraint we introduce addresses a specific way that a solution could fail in the real world. As we develop each constraint, we'll ask: What goes wrong if we omit this rule?
Constraint 1: Complete Service Coverage
The requirement: Every customer must be visited exactly once by exactly one vehicle.
What could go wrong without it: A cost-minimizing solver might skip difficult-to-reach customers entirely, or send multiple vehicles to easy customers while ignoring others.
where:
- : set of vehicles
- : set of all nodes (depot and customers)
- : binary variable equal to 1 if vehicle travels from node to node
- : customer node (the constraint applies to all customers, i.e., )
Interpreting the mathematics: For each customer (excluding the depot), count how many arcs leave that customer across all vehicles and all possible destinations. That count must equal exactly 1.
The summation counts arcs of the form " → something" for vehicle , that is, arcs departing from customer . Summing over all vehicles gives the total number of departures from customer . Since every visit requires exactly one departure, requiring this sum to equal 1 ensures:
- No customer is skipped (sum ≥ 1)
- No customer is visited multiple times (sum ≤ 1)
For any valid route, a vehicle that enters a customer must also leave. Thus, counting departing arcs is equivalent to counting arriving arcs. The formulation could equivalently use (counting arrivals), but the departure form is more common in the literature.
This constraint forms the foundation of our model. It defines what it means to "serve" all customers.
Constraint 2: Vehicle Capacity Limits
The requirement: No vehicle can carry more than its capacity allows.
What could go wrong without it: The solver might assign every customer to a single vehicle if that minimizes travel cost, ignoring physical impossibility.
where:
- : set of customer nodes (excluding the depot)
- : demand at customer (units of capacity required)
- : binary variable equal to 1 if vehicle travels from to
- : capacity of vehicle (maximum total demand it can serve)
- : vehicle index
Interpreting the mathematics: For each vehicle , we compute the total demand it serves. The inner sum equals 1 if vehicle visits customer , and 0 otherwise. Multiplying by demand and summing over all customers gives the total load. This must not exceed the vehicle's capacity .
Step-by-step logic:
- Does vehicle visit customer ? Check if (yes) or 0 (no)
- If yes, that customer's demand counts toward the vehicle's load
- Sum all such demands:
- Ensure total ≤ capacity: the inequality
This constraint bridges the abstract routing model with physical reality. Trucks have finite space, and our solution must respect this.
Constraint 3: Flow Conservation (Route Continuity)
The requirement: If a vehicle enters a location, it must also leave that location.
What could go wrong without it: The solver might create "broken" routes where vehicles appear at customers without a connected path from the depot, or routes that form isolated loops disconnected from the depot.
where:
- : binary variable equal to 1 if vehicle travels from to (arc leaving )
- : binary variable equal to 1 if vehicle travels from to (arc entering )
- The constraint applies to all nodes and all vehicles
Interpreting the mathematics: This is a classic "flow balance" constraint from network optimization. For any node and vehicle :
- Left side: counts arcs leaving (from to anywhere)
- Right side: counts arcs entering (from anywhere to )
These must be equal. Since all variables are binary, this means: enter a node exactly once → leave exactly once. Never enter → never leave.
Why this works: Combined with the service coverage constraint, flow conservation ensures that every vehicle's route forms a connected path starting at the depot, visiting some customers, and returning to the depot. There are no gaps, no teleportation, no vehicles stuck at customers.
Constraint 4: Time Window Compliance
The requirement: Arrivals must fall within each customer's specified time window.
What could go wrong without it: The solver might schedule arrivals at 3 AM for customers who are only open 9-5, or create routes that are spatially efficient but temporally infeasible.
where:
- : earliest acceptable arrival time at location (lower bound of time window)
- : latest acceptable arrival time at location (upper bound of time window)
- : continuous variable representing when vehicle arrives at location
- The constraint applies to all nodes and all vehicles
Interpreting the mathematics: This is a simple bound constraint on the arrival time variable. For each location and vehicle , the arrival time must be:
- Not earlier than the earliest acceptable time
- Not later than the latest acceptable time
The "TW" in VRPTW: This constraint is what distinguishes VRPTW from basic vehicle routing. Time windows add a temporal dimension that interacts with spatial routing decisions. You can't just find the shortest path; you must find a path that's also timely.
Constraint 5: Temporal Precedence (The Big-M Technique)
The requirement: If a vehicle travels from to , the arrival time at must be consistent with departing from .
What could go wrong without it: The solver might set arrival times arbitrarily, claiming a vehicle arrives at customer B before customer A even though the route visits A first. The route structure and the timing schedule would be disconnected from each other.
where:
- : arrival time of vehicle at node
- : service time at node (time spent performing the delivery)
- : travel time from node to node
- : arrival time of vehicle at node
- : binary variable equal to 1 if vehicle travels directly from to
- : a sufficiently large constant (the "big-M" value)
- : set of all arcs (possible direct connections)
This is the most subtle constraint in our formulation. It must accomplish something tricky: enforce a timing relationship only when an arc is used. The Big-M technique achieves this through clever algebraic manipulation.
Case 1: When the arc IS used ():
The right-hand side becomes , so the constraint simplifies to:
This says: arrival at must be at least arrival at + service time at + travel time from to . Time moves forward along the route.
Case 2: When the arc is NOT used ():
The right-hand side becomes , where is a very large number. The constraint becomes:
Since is huge, this is essentially always satisfied. The arrival times at and are independent, as they should be, since there's no direct route connecting them.
Choosing : The value must be large enough that the constraint is non-binding when , but not so large as to cause numerical instability. A safe choice is , which represents the maximum possible time span in the problem.


The diagram above makes the Big-M mechanism visible. The key insight is that this single constraint accomplishes two things: it enforces physical causality when routes are used, while remaining "invisible" for routes that don't exist in the solution. This conditional behavior is what makes Big-M constraints effective for modeling conditional relationships in optimization problems.
Constraint 6: The Binary Domain
The requirement: Routing decisions must be discrete: either a vehicle takes an arc, or it doesn't.
where:
- : the routing decision variable for arc and vehicle
- : the binary domain (variable can only take value 0 or 1)
- : set of all arcs
- : set of all vehicles
This might seem like a trivial "constraint," but it makes VRPTW computationally challenging. If we allowed to take any value between 0 and 1 (a "relaxation"), we could solve the problem much faster using continuous optimization techniques. A solution might say "send 0.3 of a vehicle from A to B," which is mathematically valid but operationally meaningless.
The binary requirement forces discrete decisions: a truck either takes a road or it doesn't. There's no "half a route." This discreteness is what maps a tractable linear program into the NP-hard mixed-integer program that VRPTW is known to be.
Seeing the System: How Constraints Interact
We've now developed six constraints, but they don't operate in isolation. They form an interconnected system where a change in one part ripples through the others. Understanding this interaction is key to understanding why VRPTW is both powerful and challenging.
The Two Decision Dimensions:
Our model has two types of decision variables that the solver must determine simultaneously:
| Variable | Type | Dimension | Role |
|---|---|---|---|
| Binary | Spatial | Defines the route structure | |
| Continuous | Temporal | Defines when events happen |
These two dimensions are coupled through the precedence constraint (Big-M): the route structure determines which temporal relationships must hold.
The Constraint Ecosystem:
Think of the constraints as forming an ecosystem where each one addresses a potential failure mode:
- Service coverage ensures no customer is forgotten
- Capacity limits prevent physical overloading
- Flow conservation ensures routes are connected paths
- Time windows honor customer requirements
- Temporal precedence links space and time
- Binary domain forces discrete decisions
The solver's task is to find values for all variables that satisfy every constraint simultaneously while minimizing total cost. This is like solving a puzzle where each piece constrains what other pieces can be, except this puzzle has an exponentially large number of possible configurations.

The diagram above visualizes this architecture. Notice how the routing variables () flow downward into spatial constraints (coverage, capacity, flow), while timing variables () connect to temporal constraints (time windows). The precedence constraint sits at the center, serving as the bridge that couples these two worlds. This coupling is what makes VRPTW fundamentally more challenging than solving routing and scheduling as separate problems. The two dimensions must be optimized jointly.
The Computational Challenge: Why VRPTW Is Hard
Having built our mathematical model, we should understand what we're asking a computer to do when we hand it this formulation.
The Curse of Combinatorics:
VRPTW inherits its difficulty from the Traveling Salesman Problem (TSP), which asks: "What's the shortest route visiting cities exactly once?" TSP is NP-hard, meaning no known algorithm can solve all instances quickly. VRPTW is strictly harder: it's TSP with additional constraints layered on top.
Consider the size of the search space for a modest problem with customers and vehicles:
| Decision | Possibilities | Growth Rate |
|---|---|---|
| Assign customers to vehicles | Partition items into groups | Exponential in |
| Sequence within each route | Order customers on each vehicle | Factorial per route |
| Time each arrival | Choose satisfying windows | Continuous but constrained |
For 20 customers and 3 vehicles, there are roughly possible customer-vehicle assignments, and for each assignment, we must consider many possible orderings. The total number of potential solutions dwarfs the number of atoms in the observable universe for even moderately sized problems.
Why We Need Sophisticated Algorithms:
Enumeration (checking every possible solution) is clearly infeasible. Instead, modern solvers employ clever strategies:
- Branch-and-bound: Systematically explore the solution space while pruning branches that can't lead to optimal solutions
- Constraint propagation: Use constraints to eliminate infeasible variable values early
- Metaheuristics: Use local search, simulated annealing, or genetic algorithms to find good (if not optimal) solutions quickly
- Hybrid approaches: Combine exact methods with heuristics for the best of both worlds
The mathematical formulation we've developed is the foundation upon which all these algorithms operate. It precisely defines what we're looking for. The algorithms provide strategies for finding it.