|
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 Condorcet\Algo\Methods; |
|
14
|
|
|
|
|
15
|
|
|
use Condorcet\Algo\Method; |
|
16
|
|
|
use Condorcet\Algo\MethodInterface; |
|
17
|
|
|
|
|
18
|
|
|
use 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->getCandidateId($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() as $oneVote) : |
|
54
|
5 |
|
$weight = ($this->_selfElection->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1; |
|
55
|
|
|
|
|
56
|
5 |
|
for ($i = 0 ; $i < $weight ; $i++) : |
|
57
|
5 |
|
$CandidatesRanked = 0; |
|
58
|
5 |
|
$oneRanking = $oneVote->getContextualRanking($this->_selfElection); |
|
59
|
|
|
|
|
60
|
5 |
|
foreach ($oneRanking as $oneRank) : |
|
61
|
5 |
|
$rankScore = 0; |
|
62
|
5 |
|
foreach ($oneRank as $oneCandidateInRank) : |
|
63
|
5 |
|
$rankScore += $this->getScoreByCandidateRanking($CandidatesRanked++); |
|
64
|
|
|
endforeach; |
|
65
|
|
|
|
|
66
|
5 |
|
foreach ($oneRank as $oneCandidateInRank) : |
|
67
|
5 |
|
$score[$this->_selfElection->getCandidateKey($oneCandidateInRank)] += $rankScore / count($oneRank); |
|
68
|
|
|
endforeach; |
|
69
|
|
|
endforeach; |
|
70
|
|
|
endfor; |
|
71
|
|
|
endforeach; |
|
72
|
|
|
|
|
73
|
5 |
|
arsort($score,SORT_NUMERIC); |
|
74
|
|
|
|
|
75
|
5 |
|
$rank = 0; |
|
76
|
5 |
|
$lastScore = null; |
|
77
|
5 |
|
$result = []; |
|
78
|
5 |
|
foreach ($score as $candidateKey => $candidateScore) : |
|
79
|
5 |
|
if ($candidateScore === $lastScore) : |
|
80
|
1 |
|
$result[$rank][] = $candidateKey; |
|
81
|
|
|
else : |
|
82
|
5 |
|
$result[++$rank] = [$candidateKey]; |
|
83
|
5 |
|
$lastScore = $candidateScore; |
|
84
|
|
|
endif; |
|
85
|
|
|
endforeach; |
|
86
|
|
|
|
|
87
|
5 |
|
$this->_Stats = $score; |
|
88
|
5 |
|
$this->_Result = $this->createResult($result); |
|
89
|
5 |
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
protected function getScoreByCandidateRanking (int $CandidatesRanked) : float |
|
92
|
|
|
{ |
|
93
|
4 |
|
return $this->_selfElection->countCandidates() + static::$starting - 1 - $CandidatesRanked; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|