예제 #1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $election = new Election();
     $candidates = [1 => new Candidate('Bjørnar Moxnes'), 2 => new Candidate('Kari Elisabeth Kaski'), 3 => new Candidate('Lan Marie Nguyen Berg'), 4 => new Candidate('Abid Raja'), 5 => new Candidate('Per Sandberg')];
     foreach ($candidates as $candidate) {
         $election->addCandidate($candidate);
     }
     $votes = [[$candidates[1], $candidates[3], $candidates[2]], [$candidates[1], $candidates[3], $candidates[4]], [$candidates[1], $candidates[2], $candidates[4]], [$candidates[1], $candidates[2]], [$candidates[1], $candidates[2], $candidates[4]], [$candidates[5]], [$candidates[1], $candidates[2], $candidates[5]], [$candidates[1], $candidates[5]], [$candidates[1]], [$candidates[1]], [$candidates[2], $candidates[1], $candidates[3], $candidates[4]], [$candidates[2], $candidates[3], $candidates[4], $candidates[1], $candidates[5]], [$candidates[5], $candidates[4], $candidates[3]]];
     foreach ($votes as $vote) {
         $election->addVote($vote);
     }
     $result = $election->getResult('RankedPairs');
     Condorcet::format($result);
 }
// 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->getContextualVote($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(array(1 => $election1->getCandidateObjectByName('Debussy'), 2 => $election2->getCandidateObjectByName('A'), 3 => $election1->getCandidateObjectByName('Olivier Messiaen'), 4 => $election2->getCandidateObjectByName('B'), 5 => $election1->getCandidateObjectByName('Koechlin'), 6 => $election1->getCandidateObjectByName('W.Lutoslawski'), 7 => new Candidate('Another candidate'), 8 => $election1->getCandidateObjectByName('Caplet'), 9 => $election2->getCandidateObjectByName('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->getContextualVote($election1);
// Here you get the vote applicable to $election 1
$myNewVote->getContextualVote($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(array(1 => $election2->getCandidateObjectByName('B'), 2 => $election1->getCandidateObjectByName('Koechlin'), 3 => $election1->getCandidateObjectByName('W.Lutoslawski')));
예제 #3
0
// 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->getCandidateObjectByName('Debussy');
// IV - Votes
# -A- Add Votes
$myVote1 = $election1->addVote(array(1 => 'Debussy', 2 => ['Caplet', 'Olivier Messiaen'], 3 => 'Ligeti', 4 => ['Wiltod Lutoslawski', 'Koechlin']));
// Return the new Vote object
$myVote2 = new Vote(array(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(array($election1->getCandidateObjectByName('Ligeti'), $myLutoCandidate, [$myMessiaenCandidate, 'Koechlin']));
// Finishing with another really nice example.
$myVote5 = new Vote(array(1 => $election1->getCandidateObjectByName('Debussy'), 2 => $election1->getCandidateObjectByName('Olivier Messiaen'), 3 => [$election1->getCandidateObjectByName('Wiltod Lutoslawski'), $election1->getCandidateObjectByName('Ligeti')], 4 => $election1->getCandidateObjectByName('Koechlin'), 5 => $election1->getCandidateObjectByName('Caplet')));
$myVote5->addTags('jusGreatVote');
$election1->addVote($myVote5);
// Please note that :
$election1->getCandidateObjectByName('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)
$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 */
unlink(__DIR__ . '/bdd.sqlite');
$pdo_object = new \PDO('sqlite:' . __DIR__ . '/bdd.sqlite');
$database_map = ['tableName' => 'Entitys', 'primaryColumnName' => 'id', 'dataColumnName' => 'data'];
$driver = new PdoHandlerDriver($pdo_object, true, $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() / pow(1024, $i = floor(log(memory_get_peak_usage(), 1024))), 2) . ' ' . ['b', 'kb', 'mb', 'gb', 'tb', 'pb'][$i] . '<br>';
// Close external driver and and retrieve data into classical internal RAM memory.
/* $myElection->closeHandler(); */