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
当矩阵在任何行中包含零时,将引发上述错误。我该如何解决?
这是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
我在 Ubuntu 中使用以下命令安装了库 munkres:
sudo apt-get install python-munkres