def _utilities2_pairwise_breaking(qfx2_utilities): print('[vote] building pairwise matrix') hstack = np.hstack cartesian = utool.cartesian tnxs = [util[1] for utils in qfx2_utilities for util in utils] altx2_tnx = utool.unique_ordered2(tnxs) tnx2_altx = {nid: altx for altx, nid in enumerate(altx2_tnx)} nUtilities = len(qfx2_utilities) nAlts = len(altx2_tnx) altxs = np.arange(nAlts) pairwise_mat = np.zeros((nAlts, nAlts)) qfx2_porder = [ np.array([tnx2_altx[util[1]] for util in utils]) for utils in qfx2_utilities ] def sum_win(ij): """ pairiwse wins on off-diagonal """ pairwise_mat[ij[0], ij[1]] += 1 def sum_loss(ij): """ pairiwse wins on off-diagonal """ pairwise_mat[ij[1], ij[1]] -= 1 nVoters = 0 for qfx in range(nUtilities): # partial and compliment order over alternatives porder = utool.unique_ordered2(qfx2_porder[qfx]) nReport = len(porder) if nReport == 0: continue #sys.stdout.write('.') corder = np.setdiff1d(altxs, porder) # pairwise winners and losers pw_winners = [porder[r:r + 1] for r in range(nReport)] pw_losers = [hstack((corder, porder[r + 1:])) for r in range(nReport)] pw_iter = zip(pw_winners, pw_losers) pw_votes_ = [cartesian((winner, losers)) for winner, losers in pw_iter] pw_votes = np.vstack(pw_votes_) #pw_votes = [(w,l) for votes in pw_votes_ for w,l in votes if w != l] list(map(sum_win, iter(pw_votes))) list(map(sum_loss, iter(pw_votes))) nVoters += 1 #print('') PLmatrix = pairwise_mat / nVoters # sum(0) gives you the sum over rows, which is summing each column # Basically a column stochastic matrix should have # M.sum(0) = 0 #print('CheckMat = %r ' % all(np.abs(PLmatrix.sum(0)) < 1E-9)) return PLmatrix, altx2_tnx
def _utilities2_pairwise_breaking(qfx2_utilities): print('[vote] building pairwise matrix') hstack = np.hstack cartesian = utool.cartesian tnxs = [util[1] for utils in qfx2_utilities for util in utils] altx2_tnx = utool.unique_ordered2(tnxs) tnx2_altx = {nid: altx for altx, nid in enumerate(altx2_tnx)} nUtilities = len(qfx2_utilities) nAlts = len(altx2_tnx) altxs = np.arange(nAlts) pairwise_mat = np.zeros((nAlts, nAlts)) qfx2_porder = [np.array([tnx2_altx[util[1]] for util in utils]) for utils in qfx2_utilities] def sum_win(ij): """ pairiwse wins on off-diagonal """ pairwise_mat[ij[0], ij[1]] += 1 def sum_loss(ij): """ pairiwse wins on off-diagonal """ pairwise_mat[ij[1], ij[1]] -= 1 nVoters = 0 for qfx in range(nUtilities): # partial and compliment order over alternatives porder = utool.unique_ordered2(qfx2_porder[qfx]) nReport = len(porder) if nReport == 0: continue #sys.stdout.write('.') corder = np.setdiff1d(altxs, porder) # pairwise winners and losers pw_winners = [porder[r:r + 1] for r in range(nReport)] pw_losers = [hstack((corder, porder[r + 1:])) for r in range(nReport)] pw_iter = zip(pw_winners, pw_losers) pw_votes_ = [cartesian((winner, losers)) for winner, losers in pw_iter] pw_votes = np.vstack(pw_votes_) #pw_votes = [(w,l) for votes in pw_votes_ for w,l in votes if w != l] list(map(sum_win, iter(pw_votes))) list(map(sum_loss, iter(pw_votes))) nVoters += 1 #print('') PLmatrix = pairwise_mat / nVoters # sum(0) gives you the sum over rows, which is summing each column # Basically a column stochastic matrix should have # M.sum(0) = 0 #print('CheckMat = %r ' % all(np.abs(PLmatrix.sum(0)) < 1E-9)) return PLmatrix, altx2_tnx
def _get_alts_from_utilities(qfx2_utilities): """ get temp name indexes """ tnxs = [utool[1] for utils in qfx2_utilities for utool in utils] altx2_tnx = utool.unique_ordered2(tnxs) tnx2_altx = {nid: altx for altx, nid in enumerate(altx2_tnx)} nUtilities = len(qfx2_utilities) nAlts = len(altx2_tnx) altxs = np.arange(nAlts) return tnxs, altx2_tnx, tnx2_altx, nUtilities, nAlts, altxs