Skip to content

Votes Constraints

Description

Votes constraints allow you to restrict or validate votes according to custom rules. You can use built-in constraints or create your own.

Excluding votes with tie

php
use \CondorcetPHP\Condorcet\Constraints\NoTie;

$election->addConstraint(NoTie::class);
$election->getConstraints(); // Return [NoTie::class]

$election->parseCandidates('A;B;C;D');
$election->parseVotes('
    B > A > C > D
    A > B = C = D
    A > B > C = D
' );

$election->getWinner(); // return Candidate B
$election->countValidVoteWithConstraints(); // return 1
$election->countInvalidVoteWithConstraints(); // return 2

$election->clearConstraints();

$election->getWinner(); // Return Candidate A
$election->countValidVoteWithConstraints(); // return 3
$election->countInvalidVoteWithConstraints(); // return 0
php
use \CondorcetPHP\Condorcet\Constraints\NoTie;

$election->allowsVoteWeight();
$election->parseCandidates('A;B;C;D');
$election->parseVotes('
    A>B>C>D
    C>B=A>D * 3 # Means 3 votes
    B>A ^42  #  Means 1 vote with weight of 42. B=D is implicit at the second rank.
' );


$election->getWinner(); // Return Candidate B

$election->addConstraint(NoTie::class);
$election->getWinner(); // Return Candidate A

$election->clearConstraints();
$election->getWinner(); // return Candidate B

$election->addConstraint(NoTie::class);
$election->getWinner(); // Return Candidate A

$election->sumValidVoteWeightsWithConstraints(); // Return 1
$election->sumVoteWeights(); // Return 46 (1 + 3 + 42)
$election->countVotes(); // Return 5
$election->countValidVoteWithConstraints(); // return 1
$election->countInvalidVoteWithConstraints(); // return 4

$election->getWinner(); // Return Candidate A
$election->setImplicitRanking(false);
$election->getWinner(); // Return Candidate B

// The vote B^42 become valid under constraint, since implicit ranking is false
$election->sumValidVoteWeightsWithConstraints(); // Return 43
$election->sumVoteWeights(); // Return 46
$election->countVotes(); // Return 5
$election->countValidVoteWithConstraints(); // Return 2
$election->countInvalidVoteWithConstraints(); // Return 3

Create your own constraint

Released under the MIT License.