def test_no_ties_allowed(self): self.longMessage = True votes = [11, 11, 11] seats = 4 for method in app.METHODS: with self.assertRaises(app.TiesException, msg=method + " failed"): app.compute(method, votes, seats, tiesallowed=False, verbose=False)
def test_saintelague_difference(self): votes = [6, 1] seats = 4 r1 = app.compute("saintelague", votes, seats, verbose=False) # [3, 1] r2 = app.compute("modified_saintelague", votes, seats, verbose=False) # [4, 0] self.assertNotEqual(r1, r2, "Sainte Lague and its modified variant" + "should produce different results.")
def test_no_ties_allowed2(self): self.longMessage = True votes = [12, 12, 11, 12] seats = 3 for method in app.METHODS: self.assertEqual( app.compute(method, votes, seats, tiesallowed=False, verbose=False), [1, 1, 0, 1], msg=method + " failed")
def test_fewerseatsthanparties(self): self.longMessage = True votes = [10, 9, 8, 8, 11, 12] seats = 3 for method in app.METHODS: result = app.compute(method, votes, seats, verbose=False) self.assertEqual(result, [1, 0, 0, 0, 1, 1], msg=method + " failed")
def test_zero_parties(self): self.longMessage = True votes = [0, 14, 28, 0, 0] seats = 6 for method in app.METHODS: result = app.compute(method, votes, seats, verbose=False) self.assertEqual(result, [0, 2, 4, 0, 0], msg=method + " failed")
def test_weak_proportionality(self): self.longMessage = True votes = [14, 28, 7, 35] seats = 12 for method in app.METHODS: result = app.compute(method, votes, seats, verbose=False) self.assertEqual(result, [2, 4, 1, 5], msg=method + " failed")
def test_tiebreaking(self): self.longMessage = True votes = [2, 1, 1, 2, 2] seats = 2 for method in app.METHODS: result = app.compute(method, votes, seats, verbose=False) self.assertEqual(result, [1, 0, 0, 1, 0], msg=method + " failed")
def test_threshold(self): votes = [41, 56, 3] seats = 60 threshold = 0.03 filtered_votes = app.apply_threshold(votes, threshold) self.assertEqual(filtered_votes, [41, 56, 3], "Threshold cut too much.") threshold = 0.031 filtered_votes = app.apply_threshold(votes, threshold) self.assertEqual(filtered_votes, [41, 56, 0], "Threshold was not applied correctly.") method = "dhondt" threshold = 0 unfiltered_result = app.compute(method, votes, seats, threshold=threshold, verbose=False) threshold = 0.04 filtered_result = app.compute(method, votes, seats, threshold=threshold, verbose=False) self.assertNotEqual(unfiltered_result, filtered_result, "Result did not change despite threshold")
def test_all_implemented(self): ALLMETHODSSTRINGS = ["quota", "lrm", "hamilton", "largest_remainder", "dhondt", "jefferson", "saintelague", "webster", "huntington", "hill", "adams", "dean", "smallestdivisor", "harmonicmean", "equalproportions", "majorfractions", "greatestdivisors", "modified_saintelague"] votes = [1] seats = 1 for method in ALLMETHODSSTRINGS: result = app.compute(method, votes, seats, verbose=False) self.assertEqual(result, [1], msg=method + " does not exist")
def test_balinski_young_example2(self): self.longMessage = True RESULTS = {"quota": [10, 7, 5, 3, 1], "largest_remainder": [9, 7, 5, 4, 1], "dhondt": [10, 7, 5, 3, 1], "saintelague": [9, 8, 5, 3, 1], "modified_saintelague": [9, 8, 5, 3, 1], "huntington": [9, 7, 6, 3, 1], "adams": [9, 7, 5, 3, 2], "dean": [9, 7, 5, 4, 1] } votes = [9061, 7179, 5259, 3319, 1182] seats = 26 for method in RESULTS.keys(): result = app.compute(method, votes, seats, verbose=False) self.assertEqual(result, RESULTS[method], msg=method + " failed")
def test_balinski_young_example1(self): self.longMessage = True RESULTS = {"quota": [52, 44, 2, 1, 1], "largest_remainder": [51, 44, 2, 2, 1], "dhondt": [52, 45, 1, 1, 1], "saintelague": [51, 43, 2, 2, 2], "modified_saintelague": [51, 43, 2, 2, 2], "huntington": [51, 43, 2, 2, 2], "adams": [51, 43, 2, 2, 2], "dean": [51, 43, 2, 2, 2] } votes = [5117, 4400, 162, 161, 160] seats = 100 for method in RESULTS.keys(): result = app.compute(method, votes, seats, verbose=False) self.assertEqual(result, RESULTS[method], msg=method + " failed")
parties = 5 seats = 20 methods = ["quota", "largest_remainder", "dhondt", "saintelague", "adams"] iterator = combinations(range(1, maxvoters + 1), parties) for iterations, votes in enumerate(iterator): apportionments = set() for method in methods: try: # in case of ties an exception occurs because tiesallowed=False apportionments.add( tuple( app.compute(method, votes, seats, tiesallowed=False, verbose=False))) except Exception: pass if len(apportionments) == len(methods): break else: print("No vote distribution found within the parameter range.") quit() print("votes = {}".format(votes)) print("found in {} iterations\n\n".format(iterations)) for method in methods:
from __future__ import print_function import apportionment.methods as app print("Parties with surplus-vote agreements are treated as coalitions") print("See https://www.knesset.gov.il/lexicon/eng/seats_eng.htm\n") with open("knesset.txt", "r") as f: for line in f: knesset_nr, partynames, votes, officialresult, threshold = \ eval(line) print("Knesset #" + str(knesset_nr) + ":") result = app.compute("dhondt", votes, sum(officialresult), parties=partynames, threshold=threshold, verbose=True) # actual results print("Identical with official result: " + (str(tuple(result) == tuple(officialresult))) + "\n\n")
from __future__ import print_function import apportionment.methods as app votes = [1, 3, 6, 7, 78] seats = 20 print("votes", "." * (25 - len("votes")), votes, "\n") print(seats, "seats", "\n") print("apportionment results:") for method in [ "quota", "largest_remainder", "dhondt", "saintelague", "huntington", "adams", "dean" ]: result = app.compute(method, votes, seats, verbose=False) print(method, "." * (25 - len(method)), result)
from __future__ import print_function import apportionment.methods as app with open("./nr_wahlen.txt", "r") as f: for line in f: year, partynames, votes, officialresult = eval(line) print(year) result = app.compute("dhondt", votes, 183, parties=partynames, threshold=.04, verbose=True) # actual results print("Identical with official result: " + (str(tuple(result) == tuple(officialresult))) + "\n\n")