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\Borda; |
14
|
|
|
|
15
|
|
|
use CondorcetPHP\Condorcet\Result; |
16
|
|
|
use CondorcetPHP\Condorcet\Algo\{Method, MethodInterface}; |
17
|
|
|
|
18
|
|
|
class BordaCount extends Method implements MethodInterface |
19
|
|
|
{ |
20
|
|
|
// Method Name |
21
|
|
|
public const METHOD_NAME = ['BordaCount','Borda Count','Borda','Méthode Borda']; |
22
|
|
|
|
23
|
|
|
public static $starting = 1; |
24
|
|
|
|
25
|
|
|
protected $_Stats; |
26
|
|
|
|
27
|
5 |
|
protected function getStats () : array |
28
|
|
|
{ |
29
|
5 |
|
$stats = []; |
30
|
|
|
|
31
|
5 |
|
foreach ($this->_Stats as $candidateKey => $oneScore) : |
32
|
5 |
|
$stats[(string) $this->_selfElection->getCandidateObjectFromKey($candidateKey)] = $oneScore; |
33
|
|
|
endforeach; |
34
|
|
|
|
35
|
5 |
|
return $stats; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/////////// COMPUTE /////////// |
40
|
|
|
|
41
|
|
|
//:: BORDA ALGORITHM. ::// |
42
|
|
|
|
43
|
5 |
|
protected function compute () : void |
44
|
|
|
{ |
45
|
5 |
|
$score = []; |
46
|
|
|
|
47
|
5 |
|
foreach ($this->_selfElection->getCandidatesList() as $oneCandidate) : |
48
|
5 |
|
$score[$this->_selfElection->getCandidateKey($oneCandidate)] = 0; |
49
|
|
|
endforeach; |
50
|
|
|
|
51
|
5 |
|
foreach ($this->_selfElection->getVotesManager()->getVotesValidUnderConstraintGenerator() as $oneVote) : |
52
|
|
|
|
53
|
5 |
|
$weight = $this->_selfElection->isVoteWeightAllowed() ? $oneVote->getWeight() : 1; |
54
|
|
|
|
55
|
5 |
|
for ($i = 0 ; $i < $weight ; $i++) : |
56
|
5 |
|
$CandidatesRanked = 0; |
57
|
5 |
|
$oneRanking = $oneVote->getContextualRanking($this->_selfElection); |
58
|
|
|
|
59
|
5 |
|
foreach ($oneRanking as $oneRank) : |
60
|
5 |
|
$rankScore = 0; |
61
|
5 |
|
foreach ($oneRank as $oneCandidateInRank) : |
62
|
5 |
|
$rankScore += $this->getScoreByCandidateRanking($CandidatesRanked++); |
63
|
|
|
endforeach; |
64
|
|
|
|
65
|
5 |
|
foreach ($oneRank as $oneCandidateInRank) : |
66
|
5 |
|
$score[$this->_selfElection->getCandidateKey($oneCandidateInRank)] += $rankScore / count($oneRank); |
67
|
|
|
endforeach; |
68
|
|
|
endforeach; |
69
|
|
|
endfor; |
70
|
|
|
endforeach; |
71
|
|
|
|
72
|
5 |
|
arsort($score,SORT_NUMERIC); |
73
|
|
|
|
74
|
5 |
|
$rank = 0; |
75
|
5 |
|
$lastScore = null; |
76
|
5 |
|
$result = []; |
77
|
5 |
|
foreach ($score as $candidateKey => $candidateScore) : |
78
|
5 |
|
if ($candidateScore === $lastScore) : |
79
|
1 |
|
$result[$rank][] = $candidateKey; |
80
|
|
|
else : |
81
|
5 |
|
$result[++$rank] = [$candidateKey]; |
82
|
5 |
|
$lastScore = $candidateScore; |
83
|
|
|
endif; |
84
|
|
|
endforeach; |
85
|
|
|
|
86
|
5 |
|
$this->_Stats = $score; |
87
|
5 |
|
$this->_Result = $this->createResult($result); |
88
|
5 |
|
} |
89
|
|
|
|
90
|
4 |
|
protected function getScoreByCandidateRanking (int $CandidatesRanked) : float |
91
|
|
|
{ |
92
|
4 |
|
return $this->_selfElection->countCandidates() + static::$starting - 1 - $CandidatesRanked; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|