Reverse direction of instant runoff

This commit is contained in:
Peter J. Holzer 2023-05-12 21:38:52 +02:00 committed by Peter J. Holzer
parent 977ecf9d07
commit 34f361a4a6
2 changed files with 33 additions and 12 deletions

39
app.py
View File

@ -173,7 +173,7 @@ def vote_date():
(session["user"]["id"], meet_id,))
dates = csr.fetchall()
result = instantrunoff_forward(meet_id, "date")
result = instantrunoff_backward(meet_id, "date")
log.debug("result = %s", result)
csr.execute(
@ -194,7 +194,7 @@ def vote_date():
@app.get("/result/<int:meet_id>/date")
def result_date(meet_id):
result = instantrunoff_forward(meet_id, "date")
result = instantrunoff_backward(meet_id, "date")
log.debug("result = %s", result)
csr = get_cursor()
@ -251,7 +251,7 @@ def vote_time():
(session["user"]["id"], meet_id,))
times = csr.fetchall()
result = instantrunoff_forward(meet_id, "time")
result = instantrunoff_backward(meet_id, "time")
log.debug("result = %s", result)
csr.execute(
@ -272,7 +272,7 @@ def vote_time():
@app.get("/result/<int:meet_id>/time")
def result_time(meet_id):
result = instantrunoff_forward(meet_id, "time")
result = instantrunoff_backward(meet_id, "time")
log.debug("result = %s", result)
csr = get_cursor()
@ -327,7 +327,7 @@ def vote_place():
(session["user"]["id"], meet_id,))
places = csr.fetchall()
result = instantrunoff_forward(meet_id, "place")
result = instantrunoff_backward(meet_id, "place")
log.debug("result = %s", result)
csr.execute(
@ -349,7 +349,7 @@ def vote_place():
@app.get("/result/<int:meet_id>/place")
def result_place(meet_id):
result = instantrunoff_forward(meet_id, "place")
result = instantrunoff_backward(meet_id, "place")
log.debug("result = %s", result)
csr = get_cursor()
@ -373,7 +373,7 @@ def result_place(meet_id):
@app.get("/result/<int:meet_id>/date/ballot")
def result_ballots_date(meet_id):
ballots = get_ballots(meet_id, "date")
result = instantrunoff_forward(meet_id, "date")
result = instantrunoff_backward(meet_id, "date")
return render_template(
"date_result_ballots.html",
@ -382,7 +382,7 @@ def result_ballots_date(meet_id):
@app.get("/result/<int:meet_id>/time/ballot")
def result_ballots_time(meet_id):
ballots = get_ballots(meet_id, "time")
result = instantrunoff_forward(meet_id, "time")
result = instantrunoff_backward(meet_id, "time")
return render_template(
"time_result_ballots.html",
@ -391,7 +391,7 @@ def result_ballots_time(meet_id):
@app.get("/result/<int:meet_id>/place/ballot")
def result_ballots_place(meet_id):
ballots = get_ballots(meet_id, "place")
result = instantrunoff_forward(meet_id, "place")
result = instantrunoff_backward(meet_id, "place")
return render_template(
"place_result_ballots.html",
@ -453,7 +453,10 @@ def runoff(ballots):
log.debug("pos = %s", pos)
count[r.id][pos] += 1
log.debug("count[%d][%d]) = %d", r.id, pos, count[r.id][pos])
result = sorted(count.keys(), key=lambda i: count[i])
if direction == "backward":
result = sorted(count.keys(), key=lambda i: list(reversed(count[i])), reverse=True)
else:
result = sorted(count.keys(), key=lambda i: count[i])
log.debug("result of this round:")
for r in result:
log.debug("%s %s", r, count[r])
@ -472,7 +475,21 @@ def instantrunoff_forward(meet_id, kind):
result = []
while max(len(b) for b in ballots):
dump_ballots(ballots)
loser, ballots = runoff(ballots)
loser, ballots = runoff(ballots, "forward")
result.append(loser)
result = list(reversed(result))
log.debug("final result")
for r in result:
log.debug(r)
return result
def instantrunoff_backward(meet_id, kind):
ballots = get_ballots(meet_id, kind)
result = []
while max(len(b) for b in ballots):
dump_ballots(ballots)
loser, ballots = runoff(ballots, "backward")
result.append(loser)
result = list(reversed(result))
log.debug("final result")

View File

@ -7,6 +7,7 @@ import psycopg.rows
def get_args():
ap = argparse.ArgumentParser()
ap.add_argument("--reverse", action="store_true")
ap.add_argument("meet", type=int)
ap.add_argument("kind")
@ -53,7 +54,10 @@ def runoff(ballots):
for ballot in ballots:
for pos, r in enumerate(ballot):
count[r.id][pos] += 1
result = sorted(count.keys(), key=lambda i: count[i])
if args.reverse:
result = sorted(count.keys(), key=lambda i: list(reversed(count[i])), reverse=True)
else:
result = sorted(count.keys(), key=lambda i: count[i])
print("result of this round:")
for r in result:
print(r, count[r])