我的问题是关于最大重量B匹配问题。
二部匹配问题对二部图中的两组顶点。最大加权二部匹配 (MWM)被定义为匹配中边值之和有一个最大值的匹配。一种著名的MWM多项式时间算法是匈牙利算法。
我感兴趣的是一个特殊的最大加权二部匹配问题,称为权值二部匹配问题。一个加权二分B匹配问题(WBM)寻求匹配顶点,使每个顶点与其容量b
允许的顶点不匹配。
这个数字(来自Chen等人)显示了WBM问题。输入图的得分为2.2,它的所有边权之和。在满足红色度约束的所有子图中,解H的蓝色边的得分最高,为1.6。
虽然最近有一些关于WBM问题(这和这)的工作,但我找不到算法的任何实现。有没有人知道像networkX这样的库中是否已经存在WBM问题?
发布于 2018-06-20 18:16:57
让我们试着一步一步地完成这一步,编写我们自己的函数来解决问题中指定的WBM问题。
当给定两个节点集(u和v,边权和顶点容量)时,用pulp
表示和求解加权二分匹配(WBM)并不是太困难。
在下面的步骤2中,您将找到一个(希望很容易理解)的函数来将WBM描述为一个ILP,并使用pulp.
进行求解,看看它是否有用。(你需要pip install pulp
)
步骤1:建立二分图容量和边权
import networkx as nx
from pulp import *
import matplotlib.pyplot as plt
from_nodes = [1, 2, 3]
to_nodes = [1, 2, 3, 4]
ucap = {1: 1, 2: 2, 3: 2} #u node capacities
vcap = {1: 1, 2: 1, 3: 1, 4: 1} #v node capacities
wts = {(1, 1): 0.5, (1, 3): 0.3,
(2, 1): 0.4, (2, 4): 0.1,
(3, 2): 0.7, (3, 4): 0.2}
#just a convenience function to generate a dict of dicts
def create_wt_doubledict(from_nodes, to_nodes):
wt = {}
for u in from_nodes:
wt[u] = {}
for v in to_nodes:
wt[u][v] = 0
for k,val in wts.items():
u,v = k[0], k[1]
wt[u][v] = val
return(wt)
第二步:求解WBM (以整数形式表示)
下面是一些描述,以使下面的代码更容易理解:
puLP
。
def solve_wbm(from_nodes, to_nodes, wt):
''' A wrapper function that uses pulp to formulate and solve a WBM'''
prob = LpProblem("WBM Problem", LpMaximize)
# Create The Decision variables
choices = LpVariable.dicts("e",(from_nodes, to_nodes), 0, 1, LpInteger)
# Add the objective function
prob += lpSum([wt[u][v] * choices[u][v]
for u in from_nodes
for v in to_nodes]), "Total weights of selected edges"
# Constraint set ensuring that the total from/to each node
# is less than its capacity
for u in from_nodes:
for v in to_nodes:
prob += lpSum([choices[u][v] for v in to_nodes]) <= ucap[u], ""
prob += lpSum([choices[u][v] for u in from_nodes]) <= vcap[v], ""
# The problem data is written to an .lp file
prob.writeLP("WBM.lp")
# The problem is solved using PuLP's choice of Solver
prob.solve()
# The status of the solution is printed to the screen
print( "Status:", LpStatus[prob.status])
return(prob)
def print_solution(prob):
# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
if v.varValue > 1e-3:
print(f'{v.name} = {v.varValue}')
print(f"Sum of wts of selected edges = {round(value(prob.objective), 4)}")
def get_selected_edges(prob):
selected_from = [v.name.split("_")[1] for v in prob.variables() if v.value() > 1e-3]
selected_to = [v.name.split("_")[2] for v in prob.variables() if v.value() > 1e-3]
selected_edges = []
for su, sv in list(zip(selected_from, selected_to)):
selected_edges.append((su, sv))
return(selected_edges)
步骤3:指定图并调用WBM求解器
wt = create_wt_doubledict(from_nodes, to_nodes)
p = solve_wbm(from_nodes, to_nodes, wt)
print_solution(p)
这意味着:
Status: Optimal
e_1_3 = 1.0
e_2_1 = 1.0
e_3_2 = 1.0
e_3_4 = 1.0
Sum of wts of selected edges = 1.6
步骤4:可以选择使用Networkx绘制图形
selected_edges = get_selected_edges(p)
#Create a Networkx graph. Use colors from the WBM solution above (selected_edges)
graph = nx.Graph()
colors = []
for u in from_nodes:
for v in to_nodes:
edgecolor = 'blue' if (str(u), str(v)) in selected_edges else 'gray'
if wt[u][v] > 0:
graph.add_edge('u_'+ str(u), 'v_' + str(v))
colors.append(edgecolor)
def get_bipartite_positions(graph):
pos = {}
for i, n in enumerate(graph.nodes()):
x = 0 if 'u' in n else 1 #u:0, v:1
pos[n] = (x,i)
return(pos)
pos = get_bipartite_positions(graph)
nx.draw_networkx(graph, pos, with_labels=True, edge_color=colors,
font_size=20, alpha=0.5, width=3)
plt.axis('off')
plt.show()
print("done")
蓝色的边缘是那些被选中的WBM。希望这能帮你继续前进。
https://stackoverflow.com/questions/50908267
复制