forked from ppau/schulze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schulze.py
396 lines (316 loc) · 12.1 KB
/
schulze.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python3
#--*-- coding:utf8 --*--
from collections import defaultdict, Counter
from urllib.parse import quote
from io import StringIO
import json
import sys
import argparse
import uuid
"""
Ballots are csv's.
- First line contains the candidates
- Consecutive lines contain preferences in candidate order
Example:
smith,jones,james
1,2,3
,,1
,1,2
,4,4
"""
def load_ballots(f):
lines = f.readlines()
header = lines[0].strip().split(',')
ballots = [line.strip().split(',') for line in lines[1:]]
return header, ballots
def withdraw_candidate(candidate, candidates, ballots):
if candidate not in candidates:
return
x = candidates.index(candidate)
del candidates[x]
for ballot in ballots:
del ballot[x]
def check_ballot(candidates, ballot):
for i in range(len(ballot)):
if ballot[i] == '':
ballot[i] = None
continue
try:
if int(ballot[i]) <= 0:
return False
except:
return False
ballot[i] = int(ballot[i])
return True
def build_matrix(size):
x = [[0 for j in range(size)] for i in range(size)]
for i in range(size):
x[i][i] = None
return x
def print_matrix(matrix):
for line in matrix:
print(("+----" * len(line)) + "+")
for n in line:
if n is None: n = 'X'
sys.stdout.write("|" + "{:^4}".format(n))
print("|")
print(("+----" * len(line)) + "+")
def calculate_first_prefs(candidates, ballots):
first_prefs = Counter()
for ballot in ballots:
if not check_ballot(candidates, ballot):
print("Invalid ballot: %s" % ballot)
del ballot
continue
highest = []
for a in range(len(ballot)):
if ballot[a] is None:
pass
elif len(highest) < 1 or ballot[a] < ballot[highest[0]]:
highest = [a]
elif ballot[a] == ballot[highest[0]]:
highest.append(a)
for i in highest:
first_prefs[candidates[i]] += 1
return first_prefs
def print_first_prefs(candidates, ballots):
fp = calculate_first_prefs(candidates, ballots)
print("Total ballots: %s" % len(ballots))
for name, value in fp.most_common():
print("{:>12}: {:<2} [All: {}%] [Compared: {}%]".format(
name,
value,
"%.2f" % (value / len(ballots) * 100),
"%.2f" % (value / sum(fp.values()) * 100)
))
def count_ballots(candidates, ballots, show_errors=False):
count = build_matrix(len(candidates))
for ballot in ballots:
if not check_ballot(candidates, ballot):
if show_errors:
print("Invalid ballot: %s" % ballot)
continue
for a in range(len(ballot)):
for b in range(len(ballot)):
# Skip same number
if a == b:
continue
# Both equal, neither are lt, thus fail
elif ballot[a] == ballot[b]:
continue
# Both blank, neither are lt, thus fail
elif ballot[a] is ballot[b] is None:
continue
# If a is blank, fail
elif ballot[a] is None:
continue
# all ints < blank or x < y
elif ballot[b] is None or ballot[a] < ballot[b]:
count[a][b] += 1
return count
def calculate_strongest_paths(count):
paths = build_matrix(len(count))
for i in range(len(count)):
for j in range(len(count)):
if i == j: continue
if count[i][j] > count[j][i]:
paths[i][j] = count[i][j]
for i in range(len(count)):
for j in range(len(count)):
if i == j: continue
for k in range(len(count)):
if i != k and j != k:
paths[j][k] = max(paths[j][k], min(paths[j][i], paths[i][k]))
return paths
def determine_rankings(paths):
ranking = [0 for i in range(len(paths))]
for i in range(len(paths)):
for j in range(len(paths)):
if i == j: continue
if paths[i][j] > paths[j][i]:
ranking[i] += 1
return ranking
def break_ties(paths, ranking):
scores = [0 for i in range(len(paths))]
for i in range(len(paths)):
if ranking[i] == 0:
continue
for j in range(len(paths)):
if i == j: continue
if paths[i][j] > paths[j][i]:
scores[i] += paths[i][j] - paths[j][i]
return scores
def print_rankings(candidates, rankings, winner_only=False):
count = Counter()
for i in range(len(candidates)):
count[i] = rankings[i]
count = count.most_common()
if winner_only:
print(candidates[count[0][0]])
return
c = 0
print("Rankings:\n")
for k, v in count:
c += 1
print("(%s) %s" % (c, candidates[k]))
def output(candidates, paths, rankings, count, tie, winner_only=False, html=False, urlencode=False, hide_grids=False):
if html:
print(convert_matrix_to_html_table(candidates, count, urlencode))
#print(strongest_path_html(candidates, paths))
print("<pre>")
if tie:
print("Tie detected. Ranking by strongest path scores:\n")
for i in range(len(candidates)):
print("%s: %s" % (candidates[i], rankings[i]))
print()
print_rankings(candidates, rankings, winner_only)
print("</pre>")
return
if not winner_only and not hide_grids:
print("Matrix order (left to right & top to bottom):")
print('- ' + '\n- '.join(candidates))
print()
print("Count matrix:")
print_matrix(count)
print()
print("Path matrix:")
print_matrix(paths)
print()
if tie:
print("Tie detected. Ranking by combined strongest path scores:\n")
for i in range(len(candidates)):
print("%s: %s" % (candidates[i], rankings[i]))
print()
print_rankings(candidates, rankings, winner_only)
def run_election(f, *withdraws, winner_only=False, hide_grids=False, first_prefs=False, html=False, urlencode=False, show_errors=False):
if isinstance(f, str):
f = StringIO(f)
candidates, ballots = load_ballots(f)
for w in withdraws:
withdraw_candidate(w, candidates, ballots)
if first_prefs:
print_first_prefs(candidates, ballots)
return
count = count_ballots(candidates, ballots, show_errors)
paths = calculate_strongest_paths(count)
rankings = determine_rankings(paths)
# check to see if highest rank is shared
tie = False
if rankings.count(max(rankings)) > 1:
tie = True
rankings = break_ties(count, rankings)
output(candidates, paths, rankings, count, tie, winner_only, html, urlencode, hide_grids)
def strongest_path_html(candidates, matrix):
cross = "×"
info = "These are the strongest paths. This is required to determine a winner in the case of no obvious majority pairwise winner."
headers = "<tr><th class='empty'></th><th><div>" +\
"</div></th><th><div>".join(candidates) + "</div></th></tr>"
x = []
for i in range(len(matrix)):
row = []
for j in range(len(matrix[i])):
if i == j:
row.append("<td></td>")
continue
total = matrix[i][j] + matrix[j][i]
if total != 0:
success = int(matrix[i][j] / total * 100)
fail = 100 - success
else:
success = fail = 0
if matrix[i][j] < matrix[j][i]:
row.append("<td class='red'>%s</td>" % cross)
elif matrix[i][j] > matrix[j][i]:
row.append("<td class='green'>%s</td>" % matrix[i][j])
elif matrix[i][j] == matrix[j][i]:
row.append("<td class='yellow'>%s</td>" % cross)
x.append("<tr><th>%s</th>%s</tr>" % (candidates[i], "".join(row)))
return ("<table><caption>%s</caption><thead>%s</thead><tbody>" % (info, headers)) + "".join(x) + "</tbody></table>"
def convert_matrix_to_html_table(candidates, matrix, urlencode):
info = "The horizontal axis is compared with the vertical axis to see who is the most preferred candidate. The ranking order is determined by the candidates with the most green squares."
headers = '<tr><th class="empty"></th><th><div>' +\
"</div></th><th><div>".join(candidates) + "</div></th></tr>"
x = []
data = {}
for i in range(len(matrix)):
row = []
for j in range(len(matrix[i])):
if i == j:
row.append("<td></td>")
continue
title = candidates[i] + ' vs ' + candidates[j]
total = matrix[i][j] + matrix[j][i]
if total != 0:
success = int(matrix[i][j] / total * 100)
fail = 100 - success
else:
success = fail = 0
content = ('''<p>%s voters prefer %s over %s.</p><div class='progress'>''' +\
'''<div class='bar bar-success' style='width: %s%%'>%s</div>''' +\
'''<div class='bar bar-danger' style='width: %s%%'>%s</div>''' +\
'''</div>''') % (matrix[i][j], candidates[i], candidates[j],
success, "%s (%s%%)" % (matrix[i][j], success),
fail, "%s (%s%%)" % (matrix[j][i], fail))
data[title] = content
if matrix[i][j] < matrix[j][i]:
row.append('<td title="%s" class="red">%s</td>' % (title, matrix[i][j]))
elif matrix[i][j] > matrix[j][i]:
row.append('<td title="%s" class="green">%s</td>' % (title, matrix[i][j]))
elif matrix[i][j] == matrix[j][i]:
row.append('<td title="%s" class="yellow">%s</td>' % (title, matrix[i][j]))
x.append("<tr><th>%s</th>%s</tr>" % (candidates[i], "".join(row)))
uniq = uuid.uuid4().hex
out = """<script data-cfasync="false">var electionData{uniq} = {script}\n</script>
<table id='election-{uniq}' class="ranking">
<caption>{caption}</caption>
<thead>{thead}</thead>
<tbody>{tbody}</tbody>
</table>""".format(
uniq=uniq,
script=script_data(data, urlencode),
caption=info,
thead=headers,
tbody="".join(x)
)
out += """<script data-cfasync="false">
$("#election-{uniq} thead th").each(function() {{
var div = $(this).find("div");
div.addClass('vertical');
$(this).height(div.width());
div.css('width', '1em');
}});
$("#election-{uniq} [title]").each(function() {{
var self = $(this);
$(this).popover({{
placement: 'bottom',
container: ".entry-content",
trigger: "hover",
animation: false,
html: true,
content: electionData{uniq}[self.attr('title')]
}});
}});</script>""".format(uniq=uniq)
return out
def script_data(data, urlencode):
if not urlencode:
return json.dumps(data, indent=2)
else:
return 'JSON.parse(decodeURIComponent("%s"))' % quote(json.dumps(data))
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument('-H', '--html', action='store_true',
help="Output HTML")
p.add_argument('-U', '--urlencode', action='store_true')
p.add_argument('-w', '--winner-only', action='store_true')
p.add_argument('-s', '--hide-grids', action='store_true')
p.add_argument('-f', '--first-prefs', action='store_true')
p.add_argument('--show-errors', action='store_true')
p.add_argument('--withdraw', nargs='*',
help="Candidates to withdraw from election")
p.add_argument('ballots', type=argparse.FileType('r'))
args = dict(p.parse_args()._get_kwargs())
ballots = args.get('ballots')
withdraw = args.get('withdraw') or []
del args['withdraw']
del args['ballots']
run_election(ballots, *withdraw, **args)