Exemplo n.º 1
0
def test_requires_no_duplicates():
    with pytest.raises(InvalidBallotDataException):
        RankedChoiceBallotBox([[1, {1}]])

    with pytest.raises(InvalidBallotDataException):
        RankedChoiceBallotBox([[{1}, 1]])

    with pytest.raises(InvalidBallotDataException):
        RankedChoiceBallotBox([[1, 2, {3, 1}]])
Exemplo n.º 2
0
def test_borda_count_ranked_choice_with_ties():
    ballots = RankedChoiceBallotBox([[1, 2, 3], [{1, 2, 3}], [1, {2, 3}]])
    election = Election(ballots)
    # assert election.ranking_by_borda_count() == [1, 2, 3]
    assert election.ranking_by_borda_count(include_score=True) == [(1, 4),
                                                                   (2, 1),
                                                                   (3, 0)]
Exemplo n.º 3
0
def evaluate_vote_induction_method(dataset, intransitivity_resolver,
                                   incompleteness_resolver):
    """Computes the Kendall tau distance between original pairwise votes from a dataset and the collapsed rankings.
    Uses the Kendall tau on the output of the specified social choice method.

    :param dataset: a list of 4-tuples, each representing a pairwise vote followed by a voter uuid
    :param intransitivity_resolver: a function that takes a win-graph and returns an acyclic win-graph
    :param incompleteness_resolver: a function that takes a win-graph and a set of candidates
    and produces a fully-connected win-graph with each candidate
    :return: a float in [-1, 1]
    """
    voter_to_vote_set = {vote[3]: [] for vote in dataset}
    candidates = {v[0] for v in dataset} | {v[1] for v in dataset}
    num_pairs = len(candidates) * (len(candidates) - 1) // 2

    # vote_sets should be a mapping from each voter to the contents of each of their votes.
    for vote in dataset:
        voter_to_vote_set[vote[3]].append(vote[0:3])

    # This is the pairwise election that serves as a baseline "ground truth" for the
    # vote induction method.
    pairwise_election = Election(
        PairwiseBallotBox([vote[0:3] for vote in dataset]))

    ranked_choice_ballots = []
    for vote_set in voter_to_vote_set.values():
        transitive_votes = intransitivity_resolver(vote_set)
        complete_votes = incompleteness_resolver(transitive_votes)
        ordering = list(nx.topological_sort(complete_votes))
        ranked_choice_ballots.append(ordering)

    ranking_election = Election(RankedChoiceBallotBox(ranked_choice_ballots))

    pairwise_ranking = pairwise_election.ranking_by_ranked_pairs()
    ranked_choice_ranking = ranking_election.ranking_by_ranked_pairs()

    tau = ranking_similarity.kendalls_tau(pairwise_ranking,
                                          ranked_choice_ranking)
    print(
        f"tau={tau},{intransitivity_resolver.func.__name__},{incompleteness_resolver.func.__name__}"
    )
def election():
    example_votes = RankedChoiceBallotBox([[1, 2, 3, 4], [1, 3, 2, 4],
                                           [1, 2, 3, 4], [1, 2, 4, 3]])
    return Election(example_votes)
Exemplo n.º 5
0
def test_no_candidate_repeats():
    with pytest.raises(InvalidBallotDataException):
        RankedChoiceBallotBox([[1, 1]])
Exemplo n.º 6
0
def test_rankings_can_include_ties():
    RankedChoiceBallotBox([[{1, 2}]])

    RankedChoiceBallotBox([[1, {2, 4}, {3, 5}]])
Exemplo n.º 7
0
def test_candidates_no_duplicates():
    with pytest.raises(InvalidElectionDataException):
        RankedChoiceBallotBox([[1, 2, 3]], candidates=[1, 1, 2, 3])
Exemplo n.º 8
0
def test_requires_full_ballots():
    with pytest.raises(InvalidBallotDataException):
        RankedChoiceBallotBox([[1, 2]], candidates=[1, 2, 3])

    with pytest.raises(InvalidBallotDataException):
        RankedChoiceBallotBox([[1, 2, 3, 4]], candidates=[1, 2, 3])
Exemplo n.º 9
0
def test_require_full_ballots_with_candidate_inference():
    with pytest.raises(InvalidBallotDataException):
        RankedChoiceBallotBox([[1, 2, 3, 4], [4, 3, 2, 1], [3, 2, 1]])