1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Michaelc\Voting\STV; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface as Logger; |
6
|
|
|
use Michaelc\Voting\Exception\VotingLogicException as LogicException; |
7
|
|
|
use Michaelc\Voting\Exception\VotingRuntimeException as RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class will calculate the outcome of an election |
11
|
|
|
* |
12
|
|
|
* References throughout are provided as to what is going on. |
13
|
|
|
* It follows a system similar to Scottish STV and all paragraph |
14
|
|
|
* references are for: http://www.legislation.gov.uk/sdsi/2011/9780111014639/pdfs/sdsi_9780111014639_en.pdf |
15
|
|
|
*/ |
16
|
|
|
class ElectionRunner |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Logger. |
20
|
|
|
* |
21
|
|
|
* @var \Psr\Log\LoggerInterface |
22
|
|
|
*/ |
23
|
|
|
protected $logger; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Election object. |
27
|
|
|
* |
28
|
|
|
* @var \Michaelc\Voting\STV\Election; |
29
|
|
|
*/ |
30
|
|
|
protected $election; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Array of all ballots in election. |
34
|
|
|
* |
35
|
|
|
* @var \MichaelC\Voting\STV\Ballot[] |
36
|
|
|
*/ |
37
|
|
|
protected $ballots; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Quota of votes needed for a candidate to be elected. |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $quota; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Number of candidates elected so far. |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
protected $electedCandidates; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Invalid ballots. |
55
|
|
|
* |
56
|
|
|
* @var \MichaelC\Voting\STV\Ballot[] |
57
|
|
|
*/ |
58
|
|
|
protected $rejectedBallots; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Number of valid ballots |
62
|
|
|
* |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
protected $validBallots; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Number of winners to still be elected (at current stage) |
69
|
|
|
* |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
protected $candidatesToElect; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor. |
76
|
|
|
* |
77
|
|
|
* @param Election $election |
78
|
|
|
*/ |
79
|
2 |
|
public function __construct(Election $election, Logger $logger) |
80
|
|
|
{ |
81
|
2 |
|
$this->logger = $logger; |
82
|
2 |
|
$this->election = $election; |
83
|
|
|
|
84
|
2 |
|
$this->ballots = $this->election->getBallots(); |
85
|
2 |
|
$this->rejectedBallots = []; |
86
|
2 |
|
$this->candidatesToElect = $this->election->getWinnersCount(); |
87
|
2 |
|
$this->validBallots = $this->election->getNumBallots(); |
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Run the election. |
92
|
|
|
* |
93
|
|
|
* @return Candidate[] Winning candidates |
94
|
|
|
*/ |
95
|
1 |
|
public function run() |
96
|
|
|
{ |
97
|
1 |
|
$this->logger->notice('Starting to run an election'); |
98
|
1 |
|
$this->logger->notice(sprintf('There are %d candidates, %d ballots and to be %d winners', $this->election->getCandidateCount(), $this->validBallots, $this->election->getWinnersCount())); |
99
|
|
|
|
100
|
|
|
// Reject invalid ballots, then calculate the quota based on remaining valid ballots |
101
|
|
|
// p. 46(3) |
102
|
|
|
// p 47 |
103
|
1 |
|
$this->rejectInvalidBallots(); |
104
|
1 |
|
$this->quota = $this->getQuota(); |
105
|
|
|
|
106
|
|
|
// First step of standard allocation of ballots |
107
|
|
|
// p. 46(1) and 46(2) |
108
|
1 |
|
$this->firstStep(); |
109
|
|
|
|
110
|
1 |
|
$candidates = $this->election->getActiveCandidates(); |
111
|
|
|
|
112
|
|
|
// All the re-allocation rounds until we have filled all seats or |
113
|
|
|
// have the same number of seats left to fill and candidates remaining |
114
|
|
|
// (then elect them) |
115
|
1 |
|
$this->processReallocationRounds($candidates); |
116
|
|
|
// p. 53 |
117
|
1 |
|
$this->reallocateRemainingVotes($candidates); |
118
|
|
|
|
119
|
1 |
|
$this->logger->notice('Election complete'); |
120
|
|
|
|
121
|
1 |
|
return $this->election->getElectedCandidates(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Perform the initial vote allocation. |
126
|
|
|
* p. 46 |
127
|
|
|
* |
128
|
|
|
* @return |
129
|
|
|
*/ |
130
|
1 |
|
protected function firstStep() |
131
|
|
|
{ |
132
|
1 |
|
$this->logger->info('Beginning the first step'); |
133
|
|
|
|
134
|
|
|
// Allocate all the ballots |
135
|
1 |
|
foreach ($this->ballots as $i => $ballot) { |
136
|
1 |
|
$this->allocateVotes($ballot); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
$this->logger->notice('Step 1 complete', |
140
|
1 |
|
['candidatesStatus' => $this->election->getCandidatesStatus()] |
141
|
|
|
); |
142
|
|
|
|
143
|
1 |
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Process re-allocation rounds (elimination re-allocations and surplus re-allocations) |
148
|
|
|
* |
149
|
|
|
* @param Candidate[] $candidates All active candidates to elect |
150
|
|
|
* |
151
|
|
|
* @return Candidate[] |
152
|
|
|
*/ |
153
|
1 |
|
protected function processReallocationRounds(array &$candidates): array |
154
|
|
|
{ |
155
|
1 |
|
$counter = 1; |
156
|
1 |
|
while (($this->candidatesToElect > 0) && ($this->election->getActiveCandidateCount() > $this->candidatesToElect)) { |
157
|
1 |
|
if (!$this->checkCandidates($candidates)) { |
158
|
|
|
// p. 51(1) |
159
|
1 |
|
$this->eliminateCandidates($candidates); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
$candidates = $this->election->getActiveCandidates(); |
163
|
|
|
|
164
|
1 |
|
$counter++; |
165
|
|
|
|
166
|
1 |
|
$this->logger->notice("Step $counter complete", |
167
|
1 |
|
['candidatesStatus' => $this->election->getCandidatesStatus()] |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
return $candidates; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Check if any candidates have reached the quota and can be elected. |
176
|
|
|
* |
177
|
|
|
* @param Candidate[] $candidates Array of active candidates to check |
178
|
|
|
* |
179
|
|
|
* @return bool Whether any candidates were changed to elected |
180
|
|
|
*/ |
181
|
1 |
|
protected function checkCandidates(array $candidates): bool |
182
|
|
|
{ |
183
|
1 |
|
$elected = false; |
184
|
1 |
|
$candidatesToElect = []; |
185
|
|
|
|
186
|
1 |
|
$this->logger->info('Checking if candidates have passed quota'); |
187
|
|
|
|
188
|
1 |
|
if (empty($candidates)) |
189
|
|
|
{ |
190
|
|
|
throw new LogicException("There are no more candidates left"); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
foreach ($candidates as $i => $candidate) { |
194
|
1 |
|
if ($candidate->getState() !== Candidate::RUNNING) |
195
|
|
|
{ |
196
|
|
|
throw new LogicException('Candidate is not marked as not running but has not been excluded'); |
197
|
|
|
} |
198
|
|
|
|
199
|
1 |
|
$votes = $candidate->getVotes(); |
200
|
|
|
|
201
|
1 |
|
$this->logger->debug("Checking candidate ($candidate) with $votes", ['candidate' => $candidate]); |
202
|
|
|
|
203
|
|
|
// p. 48(1) |
204
|
|
|
// Immediately elect a candidate if they hit the quota |
205
|
|
|
// We check all the candidates, see who has hit the quota, |
206
|
|
|
// add them to a queue, then elect those who have hit the quota to prevent |
207
|
|
|
// surplus allocations pushing a candidate over the quota early. |
208
|
1 |
|
if ($votes >= $this->quota) { |
209
|
1 |
|
$candidatesToElect[] = $candidate; |
210
|
1 |
|
$elected = true; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// TODO: Put this in a try, and catch the RuntimeError. |
215
|
|
|
// Then sort by the surplus, and fill available seats. |
216
|
|
|
// If have same surplus then select randomly (Contary to Scottish STV) |
217
|
|
|
// p. 50 |
218
|
1 |
|
$this->electCandidates($candidatesToElect); |
219
|
|
|
|
220
|
1 |
|
$this->logger->info(('Candidate checking complete. Elected: ' . count($candidatesToElect))); |
221
|
|
|
|
222
|
1 |
|
return $elected; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Elect an array of candidates |
227
|
|
|
* |
228
|
|
|
* @param Candidate[] $candidates Array of candidates to elect |
229
|
|
|
* |
230
|
|
|
* @return |
231
|
|
|
*/ |
232
|
1 |
|
protected function electCandidates(array $candidates) |
233
|
|
|
{ |
234
|
1 |
|
if ($this->candidatesToElect < count($candidates)) |
235
|
|
|
{ |
236
|
|
|
throw new RuntimeException('Cannot elect candidate as not enough seats to fill'); |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
foreach ($candidates as $i => $candidate) { |
240
|
1 |
|
$this->electCandidate($candidate); |
241
|
|
|
} |
242
|
|
|
|
243
|
1 |
|
return; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Allocate the next votes from a Ballot. |
248
|
|
|
* p. 49 |
249
|
|
|
* |
250
|
|
|
* @param Ballot $ballot The ballot to allocate the votes from |
251
|
|
|
* @param float $multiplier Number to multiply the weight by (surplus) |
252
|
|
|
* @param float $divisor The divisor of the weight (Total number of |
253
|
|
|
* candidate votes) |
254
|
|
|
* |
255
|
|
|
* @return Ballot The same ballot passed in modified |
256
|
|
|
*/ |
257
|
1 |
|
protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
258
|
|
|
{ |
259
|
1 |
|
$currentWeight = $ballot->getWeight(); |
260
|
|
|
// p. 49(3) |
261
|
|
|
// "A divided by B" Where A = the value which is calculated |
262
|
|
|
// by multiplying the surplus of the transferring candidate |
263
|
|
|
// by the value of the ballot paper when received by that candidate; and |
264
|
|
|
// B = the total number of votes credited to that candidate |
265
|
1 |
|
$weight = $ballot->setWeight(($currentWeight * $multiplier) / $divisor); |
266
|
|
|
|
267
|
|
|
// Get the next candidate on their ballot paper which has not been assigned |
268
|
|
|
// a vote this could be the first candidate in round 1 |
269
|
1 |
|
$candidate = $ballot->getNextChoice(); |
270
|
|
|
|
271
|
1 |
|
$this->logger->debug("Allocating vote of weight $weight to $candidate. Previous weight: $currentWeight", array( |
272
|
1 |
|
'ballot' => $ballot, |
273
|
|
|
)); |
274
|
|
|
|
275
|
|
|
// Check there was a next candidate, if only x candidates where listed where |
276
|
|
|
// x < the number of candidates standing this will occur. |
277
|
1 |
|
if ($candidate !== null) { |
278
|
|
|
// Allocate those surplus votes. p. 49(1) and p. 49(2) |
279
|
1 |
|
$this->election->getCandidate($candidate)->addVotes($weight); |
280
|
1 |
|
$ballot->incrementLevelUsed(); |
281
|
1 |
|
$this->logger->debug('Vote added to candidate'); |
282
|
|
|
|
283
|
|
|
// If the candidate is no longer running due to being defeated or |
284
|
|
|
// elected then we re-allocate their vote again. |
285
|
1 |
|
if (!in_array($candidate, $this->election->getActiveCandidateIds())) |
286
|
|
|
{ |
287
|
1 |
|
$this->allocateVotes($ballot); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
1 |
|
return $ballot; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Transfer the votes from one candidate to other candidates. |
296
|
|
|
* |
297
|
|
|
* @param float $surplus The number of surplus votes to transfer |
298
|
|
|
* @param Candidate $candidate The candidate being elected to transfer |
299
|
|
|
* the votes from |
300
|
|
|
* |
301
|
|
|
* @return |
302
|
|
|
*/ |
303
|
1 |
|
protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
304
|
|
|
{ |
305
|
1 |
|
$totalVotes = $candidate->getVotes(); |
306
|
1 |
|
$candidateId = $candidate->getId(); |
307
|
|
|
|
308
|
1 |
|
$this->logger->info('Transfering surplus votes'); |
309
|
|
|
|
310
|
1 |
|
foreach ($this->ballots as $i => $ballot) { |
311
|
1 |
|
if ($ballot->getLastChoice() == $candidateId) { |
312
|
1 |
|
$this->allocateVotes($ballot, $surplus, $totalVotes); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
1 |
|
return; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Transfer the votes from one eliminated candidate to other candidates. |
321
|
|
|
* p. 51(2) |
322
|
|
|
* |
323
|
|
|
* @param Candidate $candidate Candidate being eliminated to transfer |
324
|
|
|
* the votes from |
325
|
|
|
* |
326
|
|
|
* @return |
327
|
|
|
*/ |
328
|
1 |
|
protected function transferEliminatedVotes(Candidate $candidate) |
329
|
|
|
{ |
330
|
1 |
|
$candidateId = $candidate->getId(); |
331
|
|
|
|
332
|
1 |
|
$votes = $candidate->getVotes(); |
333
|
|
|
|
334
|
1 |
|
$this->logger->info("Transfering votes from eliminated candidate ($candidate) with $votes votes"); |
335
|
|
|
|
336
|
|
|
// p. 51(2)(a) - Sort into next preference candidates |
337
|
|
|
// p. 51(3) - Add votes to candidates |
338
|
|
|
// p. 51(4) - Use previous weighting |
339
|
1 |
|
foreach ($this->ballots as $i => $ballot) { |
340
|
1 |
|
if ($ballot->getLastChoice() == $candidateId) { |
341
|
1 |
|
$this->allocateVotes($ballot); |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
345
|
1 |
|
return; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Elect a candidate after they've passed the threshold. |
350
|
|
|
* |
351
|
|
|
* @param Candidate $candidate |
352
|
|
|
*/ |
353
|
1 |
|
protected function electCandidate(Candidate $candidate) |
354
|
|
|
{ |
355
|
1 |
|
$this->logger->notice("Electing a candidate: $candidate"); |
356
|
|
|
|
357
|
1 |
|
$candidate->setState(Candidate::ELECTED); |
358
|
1 |
|
$this->electedCandidates++; |
359
|
1 |
|
$this->candidatesToElect--; |
360
|
|
|
|
361
|
1 |
|
if ($this->electedCandidates < $this->election->getWinnersCount()) { |
362
|
1 |
|
$surplus = $candidate->getVotes() - $this->quota; |
363
|
1 |
|
if ($surplus > 0) { |
364
|
1 |
|
$this->transferSurplusVotes($surplus, $candidate); |
365
|
|
|
} else { |
366
|
1 |
|
$this->logger->notice("No surplus votes from $candidate to reallocate"); |
367
|
|
|
} |
368
|
|
|
|
|
|
|
|
369
|
|
|
} |
370
|
|
|
|
371
|
1 |
|
return; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Eliminate the candidate with the lowest number of votes |
376
|
|
|
* and reallocated their votes. |
377
|
|
|
* p. 51 |
378
|
|
|
* |
379
|
|
|
* @param Candidate[] $candidates Array of active candidates |
380
|
|
|
* |
381
|
|
|
* @return int Number of candidates eliminated |
382
|
|
|
*/ |
383
|
1 |
|
protected function eliminateCandidates(array $candidates): int |
384
|
|
|
{ |
385
|
1 |
|
$minimumCandidates = $this->getLowestCandidates($candidates); |
386
|
1 |
|
$count = count($minimumCandidates); |
387
|
|
|
|
388
|
|
|
// p. 52(2)(b) - the returning officer shall decide, by lot, which of those |
389
|
|
|
// candidates is to be excluded. |
390
|
|
|
// We do not look back on previous rounds at all as per p. 52(2)(a) |
391
|
1 |
|
$minimumCandidate = $minimumCandidates[(array_rand($minimumCandidates))]; |
392
|
|
|
|
393
|
1 |
|
$this->logger->notice(sprintf("There were %d joint lowest candidates, |
394
|
1 |
|
%d was randomly selected to be eliminated", $count, $minimumCandidate->getId())); |
395
|
|
|
|
396
|
1 |
|
$this->transferEliminatedVotes($minimumCandidate); |
397
|
1 |
|
$minimumCandidate->setState(Candidate::DEFEATED); |
398
|
|
|
|
399
|
1 |
|
return count($minimumCandidates); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Get candidates with the lowest number of votes |
404
|
|
|
* p. 51 |
405
|
|
|
* p. 52(1) |
406
|
|
|
* |
407
|
|
|
* @param Candidate[] $candidates |
408
|
|
|
* Array of active candidates |
409
|
|
|
* |
410
|
|
|
* @return Candidate[] |
411
|
|
|
* Candidates with lowest score |
412
|
|
|
*/ |
413
|
1 |
|
public function getLowestCandidates(array $candidates): array |
414
|
|
|
{ |
415
|
1 |
|
$minimum = count($this->election->getBallots()); |
416
|
1 |
|
$minimumCandidates = []; |
417
|
|
|
|
418
|
1 |
|
foreach ($candidates as $i => $candidate) { |
419
|
1 |
|
if ($candidate->getVotes() < $minimum) { |
420
|
1 |
|
$minimum = $candidate->getVotes(); |
421
|
1 |
|
unset($minimumCandidates); |
422
|
1 |
|
$minimumCandidates[] = $candidate; |
423
|
1 |
|
} elseif ($candidate->getVotes() == $minimum) { |
424
|
1 |
|
$minimumCandidates[] = $candidate; |
425
|
1 |
|
$this->logger->info("Calculated as a lowest candidate: $candidate"); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|
429
|
1 |
|
return $minimumCandidates; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Reject any invalid ballots. |
434
|
|
|
* p. 46(3) |
435
|
|
|
* |
436
|
|
|
* @return int Number of rejected ballots |
437
|
|
|
*/ |
438
|
1 |
|
protected function rejectInvalidBallots(): int |
439
|
|
|
{ |
440
|
1 |
|
foreach ($this->ballots as $i => $ballot) { |
441
|
1 |
|
if (!$this->checkBallotValidity($ballot)) { |
442
|
1 |
|
$this->rejectedBallots[] = clone $ballot; |
443
|
1 |
|
unset($this->ballots[$i]); |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|
447
|
1 |
|
$count = count($this->rejectedBallots); |
448
|
|
|
|
449
|
1 |
|
$this->logger->notice("Found $count rejected ballots"); |
450
|
|
|
|
451
|
1 |
|
$this->validBallots = $this->validBallots - $count; |
452
|
|
|
|
453
|
1 |
|
return $count; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Check if ballot is valid. |
458
|
|
|
* |
459
|
|
|
* @param Ballot $ballot Ballot to test |
460
|
|
|
* |
461
|
|
|
* @return bool True if valid, false if invalid |
462
|
|
|
*/ |
463
|
2 |
|
public function checkBallotValidity(Ballot $ballot): bool |
464
|
|
|
{ |
465
|
2 |
|
if (count($ballot->getRanking()) > $this->election->getCandidateCount()) { |
466
|
2 |
|
$this->logger->debug('Invalid ballot - number of candidates', ['ballot' => $ballot]); |
467
|
|
|
|
468
|
2 |
|
return false; |
469
|
|
|
} else { |
470
|
2 |
|
$candidateIds = $this->election->getCandidateIds(); |
471
|
|
|
|
472
|
2 |
|
foreach ($ballot->getRanking() as $i => $candidate) { |
473
|
2 |
|
if (!in_array($candidate, $candidateIds)) { |
474
|
2 |
|
$this->logger->debug('Invalid ballot - invalid candidate'); |
475
|
|
|
|
476
|
2 |
|
return false; |
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
// TODO: Check for candidates multiple times on the same ballot paper |
482
|
|
|
|
483
|
2 |
|
$this->logger->debug('Ballot is valid', ['ballot' => $ballot]); |
484
|
|
|
|
485
|
2 |
|
return true; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Reallocate any remaining votes |
490
|
|
|
* p. 53 |
491
|
|
|
* |
492
|
|
|
* @param Candidate[] $candidates All remaining candidates to elect |
493
|
|
|
* @return |
494
|
|
|
*/ |
495
|
1 |
|
protected function reallocateRemainingVotes(array &$candidates) |
496
|
|
|
{ |
497
|
1 |
|
if (!empty($candidates)) |
498
|
|
|
{ |
499
|
1 |
|
$this->logger->info('All votes re-allocated. Electing all remaining candidates'); |
500
|
|
|
|
501
|
1 |
|
if ($this->candidatesToElect < count($candidates)) |
502
|
|
|
{ |
503
|
|
|
throw new LogicException('Cannot elect candidate as no more seats to fill'); |
504
|
|
|
} |
505
|
|
|
|
506
|
1 |
|
$this->electCandidates($candidates); |
507
|
|
|
} |
508
|
|
|
|
509
|
1 |
|
return; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Get the quota to win. |
514
|
|
|
* p. 47 |
515
|
|
|
* |
516
|
|
|
* @return int |
517
|
|
|
*/ |
518
|
1 |
|
public function getQuota(): int |
519
|
|
|
{ |
520
|
1 |
|
$quota = floor( |
521
|
1 |
|
($this->validBallots / |
522
|
1 |
|
($this->election->getWinnersCount() + 1) |
523
|
|
|
) // p. 47 (1) |
524
|
1 |
|
+ 1); // p. 47 (2) |
525
|
|
|
|
526
|
1 |
|
$this->logger->info(sprintf("Quota set at %d based on %d winners and %d valid ballots", $quota, $this->election->getWinnersCount(), $this->validBallots)); |
527
|
|
|
|
528
|
1 |
|
return $quota; |
529
|
|
|
} |
530
|
|
|
} |
531
|
|
|
|