In today’s fast-paced logistics world, planning efficient delivery routes isn’t just about finding the shortest path between points.
Real-world delivery operations face constant uncertainty - customer demands fluctuate, traffic conditions change, and unexpected events disrupt even the best-laid plans.
This is where the Stochastic Vehicle Routing Problem (SVRP) comes into play.
Unlike its deterministic counterpart, SVRP acknowledges that customer demands aren’t fixed but follow probability distributions.
Today, I’ll walk through a practical example of SVRP and solve it using Python.
Understanding the Stochastic Vehicle Routing Problem
In the SVRP, we need to design optimal routes for a fleet of vehicles to serve customers whose demands are uncertain.
The objective is typically to minimize the expected total cost while ensuring that the probability of route failure (where demand exceeds vehicle capacity) stays below an acceptable threshold.
The mathematical formulation of SVRP can be expressed as:
$$\min E[f(x,\xi)]$$
where $x$ represents the routing decisions, $\xi$ represents the random demand, and $f(x,\xi)$ is the cost function.
The constraints include:
$$P(g_i(x,\xi) \leq 0) \geq 1-\alpha_i, \forall i$$
Here, $g_i$ represents constraints (like capacity constraints), and $\alpha_i$ is the acceptable risk level.
Building a Practical SVRP Example
Let’s tackle a concrete example: A company operates a distribution center that needs to deliver products to 10 customers.
The demands of these customers are not fixed but follow normal distributions.
The company wants to determine optimal routes that minimize the total distance traveled while keeping the risk of exceeding vehicle capacity below 10%.
1 | import numpy as np |
Code Implementation Explained
Let’s break down our implementation by key components:
1. Problem Setup
First, we set up our problem parameters and generate random customer locations and stochastic demands:
1 | # Problem parameters |
Here, we’re creating 10 customers with randomly generated coordinates.
The demands follow normal distributions with means between 5 and 15 units, and standard deviations at 30% of the respective means.
This reflects real-world scenarios where customer demands fluctuate around expected values.
2. Handling Demand Uncertainty
The core of SVRP is managing uncertainty.
For this, we implement a function that checks if routes satisfy capacity constraints with a given confidence level:
1 | def check_capacity_constraint(route, means, stds, capacity, confidence): |
This function applies probability theory to calculate an “effective demand” that represents the demand level we’re confident won’t be exceeded (in this case, with 90% confidence).
It uses the properties of normal distributions where:
$$P(X \leq \mu + z \cdot \sigma) = \Phi(z)$$
Where $\Phi$ is the cumulative distribution function of the standard normal distribution, and $z$ is the critical value for our desired confidence level.
3. Route Construction Algorithm
We implement a modified version of the Clarke-Wright savings algorithm adapted for stochastic demands:
1 | def savings_algorithm_svrp(dist_mat, means, stds, capacity, confidence, num_nodes): |
The key modification from the standard savings algorithm is that we check the stochastic capacity constraint when merging routes.
This ensures that our routes have a high probability of not exceeding the vehicle capacity.
4. Solution Validation with Monte Carlo Simulation
To validate our analytical solution, we use Monte Carlo simulation to empirically test how often our routes would exceed capacity:
1 | def simulate_demands(means, stds, num_simulations=1000): |
This simulation helps us confirm that our theoretical risk calculations align with what would happen in practice.
Results and Visualization
Let’s examine the results of our implementation:
Route Planning Results
When we run the code, we get a solution with optimal routes that balance distance minimization with risk management.
The output shows each route’s customers, mean demand, effective demand (with safety buffer), risk of exceeding capacity, and total distance.
Visual Route Map
Let’s look at the generated route map:
Number of routes: 5 Total expected distance: 96.06 Route Statistics: Route Customers Mean Demand Effective Demand \ 0 1 [9] 10.924146 15.124102 1 2 [5, 6] 22.412459 28.570808 2 3 [8, 2, 3] 24.458729 29.983696 3 4 [1, 7] 18.115267 23.165920 4 5 [4, 10] 14.128123 18.066203 Risk of Exceeding Capacity Distance 0 2.930074e-07 4.875363 1 5.717190e+00 18.753356 2 9.933792e+00 29.676845 3 1.282233e-01 28.077425 4 1.201471e-05 14.675028

This visualization shows our depot (red star), customers (blue dots), and the optimized routes (colored lines).
Each route is assigned a different color, making it easy to see which customers are served by which vehicle.
Customer Demand Distributions
Understanding the stochastic nature of demands is crucial:

This graph shows the demand distribution for each customer.
Notice how they follow normal distributions with different means and standard deviations.
The red dashed line represents our vehicle capacity constraint.
Route Demand Distributions
When we combine customers into routes, we get aggregated demand distributions:

Each colored curve represents the total demand distribution for a route.
The red line again shows our vehicle capacity.
Routes that have curves extending far beyond the capacity line have higher risks of failure.
Risk Analysis
Let’s analyze the risk of exceeding capacity for each route:

This chart shows the probability that each route will exceed the vehicle capacity.
The red dashed line represents our maximum acceptable risk level (10%).
Theoretical vs. Empirical Risk Comparison
Our Monte Carlo simulation validates our theoretical calculations:

Risk Comparison (Theoretical vs. Empirical):
Route Theoretical Risk (%) Empirical Risk (%)
0 Route 1 2.930074e-07 0.00
1 Route 2 5.717190e+00 5.96
2 Route 3 9.933792e+00 9.66
3 Route 4 1.282233e-01 0.08
4 Route 5 1.201471e-05 0.00
The close alignment between theoretical and empirical risks confirms that our probabilistic modeling approach is accurate.
Practical Implications
This implementation has several real-world applications:
- Risk Management: Companies can set their risk tolerance levels and design routes accordingly.
- Resource Planning: By understanding demand variability, logistics planners can allocate appropriate vehicles and resources.
- Service Level Guarantees: The ability to quantify and control route failure probabilities enables more reliable customer service.
- Cost Optimization: Balancing route efficiency with risk management leads to better long-term cost structures.
Conclusion
The Stochastic Vehicle Routing Problem represents a significant leap forward in realistic logistics planning.
By incorporating uncertainty into our models, we create more robust solutions that can withstand the variability inherent in real-world operations.
Our Python implementation demonstrates how probability theory, heuristic algorithms, and simulation techniques can be combined to solve complex logistics problems under uncertainty.
The visual analytics help decision-makers understand both the solutions and the risks involved.
Next time you’re planning deliveries with uncertain demands, consider applying these stochastic optimization techniques - your operations will be more resilient and your customers more satisfied!















