1
0
This commit is contained in:
2022-09-21 14:01:45 +02:00
parent 0a30f39eb4
commit cfee4d93d0
349 changed files with 4 additions and 4 deletions

View File

@ -0,0 +1,182 @@
<?php
declare(strict_types=1);
use CondorcetPHP\Condorcet\{Condorcet, Election};
use CondorcetPHP\Condorcet\Utils\CondorcetUtil;
require_once __DIR__.'/../../__CondorcetAutoload.php';
Condorcet::$UseTimer = true;
$election = new Election;
// Inluding Data
require_once 'vote_data'.\DIRECTORY_SEPARATOR.'ComplexeVoteConf.php';
\define('TEST_NAME', 'Condorcet Global Example');
// View :
?><!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo TEST_NAME; ?></title>
<style>
.votant {
display: inline-block;
margin-right: 2cm;
}
</style>
</head>
<body>
<header style="text-align:center;">
<img src="../../condorcet-logo.png" alt="Condorcet Class" style="width:15%;">
</header>
<h1><?php echo TEST_NAME; ?></h1>
<em style="font-weight:bold;"><a href="https://github.com/julien-boudry/Condorcet" target="_blank">Condorcet Class</a> version : <?php echo Condorcet::getVersion(); ?></em><br>
<em>
Number of Candidates :
<?php echo $election->countCandidates(); ?>
|
Number of votes :
<?php echo $election->countVotes(); ?>
</em>
<h2>Candidates list :</h2>
<ul>
<?php
foreach ($election->getCandidatesList() as $candidatName) {
echo '<li>'.$candidatName.'</li>';
}
?>
</ul>
<h2>Registered votes details :</h2>
<?php
foreach ($election->getVotesList() as $vote) {
echo '<div class="votant">';
echo '<strong style="color:green;">'.implode(' / ', $vote->getTags()).'</strong><br>';
echo '<ol>';
foreach ($vote as $rank => $value) {
if ($rank === 'tag') {
continue;
} ?>
<li><?php echo implode(',', $value); ?></li>
<?php
}
echo '</ol><br></div>';
}
?>
<hr style="clear:both;">
<h2>Winner by <a target="blank" href="http://en.wikipedia.org/wiki/Condorcet_method">natural Condorcet</a> :</h2>
<strong style="color:green;">
<?php
if ($election->getWinner() !== null) {
echo $election->getWinner();
} else {
echo '<span style="color:red;">The votes of this group do not allow natural Condorcet winner because of <a href="http://fr.wikipedia.org/wiki/Paradoxe_de_Condorcet" target="_blank">Condorcet paradox</a>.</span>';
}
?>
<br>
<em style="color:green;">computed in <?php echo number_format($election->getLastTimer(), 5); ?> second(s).</em> </strong>
<h2>Loser by <a target="blank" href="http://en.wikipedia.org/wiki/Condorcet_method">natural Condorcet</a> :</h2>
<strong style="color:green;">
<?php
if ($election->getLoser() !== null) {
echo $election->getLoser();
} else {
echo '<span style="color:red;">The votes of this group do not allow natural Condorcet loser because of <a href="http://fr.wikipedia.org/wiki/Paradoxe_de_Condorcet" target="_blank">Condorcet paradox</a>.</span>';
}
?>
<br>
<em style="color:green;">computed in <?php echo number_format($election->getLastTimer(), 5); ?> second(s).</em> </strong>
</strong>
<br><br><hr>
<?php
foreach (Condorcet::getAuthMethods() as $method) { ?>
<h2>Ranking by <?php echo $method; ?>:</h2>
<?php
$result = $election->getResult($method);
$lastTimer = $election->getLastTimer();
if ($method === 'KemenyYoung' && !empty($result->getWarning(\CondorcetPHP\Condorcet\Algo\Methods\KemenyYoung\KemenyYoung::CONFLICT_WARNING_CODE))) {
$kemeny_conflicts = explode(';', $result->getWarning(\CondorcetPHP\Condorcet\Algo\Methods\KemenyYoung\KemenyYoung::CONFLICT_WARNING_CODE)[0]['msg']);
echo '<strong style="color:red;">Arbitrary results: Kemeny-Young has '.$kemeny_conflicts[0].' possible solutions at score '.$kemeny_conflicts[1].'</strong>';
}
?>
<pre>
<?php var_dump(CondorcetUtil::format($result)); ?>
</pre>
<em style="color:green;">computed in <?php echo $lastTimer; ?> second(s).</em>
<?php }
?>
<br><br><hr><br>
<strong style="color:green;">Total computed in <?php echo number_format($election->getGlobalTimer(), 5); ?> second(s).</strong>
<br>
<?php var_dump($election->getTimerManager()->getHistory()); ?>
<br><br><hr>
<h2>Computing statistics :</h2>
<h3>Pairwise :</h3>
<pre>
<?php var_dump(CondorcetUtil::format($election->getPairwise())); ?>
</pre>
<?php
foreach (Condorcet::getAuthMethods() as $method) { ?>
<h3>Stats for <?php echo $method; ?>:</h3>
<pre>
<?php var_dump(CondorcetUtil::format($election->getResult($method)->getStats())); ?>
</pre>
<?php } ?>
<br><br><hr>
<h2>Debug Data :</h2>
<h4>Defaut method (not used explicitly before) :</h4>
<pre>
<?php var_dump(CondorcetUtil::format(Condorcet::getDefaultMethod())); ?>
</pre>
<!-- <h4>CondorcetUtil::format (for debug only) :</h4>
<pre>
<?php // CondorcetUtil::format($election);?>
</pre> -->
</body>
</html>

