-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcv-simulation.py
47 lines (31 loc) · 1.15 KB
/
rcv-simulation.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
from rcv import RCV
from itertools import filterfalse
""" This is a sample script using the RCV class and its methods
to simulate a ranked choice voting election.
This example uses the default arguments of the ballots method:
fieldsize = 5 (number of candidates)
ranksize = 5 (number of choices a voter can rank)
turnout = 10000 (number of voters/ballots)
You can easily change any or all of these by using keyword arguments
in the ballots method, e.g., r.ballots(turnout=20000).
"""
r = RCV()
ballots = r.ballots()
originals = ballots[:]
round1 = r.count(ballots)
print("ROUND 1\n")
for k, v in round1[0].items():
print("Candidate", k, "received", v, "first-choice votes")
print("Total:", sum(round1[0].values()))
d = r.runoff(
first=round1[1], last=round1[2], d=round1[0], rnd=2, ballots=ballots)
print(', '.join(list(d)), "were the finalists.")
print(len(originals), "first-round ballots cast.")
def uncounted(alist):
tests = []
for k in d.keys():
test = k in alist
tests.append(test)
return any(tests)
wasted = list(filterfalse(uncounted, originals))
print(len(wasted), "voters cast ballots without any finalists.")