简体   繁体   English

Scipy 的 linear_sum_assignment 给出不正确的结果

[英]Scipy's linear_sum_assignment giving incorrect result

When I tried using scipy.optimize.linear_sum_assignment as shown, it gives the assignment vector [0 2 3 1] with a total cost of 15.当我如图所示尝试使用scipy.optimize.linear_sum_assignment时,它给出了总成本为 15 的分配向量[0 2 3 1]

However, from the cost matrix c , you can see that for the second task, the 5th agent has a cost of 1 .但是,从成本矩阵c可以看出,对于第二个任务,第 5 个代理的成本为1 So the expected assignment should be [0 3 None 2 1] (total cost of 9)所以预期的分配应该是[0 3 None 2 1] (总成本 9)

Why is linear_sum_assignment not returning the optimal assignments?为什么linear_sum_assignment没有返回最佳分配?

from scipy.optimize import linear_sum_assignment

c = [
    [1, 5, 9, 5],
    [5, 8, 3, 2],
    [3, 2, 6, 8],
    [7, 3, 5, 4],
    [2, 1, 9, 9],
]

results = linear_sum_assignment(c)
print(results[1]) # [0 2 3 1]

linear_sum_assignment returns a tuple of two arrays. These are the row indices and column indices of the assigned values. linear_sum_assignment返回两个 arrays 的元组。这些是分配值的行索引和列索引。 For your example (with c converted to a numpy array):对于您的示例(将c转换为 numpy 数组):

In [51]: c
Out[51]: 
array([[1, 5, 9, 5],
       [5, 8, 3, 2],
       [3, 2, 6, 8],
       [7, 3, 5, 4],
       [2, 1, 9, 9]])

In [52]: row, col = linear_sum_assignment(c)

In [53]: row
Out[53]: array([0, 1, 3, 4])

In [54]: col
Out[54]: array([0, 2, 3, 1])

The corresponding index pairs from row and col give the selected entries.来自rowcol的相应索引对给出了选定的条目。 That is, the indices of the selected entries are (0, 0), (1, 2), (3, 3) and (4, 1).即,所选条目的索引为 (0, 0)、(1, 2)、(3, 3) 和 (4, 1)。 It is these pairs that are the "assignments".正是这些对是“任务”。

The sum associated with this assignment is 9:与此分配相关的总和为 9:

In [55]: c[row, col].sum()
Out[55]: 9

In the original version of the question (but since edited), it looks like you wanted to know the row index for each column, so you expected [0, 4, 1, 3].在问题的原始版本中(但经过编辑),看起来您想知道每一列的行索引,因此您期望 [0, 4, 1, 3]。 The values that you want are in row , but the order is not what you expect, because the indices in col are not simply [0, 1, 2, 3].您想要的值在row中,但顺序不是您期望的,因为col中的索引不仅仅是 [0, 1, 2, 3]。 To get the result in the form that you expected, you have to reorder the values in row based on the order of the indices in col .要以您期望的形式获得结果,您必须根据col中索引的顺序对row中的值重新排序。 Here are two ways to do that.这里有两种方法可以做到这一点。

First:第一的:

In [56]: result = np.zeros(4, dtype=int)

In [57]: result[col] = row

In [58]: result
Out[58]: array([0, 4, 1, 3])

Second:第二:

In [59]: result = row[np.argsort(col)]

In [60]: result
Out[60]: array([0, 4, 1, 3])

Note that the example in the linear_sum_assignment docstring is potentially misleading;请注意, linear_sum_assignment文档字符串中的示例可能具有误导性; because it displays only col_ind in the python session, it gives the impression that col_ind is "the answer".因为它只显示col_ind中的 col_ind,所以给人的印象是col_ind就是“答案”。 In general, however, the answer involves both of the returned arrays.然而,一般来说,答案涉及返回的两个 arrays。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM