Exemplo n.º 1
0
    def test_simple_pbv(self):

        stay = Candidate("Stay")
        soft = Candidate("Soft Brexit")
        hard = Candidate("Hard Brexit")

        candidates = [stay, soft, hard]

        ballots = [
            Ballot(ranked_candidates=[soft, stay]),
            Ballot(ranked_candidates=[stay, soft]),
            Ballot(ranked_candidates=[stay, soft]),
            Ballot(ranked_candidates=[hard, soft]),
            Ballot(ranked_candidates=[hard, stay, soft]),
        ]

        election_result = pyrankvote.preferential_block_voting(
            candidates, ballots, number_of_seats=1)
        winners = election_result.get_winners()

        self.assertEqual(1, len(winners),
                         "Function should return a list with one item")

        winner = winners[0]
        self.assertEqual(stay, winner, "Winner should be Soft")
Exemplo n.º 2
0
    def get_election_res(pred_df, id_orig, n_ans):
        df = pred_df[pred_df.id.str.startswith(id_orig)]
        candidates = [Candidate(el) for el in df.answers.unique()]
        ballots = [
            Ballot(ranked_candidates=[
                Candidate(vote) for vote in df[df.id == id_].answers
            ]) for id_ in df.id.unique()
        ]

        if len(candidates) <= n_ans:
            return list(df.answers)
        if args['voting'] == 'pbv':
            election_result = pyrankvote.preferential_block_voting(
                candidates, ballots, min(n_ans, len(candidates)))
        else:
            election_result = pyrankvote.single_transferable_vote(
                candidates, ballots, min(n_ans, len(candidates)))

        return election_result
Exemplo n.º 3
0
    def test_simple_pbv_with_second_selection_if_equal(self):

        stay = Candidate("Stay")
        soft = Candidate("Soft Brexit")
        hard = Candidate("Hard Brexit")

        candidates = [stay, soft, hard]

        ballots = [
            Ballot(ranked_candidates=[stay, soft, hard]),
            Ballot(ranked_candidates=[hard, soft, stay]),
            Ballot(ranked_candidates=[soft, stay, hard]),
        ]

        election_result = pyrankvote.preferential_block_voting(
            candidates, ballots, number_of_seats=2)
        winners = election_result.get_winners()

        self.assertEqual(2, len(winners),
                         "Function should return a list with two items")
        self.assertIn(soft, winners, "Soft should be one of the winners")
        self.assertIn(stay, winners, "Stay should be one of the winners")
Exemplo n.º 4
0
    def test_simple_pbv2(self):

        per = Candidate("Per")
        paal = Candidate("Pål")
        askeladden = Candidate("Askeladden")

        candidates = [per, paal, askeladden]

        ballots = [
            Ballot(ranked_candidates=[askeladden, per]),
            Ballot(ranked_candidates=[per, paal]),
            Ballot(ranked_candidates=[per, paal]),
            Ballot(ranked_candidates=[paal, per]),
            Ballot(ranked_candidates=[paal, per, askeladden]),
        ]

        election_result = pyrankvote.preferential_block_voting(
            candidates, ballots, number_of_seats=1)
        winners = election_result.get_winners()

        self.assertEqual(1, len(winners),
                         "Function should return a list with one item")
        self.assertListEqual([per], winners, "Winners should be Per")
Exemplo n.º 5
0
    def test_simple_pbv(self):

        per = Candidate("Per")
        paal = Candidate("Pål")
        askeladden = Candidate("Askeladden")

        candidates = [per, paal, askeladden]

        ballots = [
            Ballot(ranked_candidates=[askeladden, per]),
            Ballot(ranked_candidates=[per, paal]),
            Ballot(ranked_candidates=[per, paal]),
            Ballot(ranked_candidates=[paal, per]),
            Ballot(ranked_candidates=[paal, per, askeladden]),
        ]

        election_result = pyrankvote.preferential_block_voting(
            candidates, ballots, number_of_seats=2)
        winners = election_result.get_winners()

        self.assertEqual(2, len(winners),
                         "Function should return a list with two items")
        self.assertIn(per, winners, "Per should be one of the winners")
        self.assertIn(paal, winners, "Pål should be one of the winners")
