VRPTW Routing › Formula

Michael BrenndoerferSeptember 4, 202523 min read
Machine Learning from ScratchAll chapters
Part 9 of 9Optimization
Chapter 66VRPTW Routing › Formula
Reading section 2 of 8 · Formula · 23 minSection 2 of 8 · 23 min

Book contents

Section 2 of 8 · 23 min read

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:

  1. Assignment: Which vehicle should visit which customers?
  2. Sequencing: In what order should each vehicle make its stops?
  3. 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.

Notation Reference

Throughout this formulation, we use the following notation:

SymbolTypeMeaning
V={0,1,...,n}V = \{0, 1, ..., n\}SetAll locations (0 = depot, others = customers)
K={1,2,...,m}K = \{1, 2, ..., m\}SetAvailable vehicles
A={(i,j):i,j∈V,i≠j}A = \{(i,j) : i,j \in V, i \neq j\}SetPossible direct connections (arcs)
did_iParameterDemand at customer ii
QkQ_kParameterCapacity of vehicle kk
tijt_{ij}ParameterTravel time from location ii to jj
[ai,bi][a_i, b_i]ParameterTime window at location ii
sis_iParameterService time at location ii
cijc_{ij}ParameterCost of traveling from ii to jj
xijkx_{ijk}Decision variable1 if vehicle kk travels from ii to jj, 0 otherwise
wikw_{ik}Decision variableArrival time of vehicle kk at location ii
MMConstantLarge value for Big-M constraints

The Problem Parameters:

