# Linear Sum Assignment Solver

This section describes the linear sum assignment solver, a specialized solver for the simple assignment problem, which can be faster than either the MIP or CP-SAT solver. However, the MIP and CP-SAT solvers can handle a much wider array of problems, so in most cases they are the best option.

### Cost matrix

The costs for workers and task are given in the table below.

0 90 76 75 70
1 35 85 55 65
2 125 95 90 105
3 45 110 95 115

The following sections present a Python program that solves an assignment problem using the linear sum assignment solver.

### Import the libraries

The code that imports the required library is shown below.

### Python

```import numpy as np

from ortools.graph.python import linear_sum_assignment```

### C++

```#include "ortools/graph/assignment.h"

#include <cstdint>
#include <numeric>
#include <string>
#include <vector>```

### Java

```import com.google.ortools.Loader;
import java.util.stream.IntStream;```

### C#

```using System;
using System.Collections.Generic;
using System.Linq;

### Define the data

The following code creates the data for the program.

### Python

```costs = np.array(
[
[90, 76, 75, 70],
[35, 85, 55, 65],
[125, 95, 90, 105],
[45, 110, 95, 115],
]
)

# Let's transform this into 3 parallel vectors (start_nodes, end_nodes,
# arc_costs)
end_nodes_unraveled, start_nodes_unraveled = np.meshgrid(
np.arange(costs.shape[1]), np.arange(costs.shape[0])
)
start_nodes = start_nodes_unraveled.ravel()
end_nodes = end_nodes_unraveled.ravel()
arc_costs = costs.ravel()```

### C++

```const int num_workers = 4;
std::vector<int> all_workers(num_workers);
std::iota(all_workers.begin(), all_workers.end(), 0);

const std::vector<std::vector<int>> costs = {{
{{90, 76, 75, 70}},    // Worker 0
{{35, 85, 55, 65}},    // Worker 1
{{125, 95, 90, 105}},  // Worker 2
{{45, 110, 95, 115}},  // Worker 3
}};```

### Java

```final int[][] costs = {
{90, 76, 75, 70},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115},
};
final int numWorkers = 4;

final int[] allWorkers = IntStream.range(0, numWorkers).toArray();

### C#

```int[,] costs = {
{ 90, 76, 75, 70 },
{ 35, 85, 55, 65 },
{ 125, 95, 90, 105 },
{ 45, 110, 95, 115 },
};
int numWorkers = 4;
int[] allWorkers = Enumerable.Range(0, numWorkers).ToArray();

The array is the cost matrix, whose i, j entry is the cost for worker i to perform task j. There are four workers, corresponding to the rows of the matrix, and four tasks, corresponding to the columns.

### Create the solver

The program uses the linear assignment solver, a specialized solver for the assignment problem.

The following code creates the solver.

### Python

`assignment = linear_sum_assignment.SimpleLinearSumAssignment()`

### C++

`SimpleLinearSumAssignment assignment;`

### Java

`LinearSumAssignment assignment = new LinearSumAssignment();`

### C#

`LinearSumAssignment assignment = new LinearSumAssignment();`

The following code adds the costs to the solver by looping over workers and tasks.

### Python

`assignment.add_arcs_with_cost(start_nodes, end_nodes, arc_costs)`

### C++

```for (int w : all_workers) {
for (int t : all_tasks) {
if (costs[w][t]) {
}
}
}```

### Java

```// Add each arc.
for (int w : allWorkers) {
for (int t : allTasks) {
if (costs[w][t] != 0) {
}
}
}```

### C#

```// Add each arc.
foreach (int w in allWorkers)
{
{
if (costs[w, t] != 0)
{
}
}
}```

### Invoke the solver

The following code invokes the solver.

### Python

`status = assignment.solve()`

### C++

`SimpleLinearSumAssignment::Status status = assignment.Solve();`

### Java

`LinearSumAssignment.Status status = assignment.solve();`

### C#

`LinearSumAssignment.Status status = assignment.Solve();`

### Display the results

The following code displays the solution.

### Python

```if status == assignment.OPTIMAL:
print(f"Total cost = {assignment.optimal_cost()}\n")
for i in range(0, assignment.num_nodes()):
print(
f"Worker {i} assigned to task {assignment.right_mate(i)}."
+ f"  Cost = {assignment.assignment_cost(i)}"
)
elif status == assignment.INFEASIBLE:
print("No assignment is possible.")
elif status == assignment.POSSIBLE_OVERFLOW:
print("Some input costs are too large and may cause an integer overflow.")```

### C++

```if (status == SimpleLinearSumAssignment::OPTIMAL) {
LOG(INFO) << "Total cost: " << assignment.OptimalCost();
for (int worker : all_workers) {
LOG(INFO) << "Worker " << std::to_string(worker) << " assigned to task "
<< std::to_string(assignment.RightMate(worker)) << ". Cost: "
<< std::to_string(assignment.AssignmentCost(worker)) << ".";
}
} else {
LOG(INFO) << "Solving the linear assignment problem failed.";
}```

### Java

```if (status == LinearSumAssignment.Status.OPTIMAL) {
System.out.println("Total cost: " + assignment.getOptimalCost());
for (int worker : allWorkers) {
System.out.println("Worker " + worker + " assigned to task "
+ assignment.getRightMate(worker) + ". Cost: " + assignment.getAssignmentCost(worker));
}
} else {
System.out.println("Solving the min cost flow problem failed.");
System.out.println("Solver status: " + status);
}```

### C#

```if (status == LinearSumAssignment.Status.OPTIMAL)
{
Console.WriteLine(\$"Total cost: {assignment.OptimalCost()}.");
foreach (int worker in allWorkers)
{
Console.WriteLine(\$"Worker {worker} assigned to task {assignment.RightMate(worker)}. " +
\$"Cost: {assignment.AssignmentCost(worker)}.");
}
}
else
{
Console.WriteLine("Solving the linear assignment problem failed.");
Console.WriteLine(\$"Solver status: {status}.");
}```

The output below shows the optimal assignment of workers to tasks.

```Total cost = 265
Worker 0 assigned to task 3.  Cost = 70
Worker 1 assigned to task 2.  Cost = 55
Worker 2 assigned to task 1.  Cost = 95
Worker 3 assigned to task 0.  Cost = 45
Time = 0.000147 seconds```

The following graph shows the solution as the dashed edges in the graph. The numbers next to the dashed edges are their costs. The total wait time of this assignment is the sum of the costs for the dashed edges, which is 265.

In graph theory, a set of edges in a bipartite graph that matches every node on the left with exactly one node on the right is called a perfect matching.

### The entire program

Here is the entire program.

### Python

```"""Solve assignment problem using linear assignment solver."""
import numpy as np

