1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Part of DODGSON QUICK 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\Dodgson; |
14
|
|
|
|
15
|
|
|
use CondorcetPHP\Condorcet\Result; |
16
|
|
|
use CondorcetPHP\Condorcet\Algo\{Method, MethodInterface}; |
17
|
|
|
|
18
|
|
|
// DODGSON Quick is an approximation for Dodgson method | https://www.maa.org/sites/default/files/pdf/cmj_ftp/CMJ/September%202010/3%20Articles/6%2009-229%20Ratliff/Dodgson_CMJ_Final.pdf |
19
|
|
|
class DodgsonQuick extends Method implements MethodInterface |
20
|
|
|
{ |
21
|
|
|
// Method Name |
22
|
|
|
public const METHOD_NAME = ['Dodgson Quick','DodgsonQuick','Dodgson Quick Winner']; |
23
|
|
|
|
24
|
|
|
protected $_Stats; |
25
|
|
|
|
26
|
10 |
|
protected function getStats () : array |
27
|
|
|
{ |
28
|
10 |
|
$stats = []; |
29
|
|
|
|
30
|
10 |
|
foreach ($this->_Stats as $candidateKey => $dodgsonQuickValue) : |
31
|
10 |
|
$stats[(string) $this->_selfElection->getCandidateObjectFromKey($candidateKey)] = $dodgsonQuickValue; |
32
|
|
|
endforeach; |
33
|
|
|
|
34
|
10 |
|
return $stats; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/////////// COMPUTE /////////// |
39
|
|
|
|
40
|
|
|
//:: DODGSON ALGORITHM. ::// |
41
|
|
|
|
42
|
10 |
|
protected function compute () : void |
43
|
|
|
{ |
44
|
10 |
|
$pairwise = $this->_selfElection->getPairwise(); |
45
|
10 |
|
$HeadToHead = []; |
46
|
|
|
|
47
|
10 |
|
foreach ($pairwise as $candidateId => $CandidateStats) : |
48
|
10 |
|
foreach ($CandidateStats['lose'] as $opponentId => $CandidateLose) : |
49
|
10 |
|
if (($diff = $CandidateLose - $CandidateStats['win'][$opponentId]) >= 0) : |
50
|
10 |
|
$HeadToHead[$candidateId][$opponentId] = $diff; |
51
|
|
|
endif; |
52
|
|
|
endforeach; |
53
|
|
|
endforeach; |
54
|
|
|
|
55
|
10 |
|
$dodgsonQuick = []; |
56
|
|
|
|
57
|
10 |
|
foreach ($HeadToHead as $candidateId => $CandidateTidemanScores) : |
58
|
10 |
|
$dodgsonQuick[$candidateId] = 0; |
59
|
|
|
|
60
|
10 |
|
foreach ($CandidateTidemanScores as $opponentId => $oneTidemanScore) : |
61
|
10 |
|
$dodgsonQuick[$candidateId] += ceil($oneTidemanScore / 2); |
62
|
|
|
endforeach; |
63
|
|
|
endforeach; |
64
|
10 |
|
asort($dodgsonQuick); |
65
|
|
|
|
66
|
10 |
|
$rank = 0; |
67
|
10 |
|
$result = []; |
68
|
|
|
|
69
|
10 |
|
if($basicCondorcetWinner = $this->_selfElection->getWinner(null)) : |
70
|
2 |
|
$result[++$rank][] = $this->_selfElection->getCandidateKey($basicCondorcetWinner); |
71
|
|
|
endif; |
72
|
|
|
|
73
|
10 |
|
$lastDodgsonQuickValue = null; |
74
|
|
|
|
75
|
10 |
|
foreach ($dodgsonQuick as $CandidateId => $dodgsonQuickValue) : |
76
|
10 |
|
if($lastDodgsonQuickValue === $dodgsonQuickValue) : |
77
|
5 |
|
$result[$rank][] = $CandidateId; |
78
|
|
|
else: |
79
|
10 |
|
$result[++$rank][] = $CandidateId; |
80
|
10 |
|
$lastDodgsonQuickValue = $dodgsonQuickValue; |
81
|
|
|
endif; |
82
|
|
|
endforeach; |
83
|
|
|
|
84
|
10 |
|
$this->_Stats = $dodgsonQuick; |
85
|
10 |
|
$this->_Result = $this->createResult($result); |
86
|
10 |
|
} |
87
|
|
|
} |
88
|
|
|
|