def test_invalid(): with pytest.raises(TypeError): election = [[0, 1], [1, 0]] condorcet(election, 'random') with pytest.raises(ValueError): condorcet_from_matrix(np.array([[0, 1]])) with pytest.raises(ValueError): condorcet(np.array([[0], [1]]))
} start_time = time.monotonic() for iteration in range(n): for n_cands in n_cands_list: v, c = normal_electorate(n_voters, n_cands, dims=D, corr=corr, disp=disp) utilities = normed_dist_utilities(v, c) rankings = honest_rankings(utilities) # If there is a Condorcet winner, analyze election, otherwise skip # it CW = condorcet(rankings) if CW is not None: count['CW'][n_cands] += 1 for name, method in ranked_methods.items(): if method(rankings, tiebreaker='random') == CW: count[name][n_cands] += 1 for name, method in rated_methods.items(): if method(utilities, tiebreaker='random') == CW: count[name][n_cands] += 1 elapsed_time = time.monotonic() - start_time print('Elapsed:', time.strftime("%H:%M:%S", time.gmtime(elapsed_time)), '\n')
101: 8.690, 201: 8.732, 301: 8.746, 401: 8.753, 501: 8.757, 601: 8.760 } # It needs many iterations to get similar accuracy as the analytical results iterations = 50_000 n_cands = 3 is_CP = Counter() # Is there a Condorcet paradox? for n_voters in WP_table: for iteration in range(iterations): election = impartial_culture(n_voters, n_cands) CW = condorcet(election) if CW is None: is_CP[n_voters] += 1 x = list(WP_table.keys()) y = list(WP_table.values()) plt.plot(x, y, label='WP') x = list(is_CP.keys()) y = list(is_CP.values()) y = np.asarray(y) / iterations * 100 # Percent likelihood of paradox plt.plot(x, y, '-', label='Simulation') plt.legend() plt.grid(True, color='0.7', linestyle='-', which='major', axis='both') plt.grid(True, color='0.9', linestyle='-', which='minor', axis='both')
def test_condorcet(): # Standard Tennessee example # https://en.wikipedia.org/wiki/Template:Tenn_voting_example Memphis, Nashville, Chattanooga, Knoxville = 0, 1, 2, 3 election = [ *42 * [[Memphis, Nashville, Chattanooga, Knoxville]], *26 * [[Nashville, Chattanooga, Knoxville, Memphis]], *15 * [[Chattanooga, Knoxville, Nashville, Memphis]], *17 * [[Knoxville, Chattanooga, Nashville, Memphis]], ] assert condorcet(election) == Nashville # Example from Ques 9 # http://www.yorku.ca/bucovets/4380/exercises/exercises_1_a.pdf v, w, x, y, z = 0, 1, 2, 3, 4 election = [ *11 * [[v, w, x, y, z]], *12 * [[w, x, y, z, v]], *13 * [[x, v, w, y, z]], *14 * [[y, w, v, z, x]], *15 * [[z, v, x, w, y]], ] assert condorcet(election) == v # Example from Ques 1 # http://www.yorku.ca/bucovets/4380/exercises/exercises_1_a.pdf v, w, x, y, z = 0, 1, 2, 3, 4 election = [ [w, v, x, y, z], [v, x, y, z, w], [x, z, v, w, y], [y, z, v, w, x], [z, w, v, y, x], ] assert condorcet(election) is None # Example from # https://en.wikipedia.org/wiki/Condorcet_method#Pairwise_counting_and_matrices election = np.array([ [1, 2, 0, 3], [3, 0, 2, 1], [0, 2, 1, 3], ]) assert condorcet(election) == 0 # Example from https://electowiki.org/wiki/Condorcet_Criterion election = np.concatenate(( np.tile([0, 1, 2], (499, 1)), np.tile([2, 1, 0], (498, 1)), np.tile([1, 2, 0], (3, 1)), )) assert condorcet(election) == 1 # Example from # https://www3.nd.edu/~apilking/Math10170/Information/Lectures/Lecture_3.Head%20To%20Head%20Comparisons.pdf Colley, Henry, Taylor = 0, 1, 2 election = np.array([ [Colley, Henry, Taylor], [Henry, Colley, Taylor], [Henry, Colley, Taylor], [Taylor, Colley, Henry], [Taylor, Henry, Colley], ]) assert condorcet(election) == 1 # Example from https://www.whydomath.org/node/voting/impossible.html election = np.array([ [0, 2, 3, 1], [1, 2, 3, 0], [3, 0, 2, 1], [0, 1, 3, 2], [3, 0, 2, 1], ]) assert condorcet(election) == 3 # Table 3.1 from Mackie - Democracy Defended A, B, C, D, E = 0, 1, 2, 3, 4 election = [ *4 * [[A, E, D, C, B]], *3 * [[B, C, E, D, A]], *2 * [[C, D, E, B, A]], ] assert condorcet(election) == C # "and C is the Condorcet winner" # Example from # https://medium.com/@t2ee6ydscv/how-ranked-choice-voting-elects-extremists-fa101b7ffb8e r, b, g, o, y = 0, 1, 2, 3, 4 election = [ *31 * [[r, b, g, o, y]], *5 * [[b, r, g, o, y]], *8 * [[b, g, r, o, y]], *1 * [[b, g, o, r, y]], *6 * [[g, b, o, r, y]], *1 * [[g, b, o, y, r]], *6 * [[g, o, b, y, r]], *2 * [[o, g, b, y, r]], *5 * [[o, g, y, b, r]], *7 * [[o, y, g, b, r]], *28 * [[y, o, g, b, r]], ] assert condorcet(election) == g
def test_legit_winner(election): election = np.asarray(election) n_cands = election.shape[1] winner = condorcet(election) assert isinstance(winner, (int, type(None))) assert winner in set(range(n_cands)) | {None}
def test_degenerate_condorcet_case(): election = [[0]] assert condorcet(election) == 0 election = [[0], [0], [0]] assert condorcet(election) == 0
def test_unanimity_condorcet(): election = [[3, 0, 1, 2], [3, 0, 2, 1], [3, 2, 1, 0]] assert condorcet(election) == 3