Beispiel #1
0
    def test_cgp_grey_animal_kingdom(self):
        """Tests CGP Grey example STV election 'Politics in the Animal Kingdom'

        Link: https://www.youtube.com/watch?v=l8XOZJkozfI

        Expected winners: Gorilla, Monkey, Tiger
        """
        # Setup
        tarsier = Candidate('tarsier', name='Tarsier')
        gorilla = Candidate('gorilla', name='Gorilla')
        monkey = Candidate('monkey', name='Monkey')
        tiger = Candidate('tiger', name='Tiger')
        lynx = Candidate('lynx', name='Lynx')

        expected_winners = set([gorilla, monkey, tiger])
        seats = 3
        tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'

        num_ballots = 10000
        ballots = (
            ballots_for_candidates([tarsier, gorilla], int(
                .05 * num_ballots)) + ballots_for_candidates(
                    [gorilla, tarsier, monkey], int(.28 * num_ballots)) +
            ballots_for_candidates([monkey], int(.33 * num_ballots)) +
            ballots_for_candidates([tiger], int(.21 * num_ballots)) +
            ballots_for_candidates([lynx, tiger, tarsier, monkey, gorilla],
                                   int(.13 * num_ballots)))

        # Test
        election = Election(seats=seats,
                            ballots=ballots,
                            random_alphanumeric=tiebreak_alphanumeric)
        results = election.compute_results()
        self.assertEqual(expected_winners, results.candidates_elected)
Beispiel #2
0
    def test_wikipedia_food_selection(self):
        """Tests Wikipedia's example STV election, using choices of food.

        Link: https://en.wikipedia.org/wiki/Single_transferable_vote#Example

        Expected winners: Chocolate, Oranges, Strawberries
        """
        # Setup
        chocolate = Candidate('chocolate', name='Chocolate')
        oranges = Candidate('oranges', name='Oranges')
        pears = Candidate('pears', name='Pears')
        strawberries = Candidate('strawberries', name='Strawberries')
        sweets = Candidate('sweets', name='Sweets')

        expected_winners = set([chocolate, oranges, strawberries])
        seats = 3
        tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'

        ballots = (ballots_for_candidates([oranges], 4) +
                   ballots_for_candidates([pears, oranges], 2) +
                   ballots_for_candidates([chocolate, strawberries], 8) +
                   ballots_for_candidates([chocolate, sweets], 4) +
                   ballots_for_candidates([strawberries], 1) +
                   ballots_for_candidates([sweets], 1))

        # Test
        election = Election(seats=seats,
                            ballots=ballots,
                            random_alphanumeric=tiebreak_alphanumeric)
        results = election.compute_results()
        self.assertEqual(expected_winners, results.candidates_elected)
Beispiel #3
0
    def test_florida_2000_presidential(self):
        """Tests the 2000 Florida presidential election, scaled 1%.

        An example of a election between two major candidates and a third-party.
        For the sake of ranking votes, this test assumes that Nader supporters
        prefer Gore to Bush by a 2:1 margin, which is not necessarily true.

        Expected winners: Gore

        Round 0
            Ballots:
              29127 * [Bush]
              29122 * [Gore]
                324 * [Nader, Bush]
                649 * [Nader, Gore]
            Votes:
                Bush: 29127
                Gore: 29122
                Nader:  974
            Threshold: (29127+29122+974) / (1+1) + 1 = 29612.5
            Result: Nader is eliminated

        Round 1
            Ballots:
                29452 * [Bush]
                29972 * [Gore]
            Votes:
                Bush: 29452
                Gore: 29972
            Threshold: (29452+29972) / (1+1) + 1 = 29612.5
            Result: Gore is elected
        """
        # Setup
        bush = Candidate('gbush', name='George Bush')
        gore = Candidate('agore', name='Al Gore')
        nader = Candidate('rnader', name='Ralph Nader')
        expected_winners = set([gore])
        seats = 1
        tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'

        ballots = (ballots_for_candidates([bush], 29127) +
                   ballots_for_candidates([gore], 29122) +
                   ballots_for_candidates([nader, bush], 324) +
                   ballots_for_candidates([nader, gore], 649))

        # Test
        election = Election(seats=seats,
                            ballots=ballots,
                            random_alphanumeric=tiebreak_alphanumeric)
        results = election.compute_results()
        self.assertEqual(expected_winners, results.candidates_elected)
