-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapportionment.py
300 lines (263 loc) · 11.6 KB
/
apportionment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
""" Apportionment methods
Martin Lackner
https://github.com/martinlackner/apportionment/
"""
from __future__ import print_function, division
import string
import math
try:
from gmpy2 import mpq as Fraction
except ImportError:
# slower
from fractions import Fraction
METHODS = ["quota", "largest_remainder", "dhondt", "saintelague",
"modified_saintelague", "huntington", "adams", "dean"]
class TiesException(Exception):
pass
def compute(method, votes, seats, parties=string.ascii_letters,
threshold=None, tiesallowed=True, verbose=True):
filtered_votes = apply_threshold(votes, threshold)
if method == "quota":
return quota(filtered_votes, seats, parties, tiesallowed, verbose)
elif method in ["lrm", "hamilton", "largest_remainder"]:
return largest_remainder(filtered_votes, seats, parties,
tiesallowed, verbose)
elif method in ["dhondt", "jefferson", "saintelague", "webster",
"modified_saintelague",
"huntington", "hill", "adams", "dean",
"smallestdivisor", "harmonicmean", "equalproportions",
"majorfractions", "greatestdivisors"]:
return divisor(filtered_votes, seats, method, parties,
tiesallowed, verbose)
else:
raise NotImplementedError("apportionment method " + method +
" not known")
def apply_threshold(votes, threshold):
"""Sets vote counts to 0 if threshold is not met."""
if threshold is not None:
v = []
combined_votes = sum(votes)
min_votes = combined_votes * threshold
for vote in votes:
if vote < min_votes:
v.append(0)
else:
v.append(vote)
return v
else:
return votes
def __print_results(representatives, parties):
for i in range(len(representatives)):
print(" " + str(parties[i]) + ": " + str(representatives[i]))
# verifies whether a given assignment of representatives
# is within quota
def within_quota(votes, representatives, parties=string.ascii_letters,
verbose=True):
n = sum(votes)
seats = sum(representatives)
within = True
for i in range(len(votes)):
upperquota = int(math.ceil(float(votes[i]) * seats / n))
if representatives[i] > upperquota:
if verbose:
print("upper quota of party", parties[i],
"violated: quota is", float(votes[i]) * seats / n,
"but has", representatives[i], "representatives")
within = False
lowerquota = int(math.floor(float(votes[i]) * seats / n))
if representatives[i] < lowerquota:
if verbose:
print("lower quota of party", parties[i],
"violated: quota is", float(votes[i]) * seats / n,
"but has only", representatives[i], "representatives")
within = False
return within
# Largest remainder method (Hamilton method)
def largest_remainder(votes, seats, parties=string.ascii_letters,
tiesallowed=True, verbose=True):
if verbose:
print("\nLargest remainder method with Hare quota (Hamilton)")
q = Fraction(sum(votes), seats)
quotas = [Fraction(p, q) for p in votes]
representatives = [int(qu.numerator) // int(qu.denominator)
for qu in quotas]
ties = False
if sum(representatives) < seats:
remainders = [a - b for a, b in zip(quotas, representatives)]
cutoff = sorted(
remainders, reverse=True)[seats - sum(representatives) - 1]
tiebreaking_message = (" tiebreaking in order of: " +
str(parties[:len(votes)]) +
"\n ties broken in favor of: ")
for i in range(len(votes)):
if sum(representatives) == seats and remainders[i] >= cutoff:
if not ties:
tiebreaking_message = tiebreaking_message[:-2]
tiebreaking_message += "\n to the disadvantage of: "
ties = True
tiebreaking_message += parties[i] + ", "
elif sum(representatives) < seats and remainders[i] > cutoff:
representatives[i] += 1
elif sum(representatives) < seats and remainders[i] == cutoff:
tiebreaking_message += parties[i] + ", "
representatives[i] += 1
if ties and verbose:
print(tiebreaking_message[:-2])
if ties and not tiesallowed:
raise TiesException("Tie occurred")
if verbose:
__print_results(representatives, parties)
return representatives
# Divisor methods
def divisor(votes, seats, method, parties=string.ascii_letters,
tiesallowed=True, verbose=True):
representatives = [0] * len(votes)
if method in ["dhondt", "jefferson", "greatestdivisors"]:
if verbose:
print("\nD'Hondt (Jefferson) method")
divisors = [i + 1 for i in range(seats)]
elif method in ["saintelague", "webster", "majorfractions"]:
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")
if seats < len(votes):
representatives = __divzero_fewerseatsthanparties(
votes, seats, parties, tiesallowed, verbose)
else:
representatives = [1 if p > 0 else 0 for p in votes]
divisors = [math.sqrt((i + 1) * (i + 2)) for i in range(seats)]
elif method in ["adams", "smallestdivisor"]:
if verbose:
print("\nAdams method")
if seats < len(votes):
representatives = __divzero_fewerseatsthanparties(
votes, seats, parties, tiesallowed, verbose)
else:
representatives = [1 if p > 0 else 0 for p in votes]
divisors = [i + 1 for i in range(seats)]
elif method in ["dean", "harmonicmean"]:
if verbose:
print("\nDean method")
if seats < len(votes):
representatives = __divzero_fewerseatsthanparties(
votes, seats, parties, tiesallowed, verbose)
else:
representatives = [1 if p > 0 else 0 for p in votes]
divisors = [Fraction(2 * (i + 1) * (i + 2), 2 * (i + 1) + 1)
for i in range(seats)]
else:
raise NotImplementedError("divisor method " + method + " not known")
# assigning representatives
if seats > sum(representatives):
weights = []
for p in votes:
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])
flatweights = sorted([w for l in weights for w in l], reverse=True)
minweight = flatweights[seats - sum(representatives) - 1]
for i in range(len(votes)):
representatives[i] += len([w for w in weights[i] if w > minweight])
ties = False
# dealing with ties
if seats > sum(representatives):
tiebreaking_message = (" tiebreaking in order of: " +
str(parties[:len(votes)]) +
"\n ties broken in favor of: ")
for i in range(len(votes)):
if sum(representatives) == seats and minweight in weights[i]:
if not ties:
if not tiesallowed:
raise TiesException("Tie occurred")
tiebreaking_message = tiebreaking_message[:-2]
tiebreaking_message += "\n to the disadvantage of: "
ties = True
tiebreaking_message += parties[i] + ", "
if sum(representatives) < seats and minweight in weights[i]:
tiebreaking_message += parties[i] + ", "
representatives[i] += 1
if ties and verbose:
print(tiebreaking_message[:-2])
if ties and not tiesallowed:
raise TiesException("Tie occurred")
if verbose:
__print_results(representatives, parties)
return representatives
# required for methods with 0 divisors (Adams, Huntington-Hill)
def __divzero_fewerseatsthanparties(votes, seats, parties,
tiesallowed, verbose):
representatives = [0] * len(votes)
if verbose:
print(" fewer seats than parties; " + str(seats) +
" strongest parties receive one seat")
tiebreaking_message = " ties broken in favor of: "
ties = False
mincount = sorted(votes, reverse=True)[seats - 1]
for i in range(len(votes)):
if sum(representatives) < seats and votes[i] >= mincount:
if votes[i] == mincount:
tiebreaking_message += parties[i] + ", "
representatives[i] = 1
elif sum(representatives) == seats and votes[i] >= mincount:
if not ties:
tiebreaking_message = tiebreaking_message[:-2]
tiebreaking_message += "\n to the disadvantage of: "
ties = True
tiebreaking_message += parties[i] + ", "
if ties and not tiesallowed:
raise TiesException("Tie occurred")
if ties and verbose:
print(tiebreaking_message[:-2])
return representatives
# The quota method
# ( see Balinski, M. L., & Young, H. P. (1975).
# The quota method of apportionment.
# The American Mathematical Monthly, 82(7), 701-730.)
def quota(votes, seats, parties=string.ascii_letters,
tiesallowed=True, verbose=True):
if verbose:
print("\nQuota method")
representatives = [0] * len(votes)
while sum(representatives) < seats:
quotas = [Fraction(votes[i], representatives[i] + 1)
for i in range(len(votes))]
# check if upper quota is violated
for i in range(len(votes)):
upperquota = int(math.ceil(float(votes[i]) *
(sum(representatives) + 1)
/ sum(votes)))
if representatives[i] >= upperquota:
quotas[i] = 0
maxquotas = [i for i in range(len(votes))
if quotas[i] == max(quotas)]
nextrep = maxquotas[0]
representatives[nextrep] += 1
if len(maxquotas) > 1 and not tiesallowed:
raise TiesException("Tie occurred")
# print tiebreaking information
if verbose and len(maxquotas) > 1:
quotas_now = [Fraction(votes[i], representatives[i] + 1)
for i in range(len(votes))]
tiebreaking_message = (" tiebreaking in order of: " +
str(parties[:len(votes)]) +
"\n ties broken in favor of: ")
ties_favor = [i for i in range(len(votes))
if quotas_now[i] == quotas_now[nextrep]]
for i in ties_favor:
tiebreaking_message += str(parties[i]) + ", "
tiebreaking_message = (tiebreaking_message[:-2] +
"\n to the disadvantage of: ")
for i in maxquotas[1:]:
tiebreaking_message += str(parties[i]) + ", "
print(tiebreaking_message[:-2])
if verbose:
__print_results(representatives, parties)
return representatives