解决的问题
在PYTHON中该函数是一种匈牙利算法的实现,最终得到最小权值,计算速度比全排列的方法快了超级多。
在使得权值最小的情况下,处理最优匹配的问题。
使用方法
from scipy.optimize import linear_sum_assignment
from numpy import random
rd = random.RandomState(10000)
task_matrix = rd.randint(0, 100, size=(8,9))
print('cost matrix = ', '\n', task_matrix)
row_ind, col_ind = linear_sum_assignment(task_matrix)
min_cost = task_matrix[row_ind, col_ind].sum()
best_solution = list(task_matrix[row_ind, col_ind])
print('min cost = ', min_cost)
print('best solution = ',best_solution)
结果展示:
cost matrix =
[[12 53 31 40 47 2 21 72 61]
[17 70 72 85 54 39 93 34 62]
[75 51 76 14 15 7 72 43 95]
[41 74 34 26 44 35 77 30 6]
[77 96 58 91 46 88 4 93 78]
[79 82 85 80 60 55 40 86 68]
[27 78 15 27 39 13 1 71 72]
[ 7 47 41 81 47 10 13 3 14]]
min cost = 121
best solution = [2, 17, 14, 6, 4, 60, 15, 3]