The world we're optimizing within is defined by several fixed quantities:

  • Locations (VV): The depot (node 0) is home base, where all vehicles start and must return. The remaining nodes {1,2,...,n}\{1, 2, ..., n\} represent customers awaiting service.

  • Fleet (KK): We have mm vehicles at our disposal. Each vehicle kk has a capacity QkQ_k 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 (AA): Between any two locations, there exists a potential route. The arc (i,j)(i,j) represents the possibility of traveling directly from ii to jj, with travel time tijt_{ij} and cost cijc_{ij}.

  • Customer Requirements: Each customer ii has a demand did_i (how much they need), a time window [ai,bi][a_i, b_i] (when they can receive service), and a service time sis_i (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 (xijkx_{ijk}): For every possible arc (i,j)(i,j) and every vehicle kk, we must decide: does vehicle kk 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 (wikw_{ik}): 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.

min⁡∑k∈K∑(i,j)∈Acijxijk\min \sum_{k \in K} \sum_{(i,j) \in A} c_{ij} x_{ijk}

Reading the formula: This expression says: "For every vehicle kk and every possible arc (i,j)(i,j), if that vehicle uses that arc (xijk=1x_{ijk} = 1), add the arc's cost cijc_{ij} to our total. If the arc isn't used (xijk=0x_{ijk} = 0), add nothing."

The binary variable xijkx_{ijk} acts as a switch: it "turns on" the cost cijc_{ij} 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 cijc_{ij} 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.

∑k∈K∑j∈V,j≠ixijk=1∀i∈V∖{0}\sum_{k \in K} \sum_{j \in V, j \neq i} x_{ijk} = 1 \quad \forall i \in V \setminus \{0\}

where:

  • KK: set of vehicles {1,2,...,m}\{1, 2, ..., m\}
  • VV: set of all nodes (depot and customers)
  • xijkx_{ijk}: binary variable equal to 1 if vehicle kk travels from node ii to node jj
  • ii: customer node (the constraint applies to all customers, i.e., V∖{0}V \setminus \{0\})

Interpreting the mathematics: For each customer ii (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 ∑j∈V,j≠ixijk\sum_{j \in V, j \neq i} x_{ijk} counts arcs of the form "ii → something" for vehicle kk, that is, arcs departing from customer ii. Summing over all vehicles kk gives the total number of departures from customer ii. 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)
Why count departures instead of arrivals?

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 ∑k∑j≠ixjik=1\sum_{k} \sum_{j \neq i} x_{jik} = 1 (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.

∑i∈V∖{0}di∑j∈V,j≠ixijk≤Qk∀k∈K\sum_{i \in V \setminus \{0\}} d_i \sum_{j \in V, j \neq i} x_{ijk} \leq Q_k \quad \forall k \in K

where:

  • V∖{0}V \setminus \{0\}: set of customer nodes (excluding the depot)
  • did_i: demand at customer ii (units of capacity required)
  • xijkx_{ijk}: binary variable equal to 1 if vehicle kk travels from ii to jj
  • QkQ_k: capacity of vehicle kk (maximum total demand it can serve)
  • kk: vehicle index

Interpreting the mathematics: For each vehicle kk, we compute the total demand it serves. The inner sum ∑j≠ixijk\sum_{j \neq i} x_{ijk} equals 1 if vehicle kk visits customer ii, and 0 otherwise. Multiplying by demand did_i and summing over all customers gives the total load. This must not exceed the vehicle's capacity QkQ_k.

Step-by-step logic:

  1. Does vehicle kk visit customer ii? Check if ∑j≠ixijk=1\sum_{j \neq i} x_{ijk} = 1 (yes) or 0 (no)
  2. If yes, that customer's demand did_i counts toward the vehicle's load
  3. Sum all such demands: ∑idi⋅(visited?)\sum_i d_i \cdot (\text{visited?})
  4. Ensure total ≤ capacity: the inequality ≤Qk\leq Q_k

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.

∑j∈V,j≠ixijk=∑j∈V,j≠ixjik∀i∈V,∀k∈K\sum_{j \in V, j \neq i} x_{ijk} = \sum_{j \in V, j \neq i} x_{jik} \quad \forall i \in V, \forall k \in K

where:

  • xijkx_{ijk}: binary variable equal to 1 if vehicle kk travels from ii to jj (arc leaving ii)
  • xjikx_{jik}: binary variable equal to 1 if vehicle kk travels from jj to ii (arc entering ii)
  • The constraint applies to all nodes i∈Vi \in V and all vehicles k∈Kk \in K

Interpreting the mathematics: This is a classic "flow balance" constraint from network optimization. For any node ii and vehicle kk:

  • Left side: ∑j≠ixijk\sum_{j \neq i} x_{ijk} counts arcs leaving ii (from ii to anywhere)
  • Right side: ∑j≠ixjik\sum_{j \neq i} x_{jik} counts arcs entering ii (from anywhere to ii)

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.

ai≤wik≤bi∀i∈V,∀k∈Ka_i \leq w_{ik} \leq b_i \quad \forall i \in V, \forall k \in K

where:

  • aia_i: earliest acceptable arrival time at location ii (lower bound of time window)
  • bib_i: latest acceptable arrival time at location ii (upper bound of time window)
  • wikw_{ik}: continuous variable representing when vehicle kk arrives at location ii
  • The constraint applies to all nodes i∈Vi \in V and all vehicles k∈Kk \in K

Interpreting the mathematics: This is a simple bound constraint on the arrival time variable. For each location ii and vehicle kk, the arrival time wikw_{ik} must be:

  • Not earlier than the earliest acceptable time aia_i
  • Not later than the latest acceptable time bib_i

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 ii to jj, the arrival time at jj must be consistent with departing from ii.

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.

wik+si+tij−wjk≤M(1−xijk)∀(i,j)∈A,∀k∈Kw_{ik} + s_i + t_{ij} - w_{jk} \leq M(1 - x_{ijk}) \quad \forall (i,j) \in A, \forall k \in K

where:

  • wikw_{ik}: arrival time of vehicle kk at node ii
  • sis_i: service time at node ii (time spent performing the delivery)
  • tijt_{ij}: travel time from node ii to node jj
  • wjkw_{jk}: arrival time of vehicle kk at node jj
  • xijkx_{ijk}: binary variable equal to 1 if vehicle kk travels directly from ii to jj
  • MM: a sufficiently large constant (the "big-M" value)
  • AA: 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 (xijk=1x_{ijk} = 1):

The right-hand side becomes M(1−1)=0M(1-1) = 0, so the constraint simplifies to:

wik+si+tij≤wjkw_{ik} + s_i + t_{ij} \leq w_{jk}

This says: arrival at jj must be at least arrival at ii + service time at ii + travel time from ii to jj. Time moves forward along the route.

Case 2: When the arc is NOT used (xijk=0x_{ijk} = 0):

The right-hand side becomes M(1−0)=MM(1-0) = M, where MM is a very large number. The constraint becomes:

wik+si+tij−wjk≤Mw_{ik} + s_i + t_{ij} - w_{jk} \leq M

Since MM is huge, this is essentially always satisfied. The arrival times at ii and jj are independent, as they should be, since there's no direct route connecting them.

Choosing MM: The value must be large enough that the constraint is non-binding when xijk=0x_{ijk} = 0, but not so large as to cause numerical instability. A safe choice is M=max⁡(bi)−min⁡(ai)+max⁡(tij)+max⁡(si)M = \max(b_i) - \min(a_i) + \max(t_{ij}) + \max(s_i), which represents the maximum possible time span in the problem.

Out[3]:
Visualization
Timeline diagram showing temporal constraint when routing arc is used, with service completion and travel time sequencing.
When the arc is used (x=1), the Big-M constraint becomes binding, enforcing that arrival at j must occur after completing service at i plus travel time. The timeline shows the temporal sequence: arrive at i, complete service, travel to j.
Timeline diagram showing relaxed constraint when routing arc is not used, with independent arrival times.
When the arc is not used (x=0), the large constant M makes the constraint trivially satisfied. The arrival times at i and j become independent, and no temporal relationship is required between them.

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.

xijk∈{0,1}∀(i,j)∈A,∀k∈Kx_{ijk} \in \{0,1\} \quad \forall (i,j) \in A, \forall k \in K

where:

  • xijkx_{ijk}: the routing decision variable for arc (i,j)(i,j) and vehicle kk
  • {0,1}\{0,1\}: the binary domain (variable can only take value 0 or 1)
  • AA: set of all arcs
  • KK: set of all vehicles

This might seem like a trivial "constraint," but it makes VRPTW computationally challenging. If we allowed xijkx_{ijk} 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:

VariableTypeDimensionRole
xijkx_{ijk}BinarySpatialDefines the route structure
wikw_{ik}ContinuousTemporalDefines 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:

  1. Service coverage ensures no customer is forgotten
  2. Capacity limits prevent physical overloading
  3. Flow conservation ensures routes are connected paths
  4. Time windows honor customer requirements
  5. Temporal precedence links space and time
  6. 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.

Out[4]:
Visualization
Diagram showing interconnected VRPTW constraints including routing decisions, timing variables, capacity limits, flow conservation, time windows, and temporal precedence.
The VRPTW constraint system architecture showing routing and timing decision variables connect through various constraints. The precedence constraint (Big-M) is the central link connecting routing decisions to timing feasibility.

The diagram above visualizes this architecture. Notice how the routing variables (xijkx_{ijk}) flow downward into spatial constraints (coverage, capacity, flow), while timing variables (wikw_{ik}) 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 nn 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 nn customers and mm vehicles:

DecisionPossibilitiesGrowth Rate
Assign customers to vehiclesPartition nn items into mm groupsExponential in nn
Sequence within each routeOrder customers on each vehicleFactorial per route
Time each arrivalChoose wikw_{ik} satisfying windowsContinuous but constrained

For 20 customers and 3 vehicles, there are roughly 320≈3.5×1093^{20} \approx 3.5 \times 10^9 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.