本文整理汇总了Python中munkres.Munkres.make_cost_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python Munkres.make_cost_matrix方法的具体用法?Python Munkres.make_cost_matrix怎么用?Python Munkres.make_cost_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类munkres.Munkres
的用法示例。
在下文中一共展示了Munkres.make_cost_matrix方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WGRAP
# 需要导入模块: from munkres import Munkres [as 别名]
# 或者: from munkres.Munkres import make_cost_matrix [as 别名]
#.........这里部分代码省略.........
def curr_rev_group(self, paper):
return np.nonzero(self.curr_assignment[:,paper])[0]
def marg_gain_for_rev_pap(self, rev, paper):
g1 = self.curr_rev_group(paper)
assert rev not in set(g1)
if not any(g1):
g2 = [rev]
else:
g2 = []
g2.extend(g1)
g2.append(rev)
return self.marginal_gain(g1, g2, paper)
def marg_gain_for_rev(self, rev):
mg = []
for pap in range(np.size(self.pap_mat, axis=0)):
if self.curr_assignment[rev, pap] == 1.0:
mg.append(-1.0) # should never reassign a reviewer to the same paper
else:
mg.append(self.marg_gain_for_rev_pap(rev, pap))
return mg
def curr_n_revs(self, rev):
return np.sum(self.curr_assignment[rev,:])
def _construct_matching_mat(self, post_refine=False):
rows = []
rows_to_revs = {}
for rev in range(np.size(self.rev_mat, axis=0)):
if post_refine:
n_rows = int(self.alpha - self.curr_n_revs(rev))
else:
n_rows = int(np.min((self.alpha - self.curr_n_revs(rev), self.nrevs_per_round)))
if n_rows > 0:
rev_marg_gain = self.marg_gain_for_rev(rev)
for row in range(n_rows):
rows_to_revs[len(rows)] = rev
rows.append(rev_marg_gain[:])
return rows, rows_to_revs
def refine(self, R=0.01, l=0.5, itr=0, show=False):
"""
Implements 1 iteration of stochastic refinement
"""
for pap in range(np.size(self.pap_mat, axis=0)):
assigned_revs = np.nonzero(self.curr_assignment[:,pap])[0]
assert len(assigned_revs) > 0
rev_scores = []
for rev in assigned_revs:
assert self.curr_assignment[rev,pap] == 1.0
r_score = np.exp(-l * itr) * \
self.score_mat[rev, pap] / np.sum(self.score_mat[rev,:])
r_score = np.max((r_score, 1.0 / R))
rev_scores.append(r_score)
rev_scores = np.array(rev_scores)
inv_rev_scores = np.sum(rev_scores) - rev_scores
normed_inv_rev_scores = inv_rev_scores / np.sum(inv_rev_scores)
sample = np.nonzero(np.random.multinomial(1, normed_inv_rev_scores))[0]
if show:
print normed_rev_scores
print sample
rev_to_remove = assigned_revs[sample]
assert self.curr_assignment[rev_to_remove, pap] == 1.0
self.curr_assignment[rev_to_remove, pap] = 0.0
def _solve_assignment_and_update(self, rows, rows_to_revs, max_val = 10.0, show=False):
"""
Implements 1 iteration of stagewise deepening (no stochastic refinement)
"""
if show:
print rows
cost_matrix = self.munkres.make_cost_matrix(rows, lambda v: max_val - v)
#indexes = self.munkres.compute(cost_matrix)
row_inds, col_inds = linear_sum_assignment(np.array(cost_matrix))
if show:
print cost_matrix
# for row, col in indexes:
for row, col in zip(row_inds, col_inds):
self.curr_assignment[rows_to_revs[row],col] = 1
if show:
value = rows[row][col]
print '(%d, %d) -> %f' % (row, col, value)
def solve(self):
for i in range(self.beta):
print "ITERATION %d" % i
rows, rows_to_revs = self._construct_matching_mat()
print "MATRIX CONSTRUCTED"
self._solve_assignment_and_update(rows, rows_to_revs)
return self.curr_assignment
def score_assignment(self):
total = 0.0
for pap in range(np.size(self.pap_mat, axis=0)):
assigned_revs = np.nonzero(self.curr_assignment[:,pap])[0]
total += self.group_score(assigned_revs, pap)
return total