from ortools.graph.python import linear_sum_assignment

def main():
"""Linear Sum Assignment example."""
assignment = linear_sum_assignment.SimpleLinearSumAssignment()

costs = np.array(
[
[90, 76, 75, 70],
[35, 85, 55, 65],
[125, 95, 90, 105],
[45, 110, 95, 115],
]
)

# Let's transform this into 3 parallel vectors (start_nodes, end_nodes,
# arc_costs)
end_nodes_unraveled, start_nodes_unraveled = np.meshgrid(
np.arange(costs.shape[1]), np.arange(costs.shape[0])
)
start_nodes = start_nodes_unraveled.ravel()
end_nodes = end_nodes_unraveled.ravel()
arc_costs = costs.ravel()

status = assignment.solve()

if status == assignment.OPTIMAL:
print(f"Total cost = {assignment.optimal_cost()}\n")
for i in range(0, assignment.num_nodes()):
print(
f"Worker {i} assigned to task {assignment.right_mate(i)}."
+ f"  Cost = {assignment.assignment_cost(i)}"
)
elif status == assignment.INFEASIBLE:
print("No assignment is possible.")
elif status == assignment.POSSIBLE_OVERFLOW:
print("Some input costs are too large and may cause an integer overflow.")

if __name__ == "__main__":
main()
```

### C++

```#include "ortools/graph/assignment.h"

#include <cstdint>
#include <numeric>
#include <string>
#include <vector>

namespace operations_research {
// Simple Linear Sum Assignment Problem (LSAP).
void AssignmentLinearSumAssignment() {
SimpleLinearSumAssignment assignment;

const int num_workers = 4;
std::vector<int> all_workers(num_workers);
std::iota(all_workers.begin(), all_workers.end(), 0);

const std::vector<std::vector<int>> costs = {{
{{90, 76, 75, 70}},    // Worker 0
{{35, 85, 55, 65}},    // Worker 1
{{125, 95, 90, 105}},  // Worker 2
{{45, 110, 95, 115}},  // Worker 3
}};

for (int w : all_workers) {
for (int t : all_tasks) {
if (costs[w][t]) {
}
}
}

SimpleLinearSumAssignment::Status status = assignment.Solve();

if (status == SimpleLinearSumAssignment::OPTIMAL) {
LOG(INFO) << "Total cost: " << assignment.OptimalCost();
for (int worker : all_workers) {
LOG(INFO) << "Worker " << std::to_string(worker) << " assigned to task "
<< std::to_string(assignment.RightMate(worker)) << ". Cost: "
<< std::to_string(assignment.AssignmentCost(worker)) << ".";
}
} else {
LOG(INFO) << "Solving the linear assignment problem failed.";
}
}

}  // namespace operations_research

int main() {
operations_research::AssignmentLinearSumAssignment();
return EXIT_SUCCESS;
}```

### Java

```package com.google.ortools.graph.samples;
import java.util.stream.IntStream;

