|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
Part of RANKED PAIRS 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; |
|
14
|
|
|
|
|
15
|
|
|
use CondorcetPHP\Condorcet\Result; |
|
16
|
|
|
use CondorcetPHP\Condorcet\Algo\{Method, MethodInterface}; |
|
17
|
|
|
use CondorcetPHP\Condorcet\Algo\Tools\PairwiseStats; |
|
18
|
|
|
|
|
19
|
|
|
// DODGSON is a Condorcet Algorithm | http://en.wikipedia.org/wiki/DODGSON_method |
|
20
|
|
|
abstract class PairwiseStatsBased_Core extends Method implements MethodInterface |
|
21
|
|
|
{ |
|
22
|
|
|
protected $_Comparison; |
|
23
|
|
|
protected $_countType; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/////////// PUBLIC /////////// |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// Get the ranking |
|
30
|
18 |
|
public function getResult () : Result |
|
31
|
|
|
{ |
|
32
|
|
|
// Cache |
|
33
|
18 |
|
if ( $this->_Result !== null ) : |
|
34
|
10 |
|
return $this->_Result; |
|
35
|
|
|
endif; |
|
36
|
|
|
|
|
37
|
|
|
////// |
|
38
|
|
|
|
|
39
|
|
|
// Comparison calculation |
|
40
|
18 |
|
$this->_Comparison = PairwiseStats::PairwiseComparison($this->_selfElection->getPairwise()); |
|
41
|
|
|
|
|
42
|
|
|
// Ranking calculation |
|
43
|
18 |
|
$this->makeRanking(); |
|
44
|
|
|
|
|
45
|
|
|
// Return |
|
46
|
18 |
|
return $this->_Result; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
// Get the stats |
|
51
|
18 |
|
protected function getStats () : array |
|
52
|
|
|
{ |
|
53
|
18 |
|
$explicit = []; |
|
54
|
|
|
|
|
55
|
18 |
|
foreach ($this->_Comparison as $candidate_key => $value) : |
|
56
|
18 |
|
$explicit[$this->_selfElection->getCandidateObjectFromKey($candidate_key)->getName()] = [$this->_countType => $value[$this->_countType]]; |
|
57
|
|
|
endforeach; |
|
58
|
|
|
|
|
59
|
18 |
|
return $explicit; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/////////// COMPUTE /////////// |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
//:: ALGORITHM. ::// |
|
67
|
|
|
|
|
68
|
18 |
|
protected function makeRanking () : void |
|
69
|
|
|
{ |
|
70
|
18 |
|
$result = []; |
|
71
|
|
|
|
|
72
|
|
|
// Calculate ranking |
|
73
|
18 |
|
$challenge = []; |
|
74
|
18 |
|
$rank = 1; |
|
75
|
18 |
|
$done = 0; |
|
76
|
|
|
|
|
77
|
18 |
|
foreach ($this->_Comparison as $candidate_key => $candidate_data) : |
|
78
|
18 |
|
$challenge[$candidate_key] = $candidate_data[$this->_countType]; |
|
79
|
|
|
endforeach; |
|
80
|
|
|
|
|
81
|
18 |
|
while ($done < $this->_selfElection->countCandidates()) : |
|
82
|
18 |
|
$looking = $this->looking($challenge); |
|
83
|
|
|
|
|
84
|
18 |
|
foreach ($challenge as $candidate => $value) : |
|
85
|
18 |
|
if ($value === $looking) : |
|
86
|
18 |
|
$result[$rank][] = $candidate; |
|
87
|
|
|
|
|
88
|
18 |
|
$done++; |
|
89
|
18 |
|
unset($challenge[$candidate]); |
|
90
|
|
|
endif; |
|
91
|
|
|
endforeach; |
|
92
|
|
|
|
|
93
|
18 |
|
$rank++; |
|
94
|
|
|
endwhile; |
|
95
|
|
|
|
|
96
|
18 |
|
$this->_Result = $this->createResult($result); |
|
97
|
18 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
abstract protected function looking (array $challenge) : int; |
|
100
|
|
|
} |
|
101
|
|
|
|