| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |     Part of BORDA COUNT method Module - From the original Condorcet PHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     Condorcet PHP - Election manager and results calculator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     https://github.com/julien-boudry/Condorcet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace CondorcetPHP\Condorcet\Algo\Methods; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use CondorcetPHP\Condorcet\Algo\Method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use CondorcetPHP\Condorcet\Algo\MethodInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use CondorcetPHP\Condorcet\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class BordaCount extends Method implements MethodInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     // Method Name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public const METHOD_NAME = ['BordaCount','Borda Count','Borda','Méthode Borda']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public static $starting = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $_Stats; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 5 |  |     protected function getStats () : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 5 |  |         $stats = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 5 |  |         foreach ($this->_Stats as $candidateKey => $oneScore) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  |              $stats[(string) $this->_selfElection->getCandidateObjectFromKey($candidateKey)] = $oneScore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         endforeach; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  |         return $stats; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | /////////// COMPUTE /////////// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     //:: BORDA ALGORITHM. ::// | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 5 |  |     protected function compute () : void | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 5 |  |         $score = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 5 |  |         foreach ($this->_selfElection->getCandidatesList() as $oneCandidate) : | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 5 |  |             $score[$this->_selfElection->getCandidateKey($oneCandidate)] = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         endforeach; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 5 |  |         foreach ($this->_selfElection->getVotesManager()->getVotesValidUnderConstraintGenerator() as $oneVote) : | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 5 |  |             $weight = ($this->_selfElection->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 5 |  |             for ($i = 0 ; $i < $weight ; $i++) : | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 5 |  |                 $CandidatesRanked = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 5 |  |                 $oneRanking = $oneVote->getContextualRanking($this->_selfElection); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 5 |  |                 foreach ($oneRanking as $oneRank) : | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 5 |  |                     $rankScore = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 5 |  |                     foreach ($oneRank as $oneCandidateInRank) : | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 5 |  |                         $rankScore += $this->getScoreByCandidateRanking($CandidatesRanked++); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                     endforeach; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 5 |  |                     foreach ($oneRank as $oneCandidateInRank) : | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 5 |  |                         $score[$this->_selfElection->getCandidateKey($oneCandidateInRank)] += $rankScore / count($oneRank); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                     endforeach; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                 endforeach; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             endfor; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         endforeach; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 5 |  |         arsort($score,SORT_NUMERIC); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 5 |  |         $rank = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 5 |  |         $lastScore = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 5 |  |         $result = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 5 |  |         foreach ($score as $candidateKey => $candidateScore) : | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 5 |  |             if ($candidateScore === $lastScore) : | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |                 $result[$rank][] = $candidateKey; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             else : | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 5 |  |                 $result[++$rank] = [$candidateKey]; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 5 |  |                 $lastScore = $candidateScore; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             endif; | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         endforeach; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 5 |  |         $this->_Stats = $score; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 5 |  |         $this->_Result = $this->createResult($result); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 4 |  |     protected function getScoreByCandidateRanking (int $CandidatesRanked) : float | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 4 |  |         return $this->_selfElection->countCandidates() + static::$starting - 1 - $CandidatesRanked; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |