Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Assignment Problem, a NumPy function?

Since an assignment problem can be posed in the form of a single matrix, I am wondering if NumPy has a function to solve such a matrix. So far I have found none. Maybe one of you guys know if NumPy/SciPy has an assignment-problem-solve function?

Edit: In the meanwhile I have found a Python (not NumPy/SciPy) implementation at http://software.clapper.org/munkres/. Still I suppose a NumPy/SciPy implementation could be much faster, right?

like image 782
Paul Avatar asked Sep 09 '09 10:09

Paul


People also ask

What is assignment problem with example?

For example, suppose an accounts officer has 4 subordinates and 4 tasks. The subordinates differ in efficiency and take different time to perform each task. If one task is to be assigned to one person in such a way that the total person hours are minimised, the problem is called an assignment problem.

What is linear_ sum_ assignment?

linear_sum_assignment() Solve the linear sum assignment problem. Parameters cost_matrixarray. The cost matrix of the bipartite graph. maximizebool (default: False)

What do you mean by Hungarian method of assignment?

The Hungarian method is defined as a combinatorial optimization technique that solves the assignment problems in polynomial time and foreshadowed subsequent primal–dual approaches.


2 Answers

There is now a numpy implementation of the munkres algorithm in scikit-learn under sklearn/utils/linear_assignment_.py its only dependency is numpy. I tried it with some approximately 20x20 matrices, and it seems to be about 4 times as fast as the one linked to in the question. cProfiler shows 2.517 seconds vs 9.821 seconds for 100 iterations.

like image 174
Sean Johnson Avatar answered Oct 04 '22 08:10

Sean Johnson


I was hoping that the newer scipy.optimize.linear_sum_assignment would be fastest, but (perhaps not surprisingly) the Cython library (which does not have pip support) is significantly faster, at least for my use case:

UPDATE: using munkres v1.1.2 and scipy v1.5.0 achieves the following results:

$ python -m timeit -s "from scipy.optimize import linear_sum_assignment; import numpy as np; np.random.seed(0); c = np.random.rand(20,30)" "a,b = linear_sum_assignment(c)"
10000 loops, best of 5: 32.8 usec per loop
$ python -m timeit -s "from munkres import Munkres; import numpy as np;  np.random.seed(0); c = np.random.rand(20,30); m = Munkres()" "a = m.compute(c)"
100 loops, best of 5: 2.41 msec per loop
$ python -m timeit -s "from scipy.optimize import linear_sum_assignment; import numpy as np; np.random.seed(0);" "c = np.random.rand(20,30); a,b = linear_sum_assignment(c)"
5000 loops, best of 5: 51.7 usec per loop
$ python -m timeit -s "from munkres import Munkres; import numpy as np;  np.random.seed(0)" "c = np.random.rand(20,30); m = Munkres(); a = m.compute(c)"
10 loops, best of : 26 msec per loop
like image 23
Matthew Avatar answered Oct 04 '22 07:10

Matthew