Advertisement
Guest User

Untitled

a guest
May 13th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 KB | None | 0 0
  1. <?php
  2.  
  3. // number of winners
  4. $max_winners = 10;
  5.  
  6. // seed mersenne twister with fixed value
  7. mt_srand(12345);
  8.  
  9. // read entries
  10. $entries = readlines("entries.txt");
  11.  
  12. // read votes
  13. $votes = array();
  14. $votefiles = scandir("votes");
  15. foreach($votefiles as $file) {
  16.     if(preg_match("/\.txt$/", $file))
  17.         $votes[basename($file, ".txt")] = readlines("votes/" . $file);
  18. }
  19.  
  20. echo "Entries:\n";
  21. print_r($entries);
  22. echo "Votes:\n";
  23. print_r($votes);
  24.  
  25. // calculate winners
  26. $winners = repeated_instant_runoff_vote($entries, $votes, min($max_winners, count($entries)));
  27.  
  28. // print result
  29. echo "#################### All winners ####################\n";
  30. print_r($winners);
  31.  
  32. function readlines($file) {
  33.     $lines = explode("\n", file_get_contents($file));
  34.     $ret = array();
  35.     foreach($lines as $line) {
  36.         $l = trim($line);
  37.         if($l != "")
  38.             $ret[] = $l;
  39.     }
  40.     return $ret;
  41. }
  42.  
  43. function random_real($a, $b) {
  44.     return $a + ($b - $a) * mt_rand() / mt_getrandmax();
  45. }
  46.  
  47. function repeated_instant_runoff_vote($entries, $votes, $repeats) {
  48.    
  49.     // make sure all votes are valid
  50.     foreach($votes as $voter => $vote) {
  51.         foreach($vote as $e) {
  52.             if(!in_array($e, $entries)) {
  53.                 die("Error: invalid vote for '$e' from '$voter'!\n");
  54.             }
  55.         }
  56.     }
  57.    
  58.     // one more winner and one less candidate after each iteration
  59.     $candidates = array_fill_keys($entries, 0);
  60.     $winners = array();
  61.     for($rep = 0; $rep < $repeats; ++$rep) {
  62.        
  63.         echo "#################### Winner " . ($rep + 1) . " ####################\n";
  64.        
  65.         // start voting
  66.         $candidates_left = $candidates;
  67.         while(count($candidates_left) > 1) {
  68.            
  69.             // count votes
  70.             $scores = $candidates_left;
  71.             foreach($votes as $vote) {
  72.                 foreach($vote as $e) {
  73.                     if(isset($scores[$e])) {
  74.                         ++$scores[$e];
  75.                         break;
  76.                     }
  77.                 }
  78.             }
  79.            
  80.             // random tie-breaker
  81.             foreach($scores as &$s) {
  82.                 $s += random_real(0.01, 0.09);
  83.             }
  84.             unset($s);
  85.            
  86.             // sort and eliminate the candidate with the lowest score
  87.             arsort($scores);
  88.             end($scores);
  89.             $eliminate = key($scores);
  90.             unset($candidates_left[$eliminate]);
  91.            
  92.             print_r($scores);
  93.             echo "'" . $eliminate . "' eliminated.\n";
  94.         }
  95.        
  96.         // get the winner
  97.         reset($candidates_left);
  98.         $winner = key($candidates_left);
  99.         unset($candidates[$winner]);
  100.         $winners[] = $winner;
  101.        
  102.         echo "=> Winner " . ($rep + 1) . " is '" . $winner . "'\n";
  103.        
  104.     }
  105.    
  106.     return $winners;
  107.    
  108. }
  109.  
  110. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement