Download tests/test_seat_count.py from RenaissanceLabsEU/practice: direct link, hf CLI and curl.
- Browser
- Download file 8.57 kB
-
https://huggingface.co/datasets/RenaissanceLabsEU/practice/resolve/main/tests/test_seat_count.py
- Command line
-
hf download hf://datasets/RenaissanceLabsEU/practice/tests/test_seat_count.py
-
curl -L -o test_seat_count.py https://huggingface.co/datasets/RenaissanceLabsEU/practice/resolve/main/tests/test_seat_count.py
8.57 kB
| """Hand-worked cases for Seat Count. Run: python3 tests/test_seat_count.py""" | |
| import os, sys | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from fractions import Fraction | |
| from envs.seat_count import gen | |
| def near(a, b, tol=1e-6): return abs(float(a) - float(b)) <= tol | |
| # ------------------------------------------------------------ highest averages | |
| # Standard example (Wikipedia, "D'Hondt method"): A 100000, B 80000, C 30000, D 20000, 8 seats. | |
| V = {"A": 100000, "B": 80000, "C": 30000, "D": 20000} | |
| def test_dhondt(): | |
| # A: 100000 50000 33333 25000 20000 B: 80000 40000 26667 20000 C: 30000 15000 D: 20000 | |
| # eight largest: 100000 80000 50000 40000 33333 30000 26667 25000 -> A 4, B 3, C 1, D 0 | |
| alloc, qs = gen.highest_averages(V, 8, "dhondt") | |
| assert alloc == {"A": 4, "B": 3, "C": 1, "D": 0}, alloc | |
| assert qs[7] == (Fraction(25000), "A") # last winning quotient is A's 100000 / 4 | |
| assert qs[8][0] == 20000 # first losing quotient (three-way tie, so not a generated case) | |
| def test_sainte_lague(): | |
| # A: 100000 33333 20000 14286 B: 80000 26667 16000 C: 30000 10000 D: 20000 | |
| # eight largest: 100000 80000 33333 30000 26667 20000(A) 20000(D) 16000(B) -> A 3, B 3, C 1, D 1 | |
| alloc, qs = gen.highest_averages(V, 8, "sainte-lague") | |
| assert alloc == {"A": 3, "B": 3, "C": 1, "D": 1}, alloc | |
| assert qs[7] == (Fraction(16000), "B") and near(qs[8][0], 100000 / 7.0) | |
| def test_modified_sainte_lague(): | |
| # 7 seats. Plain Sainte-Lague: 100000 80000 33333 30000 26667 20000(A) 20000(D) -> A 3, B 2, C 1, D 1. | |
| # First divisor 1.4: A 71429 33333 20000 B 57143 26667 16000 C 21429 D 14286 | |
| # seven largest: 71429 57143 33333 26667 21429 20000 16000 -> A 3, B 3, C 1, D 0 (D's first seat is harder to win). | |
| assert gen.highest_averages(V, 7, "sainte-lague")[0] == {"A": 3, "B": 2, "C": 1, "D": 1} | |
| alloc, qs = gen.highest_averages(V, 7, "modified-sainte-lague", 1.4) | |
| assert alloc == {"A": 3, "B": 3, "C": 1, "D": 0}, alloc | |
| assert qs[0] == (Fraction(100000) / Fraction(7, 5), "A") and near(qs[0][0], 71428.571429) | |
| assert qs[6] == (Fraction(16000), "B") | |
| # first divisor 1.2: C's first quotient is 30000 / 1.2 = 25000, D's is 16666.67 | |
| # seven largest: 83333 66667 33333 26667 25000 20000 16667(D) -> A 3, B 2, C 1, D 1 | |
| assert gen.highest_averages(V, 7, "modified-sainte-lague", 1.2)[0] == {"A": 3, "B": 2, "C": 1, "D": 1} | |
| def test_threshold_then_dhondt(): | |
| # X 5200, Y 3100, Z 1300, W 400: 10000 valid votes. A 5% threshold removes W (4%). | |
| # X: 5200 2600 1733 1300 Y: 3100 1550 1033 Z: 1300 -> five largest 5200 3100 2600 1733 1550 -> X 3, Y 2, Z 0 | |
| votes = {"X": 5200, "Y": 3100, "Z": 1300, "W": 400} | |
| qual = dict((p, v) for p, v in votes.items() if 100.0 * v / 10000 >= 5) | |
| alloc, qs = gen.highest_averages(qual, 5, "dhondt") | |
| assert alloc == {"X": 3, "Y": 2, "Z": 0} | |
| assert qs[4] == (Fraction(1550), "Y") and qs[5][0] == 1300 | |
| assert not gen.clear_quotients(gen.highest_averages(V, 8, "dhondt")[1], 8) # the tie at 20000 is rejected | |
| # ------------------------------------------------------------ largest remainder | |
| # Standard example (Wikipedia, "Largest remainder method"): 100000 votes, 10 seats. | |
| W = {"Yellows": 47000, "Whites": 16000, "Reds": 15800, "Greens": 12000, "Blues": 6100, "Pinks": 3100} | |
| def test_hare(): | |
| # Q = 100000 / 10 = 10000. Whole quotas 4 1 1 1 0 0 (7 seats). Remainders 7000 6000 5800 2000 6100 3100. | |
| # Three seats left: Yellows (7000), Blues (6100), Whites (6000) -> 5 2 1 1 1 0 | |
| total, auto, extra, q, margin, left = gen.largest_remainder(W, 10, "hare") | |
| assert q == 10000 and left == 3 | |
| assert auto == {"Yellows": 4, "Whites": 1, "Reds": 1, "Greens": 1, "Blues": 0, "Pinks": 0} | |
| assert sorted(extra) == ["Blues", "Whites", "Yellows"] | |
| assert total == {"Yellows": 5, "Whites": 2, "Reds": 1, "Greens": 1, "Blues": 1, "Pinks": 0} | |
| assert near(margin, (6000 - 5800) / 10000.0) | |
| def test_droop(): | |
| # Q = floor(100000 / 11) + 1 = 9090 + 1 = 9091. Whole quotas 5 1 1 1 0 0 (8 seats). | |
| # Remainders 47000 - 45455 = 1545, 6909, 6709, 2909, 6100, 3100. Two left: Whites, Reds -> 5 2 2 1 0 0 | |
| total, auto, extra, q, margin, left = gen.largest_remainder(W, 10, "droop") | |
| assert q == 9091 and left == 2 | |
| assert auto["Yellows"] == 5 and sorted(extra) == ["Reds", "Whites"] | |
| assert total == {"Yellows": 5, "Whites": 2, "Reds": 2, "Greens": 1, "Blues": 0, "Pinks": 0} | |
| assert near(margin, (6709 - 6100) / 9091.0) | |
| def test_small_both_quotas(): | |
| # 420, 310, 170, 100 (1000 votes), 5 seats. | |
| # Hare Q = 200: whole 2 1 0 0, remainders 20 110 170 100, two left -> C, B: 2 2 1 0 | |
| # Droop Q = floor(1000 / 6) + 1 = 167: whole 2 1 1 0, remainders 86 143 3 100, one left -> B: 2 2 1 0 | |
| v = {"A": 420, "B": 310, "C": 170, "D": 100} | |
| total, auto, extra, q, margin, left = gen.largest_remainder(v, 5, "hare") | |
| assert (q, auto, sorted(extra), total) == (200, {"A": 2, "B": 1, "C": 0, "D": 0}, ["B", "C"], {"A": 2, "B": 2, "C": 1, "D": 0}) | |
| assert near(margin, 0.05) | |
| total, auto, extra, q, margin, left = gen.largest_remainder(v, 5, "droop") | |
| assert (q, auto, extra, total) == (167, {"A": 2, "B": 1, "C": 1, "D": 0}, ["B"], {"A": 2, "B": 2, "C": 1, "D": 0}) | |
| # Hare quota need not be whole: 1000 votes, 3 seats -> 333.33 | |
| assert gen.quota(1000, 3, "hare") == Fraction(1000, 3) and gen.quota(1000, 3, "droop") == 251 | |
| # ------------------------------------------------------------ Gallagher index | |
| def test_gallagher(): | |
| # votes 40 30 20 10 %, seats 5 3 2 0 of 10 = 50 30 20 0 %: squares 100 0 0 100 -> sqrt(0.5 * 200) = 10 | |
| assert near(gen.gallagher({"a": 40, "b": 30, "c": 20, "d": 10}, {"a": 5, "b": 3, "c": 2}), 10.0) | |
| # perfectly proportional -> 0 | |
| assert near(gen.gallagher({"a": 600, "b": 400}, {"a": 3, "b": 2}), 0.0) | |
| # votes 45 35 20 %, seats 5 4 1 of 10 = 50 40 10 %: squares 25 25 100 -> sqrt(75) = 8.660254 | |
| assert near(gen.gallagher({"a": 45, "b": 35, "c": 20}, {"a": 5, "b": 4, "c": 1}), 8.660254) | |
| # votes 60 40 %, seats 7 3: squares 100 100 -> 10 | |
| assert near(gen.gallagher({"a": 6000, "b": 4000}, {"a": 7, "b": 3}), 10.0) | |
| # ------------------------------------------------------------ levelling seats | |
| NAT = {"P": 5000, "Q": 3000, "R": 2000} | |
| def test_levelling_plain(): | |
| # 8 fixed (5 2 1) + 2 levelling = 10 seats, Sainte-Lague on 5000 3000 2000: | |
| # P 5000 1667 1000 714 556 | 455 Q 3000 1000 600 | 429 R 2000 667 | 400 -> entitlement 5 3 2 | |
| # nobody is over, so levelling = 0 1 1 | |
| total, used = gen.levelling(NAT, {"P": 5, "Q": 2, "R": 1}, 10, "sainte-lague") | |
| assert total == {"P": 5, "Q": 3, "R": 2} and len(used) == 1 | |
| def test_levelling_overhang(): | |
| # 10 fixed (7 2 1) + 2 levelling = 12 seats. Entitlement on 12: the next quotients are 455 (P) and 429 (Q) -> 6 4 2. | |
| # P holds 7 > 6: it keeps 7 and is set aside. 12 - 7 = 5 seats for Q and R: | |
| # Q 3000 1000 600 | 429 R 2000 667 | 400 -> 3 and 2. Totals 7 3 2, levelling 0 1 1. | |
| total, used = gen.levelling(NAT, {"P": 7, "Q": 2, "R": 1}, 12, "sainte-lague") | |
| assert total == {"P": 7, "Q": 3, "R": 2} and len(used) == 2 | |
| def test_levelling_cascade(): | |
| # D'Hondt, 10 fixed (6 4 0) + 1 levelling = 11 seats. | |
| # P 5000 2500 1667 1250 1000 833 | 714 Q 3000 1500 1000 | 750 R 2000 1000 | 667 -> entitlement 6 3 2 | |
| # Q holds 4 > 3: set aside, 7 seats left for P and R: 5000 2500 2000 1667 1250 1000(P) 1000(R) -> 5 and 2 | |
| # now P holds 6 > 5: set aside, 11 - 4 - 6 = 1 seat left for R -> 1. Totals 6 4 1. | |
| total, used = gen.levelling(NAT, {"P": 6, "Q": 4, "R": 0}, 11, "dhondt") | |
| assert total == {"P": 6, "Q": 4, "R": 1} and len(used) == 3 | |
| def test_generated_tasks(): | |
| for fam in sorted(gen.FAMILIES): | |
| for d in (1, 2, 3): | |
| for seed in range(8): | |
| t = gen.generate(seed, d, fam) | |
| assert 6 <= len(t.checks) <= 40, (t.id, len(t.checks)) | |
| assert "—" not in t.instruction | |
| if fam == "constituencies" and d == 3: # levelling seats add up to the stated number | |
| import json | |
| lev = sum(c["value"] for c in t.checks if c["path"].startswith("levelling_seats.")) | |
| assert lev == json.loads(t.files["election.json"])["levelling_seats"] | |
| if __name__ == "__main__": | |
| tests = [v for k, v in sorted(globals().items()) if k.startswith("test_")] | |
| for t in tests: t() | |
| print("test_seat_count: %d tests passed" % len(tests)) | |