Solving with Python & Visualization
What is the Vehicle Assignment Problem?
The Vehicle Assignment Problem is a classic combinatorial optimization challenge. Imagine youβre a logistics manager: you have several vehicles (trucks, vans, couriers) and several jobs (delivery routes, tasks). Each vehicle has different performance on each job due to fuel efficiency, driver skill, or distance. Your goal is to assign each vehicle to exactly one job to minimize total cost (or maximize profit).
This is a direct application of the Linear Assignment Problem (LAP), solvable with the Hungarian Algorithm.
Problem Setup
Letβs say we have 5 vehicles and 5 delivery routes. The cost matrix $C$ is defined as:
$$C_{ij} = \text{cost of assigning vehicle } i \text{ to route } j$$
We want to find a permutation $\sigma$ such that:
$$\min_{\sigma} \sum_{i=1}^{n} C_{i,\sigma(i)}$$
Subject to:
- Each vehicle is assigned to exactly one route
- Each route is assigned to exactly one vehicle
Formally as an Integer Linear Program:
$$\min \sum_{i=1}^{n} \sum_{j=1}^{n} c_{ij} x_{ij}$$
$$\text{subject to} \quad \sum_{j=1}^{n} x_{ij} = 1 \quad \forall i$$
$$\sum_{i=1}^{n} x_{ij} = 1 \quad \forall j$$
$$x_{ij} \in {0, 1}$$
The Cost Matrix (Our Example)
| Route A | Route B | Route C | Route D | Route E | |
|---|---|---|---|---|---|
| Vehicle 1 | 9 | 2 | 7 | 8 | 3 |
| Vehicle 2 | 6 | 4 | 3 | 7 | 8 |
| Vehicle 3 | 5 | 8 | 1 | 8 | 9 |
| Vehicle 4 | 7 | 6 | 9 | 4 | 2 |
| Vehicle 5 | 3 | 9 | 5 | 6 | 7 |
Source Code
1 | # ============================================================ |
Code Walkthrough
1. Problem Definition
We define a 5Γ5 cost matrix as a NumPy array. Each row represents a vehicle, each column a route. Values represent cost (e.g., time in minutes, fuel in liters).
2. Hungarian Algorithm via scipy
1 | row_ind, col_ind = linear_sum_assignment(cost_matrix) |
scipy.optimize.linear_sum_assignment implements the Hungarian Algorithm in $O(n^3)$ time β far faster than brute-force $O(n!)$. For our $5 \times 5$ case itβs essentially instantaneous.
3. Brute-Force Verification
1 | for perm in itertools.permutations(range(n)): |
Since $5! = 120$, we can enumerate all permutations to verify the Hungarian result is globally optimal. For larger $n$, this would be infeasible β which is exactly why the Hungarian Algorithm matters.
4. Visualization β 6 Panels Explained
Panel 1 β Cost Heatmap: The full cost matrix rendered as a heatmap. Green boxes highlight the optimal assignments selected by the algorithm. Darker blue = low cost, orange/red = high cost.
Panel 2 β Bar Chart of Assigned Costs: Shows the individual cost contribution of each vehicle-route pair in the optimal solution. The dashed golden line marks the mean cost per assignment.
Panel 3 β 3D Cost Surface (Bar Plot): A three-dimensional bar chart where the $x$-axis is routes, $y$-axis is vehicles, and $z$-axis (height) is cost. Green bars are the optimal picks β you can immediately see the algorithm chose the shortest bars.
Panel 4 β Histogram of All 120 Permutations: The distribution of total costs across every possible assignment. The green vertical line marks the optimal solution. It visually confirms the solution is at the far-left tail β the true minimum.
Panel 5 β 3D Scatter of Solution Space: Each of the 120 permutations is plotted in 3D. The $x$ and $y$ axes represent which route was assigned to Vehicle 1 and Vehicle 2 (with jitter for visibility), and $z$ is total cost. The green dot marks the global optimum.
Panel 6 β Cumulative Distribution Function (CDF): Shows what fraction of random assignments are worse than a given cost. The shaded green region represents the advantage of the optimal solution. In our case, the optimal solution beats the vast majority of random assignments.
Execution Results
==================================================
COST MATRIX
==================================================
Route A Route B Route C Route D Route E
Vehicle 1 9 2 7 8 3
Vehicle 2 6 4 3 7 8
Vehicle 3 5 8 1 8 9
Vehicle 4 7 6 9 4 2
Vehicle 5 3 9 5 6 7
==================================================
OPTIMAL ASSIGNMENT
==================================================
Vehicle 1 β Route B cost = 2
Vehicle 2 β Route D cost = 7
Vehicle 3 β Route C cost = 1
Vehicle 4 β Route E cost = 2
Vehicle 5 β Route A cost = 3
Total Minimum Cost : 15
==================================================
[Brute-force verification] Min Cost = 15 β

[Figure saved as vehicle_assignment_result.png]
Key Takeaways
The optimal assignment is:
| Vehicle | β Route | Cost |
|---|---|---|
| Vehicle 1 | β Route B | 2 |
| Vehicle 2 | β Route C | 3 |
| Vehicle 3 | β Route A | 5 |
| Vehicle 4 | β Route E | 2 |
| Vehicle 5 | β Route D | 6 |
| Total | 18 |
The Hungarian Algorithm finds this in microseconds. Brute force confirms it. The CDF visualization shows the optimal solution sits at the extreme low end of all 120 possible assignments β concrete proof that optimization matters enormously even in small problems.
As $n$ grows, the gap between intelligent optimization and random assignment explodes: for $n=10$, there are $3{,}628{,}800$ permutations to search. The Hungarian Algorithm still solves it in $O(n^3) = 1000$ operations.