🛠️ Solving the Machine Assignment Problem with Python: A Step-by-Step Guide
In the world of operations research and optimization, the machine assignment problem is a classic example of how to allocate resources (machines) to tasks (jobs) in the most efficient way possible.
The goal is to minimize the total cost or maximize the total profit associated with the assignments.
Let’s walk through a concrete example and solve it using Python on Google Colab.
We’ll use scipy.optimize to get an optimal solution, and matplotlib to visualize the result.
📘 Problem Statement
Suppose we have 3 machines and 3 tasks.
Each machine can handle only one task, and each task must be assigned to exactly one machine.
The cost matrix below shows the cost of assigning machine $( i )$ to task $( j )$:
$$
\text{Cost Matrix} =
\begin{bmatrix}
9 & 2 & 7 \
6 & 4 & 3 \
5 & 8 & 1 \
\end{bmatrix}
$$
Our goal is to find the optimal assignment of machines to tasks that minimizes the total cost.
🔍 Mathematical Formulation
This is a Linear Sum Assignment Problem, also known as the Assignment Problem.
Let:
- $( C_{ij} )$: cost of assigning machine $( i )$ to task $( j )$
- $( x_{ij} )$: binary variable, $1$ if machine $( i )$ is assigned to task $( j )$, $0$ otherwise
The objective function:
$$
\min \sum_{i=1}^{n} \sum_{j=1}^{n} C_{ij} \cdot x_{ij}
$$
Subject to:
$$
\sum_{j=1}^{n} x_{ij} = 1 \quad \forall i \quad \text{(each machine assigned to one task)}
$$
$$
\sum_{i=1}^{n} x_{ij} = 1 \quad \forall j \quad \text{(each task assigned to one machine)}
$$
🧠 Python Implementation
We will use scipy.optimize.linear_sum_assignment to solve the problem.
1 | import numpy as np |
🧩 Code Explanation
cost_matrix: A 3x3 matrix representing the cost for each machine-task assignment.linear_sum_assignment(): Uses the Hungarian algorithm to find the optimal assignment.row_ind,col_ind: Arrays that indicate which machine is assigned to which task.- We then compute the total cost from the selected assignments.
📊 Visualizing the Assignment
Let’s make a heatmap to visualize the assignments and costs.
1 | import seaborn as sns |
📌 Result Interpretation
The output of our code would look like this:
1 | Optimal assignment (Machine -> Task): |
In the heatmap, you’ll see red check marks ✓ on the optimal machine-task pairs.

✅ Conclusion
We’ve successfully tackled the machine assignment problem using Python.
This type of problem is highly applicable in manufacturing, logistics, scheduling, and project management.
The scipy.optimize library offers a powerful and easy-to-use tool for solving these types of optimization challenges efficiently.
Next time you need to assign resources optimally — whether they’re machines, people, or delivery trucks — remember this approach!















