Skip to main content

PyRankVote is a python library for different ranked voting methods, like instant-runoff voting, single transferable vote and preferential block voting, created by Jon Tingvold.

Project description

PyRankVote —A Ranked Choice Voting System for Python

PyRankVote is a python library for different ranked-choice voting systems (sometimes called preferential voting systems) created by Jon Tingvold in June 2019.

The following ranking methods are implemented for electing one person/alternative (e.g. electing the chairman to a board):

  • Instant-runoff voting (IRV)

The following ranking methods are implemented for electing multiple people/alternatives (e.g. electing board members):

  • Single transferable vote (STV)
  • Preferential block voting (PBV)

Different ranking methods

Instant runoff voting (IRV) is a single candidate election method that elects the candidate that can obtain majority support (more than 50%).

Voters rank candidates and are granted one vote. The candidate with fewest votes is removed and this candidate's votes are transferred according to the 2nd preference (or 3rd etc.).

Preferential block voting (PBV) is a multiple candidate election method that elects candidates that can obtain majority support (more than 50%). PBV tend to elect uncontroversial candidates that agree with each other. Minority group often lose their representation.

Voters rank candidates and are granted as many votes as there are people that should be elected. The candidate with fewest votes are removed and this candidate's votes are transferred according to the 2nd preference (or 3rd etc.).

Single transferable vote (STV) is a multiple candidate election method that elects candidates based on proportional representation. Minority groups get representation. STV is therefore the preferred ranked-choice voting method for parliament elections.

Voters rank candidates and are granted as one vote each. If a candidate gets more votes than the threshold for being elected, the candidate is proclaimed as winner. This function uses the Droop quota, where

droop_quota = votes/(seats+1) + 1

If one candidate gets more votes than the threshold the excess votes are transferred to voters that voted for this candidate's 2nd (or 3rd, 4th, etc) alternative. If no candidate gets over the threshold, the candidate with fewest votes is removed. Votes for this candidate is then transferred to voters 2nd (or 3rd, 4th, etc) alternative.

Preferential block voting and Single transferable vote are the same as Instant-runoff voting when only one candidate is elected.

Instant-runoff voting and Preferential block voting are basically the same as exhaustive ballot, the preferred method in Robers rules of order. The only difference is that in exhaustive ballot voters can adjust their preferences between each round (elimination or election of one candidate).

For more info see pyrankvote/ranking_methods.py and Wikipedia.

Installation

Use the package manager pip to install (or upgrade) pyrankvote.

pip install pyrankvote

Usage

import pyrankvote
from pyrankvote.models import Candidate, Ballot

per = Candidate("Per")
paal = Candidate("Pål")
askeladden = Candidate("Askeladden")

candidates = [per, paal, askeladden]

ballots = [
    Ballot(ranked_candidates=[askeladden, per]),
    Ballot(ranked_candidates=[per, paal]),
    Ballot(ranked_candidates=[per, paal]),
    Ballot(ranked_candidates=[paal, per]),
    Ballot(ranked_candidates=[paal, per, askeladden])
]

election_result = pyrankvote.multiple_seat_ranking_methods.single_transferable_vote(candidates, ballots, number_of_seats=2)

winners = election_result.get_winners()

print(election_result)

"""
ROUND 1
Candidate      Votes  Status
-----------  -------  --------
Per                2  Hopeful
Pål                2  Hopeful
Askeladden         1  Hopeful


ROUND 2
Candidate      Votes  Status
-----------  -------  --------
Per                3  Hopeful
Pål                2  Hopeful
Askeladden         0  Rejected


FINAL RESULT
Candidate      Votes  Status
-----------  -------  --------
Per                3  Elected
Pål                2  Elected
Askeladden         0  Rejected
"""

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrankvote-1.0.2.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrankvote-1.0.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file pyrankvote-1.0.2.tar.gz.

File metadata

  • Download URL: pyrankvote-1.0.2.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.13.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

File hashes

Hashes for pyrankvote-1.0.2.tar.gz
Algorithm Hash digest
SHA256 9289c82f4241d9b2e37a004b6dff66d9307b03c3021f7378eb4b66cad1ff2bfb
MD5 0c70867a73368830df005b12ff110ddc
BLAKE2b-256 c0786435bed66de838e5ac14b59a099c2566465803201d0e33b999beadfd668a

See more details on using hashes here.

File details

Details for the file pyrankvote-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyrankvote-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.13.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

File hashes

Hashes for pyrankvote-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6420eb0c8924d3c10124407c96a2223e3933cde517ab6a30e32dba941dca29f6
MD5 15d996a43a585a3258210ce458c50fb6
BLAKE2b-256 63d4a37b975be3b4bbfc0a0d81484469b884994dbbc548d5f8fa4315a89695fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page