Beispiel #4
0
    def test_cgp_grey_stv_election_walkthrough(self):
        """Tests CGP Grey example STV election 'Extra: STV Election Walkthrough'

        Link: https://www.youtube.com/watch?v=Ac9070OIMUg

        Expected winners: Gorilla, Silverback, Owl, Turtle, Tiger
        """
        # Setup
        tarsier = Candidate('tarsier', name='Tarsier')
        gorilla = Candidate('gorilla', name='Gorilla')
        silverback = Candidate('silverback', name='Silverback')
        owl = Candidate('owl', name='Owl')
        turtle = Candidate('turtle', name='Turtle')
        snake = Candidate('snake', name='Snake')
        tiger = Candidate('tiger', name='Tiger')
        lynx = Candidate('lynx', name='Lynx')
        jackalope = Candidate('jackalope', name='Jackalope')
        buffalo = Candidate('buffalo', name='Buffalo')

        expected_winners = set([gorilla, silverback, owl, turtle, tiger])
        seats = 5
        tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'

        num_ballots = 10000
        ballots = (
            ballots_for_candidates([tarsier, silverback], int(
                .05 * num_ballots)) + ballots_for_candidates(
                    [gorilla, silverback], int(.21 * num_ballots)) +
            ballots_for_candidates([gorilla, tarsier, silverback],
                                   int(.11 * num_ballots)) +
            ballots_for_candidates([silverback], int(.03 * num_ballots)) +
            ballots_for_candidates([owl, turtle], int(.33 * num_ballots)) +
            ballots_for_candidates([turtle], int(.01 * num_ballots)) +
            ballots_for_candidates([snake, turtle], int(.01 * num_ballots)) +
            ballots_for_candidates([tiger], int(.16 * num_ballots)) +
            ballots_for_candidates([lynx, tiger], int(.04 * num_ballots)) +
            ballots_for_candidates([jackalope], int(.02 * num_ballots)) +
            ballots_for_candidates([buffalo, jackalope], int(
                .02 * num_ballots)) + ballots_for_candidates(
                    [buffalo, jackalope, turtle], int(.01 * num_ballots)))

        # Test
        election = Election(seats=seats,
                            ballots=ballots,
                            random_alphanumeric=tiebreak_alphanumeric)
        results = election.compute_results()
        self.assertEqual(expected_winners, results.candidates_elected)
 def castVote(self, vote):
     for i in range(10):
         vote = Vote(Voter("Bad Voter {}".format(i)),
                     Candidate("Bad Candidate"))
         block = block(vote, self.chain.last)
         # mostly invalid proof
         proof = hash(block)
         self.chain.addBlock(block, proof)
     self.announceNewBlock()
     return true
Beispiel #6
0
    def setUp(self):

    	self.nCandidates = 3
    	self.nVoters = 10
    	self.nNodes = 5

    	self.candidates = [Candidate("Candidate %s" % i) for i in range(self.nCandidates)]
    	self.voters = [Voter("Voter %s" % i) for i in range(self.nVoters)]

    	self.network = Network()
    	for i in range(self.nNodes):
    		self.network.addNode()
Beispiel #7
0
def candidate_from_input(candidate_input):
    """Returns a Candidate representing the input string.

    Args:
        candidate_input: String representing user input for a Candidate. The
            expected format is 'uid' or optionally 'uid (name)'.

    Returns:
        Candidate representing the input uid (and optionally name).
    """
    if input_string_is_no_confidence(candidate_input):
        return NoConfidence()
    else:
        expr = '(.*?)\s*\((.*?)\)'
        regex = re.compile(expr)
        result = regex.match(candidate_input)
        if result is not None:
            uid = result.group(1)
            name = result.group(2)
        else:
            uid = candidate_input
            name = None
        return Candidate(uid, name=name)
Beispiel #8
0
def candidates_for_ids(candidate_ids):
    """Returns an ordered list of Candidates for an ordered list of ids.

    This is helpful for tests where candidates on ballots can be defined as
    short strings (i.e. 'A', 'B', 'NC') in the test cases, which can then be
    converted to proper Candidates.

    Args:
        candidate_ids: List of strings representing candidate ids.

    Returns:
        List of Candidates corresponding to the given ids.
    """
    candidate_for_id = {
        'NC': NoConfidence(),
        'A': Candidate('dgund', name='Devin Gund'),
        'B': Candidate('gwashington', name='George Washington'),
        'C': Candidate('jadams', name='John Adams'),
        'D': Candidate('tjefferson', name='Thomas Jefferson'),
        'E': Candidate('jmadison', name='James Madison'),
        'F': Candidate('jmonroe', name='James Monroe'),
        'G': Candidate('jqadams', name='John Quincy Adams'),
        'H': Candidate('ajackson', name='Andrew Jackson'),
        'I': Candidate('mvburen', name='Martin Van Buren'),
        'J': Candidate('wharrison', name='William Harrison'),
        'K': Candidate('jtyler', name='John Tyler'),
        'L': Candidate('jpolk', name='James Polk'),
        'M': Candidate('ztaylor', name='Zachary Taylor'),
        'N': Candidate('mfillmore', name='Millard Fillmore'),
        'O': Candidate('fpierce', name='Franklin Pierce'),
        'P': Candidate('jbuchanan', name='James Buchanan'),
        'Q': Candidate('alincoln', name='Abraham Lincoln'),
        'R': Candidate('ajohnson', name='Andrew Johnson'),
        'S': Candidate('ugrant', name='Ulysses Grant'),
        'T': Candidate('rhayes', name='Rutherford Hayes'),
        'U': Candidate('jgarfield', name='James Garfield'),
        'V': Candidate('carthur', name='Chester Arthur'),
        'W': Candidate('gcleveland', name='Grover Cleveland'),
        'X': Candidate('bharrison', name='Benjamin Harrison'),
        'Y': Candidate('wmckinley', name='William McKinley'),
        'Z': Candidate('troosevelt', name='Theodore Roosevelt'),
    }

    candidates = []
    for candidate_id in candidate_ids:
        candidates.append(candidate_for_id[candidate_id])
    return candidates
Beispiel #9
0
import unittest

from block import Block
from election import Vote, Voter, Candidate
from blockchain import Blockchain, InvalidBlockchain

# Voters
Amany = Voter("Amany")
Anthony = Voter("Anthony")
Owen = Voter("Owen")

# Candidates
James = Candidate("James")
Marcus = Candidate("Marcus")
Ronny = Candidate("Ronny")

ab = {
    'jy': Vote(Amany, James),
    'mc': Vote(Amany, Marcus),
    'rk': Vote(Amany, Ronny)
}

ak = {
    'jy': Vote(Anthony, James),
    'mc': Vote(Anthony, Marcus),
    'rk': Vote(Anthony, Ronny)
}

on = {
    'jy': Vote(Owen, James),
    'mc': Vote(Owen, Marcus),