Merge pull request #4 from benjaminkrenn/master

add modified saintelague, add israel example
pull/7/head
Martin Lackner 2020-01-14 16:51:35 +01:00 zatwierdzone przez GitHub
commit 4bdce9d824
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 70 dodań i 5 usunięć

Wyświetl plik

@ -12,6 +12,7 @@ def method(method, votes, seats, parties=string.ascii_letters,
elif method in ["lrm", "hamilton", "largest_remainder"]:
return largest_remainder(filtered_votes, seats, parties, verbose)
elif method in ["dhondt", "jefferson", "saintelague", "webster",
"modified_saintelague",
"huntington", "hill", "adams", "dean",
"smallestdivisor", "harmonicmean", "equalproportions",
"majorfractions", "greatestdivisors"]:
@ -115,6 +116,10 @@ def divisor(votes, seats, method, parties=string.ascii_letters,
if verbose:
print("\nSainte Lague (Webster) method")
divisors = [2*i+1 for i in range(seats)]
elif method in ["modified_saintelague"]:
if verbose:
print("\nModified Sainte Lague (Webster) method")
divisors = [1.4] + [2*i+1 for i in range(1,seats)]
elif method in ["huntington", "hill", "equalproportions"]:
if verbose:
print("\nHuntington-Hill method")
@ -137,7 +142,7 @@ def divisor(votes, seats, method, parties=string.ascii_letters,
divisors = [i+1 for i in range(seats)]
elif method in ["dean", "harmonicmean"]:
if verbose:
print("\Dean method")
print("\nDean method")
if seats < len(votes):
representatives = __divzero_fewerseatsthanparties(votes,
seats, parties,
@ -153,7 +158,7 @@ def divisor(votes, seats, method, parties=string.ascii_letters,
if seats > sum(representatives):
weights = []
for p in votes:
if method in ["huntington", "hill"]:
if method in ["huntington", "hill", "modified_saintelague"]:
weights.append([(p / div) for div in divisors])
else:
weights.append([Fraction(p, div) for div in divisors])

Wyświetl plik

@ -0,0 +1,20 @@
from __future__ import print_function
import sys
sys.path.insert(0, '..')
import apportionment
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 = apportionment.method("dhondt", votes,
sum(officialresult),
parties=partynames,
threshold=None, # is already excluded from input
verbose=True)
# actual results
print("Identical with official result: "
+ (str(tuple(result) == tuple(officialresult)))
+ "\n\n")

Wyświetl plik

@ -0,0 +1,4 @@
[19, ['Likud Yisrael Beitenu+Habayit Hayehudi', 'Yesh Atid+Israel Labor Party', 'Shas+United Torah Judaism', 'Hatenua+Meretz', 'United Arab List', 'Hadash+National Democratic Assembly - Balad', 'Kadima'], [885163+345985, 543458+432118, 331868+195892, 189167+172403, 138450, 113439+97030, 78974], [31+12, 19+15, 11+7, 6+6, 4, 4+3, 2], 0.02]
[20, ['Likud Chaired by Benjamin Netanyahu for Prime Minister+Habayit Hayehudi Chaired by Naftali Bennett', "Zionist Camp Chaired by Isaac Herzog and Tzipi Livni+Israel's Left", 'Joint List (Hadash, National Democratic Assembly, Arab Movement for Renewal, United Arab List)', 'Yesh Atid Chaired by Yair Lapid', 'Kulanu Chaired by Moshe Kahlon+Yisrael Beitenu Chaired by Avigdor Liberman', 'Shas+United Torah Judaism'], [985408+283910, 786313+165529, 446583, 371602, 315360+214906, 241613+210143], [30+8, 24+5, 13, 11, 10+6, 7+6], 0.0325]
[21, ['Likud Chaired by Benjamin Netanyahu for Prime Minister+United Right', 'Blue and White', 'Shas+United Torah Judaism', "Hadash-Ta'al+Ra'am-Balad", 'Israeli Labor Party+Meretz', 'Yisrael Beitenu', 'Kulanu Chaired by Moshe Kahlon'], [1140370+159468, 1125881, 258275+249049, 193442+143666, 190870+156473, 173004, 152756], [35+5, 35, 8+8, 6+4, 6+4, 5, 4], 0.0325]
[22, ['Blue and White+Yisrael Beitenu', 'Likud+Yemina', 'Joint List (Hadash, Ra\xe2\x80\x99am, Ta\xe2\x80\x99al, Balad)', 'Shas+United Torah Judaism', 'Labor-Gesher+Democratic Union'], [1151214+310154, 1113617+260655, 470211, 330199+268775, 212782+192495], [33+8, 32+7, 13, 9+7, 6+5], 0.0325]

Wyświetl plik

@ -7,8 +7,8 @@ import unittest
import apportionment
METHODS = ["quota", "largest_remainder", "dhondt",
"saintelague", "huntington", "adams", "dean"]
METHODS = ["quota", "largest_remainder", "dhondt", "saintelague",
"modified_saintelague", "huntington", "adams", "dean"]
class TestApprovalMultiwinner(unittest.TestCase):
@ -19,7 +19,7 @@ class TestApprovalMultiwinner(unittest.TestCase):
"huntington", "hill", "adams", "dean",
"smallestdivisor", "harmonicmean",
"equalproportions", "majorfractions",
"greatestdivisors"]
"greatestdivisors", "modified_saintelague"]
votes = [1]
seats = 1
@ -73,6 +73,7 @@ class TestApprovalMultiwinner(unittest.TestCase):
"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]
@ -109,6 +110,41 @@ class TestApprovalMultiwinner(unittest.TestCase):
self.assertFalse(apportionment.within_quota(votes, representatives,
verbose=False))
def test_threshold(self):
votes = [41, 56, 3]
seats = 60
threshold = 0.03
filtered_votes = apportionment.apply_threshold(votes, threshold)
self.assertEqual(filtered_votes, [41, 56, 3],
"Threshold cut too much.")
threshold = 0.031
filtered_votes = apportionment.apply_threshold(votes, threshold)
self.assertEqual(filtered_votes, [41, 56, 0],
"Threshold was not applied correctly.")
method = "dhondt"
threshold = 0
unfiltered_result = apportionment.method(method, votes, seats,
threshold=threshold,
verbose=False)
threshold = 0.04
filtered_result = apportionment.method(method, votes, seats,
threshold=threshold,
verbose=False)
self.assertNotEqual(unfiltered_result, filtered_result,
"Result did not change despite threshold")
def test_saintelague_difference(self):
votes = [6, 1]
seats = 4
r1 = apportionment.method("saintelague", votes,
seats, verbose=False) # [4, 0]
r2 = apportionment.method("modified_saintelague", votes,
seats, verbose=False) # [3, 1]
self.assertNotEqual(r1, r2,
"Saintelague should produce different "+
"result than modified variant.")
if __name__ == '__main__':
unittest.main()