Exemplo n.º 6
0
print(election_result)
# Prints:
"""
FINAL RESULT
Candidate                    Votes  Status
-------------------------  -------  --------
William, popular moderate        4  Elected
Thomas, far-left                 4  Elected
John, moderate                   2  Rejected
Charles, moderate                0  Rejected
"""

# PREFERENTIAL BLOCK VOTING

election_result = pyrankvote.preferential_block_voting(candidates, ballots, number_of_seats=2)
# Elects: William, popular moderate; and John, moderate

print(election_result)
# Prints:
"""
FINAL RESULT
Candidate                    Votes  Status
-------------------------  -------  --------
William, popular moderate        8  Elected
John, moderate                   6  Elected
Thomas, far-left                 4  Rejected
Charles, moderate                2  Rejected
"""

#   COMMENT
Exemplo n.º 7
0
    def test_example(self):
        popular_moderate = Candidate("William, popular moderate")
        moderate2 = Candidate("John, moderate")
        moderate3 = Candidate("Charles, moderate")
        far_left = Candidate("Thomas, far-left")

        candidates = [popular_moderate, moderate2, moderate3, far_left]

        ballots = [
            Ballot(ranked_candidates=[
                popular_moderate, moderate2, moderate3, far_left
            ]),
            Ballot(ranked_candidates=[
                popular_moderate, moderate2, moderate3, far_left
            ]),
            Ballot(ranked_candidates=[
                popular_moderate, moderate3, moderate2, far_left
            ]),
            Ballot(ranked_candidates=[
                popular_moderate, moderate3, moderate2, far_left
            ]),
            Ballot(ranked_candidates=[
                moderate2, popular_moderate, moderate3, far_left
            ]),
            Ballot(ranked_candidates=[
                moderate2, popular_moderate, moderate3, far_left
            ]),
            Ballot(ranked_candidates=[
                far_left, popular_moderate, moderate2, moderate3
            ]),
            Ballot(ranked_candidates=[
                far_left, popular_moderate, moderate2, moderate3
            ]),
            Ballot(ranked_candidates=[
                far_left, moderate2, popular_moderate, moderate3
            ]),
            Ballot(ranked_candidates=[
                far_left, moderate2, popular_moderate, moderate3
            ]),
        ]

        election_result = pyrankvote.preferential_block_voting(
            candidates, ballots, number_of_seats=2)

        round_nr = 0
        candidates_results_in_round = election_result.rounds[
            round_nr].candidate_results

        ranking_in_round = [
            candidate_result.candidate
            for candidate_result in candidates_results_in_round
        ]
        correct_ranking_in_round = [
            popular_moderate, moderate2, far_left, moderate3
        ]
        self.assertEqual(4, len(ranking_in_round),
                         "All four candidates should be in the result list")
        self.assertListEqual(correct_ranking_in_round, ranking_in_round)

        votes_in_round = [
            candidate_result.number_of_votes
            for candidate_result in candidates_results_in_round
        ]
        correct_votes_in_round = [8, 6, 4, 2]
        assert_list_almost_equal(self, correct_votes_in_round, votes_in_round)

        status_in_round = [
            candidate_result.status
            for candidate_result in candidates_results_in_round
        ]
        correct_status_in_round = [
            CandidateStatus.Elected, CandidateStatus.Elected,
            CandidateStatus.Rejected, CandidateStatus.Rejected
        ]
        self.assertListEqual(correct_status_in_round, status_in_round)

        winners = election_result.get_winners()
        self.assertEqual(2, len(winners),
                         "Function should return a list with two items")

        self.assertIn(popular_moderate, winners, "William should be a winner")
        self.assertIn(moderate2, winners, "John should be a winner")