def runTest2000ElectionApproximation(): description = "2000 Election Approximation Test" print(description) print("Generating ballots...") nader = "Ralph Nader" bush = "George Bush" gore = "Al Gore" num_ballots = 100000 ballots = set() naderPercentage = .03 bushPercentage = .49 gorePercentage = .48 for i in xrange(num_ballots): ballot = Ballot() if i < naderPercentage * num_ballots: ballot.set_candidate_with_rank(nader, 1) ballot.set_candidate_with_rank(gore, 2) elif i < (naderPercentage + bushPercentage) * num_ballots: ballot.set_candidate_with_rank(bush, 1) ballot.set_candidate_with_rank(Ballot.NO_CONFIDENCE, 2) else: ballot.set_candidate_with_rank(gore, 1) ballot.set_candidate_with_rank(nader, 2) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 1 election.ballots = ballots winners = election.compute_winners(verbose=True) print(winners)
def runTestWithTwoCandidatesOneSeat(): description = "Two Candidates One Seat Winner Test" print(description) print("Generating ballots...") candidateOne = "Candidate One" candidateTwo = "Candidate Two" num_ballots = 10 ballots = set() for i in xrange(num_ballots): ballot = Ballot() if i < .4 * num_ballots: ballot.set_candidate_with_rank(candidateOne, 1) else: ballot.set_candidate_with_rank(candidateTwo, 1) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 1 election.ballots = ballots winners = election.compute_winners(verbose=True) print(winners)
def stv(): data = json.loads(request.args['data']) raw_ballots = data['ballots'] _candidates = data['candidates'] seats = data['seats'] ballots = set() for ballot_number, raw_ballot in raw_ballots.items(): ballot = Ballot() for rank, candidate in raw_ballot.items(): if candidate and rank.isdigit(): ballot.set_candidate_with_rank(candidate.strip(), int(rank)) ballots.add(ballot) election = Election() election.seats = seats election.ballots = ballots old_stdout = sys.stdout sys.stdout = output = StringIO() _winners, counter = election.compute_winners(verbose=True) sys.stdout = old_stdout output = { 'winners': list(counter.winning_candidates), 'losers': list(counter.losing_candidates), 'votes': counter._votes_for_candidate_per_round, 'quota': election.droop_quota(len(ballots), seats), 'output': output.getvalue() } return json.dumps(output)
def runTestWikipediaFoodSelection(): description = "Wikipedia's Food Selection Example Test" print(description) print("Generating ballots...") oranges = "Oranges" pears = "Pears" chocolate = "Chocolate" strawberries = "Strawberries" sweets = "Sweets" ballots = set() # Oranges-preferred ballots for i in xrange(4): ballot = Ballot() ballot.set_candidate_with_rank(oranges, 1) ballots.add(ballot) # Pears-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(pears, 1) ballot.set_candidate_with_rank(oranges, 2) ballots.add(ballot) # Chocolate-preferred ballots (type 1) for i in xrange(8): ballot = Ballot() ballot.set_candidate_with_rank(chocolate, 1) ballot.set_candidate_with_rank(strawberries, 2) ballots.add(ballot) # Chocolate-preferred ballots (type 2) for i in xrange(4): ballot = Ballot() ballot.set_candidate_with_rank(chocolate, 1) ballot.set_candidate_with_rank(sweets, 2) ballots.add(ballot) # Strawberries-preferred ballots for i in xrange(1): ballot = Ballot() ballot.set_candidate_with_rank(strawberries, 1) ballots.add(ballot) # Sweets-preferred ballots for i in xrange(1): ballot = Ballot() ballot.set_candidate_with_rank(sweets, 1) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 3 election.ballots = ballots winners = election.compute_winners(verbose=True) print(winners)
def runTestCGPGreyAnimalKingdom(): description = "CGP Grey's Politics in the Animal Kingdom Test" print(description) print("Generating ballots...") tarsier = "Tarsier" gorilla = "Gorilla" monkey = "Monkey" tiger = "Tiger" lynx = "Lynx" num_ballots = 10000 ballots = set() tarsier_percentage = .05 gorilla_percentage = .28 monkey_percentage = .33 tiger_percentage = .21 lynx_percentage = .13 tarsier_max_range = tarsier_percentage gorilla_max_range = tarsier_max_range + gorilla_percentage monkey_max_range = gorilla_max_range + monkey_percentage tiger_max_range = monkey_max_range + tiger_percentage for i in xrange(num_ballots): ballot = Ballot() if i < tarsier_max_range * num_ballots: # tarsier-preferred ballots ballot.set_candidate_with_rank(tarsier, 1) ballot.set_candidate_with_rank(gorilla, 2) elif i < gorilla_max_range * num_ballots: # gorilla-preferred ballots ballot.set_candidate_with_rank(gorilla, 1) ballot.set_candidate_with_rank(tarsier, 2) ballot.set_candidate_with_rank(monkey, 3) elif i < monkey_max_range * num_ballots: # monkey-preferred ballots ballot.set_candidate_with_rank(monkey, 1) elif i < tiger_max_range * num_ballots: # tiger-preferred ballots ballot.set_candidate_with_rank(tiger, 1) else: # lynx-preferred ballots ballot.set_candidate_with_rank(lynx, 1) ballot.set_candidate_with_rank(tiger, 2) ballot.set_candidate_with_rank(tarsier, 3) ballot.set_candidate_with_rank(monkey, 4) ballot.set_candidate_with_rank(gorilla, 5) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 3 election.ballots = ballots winners = election.compute_winners(verbose=True) print(winners)
def runTestWithRandomTiebreakBetweenTwoLosers(): description = "Random Tiebreak Between Losing Candidates Test" print(description) print("Generating ballots...") candidateOne = "Candidate One" candidateTwo = "Candidate Two" candidateThree = "Candidate Three" candidateFour = "Candidate Four" num_ballots = 15 ballots = set() # Candidate-One-preferred ballots for i in xrange(6): ballot = Ballot() ballot.set_candidate_with_rank(candidateOne, 1) ballots.add(ballot) # Candidate-Two-preferred ballots for i in xrange(5): ballot = Ballot() ballot.set_candidate_with_rank(candidateTwo, 1) ballots.add(ballot) # Candidate-Three-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(candidateThree, 1) ballot.set_candidate_with_rank(candidateFour, 2) ballots.add(ballot) # Candidate-Four-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(candidateFour, 1) ballot.set_candidate_with_rank(candidateThree, 2) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 2 election.ballots = ballots election.is_final_tiebreak_manual = True winners = election.compute_winners(verbose=True) print(winners)
def runTestWithThreeCandidatesOneSeatNoConfidence(): description = "Three Candidates One Seat No Confidence Test" print(description) print("Generating ballots...") candidateOne = "Devin Gund" candidateTwo = "Hillary Clinton" candidateThree = "Donald Trump" ballots = set() # Candidate-One-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(candidateOne, 1) ballots.add(ballot) # Candidate-Two-preferred ballots for i in xrange(1): ballot = Ballot() ballot.set_candidate_with_rank(candidateTwo, 1) ballot.set_candidate_with_rank(candidateOne, 2) ballots.add(ballot) # Candidate-Three-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(candidateThree, 1) ballots.add(ballot) # No-Confidence-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(Ballot.NO_CONFIDENCE, 1) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 1 election.ballots = ballots winners = election.compute_winners(verbose=True) print(winners)
def runTestWithOneCandidateTwoSeats(): description = "One Candidate Two Seats Winner Test" print(description) print("Generating ballots...") candidate = "Candidate" num_ballots = 10 ballots = set() for i in xrange(num_ballots): ballot = Ballot() ballot.set_candidate_with_rank(candidate, 1) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 2 election.ballots = ballots winners = election.compute_winners(verbose=True) print(winners)
def runTestWithThreeCandidatesTwoSeats(): description = "Three Candidates Two Seats" print(description) print("Generating ballots...") candidateOne = "Candidate One" candidateTwo = "Candidate Two" candidateThree = "Candidate Three" ballots = set() # Candidate-One-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(candidateOne, 1) ballot.set_candidate_with_rank(candidateThree, 2) ballots.add(ballot) # Candidate-Two-preferred ballots for i in xrange(2): ballot = Ballot() ballot.set_candidate_with_rank(candidateTwo, 1) ballot.set_candidate_with_rank(Ballot.NO_CONFIDENCE, 2) ballots.add(ballot) # Candidate-Three-preferred ballots for i in xrange(1): ballot = Ballot() ballot.set_candidate_with_rank(candidateThree, 1) ballot.set_candidate_with_rank(candidateOne, 2) ballot.set_candidate_with_rank(Ballot.NO_CONFIDENCE, 3) ballots.add(ballot) print("Running election...") election = Election() election.name = description election.seats = 2 election.ballots = ballots winners = election.compute_winners(verbose=True) print(winners)
def main(): no_confidence = Ballot.NO_CONFIDENCE no_confidence_short = "NC" run_command = "run" file_command = "file" quit_command = "quit" undo_command = "undo" help_command = "help" election_name = None election_seats = None election_ballots = list() print_introduction() election_name = prompt_election_name() election_seats = prompt_election_seats() print_ballot_instructions(no_confidence_short, run_command, quit_command, undo_command, file_command, help_command) while True: ballot_or_command = prompt_ballot_command(len(election_ballots)) # Empty input if len(ballot_or_command) == 0: continue # 'run' command elif ballot_or_command.lower() == run_command: # Create the Election election = Election(name=election_name, seats=election_seats) election.is_final_tiebreak_manual = True election.ballots = set(election_ballots) # Run the election and compute winners winners = election.compute_winners(verbose=True)[0] # Print the winners and quit the program print_conclusion(winners, election_seats) return 0 # 'quit' command elif ballot_or_command.lower() == quit_command: # Quit the program return 0 # 'undo' command elif ballot_or_command.lower() == undo_command: # Pop the last ballot from the list of ballots if len(election_ballots) > 0: election_ballots.pop() print("The most-recent ballot has been removed.") else: print("There are no ballots to remove.") # 'help' command elif ballot_or_command.lower() == help_command: # Print the instructions again print_ballot_instructions(no_confidence_short, run_command, quit_command, undo_command, file_command, help_command) # 'file' command elif ballot_or_command.lower().startswith(file_command): filename = ballot_or_command[len('find '):] with open(filename) as file: # For each ballot in the CSV for ranked_candidates in csv.reader(file): ballot = Ballot() for rank, candidate in enumerate(ranked_candidates): if candidate: ballot.set_candidate_with_rank(candidate, rank + 1) # Add the ballot to the list of ballots election_ballots.append(ballot) # Ballot input else: # Parse the input into a list of strings representing candidates ranked_candidates = [candidate_input.strip() for candidate_input in ballot_or_command.split(',')] # For each candidate in the list, add them to the Ballot with increasing rank ballot = Ballot() rank = 1 for candidate in ranked_candidates: # Replace No Confidence abbreviations if candidate == no_confidence_short: candidate = no_confidence ballot.set_candidate_with_rank(candidate, rank) rank += 1 # Add the ballot to the list of ballots election_ballots.append(ballot)