~/scripts/political

Instant-Runoff Voting

Introduction

This is an implementation of an instant-runoff voting algorithm. Instant-runoff voting is a fairer alternative to the “first past the post” algorithms usually employed to elect candidates, which often forces candidates to vote for the least objectionable of two main candidates. Instant-runoff voting allows users to vote for their favorite candidates without risk of “wasting their vote”.

In instant-runoff voting, each voter may submit a list of candidates ordered by preference. The election then proceeds in rounds, where each voter’s top choice receives the voter’s vote for that round. The candidate with the least number of votes is eliminated and a new round instantly begins. The votes of all voters who had placed the eliminated candidate at the top of their list now go to the next candidate on their list (if any), and the votes are re-tallied and a candidate is again eliminated.

As a simple example, imagine an election with two major party candidates, James Smith and John Doe, and a minor independent party candidate, Jane Bloggs. Your ideal candidate is Jane Bloggs but you believe that she has very little chance of winning the popular vote. If you are afraid of James Smith winning the election then with a first-past-the-post system you may feel that you must vote for John Doe to ensure that James Smith doesn’t win. This effectively eliminates any chance of Jane Bloggs winning and you end up locked in a two-party system that doesn’t represent the candidates.

With instant-runoff voting, your ballot would instead look like this:

1. Jane Bloggs
2. John Doe

When the votes are tallied, the results may be

James Smith: 10535
John Doe:    10034
Jane Bloggs:   692

Rather than giving the election to James Smith, Jane Bloggs is eliminated and all votes for her are redistributed to the second choice (if there is one). After re-tallying, the results may be

James Smith: 10572
John Doe:    10615

In this round, James Smith is eliminated and John Doe wins the election.

Further Information

The script includes comments that explain each aspect of the algorithm. If you find a bug, please report it.

Technical

The script provides a number of functions for performing instant-runoff voting and can be installed as a module. I will likely package it at some point.

Dependencies

The script uses python3-tabulator to display data in tables.

Output Example

This output was generated by the random election demo.

1: Standard Election Round

Candidate   Votes Support by Voters Support by Population Status
----------- ----- ----------------- --------------------- ----------
Candidate 1   788            19.61%                15.76%
Candidate 2   833            20.73%                16.66%
Candidate 3   794            19.76%                15.88%
Candidate 4   832            20.70%                16.64%
Candidate 5   772            19.21%                15.44% eliminated
----------- ----- ----------------- --------------------- ----------


2: Standard Election Round

Candidate   Votes Support by Voters Support by Population Status
----------- ----- ----------------- --------------------- -------------------
Candidate 1   933            23.21%                18.66% tied for last place
Candidate 2   969            24.11%                19.38%
Candidate 3   933            23.21%                18.66% tied for last place
Candidate 4   989            24.61%                19.78%
----------- ----- ----------------- --------------------- -------------------


  2.1: Last Place Elimination Round

  Candidate   Votes Support by Voters Support by Population Status
  ----------- ----- ----------------- --------------------- ----------
  Candidate 1  1495            37.20%                29.90% eliminated
  Candidate 3  1509            37.55%                30.18%
  ----------- ----- ----------------- --------------------- ----------


3: Standard Election Round

Candidate   Votes Support by Voters Support by Population Status
----------- ----- ----------------- --------------------- ----------
Candidate 2  1206            30.01%                24.12%
Candidate 3  1106            27.52%                22.12% eliminated
Candidate 4  1213            30.18%                24.26%
----------- ----- ----------------- --------------------- ----------


4: Standard Election Round

Candidate   Votes Support by Voters Support by Population Status
----------- ----- ----------------- --------------------- --------------------
Candidate 2  1526            37.97%                30.52% tied for first place
Candidate 4  1526            37.97%                30.52% tied for first place
----------- ----- ----------------- --------------------- --------------------


  4.1: Tie Breaker Round

  Candidate   Votes Support by Voters Support by Population Status
  ----------- ----- ----------------- --------------------- ----------
  Candidate 2   488            12.14%                 9.76% eliminated
  Candidate 4   518            12.89%                10.36%
  ----------- ----- ----------------- --------------------- ----------

5: Final Tally Round

Candidate   Votes Support by Voters Support by Population Status
----------- ----- ----------------- --------------------- ------
Candidate 4  2044            50.86%                40.88% winner
----------- ----- ----------------- --------------------- ------

Directory Listing

FileSizeLast Modified
instant_runoff_voting.py18.59 KiB2016-05-05 00:44 UTC
Contact
echo xyne.archlinux.org | sed 's/\./@/'
Validation
XHTML 1.0 Strict CSS level 3 Atom 1.0