pythonhungarian-algorithm

print_matrix of munkres library python throws an exception on matrix containing zeroes


Lowest cost through this matrix:
Traceback (most recent call last):
    File "muncre.py", line 8, in <module>
        print_matrix(matrix, msg='Lowest cost through this matrix:')
    File "/usr/lib/python2.7/dist-packages/munkres.py", line 730, in    print_matrix
        width = max(width, int(math.log10(val)) + 1)
ValueError: math domain error

When the matrix is containing zero in any of the rows, the above error is thrown. How can I fix it?

This is the piece of code in python:

from munkres import Munkres, print_matrix
matrix = [[6, 9, 1],
          [10, 9, 2],
          [0,8,7]]
m = Munkres()
indexes = m.compute(matrix)
print_matrix(matrix, msg='Lowest cost through this matrix:')
total = 0
for row, column in indexes:
    value = matrix[row][column]
    total += value
    print '(%d, %d) -> %d' % (row, column, value)
print 'total cost: %d' % total

I installed the library munkres using the following command in Ubuntu:

sudo apt-get install python-munkres


Solution

  • This really looks like a bug with the munkres library. The print_matrix is just a "convenience" function and I'd suggest filing a bug report and in the interim just replacing it with something like the following (which is just their code with a fix to avoid trying to apply 0 or negative numbers to the logarithm). What there were trying to do is make it properly space each column to be the largest width for a number. Note that if you pass in negative numbers, this may have an off by 1 issue, but on the other hand, if you have negative costs, you may have bigger issues.

    def print_matrix(matrix, msg=None):
        """
        Convenience function: Displays the contents of a matrix of integers.
        :Parameters:
            matrix : list of lists
                Matrix to print
            msg : str
                Optional message to print before displaying the matrix
        """
        import math
    
        if msg is not None:
            print(msg)
    
        # Calculate the appropriate format width.
        width = 1
        for row in matrix:
            for val in row:
                if abs(val) > 1:
                   width = max(width, int(math.log10(abs(val))) + 1)
    
        # Make the format string
        format = '%%%dd' % width
    
        # Print the matrix
        for row in matrix:
            sep = '['
            for val in row:
                sys.stdout.write(sep + format % val)
                sep = ', '
            sys.stdout.write(']\n')