Use all positions to determine order
Since we expect very few votes there is a high probability that several candidates will be not be ranked first by anybody. Which one is eliminated is essentially random, so it could happen that a candidate which is ranked second by everybody (and therefore would probably be the winner) is eliminated in the first round and never considered again. To prevent this, we sort by all ranks in order. So among those with the same number of first ranks the one with the most second ranks wins, if those are also the same we use the third rank etc. This should ensure that each round really eliminates the worst candidate (ties are still possible, but only if the votes at all ranks are the same - in which case no objective distinction is left and random choice is acceptable).
This commit is contained in:
parent
182b292bc1
commit
37ed14e1d6
6
app.py
6
app.py
|
@ -303,11 +303,11 @@ def runoff(ballots):
|
|||
candidates = {}
|
||||
for ballot in ballots:
|
||||
for r in ballot:
|
||||
count[r.id] = 0
|
||||
count[r.id] = [0] * len(ballot)
|
||||
candidates[r.id] = r
|
||||
for ballot in ballots:
|
||||
# The votes are sorted by position, so the first entry is the favourite
|
||||
count[ballot[0].id] += 1
|
||||
for pos, r in enumerate(ballot):
|
||||
count[r.id][pos] += 1
|
||||
result = sorted(count.keys(), key=lambda i: count[i])
|
||||
log.debug("result of this round:")
|
||||
for r in result:
|
||||
|
|
|
@ -48,11 +48,11 @@ def runoff(ballots):
|
|||
candidates = {}
|
||||
for ballot in ballots:
|
||||
for r in ballot:
|
||||
count[r.id] = 0
|
||||
count[r.id] = [0] * len(ballot)
|
||||
candidates[r.id] = r
|
||||
for ballot in ballots:
|
||||
# The votes are sorted by position, so the first entry is the favourite
|
||||
count[ballot[0].id] += 1
|
||||
for pos, r in enumerate(ballot):
|
||||
count[r.id][pos] += 1
|
||||
result = sorted(count.keys(), key=lambda i: count[i])
|
||||
print("result of this round:")
|
||||
for r in result:
|
||||
|
|
Loading…
Reference in New Issue