Skip to content

Full Ranking

Usual Use

php
use \CondorcetPHP\Condorcet\Election;

$election = new Election;
$election->parseCandidates('Copland; Ives; Bernstein; Barber; Cage');

$election->addVote('Copland > Ives = Bernstein > Barber > Cage');
$election->addVote('Copland > Cage > Ives = Bernstein > Barber');

$result = $election->getResult('Schulze');

$winner = $result->Winner->name;
$loser = $result->Loser;
$condorcetWinner = $election->getCondorcetWinner() ?? 'No Condorcet winner';
$condorcetLoser =  $election->getCondorcetLoser() ?? 'No Condorcet Loser';

// Schulze Ranking
$ranking = $result->rankingAsArrayString;

_This example is dangerous, becouse Schulze winner and Loser can return array, tie is possible ; this code doesn't bother with that.

Results with with tags filter option

WARNING

Performances: Using getResult() with tags filter option doesn't use cache engine and computing each time you call it. Prefer to store the result object instead of call it multiple times.

php
$election->parseCandidates('A;B;C');

// Add votes with 'Julien' tag
$election->addVote('A > B > C', ['Julien']);
$election->addVote('A > C > B', ['Julien']);

// Add votes with 'Beethoven' tag
$election->addVote('B > A > C', ['Beethoven']);
$election->addVote('B > C > A', ['Beethoven']);

// Add votes with both tags
$election->addVote('C > A > B', ['Julien', 'Beethoven']);

// Add votes with no tags
$election->addVote('C > B > A');

// Use the Schulze ranking method, but only compute votes with tags 'Julien' or tag 'Beethoven'.
$election->getResult(
                        method: 'Schulze',
                        methodOptions: ['tags' => ['Julien', 'Beethoven'], 'withTag' => true]
                    );

// Use the Copeland method, no special parameters to it, but only compute with a vote without tag 'Julien' and without tag 'Beethoven'.
$election->getResult(
                        method: 'Copeland',
                        methodOptions: ['tags' => ['Julien', 'Beethoven'], 'withTag' => false]
                    );

Released under the MIT License.