PHP code example of mieuxvoter / majority-judgment

1. Go to this page and download the library: Download mieuxvoter/majority-judgment library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mieuxvoter / majority-judgment example snippets


use MieuxVoter\MajorityJudgment\MajorityJudgment;
use MieuxVoter\MajorityJudgment\Model\Tally\ArrayPollTally;

$grades = [
    "to reject",
    "insufficient",
    "passable",
    "somewhat good",
    "good",
    "very good",
    "excellent",
];
$proposals = [
    "Arancini",
    "Burger",
    "Chips",
];
$meritProfiles = [
    [1, 1, 4, 3, 7, 4, 1], // for each proposal, tally the
    [2, 2, 5, 5, 5, 0, 2], // amount of judgments for each grade
    [0, 1, 2, 1, 6, 7, 4], // from "worst" grade to "best" grade
];

$pollTally = new ArrayPollTally($meritProfiles);
$mj = new MajorityJudgment();
$result = $mj->deliberate($pollTally);

foreach($result->getProposalResultsRanked() as $proposalResult) {
    // … Do something, for example:
    print(sprintf(
        "#%d %s (%s)\n",
        $proposalResult->getRank(),
        $proposals[$proposalResult->getIndex()],
        $grades[$proposalResult->getMedian()],
    ));
}

// #1 Chips (very good)
// #2 Arancini (good)
// #3 Burger (somewhat good)


$meritProfiles = [
    [3, 3, 3, 3, 3],
    [0, 1, 2, 3, 4],
    [0, 2, 4, 6, 8],
    [7, 7, 7, 7, 7],
];

$balanceUsingNormalization = true;
$pollTally = new ArrayPollTally($meritProfiles);
$mj = new MajorityJudgment();
$result = $mj->deliberate($pollTally, $balanceUsingNormalization);

// Ranks:
// 3
// 1
// 1
// 3

use MieuxVoter\MajorityJudgment\Balancer;

$initialPollTally = new ArrayPollTally(
    [
        "Arancini" => [1, 2, 3, 4],
        "Burger" => [4, 0, 0, 0],
        "Chips" => [0, 1, 1, 2],
    ],
);

$balancedPollTally = Balancer::balanceUsingStaticDefaultGrade($initialPollTally);

print_r($balancedPollTally->getProposalsTallies()[0]->getGradesTallies());
print_r($balancedPollTally->getProposalsTallies()[1]->getGradesTallies());
print_r($balancedPollTally->getProposalsTallies()[2]->getGradesTallies());

// [  1, 2, 3, 4 ]
// [ 10, 0, 0, 0 ]
// [  6, 1, 1, 2 ]

$initialPollTally = new ArrayPollTally(
    [
        "Arancini" => [1, 2, 3, 4],
        "Burger" => [2, 0, 0, 0],
        "Chips" => [0, 1, 1, 0],
    ],
);

$balancedPollTally = Balancer::balanceUsingMedianDefaultGrade($initialPollTally);

print_r($balancedPollTally->getProposalsTallies()[0]->getGradesTallies());
print_r($balancedPollTally->getProposalsTallies()[1]->getGradesTallies());
print_r($balancedPollTally->getProposalsTallies()[2]->getGradesTallies());

// [1, 2, 3, 4]
// [10, 0, 0, 0]
// [0, 9, 1, 0]