简体   繁体   English

Munkres Python Package 未生成最大总数

[英]Munkres Python Package not generating maximum total

I am currently writing an algorithm to match users by compatibility score.我目前正在编写一个算法来通过兼容性分数匹配用户。 So each user has a compatibility score with each of the other users.因此,每个用户与其他每个用户都有一个兼容性分数。 The goal is to maximize the total compatibility score.目标是最大化总兼容性分数。 I am using the munkres python package to get the indices and then filtering the duplicates.我正在使用munkres python package 来获取索引,然后过滤重复项。 (ie if user1 matches with user2, we remove the match with user2 and user1). (即如果 user1 与 user2 匹配,我们删除与 user2 和 user1 的匹配)。

Here is an example matrix: (DISALLOWED in place where a user cannot be matched with themselves.)这是一个示例矩阵:(在用户无法与自己匹配的地方禁止使用。)

matrix = [[DISALLOWED, 2, 5, 7, 13, 10],
         [2, DISALLOWED, 25, 13, 5, 14],
         [5, 25, DISALLOWED, 21, 100, 17],
         [7, 13, 21, DISALLOWED, 70, 2],
         [13, 5, 100, 70, DISALLOWED, 200],
         [10, 14, 17, 2, 200, DISALLOWED]]

Here is what I have tried.这是我尝试过的。 It works for this smaller matrix example I gave but is not giving the optimal score for the larger dataset.它适用于我给出的这个较小的矩阵示例,但没有给出较大数据集的最佳分数。 I know the total score is not optimal as a non-optimized algorithm (simply sorting the scores and matching each user in order) is generating a much higher score.我知道总分不是最优的,因为非优化算法(简单地对分数进行排序并按顺序匹配每个用户)会产生更高的分数。

def optimize(matrix):
    optimal_matrix = make_cost_matrix(matrix, lambda cost: (sys.maxsize - cost) if
                                    (cost != DISALLOWED) else DISALLOWED)

    m = Munkres()
    indexes = m.compute(optimal_matrix)
    
    matches = []
    for row, column in indexes:
        value = matrix[row][column]
        match = [row, column, value]
        matches.append(match)

    matches.sort(key=thirdValue, reverse=True)

    seen = []
    total = 0
    for match in matches:

        if match[0] not in seen and match[1] not in seen:
            total += match[2]
            seen.append(match[0])
            seen.append(match[1])
    print(total)

Is there a certain matrix that would break my code?是否有某个矩阵会破坏我的代码? Any help or pointers would be appreciated.任何帮助或指示将不胜感激。

The Hungarian Algorithm is for "bipartite" graphs, but you don't have a bipartite graph.匈牙利算法适用于“二分”图,但您没有二分图。 A bipartite graph is one in which you have two distinct sets of nodes.二分图是其中您有两组不同的节点的图。 For example: assigning workers to jobs.例如:为工人分配工作。 Nodes within a set do not connect to each other;集合内的节点不相互连接; a worker can be assigned to a job, or a job to a worker, but workers are not assigned to workers nor jobs assigned to jobs.可以将工人分配给工作,也可以将工作分配给工人,但工人既没有分配给工人,也没有分配给工作。 In your case you are trying to assign users to other users .在您的情况下,您正在尝试将用户分配给其他用户 That is, you only have one set, not two.也就是说,您只有一套,而不是两套。

By marking the diagonal DISALLOWED you are preventing a user from being matched with themselves, but you aren't preventing a user from being matched with more than one other user.通过将对角线标记为DISALLOWED ,您可以阻止用户与他们自己匹配,但您不会阻止用户与多个其他用户匹配。 For example, with only four users who all have the same compatibility:例如,只有四个具有相同兼容性的用户:

A一个 B C C D D
A一个 0 0 0 0 0 0
B 0 0 0 0 0 0
C C 0 0 0 0 0 0
D D 0 0 0 0 0 0

A matched with C A与C配套
B matched with D B 与 D 匹配
C matched with B C 配B
D matched with A D 与 A 匹配

is a valid result for the Hungarian algorithm, but is not valid for your situation.是匈牙利算法的有效结果,但不适用于您的情况。 I don't know that this is what's happening, but it could happen, so you need to use a different algorithm.我不知道这是正在发生的事情,但它可能会发生,所以你需要使用不同的算法。

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

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