View File

@ -0,0 +1,254 @@
<?php
declare(strict_types=1);
use CondorcetPHP\Condorcet\{Condorcet, Election};
use CondorcetPHP\Condorcet\Utils\CondorcetUtil;
require_once __DIR__.'/../../__CondorcetAutoload.php';
Condorcet::$UseTimer = true;
$election = new Election;
// Inluding Data
require_once 'vote_data'.\DIRECTORY_SEPARATOR.'BasicVoteConf.php';
\define('TEST_NAME', 'Condorcet Bonus Example');
// View :
?><!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo TEST_NAME; ?></title>
<style>
.votant {
float: left;
margin-right: 2cm;
}
</style>
</head>
<body>
<header style="text-align:center;">
<img src="../../condorcet-logo.png" alt="Condorcet Class" style="width:15%;">
</header>
<h1><?php echo TEST_NAME; ?></h1>
<em style="font-weight:bold;"><a href="https://github.com/julien-boudry/Condorcet" target="_blank">Condorcet Class</a> version : <?php echo Condorcet::getVersion(); ?></em><br>
<em>
Number of Candidates :
<?php echo $election->countCandidates(); ?>
|
Number of votes :
<?php echo $election->countVotes(); ?>
</em>
<h2>Candidates list :</h2>
<ul>
<?php
foreach ($election->getCandidatesList() as $candidatName) {
echo '<li>'.$candidatName.'</li>';
}
?>
</ul>
<h2>Registered votes details :</h2>
<?php
foreach ($election->getVotesList() as $vote) {
echo '<div class="votant">';
echo '<strong style="color:green;">'.implode(' / ', $vote->getTags()).'</strong><br>';
echo '<ol>';
foreach ($vote as $rank => $value) {
if ($rank === 'tag') {
continue;
} ?>
<li><?php echo implode(',', $value); ?></li>
<?php
}
echo '</ol><br></div>';
}
?>
<br><hr style="clear:both;">
<h2>Get pairwise :</h2>
<pre>
<?php var_dump(CondorcetUtil::format($election->getPairwise())); ?>
</pre>
<br>
<em style="color:green;">computed in <?php echo number_format($election->getLastTimer(), 5); ?> second(s).</em>
<br><br><hr style="clear:both;">
<h2>Winner by <a target="blank" href="http://en.wikipedia.org/wiki/Condorcet_method">natural Condorcet</a> :</h2>
<strong style="color:green;">
<?php
if ($election->getWinner() !== null) {
echo $election->getWinner();
} else {
echo '<span style="color:red;">The votes of this group do not allow natural Condorcet winner because of <a href="http://fr.wikipedia.org/wiki/Paradoxe_de_Condorcet" target="_blank">Condorcet paradox</a>.</span>';
}
?>
</strong>
<h2>Loser by <a target="blank" href="http://en.wikipedia.org/wiki/Condorcet_method">natural Condorcet</a> :</h2>
<strong style="color:green;">
<?php
if ($election->getLoser() !== null) {
echo $election->getLoser();
} else {
echo '<span style="color:red;">The votes of this group do not allow natural Condorcet loser because of <a href="http://fr.wikipedia.org/wiki/Paradoxe_de_Condorcet" target="_blank">Condorcet paradox</a>.</span>';
}
?>
</strong>
<br><br><hr>
<h2>Some pratices about default method :</h2>
<h3>Use default method :</h3>
<strong>Defaut:</strong> <?php echo Condorcet::getDefaultMethod(); ?> <br>
<pre>
<?php var_dump(CondorcetUtil::format($election->getResult())); ?>
</pre>
<h3>Change it to MiniMax_Margin :</h3>
<?php Condorcet::setDefaultMethod('Minimax_Margin'); ?>
<strong>Defaut:</strong> <?php echo Condorcet::getDefaultMethod(); ?> <br>
<pre>
<?php var_dump(CondorcetUtil::format($election->getResult())); ?>
</pre>
<br><br><hr>
<h2>Vote manipulation :</h2>
<h3>Display votes with tag "custom_tag_One"</h3>
<?php
foreach ($election->getVotesList('custom_tag_One', true) as $vote) {
echo '<div class="votant">';
echo '<strong style="color:green;">'.implode(' / ', $vote->getTags()).'</strong><br>';
echo '<ol>';
foreach ($vote as $rank => $value) {
if ($rank === 'tag') {
continue;
} ?>
<li><?php echo implode(',', $value); ?></li>
<?php
}
echo '</ol><br></div>';
}
?>
<div style="clear:both;"></div>
<h3>Or without with tag "custom_tag_Two"</h3>
<?php
foreach ($election->getVotesList('custom_tag_Two', false) as $vote) {
echo '<div class="votant">';
echo '<strong style="color:green;">'.implode(' / ', $vote->getTags()).'</strong><br>';
echo '<ol>';
foreach ($vote as $rank => $value) {
if ($rank === 'tag') {
continue;
} ?>
<li><?php echo implode(',', $value); ?></li>
<?php
}
echo '</ol><br></div>';
}
?>
<div style="clear:both;"></div>
<h3>Get a ranking without "custom_tag_One" & "custom_tag_Two" tags and display Kemeny-Young result but don't delete it</h3>
<pre>
<?php
$options = [
'tags' => ['custom_tag_One', 'custom_tag_Two'],
'withTag' => false,
];
var_dump(CondorcetUtil::format($election->getResult('KemenyYoung', $options))); ?>
</pre>
<div style="clear:both;"></div>
<h3>Delete vote with "custom_tag_One" & "custom_tag_Two" tags and display Kemeny-Young result</h3> <?php // you can also delete vote without this tag, read the doc ( tips: removeVotesByTags('custom_tag_One', false) )?>
<?php
$election->removeVotesByTags(['custom_tag_One', 'custom_tag_Two']);
?>
<pre>
<?php var_dump(CondorcetUtil::format($election->getResult('KemenyYoung'))); ?>
</pre>
<h3>Check the new vote list</h3>
<?php
foreach ($election->getVotesList() as $vote) {
echo '<div class="votant">';
echo '<strong style="color:green;">'.implode(' / ', $vote->getTags()).'</strong><br>';
echo '<ol>';
foreach ($vote as $rank => $value) {
if ($rank === 'tag') {
continue;
} ?>
<li><?php echo implode(',', $value); ?></li>
<?php
}
echo '</ol><br></div>';
}
?>
<div style="clear:both;"></div>
<br><br><hr>
<!-- <h4>CondorcetUtil::format (for debug only) :</h4>
<pre>
<?php // CondorcetUtil::format($election);?>
</pre> -->
</body>
</html>

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
// Candidates
$election->addCandidate('Memphis');
$election->addCandidate('Nashville');
$election->addCandidate('Chatta');
$election->addCandidate('Knoxville');
// Votes
$vote[] = 'Memphis';
$vote[] = 'Nashville';
$vote[] = 'Chatta';
$vote[] = 'Knoxville';
for ($i = 1; $i <= 42; $i++) {
$election->addVote($vote, 'custom_tag_Two');
}
$vote = [];
$vote[] = 'Nashville';
$vote[] = 'Chatta';
$vote[] = 'Knoxville';
$vote[] = 'Memphis';
for ($i = 1; $i <= 26; $i++) {
$election->addVote($vote, 'custom_tag_Two');
}
$vote = [];
$vote[] = 'Chatta';
$vote[] = 'Knoxville';
$vote[] = 'Nashville';
$vote[] = 'Memphis';
for ($i = 1; $i <= 12; $i++) {
$election->addVote($vote);
}
for ($i = 1; $i <= 3; $i++) {
$election->addVote($vote, 'custom_tag_One');
}
$vote = [];
$vote[] = 'Knoxville';
$vote[] = 'Chatta';
$vote[] = 'Nashville';
$vote[] = 'Memphis';
for ($i = 1; $i <= 17; $i++) {
$election->addVote($vote, 'custom_tag_Two');
}
$vote = [];

