1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Condorcet PHP Class, with Schulze Methods and others ! |
4
|
|
|
|
5
|
|
|
By Julien Boudry - MIT LICENSE (Please read LICENSE.txt) |
6
|
|
|
https://github.com/julien-boudry/Condorcet |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Condorcet\Algo; |
12
|
|
|
|
13
|
|
|
use Condorcet\CondorcetVersion; |
14
|
|
|
use Condorcet\Election; |
15
|
|
|
use Condorcet\Timer\Chrono as Timer_Chrono; |
16
|
|
|
|
17
|
|
|
class Pairwise implements \ArrayAccess, \Iterator |
18
|
|
|
{ |
19
|
|
|
use CondorcetVersion; |
20
|
|
|
|
21
|
|
|
// Implement ArrayAccess |
22
|
|
|
public function offsetSet($offset, $value) : void {} |
23
|
|
|
|
24
|
|
|
public function offsetExists($offset) : bool { |
25
|
|
|
return isset($this->_Pairwise[$offset]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function offsetUnset($offset) : void {} |
29
|
|
|
|
30
|
34 |
|
public function offsetGet($offset) : ?array { |
31
|
34 |
|
return $this->_Pairwise[$offset] ?? null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
// Implement Iterator |
36
|
|
|
private $valid = true; |
37
|
|
|
|
38
|
77 |
|
public function rewind() : void { |
39
|
77 |
|
reset($this->_Pairwise); |
40
|
77 |
|
$this->valid = true; |
41
|
77 |
|
} |
42
|
|
|
|
43
|
77 |
|
public function current() : array { |
44
|
77 |
|
return $this->_Pairwise[$this->key()]; |
45
|
|
|
} |
46
|
|
|
|
47
|
77 |
|
public function key() : ?int { |
48
|
77 |
|
return key($this->_Pairwise); |
49
|
|
|
} |
50
|
|
|
|
51
|
77 |
|
public function next() : void { |
52
|
77 |
|
if (next($this->_Pairwise) === false) : |
53
|
63 |
|
$this->valid = false; |
54
|
|
|
endif; |
55
|
77 |
|
} |
56
|
|
|
|
57
|
77 |
|
public function valid() : bool { |
58
|
77 |
|
return $this->valid; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
// Pairwise |
63
|
|
|
|
64
|
|
|
protected $_Election; |
65
|
|
|
protected $_Pairwise = []; |
66
|
|
|
|
67
|
80 |
|
public function __construct (Election &$link) |
68
|
|
|
{ |
69
|
80 |
|
$this->setElection($link); |
70
|
80 |
|
$this->doPairwise(); |
71
|
80 |
|
} |
72
|
|
|
|
73
|
1 |
|
public function __clone () |
74
|
|
|
{ |
75
|
1 |
|
$this->_Election = null; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
80 |
|
public function setElection (Election $election) : void |
79
|
|
|
{ |
80
|
80 |
|
$this->_Election = $election; |
81
|
80 |
|
} |
82
|
|
|
|
83
|
4 |
|
public function getExplicitPairwise () : array |
84
|
|
|
{ |
85
|
4 |
|
$explicit_pairwise = []; |
86
|
|
|
|
87
|
4 |
|
foreach ($this->_Pairwise as $candidate_key => $candidate_value) : |
88
|
|
|
|
89
|
4 |
|
$candidate_name = $this->_Election->getCandidateId($candidate_key, true); |
90
|
|
|
|
91
|
4 |
|
foreach ($candidate_value as $mode => $mode_value) : |
92
|
|
|
|
93
|
4 |
|
foreach ($mode_value as $candidate_list_key => $candidate_list_value) : |
94
|
4 |
|
$explicit_pairwise[$candidate_name][$mode][$this->_Election->getCandidateId($candidate_list_key, true)] = $candidate_list_value; |
95
|
|
|
endforeach; |
96
|
|
|
|
97
|
|
|
endforeach; |
98
|
|
|
|
99
|
|
|
endforeach; |
100
|
|
|
|
101
|
4 |
|
return $explicit_pairwise; |
102
|
|
|
} |
103
|
|
|
|
104
|
80 |
|
protected function doPairwise () : void |
105
|
|
|
{ |
106
|
|
|
// Chrono |
107
|
80 |
|
new Timer_Chrono ( $this->_Election->getTimerManager(), 'Do Pairwise' ); |
108
|
|
|
|
109
|
80 |
|
$this->formatNewpairwise(); |
110
|
|
|
|
111
|
|
|
// Win && Null |
112
|
80 |
|
foreach ( $this->_Election->getVotesManager() as $vote_id => $oneVote ) : |
113
|
80 |
|
$vote_ranking = $oneVote->getContextualRanking($this->_Election); |
114
|
|
|
|
115
|
80 |
|
$voteWeight = ($this->_Election->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1; |
116
|
|
|
|
117
|
80 |
|
$vote_candidate_list = (function (array $r) : array { $list = []; |
118
|
80 |
|
foreach ($r as $rank) : |
119
|
80 |
|
foreach ($rank as $oneCandidate) : |
120
|
80 |
|
$list[] = $oneCandidate; |
121
|
|
|
endforeach; |
122
|
|
|
endforeach; |
123
|
|
|
|
124
|
80 |
|
return $list;})($vote_ranking); |
125
|
|
|
|
126
|
80 |
|
$done_Candidates = []; |
127
|
|
|
|
128
|
80 |
|
foreach ($vote_ranking as $candidates_in_rank) : |
129
|
|
|
|
130
|
80 |
|
$candidates_in_rank_keys = []; |
131
|
|
|
|
132
|
80 |
|
foreach ($candidates_in_rank as $candidate) : |
133
|
80 |
|
$candidates_in_rank_keys[] = $this->_Election->getCandidateKey($candidate); |
134
|
|
|
endforeach; |
135
|
|
|
|
136
|
80 |
|
foreach ($candidates_in_rank as $candidate) : |
137
|
|
|
|
138
|
80 |
|
$candidate_key = $this->_Election->getCandidateKey($candidate); |
139
|
|
|
|
140
|
|
|
// Process |
141
|
80 |
|
foreach ( $vote_candidate_list as $g_Candidate ) : |
142
|
|
|
|
143
|
80 |
|
$g_candidate_key = $this->_Election->getCandidateKey($g_Candidate); |
144
|
|
|
|
145
|
80 |
|
if ($candidate_key === $g_candidate_key) : |
146
|
80 |
|
continue; |
147
|
|
|
endif; |
148
|
|
|
|
149
|
|
|
// Win & Lose |
150
|
80 |
|
if ( !in_array($g_candidate_key, $done_Candidates, true) && |
151
|
80 |
|
!in_array($g_candidate_key, $candidates_in_rank_keys, true) ) : |
152
|
|
|
|
153
|
80 |
|
$this->_Pairwise[$candidate_key]['win'][$g_candidate_key] += $voteWeight; |
154
|
80 |
|
$this->_Pairwise[$g_candidate_key]['lose'][$candidate_key] += $voteWeight; |
155
|
|
|
|
156
|
80 |
|
$done_Candidates[] = $candidate_key; |
157
|
|
|
|
158
|
|
|
// Null |
159
|
80 |
|
elseif (in_array($g_candidate_key, $candidates_in_rank_keys, true)) : |
160
|
80 |
|
$this->_Pairwise[$candidate_key]['null'][$g_candidate_key] += $voteWeight; |
161
|
|
|
endif; |
162
|
|
|
|
163
|
|
|
endforeach; |
164
|
|
|
|
165
|
|
|
endforeach; |
166
|
|
|
|
167
|
|
|
endforeach; |
168
|
|
|
|
169
|
|
|
endforeach; |
170
|
80 |
|
} |
171
|
|
|
|
172
|
80 |
|
protected function formatNewpairwise () : void |
173
|
|
|
{ |
174
|
80 |
|
foreach ( $this->_Election->getCandidatesList(false) as $candidate_key => $candidate_id ) : |
175
|
|
|
|
176
|
80 |
|
$this->_Pairwise[$candidate_key] = [ 'win' => [], 'null' => [], 'lose' => [] ]; |
177
|
|
|
|
178
|
80 |
|
foreach ( $this->_Election->getCandidatesList(false) as $candidate_key_r => $candidate_id_r ) : |
179
|
|
|
|
180
|
80 |
|
if ($candidate_key_r !== $candidate_key) : |
181
|
80 |
|
$this->_Pairwise[$candidate_key]['win'][$candidate_key_r] = 0; |
182
|
80 |
|
$this->_Pairwise[$candidate_key]['null'][$candidate_key_r] = 0; |
183
|
80 |
|
$this->_Pairwise[$candidate_key]['lose'][$candidate_key_r] = 0; |
184
|
|
|
endif; |
185
|
|
|
|
186
|
|
|
endforeach; |
187
|
|
|
|
188
|
|
|
endforeach; |
189
|
80 |
|
} |
190
|
|
|
|
191
|
|
|
} |