|
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
|
|
|
/////////// TOOLS FOR MODULAR ALGORITHMS /////////// |
|
12
|
|
|
|
|
13
|
|
|
namespace CondorcetPHP\Condorcet\Algo\Tools; |
|
14
|
|
|
|
|
15
|
|
|
use CondorcetPHP\Condorcet\Algo\Pairwise; |
|
16
|
|
|
|
|
17
|
|
|
// Generic for Algorithms |
|
18
|
|
|
abstract class PairwiseStats |
|
19
|
|
|
{ |
|
20
|
20 |
|
public static function PairwiseComparison (Pairwise $pairwise) : array |
|
21
|
|
|
{ |
|
22
|
20 |
|
$comparison = []; |
|
23
|
|
|
|
|
24
|
20 |
|
foreach ($pairwise as $candidate_key => $candidate_data) : |
|
25
|
|
|
|
|
26
|
20 |
|
$comparison[$candidate_key]['win'] = 0; |
|
27
|
20 |
|
$comparison[$candidate_key]['null'] = 0; |
|
28
|
20 |
|
$comparison[$candidate_key]['lose'] = 0; |
|
29
|
20 |
|
$comparison[$candidate_key]['balance'] = 0; |
|
30
|
20 |
|
$comparison[$candidate_key]['sum_defeat_margin'] = 0; |
|
31
|
20 |
|
$comparison[$candidate_key]['worst_pairwise_defeat_winning'] = 0; |
|
32
|
20 |
|
$comparison[$candidate_key]['worst_pairwise_defeat_margin'] = null; |
|
33
|
20 |
|
$comparison[$candidate_key]['worst_pairwise_opposition'] = 0; |
|
34
|
|
|
|
|
35
|
20 |
|
foreach ($candidate_data['win'] as $opponentKey => $opponentLose) : |
|
36
|
|
|
|
|
37
|
20 |
|
$defeat_margin = $candidate_data['lose'][$opponentKey] - $opponentLose; |
|
38
|
|
|
|
|
39
|
|
|
// Worst margin defeat |
|
40
|
20 |
|
if ($comparison[$candidate_key]['worst_pairwise_defeat_margin'] === null || $comparison[$candidate_key]['worst_pairwise_defeat_margin'] < $defeat_margin) : |
|
41
|
|
|
|
|
42
|
20 |
|
$comparison[$candidate_key]['worst_pairwise_defeat_margin'] = $defeat_margin; |
|
43
|
|
|
|
|
44
|
|
|
endif; |
|
45
|
|
|
|
|
46
|
|
|
// Worst pairwise opposition |
|
47
|
20 |
View Code Duplication |
if ($comparison[$candidate_key]['worst_pairwise_opposition'] < $candidate_data['lose'][$opponentKey]) : |
|
48
|
|
|
|
|
49
|
19 |
|
$comparison[$candidate_key]['worst_pairwise_opposition'] = $candidate_data['lose'][$opponentKey]; |
|
50
|
|
|
endif; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
// for each Win, null, Lose |
|
54
|
20 |
|
if ( $opponentLose > $candidate_data['lose'][$opponentKey] ) : |
|
55
|
|
|
|
|
56
|
18 |
|
$comparison[$candidate_key]['win']++; |
|
57
|
18 |
|
$comparison[$candidate_key]['balance']++; |
|
58
|
|
|
|
|
59
|
20 |
|
elseif ( $opponentLose === $candidate_data['lose'][$opponentKey] ) : |
|
60
|
|
|
|
|
61
|
5 |
|
$comparison[$candidate_key]['null']++; |
|
62
|
|
|
|
|
63
|
|
|
else : |
|
64
|
|
|
|
|
65
|
18 |
|
$comparison[$candidate_key]['lose']++; |
|
66
|
18 |
|
$comparison[$candidate_key]['balance']--; |
|
67
|
|
|
|
|
68
|
18 |
|
$comparison[$candidate_key]['sum_defeat_margin'] += $defeat_margin; |
|
69
|
|
|
|
|
70
|
|
|
// Worst winning defeat |
|
71
|
18 |
View Code Duplication |
if ($comparison[$candidate_key]['worst_pairwise_defeat_winning'] < $candidate_data['lose'][$opponentKey]) : |
|
72
|
|
|
|
|
73
|
20 |
|
$comparison[$candidate_key]['worst_pairwise_defeat_winning'] = $candidate_data['lose'][$opponentKey]; |
|
74
|
|
|
|
|
75
|
|
|
endif; |
|
76
|
|
|
|
|
77
|
|
|
endif; |
|
78
|
|
|
|
|
79
|
|
|
endforeach; |
|
80
|
|
|
|
|
81
|
|
|
endforeach; |
|
82
|
|
|
|
|
83
|
20 |
|
return $comparison; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|