| 15 |
jtkorb |
1 |
## Compute Hare Ballot
|
|
|
2 |
##
|
|
|
3 |
## Tim Korb (jtk@cs.purdue.edu), May 2007
|
|
|
4 |
##
|
|
|
5 |
## A few definitions and notes...
|
|
|
6 |
##
|
|
|
7 |
## tally: dictionary in which the keys are candidate names and the
|
|
|
8 |
## values are lists of ballots currently assigned to that candidate
|
|
|
9 |
##
|
|
|
10 |
## ballot: list of candidates in the order determined by the voter
|
|
|
11 |
##
|
|
|
12 |
## winners: list of candidates that have reached the quota of ballots
|
|
|
13 |
## or have remained in the running long enough to be declared a winner
|
|
|
14 |
##
|
|
|
15 |
## losers: list of candidates that have been eliminated from the running
|
|
|
16 |
##
|
|
|
17 |
## Note that plurals are generally used to indicate lists of other
|
|
|
18 |
## items, e.g., ballots is a list of ballot items.
|
|
|
19 |
##
|
| 24 |
jtkorb |
20 |
## To download the complete source distribution, use Subversion:
|
|
|
21 |
##
|
|
|
22 |
## % svn co http://www.bikmort.com/hare
|
|
|
23 |
##
|
| 15 |
jtkorb |
24 |
|
|
|
25 |
import sys
|
|
|
26 |
import math
|
|
|
27 |
import random
|
|
|
28 |
|
|
|
29 |
fTrace = 1
|
| 39 |
jtkorb |
30 |
randcalls = 0
|
| 15 |
jtkorb |
31 |
|
|
|
32 |
def trace(s):
|
|
|
33 |
global fTrace
|
|
|
34 |
if fTrace: print s
|
|
|
35 |
return
|
|
|
36 |
|
|
|
37 |
def findWinner(winners, losers, tally, quota, nWinners):
|
| 39 |
jtkorb |
38 |
global randcalls
|
|
|
39 |
cWin = quota # number of votes for highest winner
|
|
|
40 |
lWin = [] # list of candidates with highest votes
|
|
|
41 |
for c in tally.keys():
|
|
|
42 |
if c not in losers and c not in winners and len(tally[c]) >= cWin:
|
|
|
43 |
if len(tally[c]) == cWin:
|
|
|
44 |
lWin.append(c)
|
|
|
45 |
else:
|
|
|
46 |
lWin = [c]
|
|
|
47 |
cWin = len(tally[c])
|
|
|
48 |
if len(lWin) == 1:
|
|
|
49 |
return lWin[0]
|
|
|
50 |
elif len(lWin) > 1:
|
|
|
51 |
trace("\tselecting winning candidate randomly from %s" % lWin)
|
|
|
52 |
randcalls += 1
|
|
|
53 |
return random.choice(lWin)
|
| 15 |
jtkorb |
54 |
|
|
|
55 |
# Check to see if only enough candidates remain
|
| 40 |
jtkorb |
56 |
## TODO: sort by len(tally[c]) to choose larger winners first
|
|
|
57 |
## randomize and count if some with equal votes
|
| 15 |
jtkorb |
58 |
n = 0
|
|
|
59 |
last = ""
|
| 40 |
jtkorb |
60 |
for c in tally.keys():
|
|
|
61 |
if c not in winners and c not in losers:
|
|
|
62 |
last = c
|
| 15 |
jtkorb |
63 |
n = n + 1
|
|
|
64 |
if nWinners - len(winners) >= n:
|
| 40 |
jtkorb |
65 |
trace("\tremaining winner(s) have fewer than quota (%d) ballots" %
|
|
|
66 |
quota)
|
| 15 |
jtkorb |
67 |
return last
|
|
|
68 |
return
|
|
|
69 |
|
|
|
70 |
def redistributeWinner(winner, winners, losers, tally, quota):
|
| 39 |
jtkorb |
71 |
global randcalls
|
| 15 |
jtkorb |
72 |
excess = len(tally[winner]) - quota
|
|
|
73 |
if excess <= 0:
|
| 21 |
jtkorb |
74 |
trace("\tno excess ballots to redistribute")
|
| 15 |
jtkorb |
75 |
else:
|
| 21 |
jtkorb |
76 |
trace("\tredistributing %d excess ballot(s) at random from %s" %
|
|
|
77 |
(excess, winner))
|
|
|
78 |
while len(tally[winner]) > quota:
|
|
|
79 |
i = int(random.uniform(0, len(tally[winner])))
|
| 39 |
jtkorb |
80 |
randcalls += 1
|
| 21 |
jtkorb |
81 |
trace("\trandom choice = ballot %d" % (i+1))
|
|
|
82 |
ballot = tally[winner][i]
|
|
|
83 |
tally[winner] = tally[winner][0:i] + tally[winner][i+1:]
|
|
|
84 |
redistributeBallot(ballot, winner, winners, losers, tally)
|
| 15 |
jtkorb |
85 |
traceTally(quota, tally)
|
|
|
86 |
|
|
|
87 |
def redistributeBallot(ballot, candidateFrom, winners, losers, tally):
|
|
|
88 |
for candidateTo in ballot:
|
| 21 |
jtkorb |
89 |
if candidateTo not in winners and candidateTo not in losers:
|
|
|
90 |
trace("\tto %s: %s" % (candidateTo, ballot))
|
|
|
91 |
if not tally.has_key(candidateTo): tally[candidateTo] = []
|
|
|
92 |
tally[candidateTo].append(ballot)
|
|
|
93 |
ballot = ""
|
|
|
94 |
break
|
| 15 |
jtkorb |
95 |
if ballot:
|
| 21 |
jtkorb |
96 |
trace("\tineffective ballot dropped: %s" % ballot)
|
| 15 |
jtkorb |
97 |
|
|
|
98 |
def findLoser(losers, winners, tally):
|
| 39 |
jtkorb |
99 |
global randcalls
|
| 15 |
jtkorb |
100 |
cMin = sys.maxint # least number of votes for candidate loser
|
|
|
101 |
lMin = [] # list of candidates with least votes
|
|
|
102 |
for c in tally.keys():
|
|
|
103 |
if c not in losers and c not in winners and len(tally[c]) <= cMin:
|
| 21 |
jtkorb |
104 |
if len(tally[c]) == cMin:
|
|
|
105 |
lMin.append(c)
|
|
|
106 |
else:
|
|
|
107 |
lMin = [c]
|
|
|
108 |
cMin = len(tally[c])
|
| 15 |
jtkorb |
109 |
if len(lMin) == 0:
|
|
|
110 |
return None
|
| 39 |
jtkorb |
111 |
elif len(lMin) == 1:
|
|
|
112 |
return lMin[0]
|
| 15 |
jtkorb |
113 |
else:
|
| 39 |
jtkorb |
114 |
trace("\teliminating low candidate randomly from %s" % lMin)
|
|
|
115 |
randcalls += 1
|
| 15 |
jtkorb |
116 |
return random.choice(lMin)
|
|
|
117 |
|
|
|
118 |
def redistributeLoser(loser, losers, winners, tally, quota):
|
|
|
119 |
excess = len(tally[loser])
|
|
|
120 |
if excess <= 0:
|
| 21 |
jtkorb |
121 |
trace("\tno ballots to redistribute")
|
| 15 |
jtkorb |
122 |
else:
|
| 21 |
jtkorb |
123 |
trace("\tredistributing %d ballot(s) from %s" % (excess, loser))
|
|
|
124 |
while len(tally[loser]) > 0:
|
|
|
125 |
ballot = tally[loser][0]
|
|
|
126 |
tally[loser] = tally[loser][1:]
|
|
|
127 |
redistributeBallot(ballot, loser, winners, losers, tally)
|
| 15 |
jtkorb |
128 |
traceTally(quota, tally)
|
|
|
129 |
return
|
|
|
130 |
|
|
|
131 |
def traceTally(quota, tally):
|
|
|
132 |
global fTrace
|
|
|
133 |
if not fTrace: return
|
|
|
134 |
trace("\nCURRENT ASSIGNMENT OF BALLOTS (%d needed to win)" % quota)
|
|
|
135 |
for candidate in tally.keys():
|
|
|
136 |
trace("\t%s:" % candidate)
|
|
|
137 |
for ballot in tally[candidate]:
|
|
|
138 |
trace("\t\t%s" % ballot)
|
|
|
139 |
return
|
|
|
140 |
|
|
|
141 |
# The basic Single Transferable Vote algorithm with Hare quota
|
|
|
142 |
#
|
|
|
143 |
# while winners < nWinners:
|
|
|
144 |
# if a candidate has more than quota votes:
|
|
|
145 |
# redistribute excess votes to next priority candidate
|
|
|
146 |
# else:
|
|
|
147 |
# eliminate lowest ranking candidate
|
|
|
148 |
# redistribute wasted votes to next priority candidate
|
|
|
149 |
#
|
|
|
150 |
def dotally(nWinners, ballots):
|
| 39 |
jtkorb |
151 |
global randcalls
|
| 15 |
jtkorb |
152 |
nBallots = len(ballots)
|
|
|
153 |
quota = int(math.ceil((nBallots + 1.0)/(nWinners + 1)))
|
|
|
154 |
|
|
|
155 |
trace("INPUT SUMMARY")
|
|
|
156 |
trace("\t%d ballots" % nBallots)
|
|
|
157 |
trace("\tChoosing %s winners" % nWinners)
|
|
|
158 |
trace("\tNeed ceil((%d + 1)/(%d + 1)) = %d ballots to win" %
|
| 21 |
jtkorb |
159 |
(nBallots, nWinners, quota))
|
| 15 |
jtkorb |
160 |
|
|
|
161 |
# Create initial tally
|
|
|
162 |
#
|
|
|
163 |
tally = {}
|
|
|
164 |
for ballot in ballots:
|
|
|
165 |
candidate = ballot[0]
|
|
|
166 |
if not tally.has_key(candidate):
|
|
|
167 |
tally[candidate] = []
|
|
|
168 |
tally[candidate].append(ballot)
|
|
|
169 |
traceTally(quota, tally)
|
|
|
170 |
|
|
|
171 |
winners = []
|
|
|
172 |
losers = []
|
|
|
173 |
|
|
|
174 |
while len(winners) < nWinners:
|
|
|
175 |
winner = findWinner(winners, losers, tally, quota, nWinners)
|
|
|
176 |
if winner:
|
|
|
177 |
winners.append(winner)
|
|
|
178 |
trace("\nSELECTION #%d: %s" % (len(winners), winner))
|
|
|
179 |
redistributeWinner(winner, winners, losers, tally, quota)
|
|
|
180 |
else:
|
|
|
181 |
loser = findLoser(losers, winners, tally)
|
|
|
182 |
if loser:
|
|
|
183 |
losers.append(loser)
|
|
|
184 |
trace("\nELIMINATED: %s" % loser)
|
|
|
185 |
redistributeLoser(loser, losers, winners, tally, quota)
|
|
|
186 |
else:
|
|
|
187 |
trace("Not enough chosen candidates to fill all positions")
|
|
|
188 |
break
|
| 39 |
jtkorb |
189 |
trace("\nNumber of random choices made: %d" % randcalls)
|
| 15 |
jtkorb |
190 |
|
|
|
191 |
return winners
|
|
|
192 |
|