1
0

test adding condorcet vote

This commit is contained in:
2022-09-21 12:39:11 +02:00
parent 1e8adfa5d5
commit 0a30f39eb4
349 changed files with 36658 additions and 0 deletions

View File

@ -0,0 +1,214 @@
<?php
declare(strict_types=1);
/* NO INTERFACE here, see html examples for it */
# Quick tour of the main features of Condorcet PHP
// I - Install
use CondorcetPHP\Condorcet\{Candidate, Election, Vote};
require_once __DIR__.'/../__CondorcetAutoload.php';
$start_time = microtime(true);
// II - Create Election
$election1 = new Election;
// III - Manage Candidate
# -A- Add candidates
// A string
$election1->addCandidate('Debussy');
$election1->addCandidate('Caplet');
$election1->addCandidate('A');
$myLutoCandidate = $election1->addCandidate('Lutoslawski'); // Return the CondorcetPHP\Candidate object.
// Automatic name
$myAutomaticCandidate = $election1->addCandidate(); // Return an automatic CondorcetPHP\Candidate
$myAutomaticCandidate->getName(); // return 'B'. Because we have already register the A candidate above.
// An objet
$myMessiaenCandidate = new Candidate('Olivier Messiaen');
$election1->addCandidate($myMessiaenCandidate);
$election1->addCandidate(new Candidate('Ligeti'));
$election1->addCandidate(new Candidate('Koechlin'));
# -B- Change your mind ?
$election1->removeCandidates('A');
$election1->removeCandidates($myAutomaticCandidate);
// Lutoslawski change his name
$myLutoCandidate->setName('Wiltod Lutoslawski'); # Done !
# What was his old names?
$myLutoCandidate->getHistory(); // return the full history with timestamp of this Candidate naming
# -C- Check your candidate list, if you forget it
$election1->getCandidatesList(); // Return an array pupulate by each Candidate objet
$election1->getCandidatesList(true); // Return an array pupulate by each Candidate name as String.
// OK, I need my Debussy (want his candidate object)
$myDebussyCandidate = $election1->getCandidateObjectFromName('Debussy');
// IV - Votes
# -A- Add Votes
$myVote1 = $election1->addVote([
1 => 'Debussy',
2 => ['Caplet', 'Olivier Messiaen'],
3 => 'Ligeti',
4 => ['Wiltod Lutoslawski', 'Koechlin'],
]); // Return the new Vote object
$myVote2 = new Vote([
1 => 'Debussy',
2 => 'Caplet',
3 => 'Olivier Messiaen',
4 => 'Koechlin',
5 => 'Wiltod Lutoslawski',
6 => 'Ligeti',
]);
$election1->addVote($myVote2);
$myVote3 = $election1->addVote('Debussy > Olivier Messiaen = Ligeti > Caplet'); // Last rank rank will be automatically deducted. A vote can not be expressed on a limited number of candidates. Non-expressed candidates are presumed to last Exaequo
// Off course, you can vote by candidate object. Which is recommended. Or mix the two methods.
$myVote4 = $election1->addVote([
$election1->getCandidateObjectFromName('Ligeti'),
$myLutoCandidate,
[$myMessiaenCandidate, 'Koechlin'],
]);
// Finishing with another really nice example.
$myVote5 = new Vote([
1 => $election1->getCandidateObjectFromName('Debussy'),
2 => $election1->getCandidateObjectFromName('Olivier Messiaen'),
3 => [$election1->getCandidateObjectFromName('Wiltod Lutoslawski'), $election1->getCandidateObjectFromName('Ligeti')],
4 => $election1->getCandidateObjectFromName('Koechlin'),
5 => $election1->getCandidateObjectFromName('Caplet'),
]);
$myVote5->addTags('jusGreatVote');
$election1->addVote($myVote5);
// Please note that :
$election1->getCandidateObjectFromName('Olivier Messiaen') === $myMessiaenCandidate; // Return TRUE
// Add some nice tags to my Vote 1 & 2 & 3 (You can do this before or after add register into to the Election)
$myVote1->addTags(['strangeVote', 'greatFrenchVote']); // By Array
$myVote2->addTags('greatFrenchVote,chauvinismVote'); // By String
$myVote3->addTags($myVote1->getTags()); // Copy & Past
// Parsing Vote
$election1->parseVotes("
Ligeti > Wiltod Lutoslawski > Olivier Messiaen = Debussy > Koechlin # A comment. A a line break for the next vote.
greatFrenchVote,chauvinismVote || Olivier Messiaen > Debussy = Caplet > Ligeti # Tags at first, vote at second, separated by '||'
strangeVote || Caplet > Koechlin * 8 # This vote and his tag will be register 8 times.
");
// Adding some random to this election
$VoteModel = $myVote2->getRanking();
for ($i = 0; $i < 95; $i++) {
shuffle($VoteModel);
$election1->addVote($VoteModel);
}
// How Many Vote could I Have now ?
$election1->countVotes(); // Return 110 (int)
# -B- Manage Votes
# 1- Get vote list
// Get the vote list
$election1->getVotesList(); // Returns an array of all votes as object.
// How many Vote with tag "strangeVote" ?
$election1->countVotes('strangeVote'); // Return 10 (int)
// Or without
$election1->countVotes('strangeVote', false); // Return 100 (int)
// Or with this tag OR this tag
$election1->countVotes(['greatFrenchVote', 'chauvinismVote']); // Return 4 (int)
// Or without this tag AND this tag
$election1->countVotes(['greatFrenchVote', 'chauvinismVote'], false); // Return 4 (int)
// Return this 10 votes !
$election1->getVotesList('strangeVote');
// And the others 100 votes without this tags
$election1->getVotesList('strangeVote', false);
// Or with this tag OR this tag
$election1->getVotesList(['greatFrenchVote', 'chauvinismVote']); // Return 4 (int)
// Or without this tag AND this tag
$election1->getVotesList(['greatFrenchVote', 'chauvinismVote'], false); // Return 4 (int)
# 2- Vote objet
$myVote3->getRanking(); // This vote specifies four candidates. Although the election comprises 6. This method return the original input.
$myVote3->getContextualRanking($election1); // Return the full ranking in the context of election 1 (with 6 candidates). It is one that is taken into account when calculating the results of the election 1.
// Change the vote
$myVote1->setRanking([
1 => 'Caplet',
2 => 'Debussy',
3=> 'Koechlin',
]);
// Check the vote history
$myVote1History = $myVote1->getHistory();
# 3- Delete Votes
// Delete a specific vote object
$election1->removeVote($myVote3);
// Delete all vote with tag "strangeVote" or "frenchies"
$election1->removeVotesByTags(['strangeVote', 'chauvinismVote']);
// Count vote
$election1->countVotes(); // Return 98
// V - Get Result
// Natural Condorcet Winner
$election1->getWinner(); // Return NULL if there is not, else return the winner candidate object
$election1->getWinner('Schulze'); // Same thing, but try to resolve winner by Schulze method that extends Condorcet method. Can return an array of winners if there are multiple.
// Natural Condorcet Loser
$election1->getLoser(); // Return NULL if there is not, else return the winner candidate object
$election1->getLoser('Schulze'); // Same thing, but try to resolve loser by Schulze method that extends Condorcet method. Can return an array of winners if there are multiple.
// Advanced Method -
$election1->getResult(); // Result set for defaut method (Should be Schulze Winning). That return a Condorcet/Result object.
$election1->getResult('Copeland'); // Do it with the Copeland method
/* Please note that a Result object is fixed and independent. It does not change automatically when you change the election. You then have requested the production of a new object result. */
// Get an easy game outcome to read and understand (Table populated by string)
$election1->getResult('Schulze')->getResultAsArray(true);
print $firstPart ?? 'Success!
Process in: '. round(microtime(true) - $start_time, 3) . 's';

View File

@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
/* NO INTERFACE here, see html examples for it */
# Quick tour of the main features of Condorcet PHP
// I - Install
use CondorcetPHP\Condorcet\{Candidate, Election, Vote};
$firstPart = '';
/* THE FOLLOWING CODE IS LIVE FOLLOWING THE FIRST PART EXPRESSED IN THE PREVIOUS FILE "1. Overview.php" */
require_once __DIR__.'/1. Overview.php';
// VI - Play with Condorcet objects (Advanced)
// Create a second election
$election2 = new Election;
// Create three candidate : 'A', 'B' and 'C'
for ($i = 0; $i < 3; $i++) {
$election2->addCandidate();
}
# Same candidate in multiple elections
// Add two participating candidates from $election1
$election2->addCandidate($election1->getCandidateObjectFromName('Debussy'));
$election2->addCandidate($myLutoCandidate);
// And, I can change again theirs name. The new name is now applied in the two elections and their votes. If namesake in another election, an exception is throw.
$myLutoCandidate->setName('W.Lutoslawski');
// Have a look on $myLutoCandidate history
$myLutoCandidate->getHistory();
// Have a look to a vote from $election1. The candidate name have changed.
$myVote4->getContextualRanking($election1, true);
// In what elections, this candidates have a part ?
$myLutoCandidate->getLinks(); // Get his the two Election objects
$myLutoCandidate->countLinks(); // Or just count it. return (int) 2.
# The same vote applied to multiple elections.
$myNewVote = new Vote([
1 => $election1->getCandidateObjectFromName('Debussy'),
2 => $election2->getCandidateObjectFromName('A'),
3 => $election1->getCandidateObjectFromName('Olivier Messiaen'),
4 => $election2->getCandidateObjectFromName('B'),
5 => $election1->getCandidateObjectFromName('Koechlin'),
6 => $election1->getCandidateObjectFromName('W.Lutoslawski'),
7 => new Candidate('Another candidate'), // This one does not takes part in any of two elections.
8 => $election1->getCandidateObjectFromName('Caplet'),
9 => $election2->getCandidateObjectFromName('C'),
]);
// Add it on election 1 and 2
$election1->addVote($myNewVote);
$election2->addVote($myNewVote);
// Check ranking
$myNewVote->getRanking(); // Here you get the original 9 ranks.
// Check ranking
$myNewVote->getContextualRanking($election1); // Here you get the vote applicable to $election 1
$myNewVote->getContextualRanking($election2); // Here you get the vote applicable to $election 2
// In what election, this candidates have a part ?
$myNewVote->getLinks(); // Get Condorcet objects
$myNewVote->countLinks(); // Or just count it
// Now we can change vote ranking. result from all election will be affected.
$myNewVote->setRanking([
1 => $election2->getCandidateObjectFromName('B'),
2 => $election1->getCandidateObjectFromName('Koechlin'),
3 => $election1->getCandidateObjectFromName('W.Lutoslawski'),
]);
# Get Ranking history
$myNewVote->getHistory();
print 'Success!
Process in: '. round(microtime(true) - $start_time, 3) . 's';

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);
}

