julien-boudry / condorcet
Complete election manager, providing natively many voting methods including: Condorcet / Borda (+ Nauru variant) / Copeland / Dodgson (2 Approximations) / FTPT / Instant-runoff (alternative vote) / Kemeny–Young / Minimax (+ variants) / Ranked Pairs (+ variants) / Schulze (+ variants), Single Transfe
Fund package maintenance!
julien-boudry
Bitcoin
Installs: 8 344
Dependents: 2
Suggesters: 0
Security: 0
Stars: 119
Watchers: 12
Forks: 11
Open Issues: 12
Requires
- php: ^8.3
- ext-json: *
- ext-mbstring: *
- brick/math: ^0.12
- symfony/console: ^7.0
- symfony/yaml: ^7.0
Requires (Dev)
- ext-pdo: *
- ext-pdo_sqlite: *
- cmgmyr/phploc: ^8.0
- haydenpierce/class-finder: >= 0.5.3
- infection/infection: ^0.27, >= 0.27.7
- laravel/pint: ^1.16
- mammothphp/woollym: dev-master
- pestphp/pest: ^2.34
- pestphp/pest-plugin-drift: ^2.0
- phpbench/phpbench: *
- phpstan/phpstan: ^1.9
Suggests
- ext-pdo: Allow to use database for very large elections.
- ext-pdo_sqlite: Use sqlite3 bases for very large elections.
- dev-master
- v4.7.0
- v4.6.1
- v4.6.0
- v4.5.1
- v4.5.0
- v4.5-rc3
- v4.5-rc1
- v4.4.1
- v4.4.0
- v4.3.1
- v4.3.0
- v4.3-rc2
- v4.3-rc1
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.2-rc3
- v4.2-rc2
- v4.2-rc1
- v4.1.1
- v4.1.0
- v4.0.0
- v4.0-beta1
- v4.0-alpha1
- v3.4-alpha1
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.3-rc1
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.2-rc2
- v3.2-rc1
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.1-rc1
- v3.1-beta2
- v3.1-beta1
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.1-beta3
- v2.1-beta2
- v2.1-beta1
- v2.1-alpha7
- v2.0.1
- v2.0.0
- v2.0.0-RC3
- v2.0.0-RC2
- v2.0.0-RC1
- v1.8.3
- v1.8.2
- v1.8.1
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.97.0
- v0.96.0
- v0.95.1
- v0.95.0
- v0.94.0
- v0.94-rc2
- v0.94-rc1
- v0.93.0
- v0.92.0
- v0.91.1
- v0.91.0
- v0.91-rc2
- v0.91-rc1
- v0.90.0
- v0.90-rc5
- v0.90-rc4
- v0.90-rc3
- v0.90-rc2
- v0.90-rc1
- v0.90-beta11
- v0.90-beta10
- v0.90-beta9
- v0.90-beta8
- v0.90-beta7
- v0.90-beta6
- v0.90-beta5
- v0.15-beta4
- v0.15-beta3
- v0.15-beta2
- v0.15-beta1
- v0.14.0
- v0.13.2
- v0.13.1
- v0.13.0
- v0.12.0
- v0.11.1
- v0.11.0
- v0.10.1
- v0.10.0
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.6-beta1
- v0.5.1
- v0.5.0
- v0.5-beta1
- dev-dev-4.8
- dev-dev-4.7
- dev-WORK/pestMigration
- dev-dev-4.6
- dev-dev-4.5
- dev-Experimental/Pest
- dev-Experimental/Schulze-STV
- dev-VERSION/4.5
- dev-Experimental/FilteredPairwise
- dev-Experimental/SchulzeSTVtests
- dev-schulze
- dev-Schulze-STV
- dev-WORK/FilteredPairwisePoc
- dev-WORK/poc_free_pairwise
- dev-dev-4.4
- dev-WORK/GeckoMethod
- dev-VERSION/4.4
- dev-WORK/CivsFormat
- dev-VERSION/4.3
- dev-dev-4.3
- dev-VERSION/4.2
- dev-dev-4.2
- dev-WORK/generics
- dev-julien-boudry-patch-2
- dev-julien-boudry-patch-1
- dev-special-boudry
- dev-dev-phpdoc
- dev-dev-production-codespace
- dev-WORK/issue_110
- dev-WORK/VoteParser
- dev-dev-4.1
- dev-VERSION/4.1
- dev-Experimental/Sainte-Laguë
- dev-dev-4.0
- dev-VERSION/4.0
- dev-dev-3.2
- dev-dev-3.3
- dev-dev-3.4
- dev-Experimental/KemenyYoungWithoutStats
- dev-Experimental/BigInt
- dev-WORK/KemenyYoungLight1
- dev-WORK/CPO_STV
- dev-VERSION/3.3
- dev-VERSION/2.2
- dev-dev-2.2
- dev-WORK/weakReference
- dev-WORK/TidemanDataCollection
- dev-dependabot/composer/infection/infection-tw-0.25.3or-tw-0.26.0
- dev-VERSION/3.2
- dev-VERSION/3.1
- dev-dev-3.1
- dev-WORK/infection
- dev-WORK/newExceptions
- dev-WORK/php_81
- dev-WORK/STV
- dev-dev-3.0
- dev-VERSION/3.0
- dev-WORK/moveDocumentationToAttributes
- dev-WORK/FasterKemenyYoung
- dev-version/2.1.x
- dev-VERSION/2.0
- dev-dev-2.0
- dev-WORK/ElectionStateCacheHandler
- dev-VERSION/1.8.x
- dev-WORK/ranking_poc
- dev-dev-1.8.x
- dev-VERSION/1.7.x
- dev-VERSION/1.6.x
- dev-TEST/ScrutinizerPHPAnalysisEngineV2
- dev-VERSION/1.5.x
- dev-VERSION/1.4.x
- dev-VERSION/1.3.x
- dev-VERSION/1.0.x
- dev-VERSION/0.14
This package is auto-updated.
Last update: 2024-11-26 14:15:16 UTC
README
Main Author: Julien Boudry
License: MIT - Please say hello if you like or use this code!
Contribute: Contribute File
Donation: ₿ bc1q3jllk3qd9fjvvuqy07tawkv7t6h7qjf55fc2gh or Github Sponsor Page
You can also offer me a bottle of good wine.
Condorcet PHP
Presentation | Documentation Book | API References | Voting Methods | Tests
Condorcet manages the stages of an electoral process (configuration, votes ingestion & manipulation, integrity) and calculates the results. It offers natively the implementation of more than 20 voting methods compatible with preferential voting ballots, including Condorcet methods, Alternative Voting, STV, and many others. => Supported Voting Methods
Two different ways to use Condorcet:
- A command line application, for quick use of essential features without complicated technical knowledge. Allowing you to easily compute your election results and stats.
- A PHP library that you can include in your code to take advantage of 100% of the advanced features (advanced manipulations & configurations, extensions & modularity, cache & high performances simulations, advanced input and output methods...).
Both approaches can handle up to hundreds of millions of votes (or more) on modest hardware. Although using it as a library will allow more configuration and control over this advanced usage.
Summary
- Condorcet PHP
Project State and Specifications
All versions require Json and Mbstring extensions (or polyfill). Pdo-Sqlite is recommended if you need to activate the default provided driver for bigs elections (hundred of thousands of votes or more)
Supported Voting Methods
Support both single-winner methods (with or without the Condorcet criterion) and proportional methods.
Complete list of natively implemented methods, their options (variants), and implementation choices
Single-Winner Methods provided natively
Single Winner returns a full ranking of all candidates, even though they are generally more designed to designate only one.
Deterministic
Condorcet / Borda (+ Nauru variant) / Copeland / Dodgson (2 Approximations) / FTPT / Instant-runoff (alternative vote) / Kemeny–Young / Minimax (+ variants) / Ranked Pairs (+ variants) / Schulze (+ variants)
Lotteries
Random Ballot / Random Candidates
Proportional Methods provided natively
Designed for electing assembly, return a full ranking of elected candidates.
Single Transferable Vote (STV) / Comparison of Pairs of Outcomes by the Single Transferable Vote (CPO-STV) / Highest Averages Methods (Sainte-Laguë, Jefferson/D'Hondt, and variants) / Largest Remainder Methods (with different quotas)
Add your own voting method as a module
Condorcet is designed to be easily extensible with new algorithms (they don't need to share the same namespace).
- More explanations with this documentation
- Modules Skeleton: A template for starting to develop fast and following best practices.
Main features
- Manage an election
- Respect an election cycle: Registering candidates, registers votes, gets results from many algorithms.
- Ordered votes, tags votes, delete votes, simulates partial results.
- Many input types available (string, Json, objects...)
- Import from Condorcet Election Format (and export to), Debian Format, David Hill Format
- Integrity check (checksumming)
- Support for storing elections (serializing Election object, export data...)
- Some methods can be used nearly front final user (vote constraints, anti-spam check, parsing input, human-friendly results and stats...)
- Get election results and stats
- Get the natural Condorcet Winner, Loser, Pairwise, Paradox...
- Get full ranking from advanced voting methods
- Get some additional stats from these methods
- Force ranking all candidates implicitly (default) or allow voters to not rank all candidates.
- Put weight on a certain vote, and give more importance to certain voters.
- Be more powerful
- All are objects, all are abstract (But there are many higher-level functions and inputs types).
- Candidates and Votes are objects which can take part in multiple elections at the same time and change their name or ranking dynamically. That allows powerful tools to simulate elections.
- Manage hundred of billions of votes by activating an external driver to store (instead of RAM) an unlimited number of votes during the computation phase. A PDO driver is provided by default, an example is provided with SQLite, an interface that allows you to design other drivers.
- Smart cache system, allows calling multiple time computation methods without performance issues.
- Extend it! Configure it!
- Modular architecture to extend it without fork Condorcet PHP! Just make your module on your own namespace.
- Election, Candidate, and Vote classes are extensible.
- Add your own ranking algorithm.
- Create your own vote constraints.
- Use your own datastore driver to manage very large elections on your way without ram limit.
- Many configuration options and methods.
- Modular architecture to extend it without fork Condorcet PHP! Just make your module on your own namespace.
Condorcet PHP is not designed for high performance. But can handle virtually unlimited voting without limit or degrading performance, it's a linear and predictable scheme.
And has no certification or proven implementation that would guarantee a very high level of reliability. However, there are many written tests for each voting method and feature. This ensures an excellent level of confidence in the results.
Use Condorcet as a command line application
Install as a command line application
Can be installed natively from source (with composer), from PHAR file, from Docker image (build or pull).
Condorcet as a command line application, installation instructions
Condorcet Book - Command Line
Use Condorcet as a PHP Library
Install / Autoloading
Namespace \CondorcetPHP\Condorcet
is used.
Can be installed as you prefer with: Composer / Natively provided autoloader / Any PSR-4 compatible autoloader.
Library Manual
Living and learning examples, giving an overview but not exhaustive of the possibilities of the library.
Class & API References
The precise documentation of methods can be found in the form of Markdown in the "Documentation" folder for each release.
PHP Library - Examples
Overview
- General Overview (not exhaustive and partial, but just a great tour.)
- Advanced Object Management
With Html output basics examples
Specifics examples
- Condorcet Documentation Book provides many code example
- Manage millions of votes with an external database drive Your own driver, or the provided simple driver for PDO.
Performance & Coding style considerations
Coding standards:
The code is very close to the respect of PSR-12 (lacks only the naming of methods) when it is not unnecessarily authoritarian or conservative and follows some additional rules. Code is checked and fixed with CS-Fixer custom rules through Laravel Pint.
Performance:
- Complete and huge use case with all voting methods chained, 6 candidates, 2 seats, and one thousand votes (many with implicit ranking).
- Memory usage: less than 3M
- Execution time (after Jit compiling): less than 160ms
- Execution time (without JIT): less than 250ms
But essentially because some voting methods are slow by design and others (like Schulze) are very fast. Have a look on methods benchmarks.
Kemeny-Youg case:
1 000 randoms votes. Memory consumption comes from votes more than combinations.
- use Kemeny-Young 7 candidates: ~5MB - 10ms
- use Kemeny-Young 8 candidates: ~6MB - 10ms
- use Kemeny-Young 9 candidates: ~7MB - 1.1s
- use Kemeny-Young 10 candidates: ~7MB - 14s
- use Kemeny-Young 11 candidates: ~8MB - 193s
Massive election case:
Extending PHP memory_limit allows you to manage hundreds of thousands of votes, but it can be a bit slower than outsourcing this data (PHP doesn't like that) and it's not extensive to infinity.
If you need to manage an election with more than 50 000 votes. You should consider externalizing your data, Condorcet provides a simple PDO driver to store data outside RAM between processing steps, this driver stores it into a classical relational database system, and it supports hundreds of millions of votes (or more). A very simple example with Sqlite is provided and very easy to activate.
You can also develop your homemade datastore driver (to store into NoSQL... all your fantasy), the modular architecture allows you to link it easily.
Have a look at the documentation book
Benchmark on a modern machine (linux - x64 - php 8.1 - cli).
Roadmap for further releases
- ...