View File

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
// Candidates
$election->addCandidate('A');
$election->addCandidate('C');
$election->addCandidate('B');
$election->addCandidate('E');
$election->addCandidate('D');
// Votes
$vote = [];
$vote[1] = 'A';
$vote[2] = 'C';
$vote[3] = 'B';
$vote[4] = 'E';
$vote[5] = 'D';
for ($i = 1; $i <= 5; $i++) {
$election->addVote($vote, 'coucou');
}
$vote = 'A>D>E>C>B';
for ($i = 1; $i <= 5; $i++) {
$election->addVote($vote);
}
$vote = 'B>E>D>A'; // It is not mandatory to indicate the role or last. It will be automatically deducted from the previous.
for ($i = 1; $i <= 8; $i++) {
$election->addVote($vote);
}
$vote = [];
$vote[1] = 'C';
$vote[2] = 'A';
$vote[3] = 'B';
$vote[4] = 'E';
// It is not mandatory to indicate the role or last. It will be automatically deducted from the previous.
for ($i = 1; $i <= 3; $i++) {
$election->addVote($vote);
}
$vote = [];
$vote[1] = 'C';
$vote[2] = 'A';
$vote[3] = 'E';
$vote[4] = 'B';
$vote[5] = 'D';
for ($i = 1; $i <= 7; $i++) {
$election->addVote($vote);
}
$vote = [];
$vote[1] = 'C';
$vote[2] = 'B';
$vote[3] = 'A';
$vote[4] = 'D';
$vote[5] = 'E';
for ($i = 1; $i <= 2; $i++) {
$election->addVote($vote);
}
$vote = [];
$vote[1] = 'D';
$vote[2] = 'C';
$vote[3] = 'E';
$vote[4] = 'B';
$vote[5] = 'A';
for ($i = 1; $i <= 7; $i++) {
$election->addVote($vote);
}
$vote = [];
$vote[1] = 'E';
$vote[2] = 'B';
$vote[3] = 'A';
$vote[4] = 'D';
$vote[5] = 'C';
for ($i = 1; $i <= 8; $i++) {
$election->addVote($vote);
}