View File

@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
/* NO INTERFACE here, see html examples for it */
# Quick tour of the main features of Condorcet PHP
// I - Install
use CondorcetPHP\Condorcet\{Candidate, Condorcet, Election};
use CondorcetPHP\Condorcet\DataManager\DataHandlerDrivers\PdoDriver\PdoHandlerDriver;
require_once __DIR__.'/../../__CondorcetAutoload.php';
$start_time = microtime(true);
// II - Create Election
$myElection = new Election;
$myElection->addCandidate(new Candidate('A'));
$myElection->addCandidate(new Candidate('B'));
$myElection->addCandidate(new Candidate('C'));
$myElection->addCandidate(new Candidate('D'));
$myElection->addCandidate(new Candidate('E'));
$myElection->addCandidate(new Candidate('F'));
// II - Setup external drivers
/* We will use PDO SQLITE, but can be MySQL or else */
if (file_exists(__DIR__.'/bdd.sqlite')) {
unlink(__DIR__.'/bdd.sqlite');
}
$pdo_object = new \PDO('sqlite:'.__DIR__.'/bdd.sqlite');
$database_map = ['tableName' => 'Entities', 'primaryColumnName' => 'id', 'dataColumnName' => 'data'];
$driver = new PdoHandlerDriver(bdd: $pdo_object, tryCreateTable: true, struct: $database_map); // true = Try to create table
$myElection->setExternalDataHandler($driver);
// III - Add hundred of thousands votes
set_time_limit(60 * 5);
$howMany = 100000;
$voteModel = $myElection->getCandidatesList();
for ($i = 0; $i < $howMany; $i++) {
shuffle($voteModel);
$myElection->addVote($voteModel);
}
// IV - Get somes results
$myElection->getWinner();
$myElection->getResult('Schulze');
print 'Success!
Process in: '. round(microtime(true) - $start_time, 2) . "s\n";
echo ' Peak of memory allocated : '.round(memory_get_peak_usage()/1024** ($i=floor(log(memory_get_peak_usage(), 1024))), 2).' '.['b', 'kb', 'mb', 'gb', 'tb', 'pb'][$i]."\n\n";
// Optionally. You can close external driver and and retrieve data into classical internal RAM memory, if there is enough space...
# $myElection->closeHandler();