-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_star.py
643 lines (522 loc) · 19.9 KB
/
test_star.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import random
import numpy as np
import pytest
import test_score
from hypothesis import given
from numpy.testing import assert_array_equal
from elsim.methods import matrix_from_scores, star
score_ballots = test_score.score_ballots
def collect_random_results(method, election):
"""
Run multiple elections with tiebreaker='random' and collect the set of all
winners.
"""
random.seed(2014) # Deterministic test
winners = set()
for trial in range(10):
winner = method(election, tiebreaker='random')
assert isinstance(winner, int)
winners.add(winner)
return winners
@pytest.mark.parametrize("method", [star])
@pytest.mark.parametrize("tiebreaker", [None, 'random', 'order'])
def test_basic(tiebreaker, method):
# Standard Tennessee example
# https://en.wikipedia.org/wiki/STAR_voting#Example
# https://electowiki.org/wiki/STAR_voting#Example
# Memphis Nashville Chattanooga Knoxville
election = [*42*[[5, 2, 1, 0]],
*26*[[0, 5, 2, 1]],
*15*[[0, 3, 5, 3]],
*17*[[0, 2, 4, 5]],
]
assert method(election, tiebreaker) == 1 # Nashville
# https://github.com/Equal-Vote/star-core/blob/master/src/Tests/ties.test.js
@pytest.mark.parametrize("method", [star])
@pytest.mark.parametrize("tiebreaker", [None, 'random', 'order'])
def test_star_condorcet_winner(tiebreaker, method):
Allison, Bill, Carmen, Doug = 0, 1, 2, 3
election = [[5, 2, 1, 4],
[5, 2, 1, 0],
[5, 2, 1, 0],
[5, 2, 1, 0],
[5, 3, 4, 0],
[5, 1, 4, 0],
[5, 1, 4, 0],
[4, 0, 5, 1],
[3, 4, 5, 0],
[3, 5, 5, 5]]
# expected = [["Allison"], ["Carmen"], ["Bill", "Doug"]];
assert method(election, tiebreaker) == Allison
@pytest.mark.parametrize("method", [star])
@pytest.mark.parametrize("tiebreaker", [None, 'random', 'order'])
def test_star_runner_up_tie(tiebreaker, method):
Allison, Bill, Carmen, Doug = 0, 1, 2, 3
election = [[5, 4, 3, 3],
[4, 5, 1, 1],
[4, 5, 1, 2],
[3, 5, 1, 0],
[5, 4, 3, 0],
[5, 0, 4, 1],
[5, 0, 4, 0],
[4, 0, 5, 1],
[3, 4, 5, 0],
[3, 5, 5, 4]]
# expected = [["Allison"], ["Bill", "Carmen"], ["Doug"]];
assert method(election, tiebreaker) == Allison
@pytest.mark.parametrize("method", [star])
def test_star_true_tie(method):
Allison, Bill, Carmen, Doug = 0, 1, 2, 3
election = [[5, 4, 1, 4],
[5, 4, 1, 4],
[2, 4, 1, 2],
[4, 3, 2, 1],
[0, 5, 4, 4],
[3, 2, 4, 2],
[3, 1, 5, 3],
[3, 1, 5, 3],
[1, 3, 2, 2],
[4, 3, 5, 5]]
# expected = [["Allison", "Bill", "Carmen"], [], ["Doug"]];
# No tiebreaker:
assert method(election, tiebreaker=None) is None
# Mode 'order' should always prefer lowest candidate ID
assert method(election, tiebreaker='order') == Allison
# Mode 'random' should choose all tied candidates at random
assert collect_random_results(method, election) == {Allison, Bill, Carmen}
@pytest.mark.parametrize("method", [star])
@pytest.mark.parametrize("tiebreaker", [None, 'random', 'order'])
def test_star_PR_ties(tiebreaker, method):
Allison, Bill, Carmen, Doug = 0, 1, 2, 3
election = [[5, 4, 1, 4],
[5, 4, 1, 4],
[2, 4, 1, 2],
[4, 3, 2, 1],
[0, 5, 4, 4],
[3, 2, 4, 2],
[3, 1, 5, 3],
[3, 1, 5, 3],
[1, 3, 2, 2],
[4, 3, 5, 5]]
# expectedWinners = ["Allison", "Carmen", "Bill"];
# No tiebreaker:
assert method(election, tiebreaker=None) is None
# Mode 'order' should always prefer lowest candidate ID
assert method(election, tiebreaker='order') == Allison
# Mode 'random' should choose all tied candidates at random
assert collect_random_results(method, election) == {Allison, Bill, Carmen}
@pytest.mark.parametrize("method", [star])
@pytest.mark.parametrize("tiebreaker", [None, 'random', 'order'])
def test_rangevoting_org_examples(tiebreaker, method):
# Every example from https://rangevoting.org/StarVoting.html
# (https://web.archive.org/web/20201105181703/ in case of changes)
# '51% vote "A5, B0, C4," while 49% vote "A0, B5, C4."'
# A B C
election = [*51*[[5, 0, 4]],
*49*[[0, 5, 4]],
]
assert method(election, tiebreaker) == 0 # "A wins that runoff"
# '51% vote "A5, B0, C4, D4" while 49% vote "A0, B5, C4, D3."'
# A B C D
election = [*51*[[5, 0, 4, 4]],
*49*[[0, 5, 4, 3]],
]
assert method(election, tiebreaker) == 2 # "whereupon C wins"
# District-partitioning paradox
# A B C D E
election = [*5*[[3, 3, 4, 0, 5]],
*3*[[5, 5, 3, 0, 0]],
*1*[[5, 3, 3, 0, 0]],
]
assert method(election, tiebreaker) == 0 # "which A wins"
# A B C D E
election = [*5*[[3, 0, 4, 3, 5]],
*3*[[5, 0, 3, 5, 0]],
*1*[[5, 0, 3, 3, 0]],
]
assert method(election, tiebreaker) == 0 # "Also A wins the East"
# A B C D E
election = [*5*[[3, 3, 4, 0, 5]],
*3*[[5, 5, 3, 0, 0]],
*1*[[5, 3, 3, 0, 0]],
*5*[[3, 0, 4, 3, 5]],
*3*[[5, 0, 3, 5, 0]],
*1*[[5, 0, 3, 3, 0]],
]
assert method(election, tiebreaker) == 2 # "C wins the countrywide"
# "No-show" paradox:
# A B C D E
election = [*4*[[3, 2, 4, 5, 0]],
*3*[[3, 5, 0, 0, 4]],
*2*[[3, 1, 4, 0, 5]],
]
assert method(election, tiebreaker) == 0 # "A wins"
# "one new voter comes"
# A B C D E
election = [*4*[[3, 2, 4, 5, 0]],
*3*[[3, 5, 0, 0, 4]],
*2*[[3, 1, 4, 0, 5]],
*1*[[5, 0, 2, 0, 0]],
]
assert method(election, tiebreaker) == 2 # "C wins"
# "loser B was a convicted multiple murderer"
# A C D E
election = [*4*[[3, 4, 5, 0]],
*3*[[3, 0, 0, 4]],
*2*[[3, 4, 0, 5]],
]
assert method(election, tiebreaker) == 1 # "C wins"
# "mucho goofiness"
# A B C
election = [* 9*[[5, 1, 0]],
*12*[[0, 5, 1]],
* 8*[[1, 0, 5]],
]
assert method(election, tiebreaker) == 0 # "A wins"
# "LESS IS MORE"
# A B C
# election = [* 9*[[5, 1, 0]],
# *10*[[0, 5, 1]],
# * 2*[[3, 0, 5]],
# * 8*[[1, 0, 5]],
# ]
# assert method(election, tiebreaker) == 1 # "That makes B win with STAR"
# Not correct. Tallies are [59, 59, 60]. A and B are tied for 2nd.
# A was preferred on more ballots than B, so finalists are A and C.
# B cannot win.
# "NO, MORE IS LESS"
# A B C
election = [*9*[[5, 1, 0]],
*7*[[0, 5, 1]],
*2*[[5, 1, 0]],
*3*[[5, 0, 0]],
*8*[[1, 0, 5]],
]
assert method(election, tiebreaker) == 2 # "causes C … to win"
# "If we delete 5-10 BCA voters"
for x in range(5, 11):
v1 = 12 - x
# A B C
election = [* 9*[[5, 1, 0]],
*v1*[[0, 5, 1]],
* 8*[[1, 0, 5]],
]
assert method(election, tiebreaker) == 2 # "C wins"
# "If 1-12 more CAB voters came"
for x in range(1, 13):
v2 = 8 + x
# A B C
election = [* 9*[[5, 1, 0]],
*12*[[0, 5, 1]],
*v2*[[1, 0, 5]],
]
assert method(election, tiebreaker) == 1 # "B win"
# "1-12 BCA voters could have"
for x in range(1, 13):
v1 = 12 - x
v2 = x
# A B C
election = [* 9*[[5, 1, 0]],
*v1*[[0, 5, 1]],
*v2*[[0, 1, 5]],
* 8*[[1, 0, 5]],
]
assert method(election, tiebreaker) != 0 # prevented … A from winning"
# "just erase B from all ballots"
# A C
election = [* 9*[[5, 0]],
*12*[[0, 1]],
* 8*[[1, 5]],
]
assert method(election, tiebreaker) == 1 # "now C wins"
# "REVERSE all ballots"
# A B C
# election = [* 9*[[5, 1, 0]],
# *12*[[0, 5, 1]],
# * 8*[[1, 0, 5]],
# ]
# # "i.e. score X becomes 5-X."
# election = 5 - np.asarray(election)
# assert method(election, tiebreaker) == 2 # "now C wins"
# Not correct. Tallies are [92, 76, 93], A and C are finalists.
# A is preferred over C by 20 voters, C is preferred over A by 9. A wins.
# "PARTITION INTO DISTRICTS"
# "West"
# A B C
election = [*3*[[5, 1, 0]],
*4*[[0, 5, 1]],
*4*[[1, 0, 5]],
]
assert method(election, tiebreaker) == 1 # "B is the STAR winner"
# "North"
# A B C
election = [*2*[[5, 1, 0]],
*3*[[0, 5, 1]], # "B1" typo in original
*4*[[1, 0, 5]],
]
assert method(election, tiebreaker) == 1 # "B is the STAR winner"
# "East"
# A B C
election = [*4*[[5, 1, 0]],
*5*[[0, 5, 1]],
]
assert method(election, tiebreaker) == 1 # "B is the STAR winner"
# "whole country"
# A B C
election = [*3*[[5, 1, 0]],
*4*[[0, 5, 1]],
*4*[[1, 0, 5]],
*2*[[5, 1, 0]],
*3*[[0, 5, 1]], # "B1" typo in original
*4*[[1, 0, 5]],
*4*[[5, 1, 0]],
*5*[[0, 5, 1]],
]
assert method(election, tiebreaker) == 0 # "A wins"
# "thwarted beats-all winner"
# A B C
election = [*13*[[5, 0, 1]],
*12*[[2, 0, 5]],
*12*[[0, 5, 1]],
* 6*[[1, 5, 0]],
]
assert method(election, tiebreaker) == 0 # "A wins"
# "3-candidate Nanson-Baldwin or STAR election"
# A B C
election = [*23*[[0, 2, 1]],
*16*[[2, 0, 1]],
*11*[[1, 0, 2]],
*10*[[2, 1, 0]],
]
assert method(election, tiebreaker) == 2 # "C wins"
# "suppose we change the 16 ACB ballots in the pink row to CAB"
# A B C
election = [*23*[[0, 2, 1]],
*16*[[1, 0, 2]],
*11*[[1, 0, 2]],
*10*[[2, 1, 0]],
]
assert method(election, tiebreaker) == 1 # "B wins"
# "17-voter 3-candidate IRV or STAR election"
# A B C
election = [*5*[[0, 1, 5]],
*4*[[5, 0, 1]],
*8*[[1, 5, 0]],
]
assert method(election, tiebreaker) == 2 # "C wins"
# "suppose two of the BAC voters"
# A B C
election = [*5*[[0, 1, 5]],
*4*[[5, 0, 1]],
*6*[[1, 5, 0]],
*2*[[5, 1, 0]],
]
assert method(election, tiebreaker) == 1 # "B wins"
# Example from https://www.youtube.com/watch?v=3-mOeUXAkV0
# Abby Ben Carmen DeAndre
election = [[3, 4, 0, 5],
[1, 0, 5, 1],
[0, 2, 5, 5],
[5, 0, 2, 3],
[1, 0, 4, 5],
]
assert method(election, tiebreaker) == 3 # "DeAndre wins"
# I tried to enumerate all possible scenarios and make a test for each.
@pytest.mark.parametrize("method", [star])
@pytest.mark.parametrize("tiebreaker", ['random', 'order']) # TODO: None
def test_all_tiebreaker_cases(tiebreaker, method):
# 1. One highest-scoring, one 2nd-highest. B preferred in runoff:
election = [[0, 5],
[2, 4],
]
assert method(election, tiebreaker) == 1
# 2. One highest-scoring, one 2nd-highest. Both tied in runoff, break tie
# using scores, B wins:
election = [[0, 5],
[3, 2],
]
assert method(election, tiebreaker) == 1
# 3. Two tied for highest-scoring, B wins in runoff:
election = [[0, 2],
[1, 2],
[4, 1],
]
assert method(election, tiebreaker) == 1
# 4. Two tied for highest-scoring, tied in runoff, decide A or B randomly:
election = [[1, 3],
[4, 2],
]
assert method(election, tiebreaker) in {0, 1}
# 5. One highest-scoring, two or more tied for second-highest. One
# Condorcet winner among tied (C). One is preferred in runoff (A):
election = [[5, 2, 3],
[5, 2, 3],
[5, 2, 0],
]
assert method(election, tiebreaker) == 0
# 6. One highest-scoring, two or more tied for second-highest. One
# Condorcet winner among tied (C). Runoff is tied, highest-scoring A wins:
election = [[5, 2, 3],
[5, 2, 3],
[1, 2, 2],
[1, 4, 2],
]
assert method(election, tiebreaker) == 0
# 7. One highest-scoring, two or more tied for second-highest. No
# Condorcet winner among tied, break 2nd place randomly (B or C). One is
# preferred in runoff (A):
election = [[5, 2, 3],
[5, 2, 3],
[4, 3, 2],
[1, 4, 3],
]
assert method(election, tiebreaker) == 0
# 8. One highest-scoring, two or more tied for second-highest. No
# Condorcet winner among tied, break 2nd place randomly (B or C). Both are
# tied in runoff, highest-scoring wins (A).],
election = [[5, 2, 3],
[5, 2, 3],
[1, 3, 2],
[1, 4, 3],
]
assert method(election, tiebreaker) == 0
# 9. Three or more tied for highest-scoring. One Condorcet winner and one
# 2nd-place Condorcet winner go to runoff. Condorcet winner wins (A):
election = [[2, 1, 0],
[2, 1, 0],
[2, 1, 0],
[2, 1, 0],
[1, 0, 0],
[0, 0, 4],
[0, 5, 5],
]
assert method(election, tiebreaker) == 0
# 10. Three or more tied for highest-scoring. Two tied for Condorcet
# winner (A and B), both go to runoff. True Tie, break randomly:
election = [[2, 2, 0],
[2, 2, 0],
[1, 1, 5],
]
assert method(election, tiebreaker) in {0, 1}
# 11. Three or more tied for highest-scoring. One Condorcet winner in
# tiebreaker and two or more tied for 2nd place CW. Break tie randomly (B
# or C). Condorcet winner A wins runoff:
election = [[2, 1, 1],
[2, 1, 1],
[3, 5, 5],
]
assert method(election, tiebreaker) == 0
# 12. Three or more tied for highest-scoring. No Condorcet winner in
# tiebreaker, choose two randomly. Neither wins runoff, choose winner
# randomly (A B or C):
election = [[5, 5, 5],
[2, 2, 2],
]
assert method(election, tiebreaker) in {0, 1, 2}
@pytest.mark.parametrize("method", [star])
def test_ties(method):
# Two-way tie between candidates 1 and 2
election = np.array([[0, 3, 2],
[3, 0, 1],
[0, 1, 3],
[0, 2, 0],
[0, 3, 3],
])
# No tiebreaker:
assert method(election, tiebreaker=None) is None
# Mode 'order' should always prefer lowest candidate ID
assert method(election, tiebreaker='order') == 1
# Mode 'random' should choose all tied candidates at random
assert collect_random_results(method, election) == {1, 2}
# Three-way tie between 0, 1, and 2
election = np.array([[0, 1, 1],
[0, 1, 2],
[0, 2, 1],
[2, 4, 0],
[4, 1, 0],
[1, 3, 0],
[1, 0, 3],
[3, 0, 1],
[1, 0, 4],
])
# No tiebreaker:
assert method(election, tiebreaker=None) is None
# # Mode 'order' should always prefer lowest candidate ID
# assert method(election, tiebreaker='order') == 0
# # Mode 'random' should choose all tied candidates at random
# assert collect_random_results(method, election) == {0, 1, 2}
@pytest.mark.parametrize("method", [star])
@pytest.mark.parametrize("tiebreaker", ['random', 'order'])
@given(election=score_ballots(min_cands=1, max_cands=25,
min_voters=1, max_voters=100))
def test_legit_winner(election, tiebreaker, method):
n_cands = election.shape[1]
winner = method(election, tiebreaker)
assert isinstance(winner, int)
assert winner in range(n_cands)
@pytest.mark.parametrize("method", [star])
@given(election=score_ballots(min_cands=1, max_cands=25,
min_voters=1, max_voters=100))
def test_legit_winner_none(election, method):
n_cands = election.shape[1]
winner = method(election)
assert isinstance(winner, (int, type(None)))
assert winner in set(range(n_cands)) | {None}
@pytest.mark.parametrize("method", [star])
def test_invalid(method):
with pytest.raises(ValueError):
method([[-2, -2, -2],
[0, +1, -1]])
def test_matrix_from_scores():
election = [[5, 5, 0],
[0, 4, 5],
[0, 3, 4]]
expected = [[0, 0, 1],
[2, 0, 1],
[2, 2, 0]]
assert_array_equal(matrix_from_scores(election), expected)
# Favorite Modern Snack?
# https://star.vote/y5www21z/ 2022-05-31
# Tide Pods, Fabuloso, Fidget Spinners
election = [[4, 3, 2],
[5, 0, 3],
[5, 4, 1],
[4, 5, 1],
[5, 3, 1],
[5, 1, 3],
[5, 3, 2],
[5, 4, 1],
[5, 0, 4],
[5, 3, 4],
[5, 3, 0],
[5, 3, 1],
[4, 5, 1],
[0, 5, 0],
[3, 1, 5]]
expected = [[0, 12, 13],
[3, 0, 10],
[1, 5, 0]]
assert_array_equal(matrix_from_scores(election), expected)
@pytest.mark.parametrize("method", [star])
@given(election=score_ballots(min_cands=1, max_cands=25,
min_voters=1, max_voters=100))
def test_matrix_from_scores_properties(election, method):
matrix = matrix_from_scores(election)
assert matrix.ndim == 2
assert matrix.shape[0] == matrix.shape[1]
for n in range(len(matrix)):
assert matrix[n, n] == 0
n_voters = len(election)
assert matrix.max() <= n_voters
assert matrix.min() >= 0
if __name__ == "__main__":
# Run unit tests, in separate process to avoid warnings about cached
# modules, printing output line by line in realtime
from subprocess import PIPE, Popen
with Popen(['pytest',
'--tb=short', # shorter traceback format
'--hypothesis-show-statistics',
str(__file__)], stdout=PIPE, bufsize=1,
universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')