|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
Condorcet PHP - Election manager and results calculator. |
|
4
|
|
|
Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems. |
|
5
|
|
|
|
|
6
|
|
|
By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt) |
|
7
|
|
|
https://github.com/julien-boudry/Condorcet |
|
8
|
|
|
*/ |
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace CondorcetPHP\Condorcet\Algo\Methods; |
|
12
|
|
|
|
|
13
|
|
|
use CondorcetPHP\Condorcet\Result; |
|
14
|
|
|
use CondorcetPHP\Condorcet\Algo\{Method, MethodInterface}; |
|
15
|
|
|
use CondorcetPHP\Condorcet\Throwable\CondorcetException; |
|
16
|
|
|
|
|
17
|
|
|
// Condorcet Basic Class, provide natural Condorcet winner or looser |
|
18
|
|
|
class CondorcetBasic extends Method implements MethodInterface |
|
19
|
|
|
{ |
|
20
|
|
|
// Method Name |
|
21
|
|
|
public const METHOD_NAME = ['CondorcetBasic']; |
|
22
|
|
|
|
|
23
|
|
|
// Basic Condorcet |
|
24
|
|
|
protected $_CondorcetWinner; |
|
25
|
|
|
protected $_CondorcetLoser; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/////////// PUBLIC /////////// |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
1 |
|
public function getResult () : Result { |
|
32
|
1 |
|
throw new CondorcetException (102); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
protected function getStats () : array { return []; } |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
// Get a Condorcet certified winner. If there is none = null. You can force a winner choice with alternative supported methods ($substitution) |
|
40
|
91 |
|
public function getWinner () : ?int |
|
41
|
|
|
{ |
|
42
|
|
|
// Cache |
|
43
|
91 |
|
if ( $this->_CondorcetWinner !== null ) : |
|
44
|
14 |
|
return $this->_CondorcetWinner; |
|
45
|
|
|
endif; |
|
46
|
|
|
|
|
47
|
|
|
////// |
|
48
|
|
|
|
|
49
|
|
|
// Basic Condorcet calculation |
|
50
|
91 |
|
foreach ( $this->_selfElection->getPairwise() as $candidate_key => $candidat_detail ) : |
|
51
|
91 |
|
$winner = true; |
|
52
|
|
|
|
|
53
|
91 |
|
foreach ($candidat_detail['win'] as $challenger_key => $win_count ) : |
|
54
|
91 |
|
if ( $win_count <= $candidat_detail['lose'][$challenger_key] ) : |
|
55
|
80 |
|
$winner = false; |
|
56
|
91 |
|
break; |
|
57
|
|
|
endif; |
|
58
|
|
|
endforeach; |
|
59
|
|
|
|
|
60
|
91 |
|
if ($winner) : |
|
61
|
43 |
|
$this->_CondorcetWinner = $candidate_key; |
|
62
|
91 |
|
return $this->_CondorcetWinner; |
|
63
|
|
|
endif; |
|
64
|
|
|
endforeach; |
|
65
|
|
|
|
|
66
|
52 |
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Get a Condorcet certified loser. If there is none = null. You can force a winner choice with alternative supported methods ($substitution) |
|
70
|
90 |
|
public function getLoser () : ?int |
|
71
|
|
|
{ |
|
72
|
|
|
// Cache |
|
73
|
90 |
|
if ( $this->_CondorcetLoser !== null ) : |
|
74
|
9 |
|
return $this->_CondorcetLoser; |
|
75
|
|
|
endif; |
|
76
|
|
|
|
|
77
|
|
|
////// |
|
78
|
|
|
|
|
79
|
|
|
// Basic Condorcet calculation |
|
80
|
90 |
|
foreach ( $this->_selfElection->getPairwise() as $candidate_key => $candidat_detail ) : |
|
81
|
90 |
|
$loser = true; |
|
82
|
|
|
|
|
83
|
90 |
|
foreach ( $candidat_detail['lose'] as $challenger_key => $lose_count ) : |
|
84
|
90 |
|
if ( $lose_count <= $candidat_detail['win'][$challenger_key] ) : |
|
85
|
76 |
|
$loser = false; |
|
86
|
90 |
|
break; |
|
87
|
|
|
endif; |
|
88
|
|
|
endforeach; |
|
89
|
|
|
|
|
90
|
90 |
|
if ($loser) : |
|
91
|
48 |
|
$this->_CondorcetLoser = $candidate_key; |
|
92
|
90 |
|
return $this->_CondorcetLoser; |
|
93
|
|
|
endif; |
|
94
|
|
|
endforeach; |
|
95
|
|
|
|
|
96
|
44 |
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|