Today, I want to share a practical approach to tackling the Vehicle Routing Problem (VRP) - one of the most fascinating challenges in logistics and operations research. If you’ve ever wondered how delivery companies optimize their routes to serve multiple customers with limited vehicles, you’re about to find out!
Understanding the Vehicle Routing Problem
The Vehicle Routing Problem involves finding optimal routes for a fleet of vehicles to deliver goods to a set of customers. The objective is typically to minimize the total distance traveled while satisfying various constraints like vehicle capacity, time windows, etc.
Mathematically, the basic VRP can be expressed as:
$$\min \sum_{i=0}^{n} \sum_{j=0}^{n} \sum_{k=1}^{m} c_{ij} x_{ijk}$$
Subject to constraints like:
$$\sum_{k=1}^{m} \sum_{j=0}^{n} x_{ijk} = 1 \quad \forall i \in {1, 2, …, n}$$
$$\sum_{j=1}^{n} q_j \sum_{i=0}^{n} x_{ijk} \leq Q_k \quad \forall k \in {1, 2, …, m}$$
Where:
- $c_{ij}$ is the cost (or distance) from location $i$ to $j$
- $x_{ijk}$ equals 1 if vehicle $k$ travels from $i$ to $j$, and 0 otherwise
- $q_j$ is the demand at customer $j$
- $Q_k$ is the capacity of vehicle $k$
Let’s implement a solution to a concrete example!
A Practical VRP Example
Consider a company that needs to deliver packages to 10 customers from a central depot. The company has 3 vehicles, each with a capacity of 10 units. Each customer has a specific demand, and we want to minimize the total distance traveled.
1 | import numpy as np |
Code Explanation
Let’s walk through the key components of this solution:
1. VRPSolver Class
The heart of our solution is the VRPSolver class, which encapsulates all the logic for solving the Vehicle Routing Problem:
Initialization: We set up the problem parameters (depot location, customer locations, demands, vehicle capacity, and number of vehicles) and compute a distance matrix.
Distance Calculation: The
_compute_distance_matrix()method calculates the Euclidean distance between all locations (depot and customers).Route Evaluation:
_calculate_route_cost()computes the total distance of a route, and_is_route_feasible()checks if a route satisfies capacity constraints.
2. Solution Approaches
The code implements two solution methods:
Greedy Algorithm (
solve_greedy()): This approach assigns customers to vehicles one by one, always choosing the nearest unassigned customer that fits within the vehicle’s remaining capacity.Local Search Heuristic (
solve_heuristic()): This method starts with the greedy solution and then applies a 2-opt local search to improve each route. The 2-opt algorithm works by swapping two edges in a route to see if a shorter path can be found.
3. Visualization
The visualize_solution() method creates a clear representation of the solution, showing:
- The depot (red square)
- Customer locations (blue circles) with their ID and demand
- Vehicle routes with different colors

Results Analysis
When we run the code, we get both a textual and visual representation of the solution:
1 | Solution: |
The solution divides the 10 customers among 3 vehicles, ensuring that each vehicle doesn’t exceed its capacity of 10 units. Let’s analyze what makes this a good solution:
Balanced Load: Each vehicle is using a significant portion of its capacity (one vehicles at 100%, one vehicles at 80% and one at 70%).
Route Efficiency: The routes minimize unnecessary travel by grouping customers that are geographically close.
Total Distance: The algorithm has found a solution with a total distance of 88.48 units.
The visualization shows us how the vehicles are routed. Each colored line represents a different vehicle’s route, starting from the depot, visiting assigned customers, and returning to the depot.
Optimization Opportunities
Our solution uses relatively simple heuristics. In a real-world scenario, you might consider:
More Advanced Algorithms: Such as simulated annealing, genetic algorithms, or exact methods like branch-and-cut for smaller instances.
Additional Constraints: Like time windows, multiple depots, or heterogeneous vehicles.
Dynamic Routing: Handling real-time changes, such as new orders or traffic conditions.
Conclusion
Vehicle Routing Problems are at the core of modern logistics operations. Even with our relatively simple implementation, we can see how mathematical modeling and algorithms can help optimize delivery routes, saving costs and time.
The next time you receive a package delivery, think about the complex optimization problem that was solved behind the scenes to get that package to your doorstep efficiently!