/** Minimal Linear Sum Assignment problem. */
public class AssignmentLinearSumAssignment {
public static void main(String[] args) {
LinearSumAssignment assignment = new LinearSumAssignment();

final int[][] costs = {
{90, 76, 75, 70},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115},
};
final int numWorkers = 4;

final int[] allWorkers = IntStream.range(0, numWorkers).toArray();

for (int w : allWorkers) {
for (int t : allTasks) {
if (costs[w][t] != 0) {
}
}
}

LinearSumAssignment.Status status = assignment.solve();

if (status == LinearSumAssignment.Status.OPTIMAL) {
System.out.println("Total cost: " + assignment.getOptimalCost());
for (int worker : allWorkers) {
System.out.println("Worker " + worker + " assigned to task "
+ assignment.getRightMate(worker) + ". Cost: " + assignment.getAssignmentCost(worker));
}
} else {
System.out.println("Solving the min cost flow problem failed.");
System.out.println("Solver status: " + status);
}
}

private AssignmentLinearSumAssignment() {}
}```

### C#

```using System;
using System.Collections.Generic;
using System.Linq;

public class AssignmentLinearSumAssignment
{
static void Main()
{
LinearSumAssignment assignment = new LinearSumAssignment();

int[,] costs = {
{ 90, 76, 75, 70 },
{ 35, 85, 55, 65 },
{ 125, 95, 90, 105 },
{ 45, 110, 95, 115 },
};
int numWorkers = 4;
int[] allWorkers = Enumerable.Range(0, numWorkers).ToArray();

foreach (int w in allWorkers)
{
{
if (costs[w, t] != 0)
{
}
}
}

LinearSumAssignment.Status status = assignment.Solve();

if (status == LinearSumAssignment.Status.OPTIMAL)
{
Console.WriteLine(\$"Total cost: {assignment.OptimalCost()}.");
foreach (int worker in allWorkers)
{
Console.WriteLine(\$"Worker {worker} assigned to task {assignment.RightMate(worker)}. " +
\$"Cost: {assignment.AssignmentCost(worker)}.");
}
}
else
{
Console.WriteLine("Solving the linear assignment problem failed.");
Console.WriteLine(\$"Solver status: {status}.");
}
}
}```

### Solution when workers can't perform all tasks

In the previous example, we assumed that all workers can perform all tasks. But this not always the case - a worker might be unable to perform one or more tasks for various reasons. However, it is easy to modify the program above to handle this.

As an example, suppose that worker 0 is unable to perform task 3. To modify the program to take this into account, make the following changes:

1. Change the 0, 3 entry of the cost matrix to the string `'NA'`. (Any string will work.)
```cost = [[90, 76, 75, 'NA'],
[35, 85, 55, 65],
[125, 95, 90, 105],
[45, 110, 95, 115]]```
2. In the section of the code that assigns costs to the solver, add the line `if cost[worker][task] != 'NA':`, as shown below.
```for worker in range(0, rows):
The added line prevents any edge whose entry in the cost matrix is `'NA'` from being added to the solver.

After making these changes and running the modified code, you see the following output:

```Total cost = 276
Worker 0 assigned to task 1.  Cost = 76
Worker 1 assigned to task 3.  Cost = 65
Worker 2 assigned to task 2.  Cost = 90
Worker 3 assigned to task 0.  Cost = 45```

Notice that the total cost is higher now than it was for the original problem. This is not surprising, since in the original problem the optimal solution assigned worker 0 to task 3, while in the modified problem that assignment is not allowed.

To see what happens if more workers are unable to perform tasks, you can replace more entries of the cost matrix with `'NA'`, to denote additional workers who can't perform certain tasks:

```cost = [[90, 76, 'NA', 'NA'],
[35, 85, 'NA', 'NA'],
[125, 95, 'NA','NA'],
[45, 110, 95, 115]]```

When you run the program this time, you get a negative result:

`No assignment is possible.`

This means there is no way to assign workers to tasks so that each worker performs a different task. You can see why this is so by looking at the graph for the problem (in which there are no edges corresponding to values of `'NA'` in the cost matrix).

Since the nodes for the three workers 0, 1, and 2 are only connected to the two nodes for tasks 0 and 1, it not possible to assign distinct tasks to these workers.

### The Marriage Theorem

There is a well-known result in graph theory, called The Marriage Theorem, which tells us exactly when you can assign each node on the left to a distinct node on the right, in a bipartite graph like the one above. Such an assignment is called a perfect matching. In a nutshell, the theorem says this is possible if there is no subset of nodes on the left (like the one in the previous example ) whose edges lead to a smaller set of nodes on the right.

More precisely, the theorem says that a bipartite graph has a perfect matching if and only if for any subset S of the nodes on the left side of the graph, the set of nodes on the right side of the graph that are connected by an edge to a node in S is at least as large as S.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]