1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Part of FTPT 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\Ftpt; |
14
|
|
|
|
15
|
|
|
use CondorcetPHP\Condorcet\Result; |
16
|
|
|
use CondorcetPHP\Condorcet\Algo\{Method, MethodInterface}; |
17
|
|
|
|
18
|
|
|
class Ftpt extends Method implements MethodInterface |
19
|
|
|
{ |
20
|
|
|
// Method Name |
21
|
|
|
public const METHOD_NAME = ['First-past-the-post voting', 'First-past-the-post', 'First Choice', 'FirstChoice', 'FTPT']; |
22
|
|
|
|
23
|
|
|
protected $_Stats; |
24
|
|
|
|
25
|
5 |
|
protected function getStats(): array |
26
|
|
|
{ |
27
|
5 |
|
$stats = []; |
28
|
|
|
|
29
|
5 |
|
foreach ($this->_Stats as $candidateKey => $oneScore) : |
30
|
5 |
|
$stats[(string)$this->_selfElection->getCandidateObjectFromKey($candidateKey)] = $oneScore; |
31
|
|
|
endforeach; |
32
|
|
|
|
33
|
5 |
|
return $stats; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/////////// COMPUTE /////////// |
38
|
|
|
|
39
|
|
|
//:: FTPT Count ::// |
40
|
|
|
|
41
|
5 |
|
protected function compute(): void |
42
|
|
|
{ |
43
|
5 |
|
$score = []; |
44
|
|
|
|
45
|
5 |
|
foreach ($this->_selfElection->getCandidatesList() as $oneCandidate) : |
46
|
5 |
|
$score[$this->_selfElection->getCandidateKey($oneCandidate)] = 0; |
47
|
|
|
endforeach; |
48
|
|
|
|
49
|
5 |
|
foreach ($this->_selfElection->getVotesManager()->getVotesValidUnderConstraintGenerator() as $oneVote) : |
50
|
|
|
|
51
|
5 |
|
$weight = $this->_selfElection->isVoteWeightAllowed() ? $oneVote->getWeight() : 1; |
52
|
|
|
|
53
|
5 |
|
for ($i = 0; $i < $weight; $i++) : |
54
|
5 |
|
$oneRanking = $oneVote->getContextualRanking($this->_selfElection); |
55
|
|
|
|
56
|
5 |
|
foreach ($oneRanking[1] as $oneCandidateInRank) : |
57
|
5 |
|
$score[$this->_selfElection->getCandidateKey($oneCandidateInRank)] += 1 / count($oneRanking[1]); |
58
|
|
|
endforeach; |
59
|
|
|
endfor; |
60
|
|
|
endforeach; |
61
|
|
|
|
62
|
5 |
|
arsort($score, SORT_NUMERIC); |
63
|
|
|
|
64
|
5 |
|
$rank = 0; |
65
|
5 |
|
$lastScore = null; |
66
|
5 |
|
$result = []; |
67
|
5 |
|
foreach ($score as $candidateKey => $candidateScore) : |
68
|
5 |
|
if ($candidateScore === $lastScore) : |
69
|
3 |
|
$result[$rank][] = $candidateKey; |
70
|
|
|
else : |
71
|
5 |
|
$result[++$rank] = [$candidateKey]; |
72
|
5 |
|
$lastScore = $candidateScore; |
73
|
|
|
endif; |
74
|
|
|
endforeach; |
75
|
|
|
|
76
|
5 |
|
$this->_Stats = $score; |
77
|
5 |
|
$this->_Result = $this->createResult($result); |
78
|
5 |
|
} |
79
|
|
|
} |
80
|
|
|
|