def test_wiki_example_2(self): # Generate data input = [ { "count": 12, "ballot": [["Andrea"], ["Brad"], ["Carter"]] }, { "count": 26, "ballot": [["Andrea"], ["Carter"], ["Brad"]] }, { "count": 12, "ballot": [["Carter"], ["Andrea"], ["Brad"]] }, { "count": 13, "ballot": [["Carter"], ["Andrea"], ["Brad"]] }, { "count": 27, "ballot": [["Brad"]] }, ] output = SchulzeSTV( input, required_winners=2, ballot_notation=SchulzeSTV.BALLOT_NOTATION_GROUPING, ).as_dict() # Run tests self.assertEqual( output, { "candidates": set(["Carter", "Brad", "Andrea"]), "actions": [ { "edges": set([ (("Brad", "Carter"), ("Andrea", "Carter")), (("Brad", "Carter"), ("Andrea", "Brad")), ]) }, { "nodes": set([("Brad", "Carter")]) }, { "edges": set([(("Andrea", "Carter"), ("Andrea", "Brad"))]) }, { "nodes": set([("Andrea", "Carter")]) }, ], "winners": set(["Andrea", "Brad"]), }, )
def test_happenstance_example_2(self): # Generate data input = [ { "count": 5, "ballot": [["A1", "A2"], ["B1", "B2"], ["C1", "C2"]] }, { "count": 2, "ballot": [["B1", "B2"], ["A1", "A2", "C1", "C2"]] }, { "count": 4, "ballot": [["C1", "C2"], ["B1", "B2"], ["A1", "A2"]] }, ] output = SchulzeSTV( input, required_winners=3, ballot_notation=SchulzeSTV.BALLOT_NOTATION_GROUPING, ).as_dict() # Run tests self.assertTrue(set(["A1", "A2"]) & output["winners"]) self.assertTrue(set(["B1", "B2"]) & output["winners"]) self.assertTrue(set(["C1", "C2"]) & output["winners"])
def test_one_ballot_two_winners(self): # Generate data input = [ { "count": 1, "ballot": { "Metal": 1, "Paper": 1, "Plastic": 2, "Wood": 2 } }, ] output = SchulzeSTV( input, required_winners=2, ballot_notation=SchulzeSTV.BALLOT_NOTATION_RANKING, ).as_dict() # Run tests self.assertEqual( output, { "candidates": set(["Paper", "Wood", "Metal", "Plastic"]), "winners": set(["Paper", "Metal"]), }, )
def test_two_ballots_two_winners(self): # Generate data input = [{ "count": 1, "ballot": { "Metal": 2, "Paper": 1, "Plastic": 2, "Wood": 2 } }, { "count": 1, "ballot": { "Metal": 2, "Paper": 2, "Plastic": 2, "Wood": 1 } }] output = SchulzeSTV( input, required_winners=2, ballot_notation=SchulzeSTV.BALLOT_NOTATION_RANKING).as_dict() # Run tests self.assertEqual( output, { "candidates": set(['Metal', 'Wood', 'Plastic', 'Paper']), "winners": set(['Paper', 'Wood']), })
def test_10_candidates_9_winners(self): # Generate data startTime = time.time() input = [ { "count": 1, "ballot": { "A": 9, "B": 1, "C": 1, "D": 9, "E": 9, "F": 2, "G": 9, "H": 9, "I": 9, "J": 9, }, }, { "count": 1, "ballot": { "A": 3, "B": 2, "C": 3, "D": 1, "E": 9, "F": 9, "G": 9, "H": 9, "I": 9, "J": 9, }, }, { "count": 1, "ballot": { "A": 9, "B": 9, "C": 9, "D": 9, "E": 1, "F": 9, "G": 9, "H": 9, "I": 9, "J": 9, }, }, ] SchulzeSTV( input, required_winners=9, ballot_notation=SchulzeSTV.BALLOT_NOTATION_RANKING, ).as_dict() # Run tests self.assertTrue(time.time() - startTime < 2)
def test_one_ballot_one_winner(self): # Generate data input = [{"count": 1, "ballot": {"a": 1, "b": 1, "c": 3}}] output = SchulzeSTV( input, required_winners=1, ballot_notation=SchulzeSTV.BALLOT_NOTATION_RATING).as_dict() # Run tests self.assertEqual(output["winners"], set(["c"]))
def test_wiki_example_2(self): # Generate data input = [ { "count": 12, "ballot": [["Andrea"], ["Brad"], ["Carter"]] }, { "count": 26, "ballot": [["Andrea"], ["Carter"], ["Brad"]] }, { "count": 12, "ballot": [["Carter"], ["Andrea"], ["Brad"]] }, { "count": 13, "ballot": [["Carter"], ["Andrea"], ["Brad"]] }, { "count": 27, "ballot": [["Brad"]] }, ] output = SchulzeSTV( input, required_winners=2, ballot_notation=SchulzeSTV.BALLOT_NOTATION_GROUPING).as_dict() # Run tests self.assertEqual( output, { 'candidates': set(['Carter', 'Brad', 'Andrea']), 'actions': [{ 'edges': set([(('Brad', 'Carter'), ('Andrea', 'Carter')), (('Brad', 'Carter'), ('Andrea', 'Brad'))]) }, { 'nodes': set([('Brad', 'Carter')]) }, { 'edges': set([(('Andrea', 'Carter'), ('Andrea', 'Brad'))]) }, { 'nodes': set([('Andrea', 'Carter')]) }], 'winners': set(['Andrea', 'Brad']) })
def test_happenstance_example(self): # Generate data input = [ { "count": 1, "ballot": { "A": 9, "B": 1, "C": 1, "D": 9, "E": 9, "F": 2 } }, { "count": 1, "ballot": { "A": 3, "B": 2, "C": 3, "D": 1, "E": 9, "F": 9 } }, { "count": 1, "ballot": { "A": 9, "B": 9, "C": 9, "D": 9, "E": 1, "F": 9 } }, ] output = SchulzeSTV( input, required_winners=2, ballot_notation=SchulzeSTV.BALLOT_NOTATION_RANKING, ).as_dict() # Run tests self.assertEqual( output["tied_winners"], set([("D", "E"), ("B", "E"), ("C", "E"), ("B", "D")]), )
def get_results(self, poll, save=False): """ Compute Schulze ballot results :param poll: Poll instance :param save: Save results :return: Results """ votes = {} for vote in Vote.select().where(Vote.poll == poll): votes.setdefault(vote.choices, 0) votes[vote.choices] += 1 inputs = [] for choices, count in votes.items(): inputs.append( dict(count=count, ballot=[[choice] for choice in choices.split()])) if poll.winners == 1: from py3votecore.schulze_method import SchulzeMethod outputs = SchulzeMethod(inputs, ballot_notation=SchulzeMethod. BALLOT_NOTATION_GROUPING).as_dict() if save: winner = outputs['winner'] Candidate.update(winner=True).where( Candidate.poll == poll, Candidate.indice == winner).execute() else: from py3votecore.schulze_stv import SchulzeSTV outputs = SchulzeSTV( inputs, required_winners=poll.winners, ballot_notation=SchulzeSTV.BALLOT_NOTATION_GROUPING).as_dict() if save: winners = outputs['winners'] Candidate.update(winner=True).where( Candidate.poll == poll, Candidate.indice.in_(winners)).execute() return outputs
def test_10_candidates_10_winners(self): # Generate data startTime = time.time() input = [{ "count": 1, "ballot": { "A": 9, "B": 1, "C": 1, "D": 9, "E": 9, "F": 2, "G": 9, "H": 9, "I": 9, "J": 9 } }, { "count": 1, "ballot": { "A": 3, "B": 2, "C": 3, "D": 1, "E": 9, "F": 9, "G": 9, "H": 9, "I": 9, "J": 9 } }, { "count": 1, "ballot": { "A": 9, "B": 9, "C": 9, "D": 9, "E": 1, "F": 9, "G": 9, "H": 9, "I": 9, "J": 9 } }] output = SchulzeSTV( input, required_winners=10, ballot_notation=SchulzeSTV.BALLOT_NOTATION_RANKING).as_dict() # Run tests self.assertAlmostEqual(startTime, time.time(), 1) self.assertEqual( output, { 'winners': set(['A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'J']), 'candidates': set(['A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'J']) })
def test_part_2_of_5_example(self): # Generate data input = [ { "count": 60, "ballot": [["a"], ["b"], ["c"], ["d"], ["e"]] }, { "count": 45, "ballot": [["a"], ["c"], ["e"], ["b"], ["d"]] }, { "count": 30, "ballot": [["a"], ["d"], ["b"], ["e"], ["c"]] }, { "count": 15, "ballot": [["a"], ["e"], ["d"], ["c"], ["b"]] }, { "count": 12, "ballot": [["b"], ["a"], ["e"], ["d"], ["c"]] }, { "count": 48, "ballot": [["b"], ["c"], ["d"], ["e"], ["a"]] }, { "count": 39, "ballot": [["b"], ["d"], ["a"], ["c"], ["e"]] }, { "count": 21, "ballot": [["b"], ["e"], ["c"], ["a"], ["d"]] }, { "count": 27, "ballot": [["c"], ["a"], ["d"], ["b"], ["e"]] }, { "count": 9, "ballot": [["c"], ["b"], ["a"], ["e"], ["d"]] }, { "count": 51, "ballot": [["c"], ["d"], ["e"], ["a"], ["b"]] }, { "count": 33, "ballot": [["c"], ["e"], ["b"], ["d"], ["a"]] }, { "count": 42, "ballot": [["d"], ["a"], ["c"], ["e"], ["b"]] }, { "count": 18, "ballot": [["d"], ["b"], ["e"], ["c"], ["a"]] }, { "count": 6, "ballot": [["d"], ["c"], ["b"], ["a"], ["e"]] }, { "count": 54, "ballot": [["d"], ["e"], ["a"], ["b"], ["c"]] }, { "count": 57, "ballot": [["e"], ["a"], ["b"], ["c"], ["d"]] }, { "count": 36, "ballot": [["e"], ["b"], ["d"], ["a"], ["c"]] }, { "count": 24, "ballot": [["e"], ["c"], ["a"], ["d"], ["b"]] }, { "count": 3, "ballot": [["e"], ["d"], ["c"], ["b"], ["a"]] }, ] output = SchulzeSTV( input, required_winners=3, ballot_notation=SchulzeSTV.BALLOT_NOTATION_GROUPING, ).as_dict() # Run tests self.assertEqual(output["winners"], set(["a", "d", "e"]))
def test_10_candidates_10_winners(self): # Generate data startTime = time.time() input = [ { "count": 1, "ballot": { "A": 9, "B": 1, "C": 1, "D": 9, "E": 9, "F": 2, "G": 9, "H": 9, "I": 9, "J": 9, }, }, { "count": 1, "ballot": { "A": 3, "B": 2, "C": 3, "D": 1, "E": 9, "F": 9, "G": 9, "H": 9, "I": 9, "J": 9, }, }, { "count": 1, "ballot": { "A": 9, "B": 9, "C": 9, "D": 9, "E": 1, "F": 9, "G": 9, "H": 9, "I": 9, "J": 9, }, }, ] output = SchulzeSTV( input, required_winners=10, ballot_notation=SchulzeSTV.BALLOT_NOTATION_RANKING, ).as_dict() # Run tests self.assertAlmostEqual(startTime, time.time(), 1) self.assertEqual( output, { "winners": set(["A", "C", "B", "E", "D", "G", "F", "I", "H", "J"]), "candidates": set(["A", "C", "B", "E", "D", "G", "F", "I", "H", "J"]), }, )