python 您所在的位置:网站首页 vm与km的求取方法 python

python

2024-01-27 14:35| 来源: 网络整理| 查看: 265

基础概念

关于匈牙利算法的基础概念就不作具体描述了,不清楚的可以自己搜索相关知识 主要需要了解的知识点

二分图匹配:最大匹配,完美匹配路径:交错路径,增广路径

算法核心:通过不断寻找增广路径找到最大匹配的道路

算法实现 1. 使用线性规划库scipy

默认取最小组合,设置maximize为True时取最大组合

import numpy as np from scipy.optimize import linear_sum_assignment a = np.array([[84, 65, 3, 34], [65, 56, 23, 35], [63, 18, 35, 12]]) row, col = linear_sum_assignment(a) print("行坐标:", row, "列坐标:", col, "最小组合:", a[row, col]) row, col = linear_sum_assignment(a, True) print("行坐标:", row, "列坐标:", col, "最大组合:", a[row, col])

输出

行坐标: [0 1 2] 列坐标: [2 3 1] 最小组合: [ 3 35 18] 行坐标: [0 1 2] 列坐标: [0 1 2] 最大组合: [84 56 35] 2. 使用munkres库

源码:https://github.com/bmc/munkres 文档:http://software.clapper.org/munkres/

目前该库已经可以使用pip install munkres安装

默认是取最小组合,需要取最大组合则使用make_cost_matrix转换数据矩阵

import numpy as np from munkres import Munkres, make_cost_matrix, DISALLOWED a = np.array([[84, 65, 3, 34], [65, 56, 23, 35], [63, 18, 35, 12]]) b = make_cost_matrix(a, lambda cost: (a.max() - cost) if (cost != DISALLOWED) else DISALLOWED) mk = Munkres() # 最小组合 indexes = mk.compute(a.copy()) # 会改变输入的源数据 print("最小组合:",indexes, a[[i[0] for i in indexes], [i[1] for i in indexes]]) # 最大组合 indexes = mk.compute(b) print("最大组合:", indexes, a[[i[0] for i in indexes], [i[1] for i in indexes]])

输出

最小组合:[(0, 2), (1, 3), (2, 1)] [ 3 35 18] 最大组合:[(0, 0), (1, 1), (2, 2)] [84 56 35]

注意使用np.array输入,mk.compute会改变输入的源数据

3. KM算法python实现

基本思想:通过引入顶标,将最优权值匹配转化为最大匹配问题 参考: https://blog.csdn.net/u010510549/article/details/91350549 https://www.cnblogs.com/fzl194/p/8848061.html

实现了矩阵的自动补0和最大最小组合计算

import numpy as np class KM: def __init__(self): self.matrix = None self.max_weight = 0 self.row, self.col = 0, 0 # 源数据行列 self.size = 0 # 方阵大小 self.lx = None # 左侧权值 self.ly = None # 右侧权值 self.match = None # 匹配结果 self.slack = None # 边权和顶标最小的差值 self.visx = None # 左侧是否加入增广路 self.visy = None # 右侧是否加入增广路 # 调整数据 def pad_matrix(self, min): if min: max = self.matrix.max() + 1 self.matrix = max-self.matrix if self.row > self.col: # 行大于列,添加列 self.matrix = np.c_[self.matrix, np.array([[0] * (self.row - self.col)] * self.row)] elif self.col > self.row: # 列大于行,添加行 self.matrix = np.r_[self.matrix, np.array([[0] * self.col] * (self.col - self.row))] def reset_slack(self): self.slack.fill(self.max_weight + 1) def reset_vis(self): self.visx.fill(False) self.visy.fill(False) def find_path(self, x): self.visx[x] = True for y in range(self.size): if self.visy[y]: continue tmp_delta = self.lx[x] + self.ly[y] - self.matrix[x][y] if tmp_delta == 0: self.visy[y] = True if self.match[y] == -1 or self.find_path(self.match[y]): self.match[y] = x return True elif self.slack[y] > tmp_delta: self.slack[y] = tmp_delta return False def km_cal(self): for x in range(self.size): self.reset_slack() while True: self.reset_vis() if self.find_path(x): break else: # update slack delta = self.slack[~self.visy].min() self.lx[self.visx] -= delta self.ly[self.visy] += delta self.slack[~self.visy] -= delta def compute(self, datas, min=False): """ :param datas: 权值矩阵 :param min: 是否取最小组合,默认最大组合 :return: 输出行对应的结果位置 """ self.matrix = np.array(datas) if not isinstance(datas, np.ndarray) else datas self.max_weight = self.matrix.sum() self.row, self.col = self.matrix.shape # 源数据行列 self.size = max(self.row, self.col) self.pad_matrix(min) print(self.matrix) self.lx = self.matrix.max(1) self.ly = np.array([0] * self.size, dtype=int) self.match = np.array([-1] * self.size, dtype=int) self.slack = np.array([0] * self.size, dtype=int) self.visx = np.array([False] * self.size, dtype=bool) self.visy = np.array([False] * self.size, dtype=bool) self.km_cal() match = [i[0] for i in sorted(enumerate(self.match), key=lambda x: x[1])] result = [] for i in range(self.row): result.append((i, match[i] if match[i]


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有