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