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
|
|
|
|
12
|
|
|
/////////// TOOLS FOR MODULAR ALGORITHMS /////////// |
13
|
|
|
|
14
|
|
|
namespace Condorcet\Algo\Tools; |
15
|
|
|
|
16
|
|
|
use Condorcet\Algo\Pairwise; |
17
|
|
|
|
18
|
|
|
// Generic for Algorithms |
19
|
|
|
abstract class PairwiseStats |
20
|
|
|
{ |
21
|
4 |
|
public static function PairwiseComparison (Pairwise $pairwise) : array |
22
|
|
|
{ |
23
|
4 |
|
$comparison = []; |
24
|
|
|
|
25
|
4 |
|
foreach ($pairwise as $candidate_key => $candidate_data) : |
26
|
|
|
|
27
|
4 |
|
$comparison[$candidate_key]['win'] = 0; |
28
|
4 |
|
$comparison[$candidate_key]['null'] = 0; |
29
|
4 |
|
$comparison[$candidate_key]['lose'] = 0; |
30
|
4 |
|
$comparison[$candidate_key]['balance'] = 0; |
31
|
4 |
|
$comparison[$candidate_key]['sum_defeat_margin'] = 0; |
32
|
4 |
|
$comparison[$candidate_key]['worst_pairwise_defeat_winning'] = 0; |
33
|
4 |
|
$comparison[$candidate_key]['worst_pairwise_defeat_margin'] = null; |
34
|
4 |
|
$comparison[$candidate_key]['worst_pairwise_opposition'] = 0; |
35
|
|
|
|
36
|
4 |
|
foreach ($candidate_data['win'] as $opponentKey => $opponentLose) : |
37
|
|
|
|
38
|
4 |
|
$defeat_margin = $candidate_data['lose'][$opponentKey] - $opponentLose; |
39
|
|
|
|
40
|
|
|
// Worst margin defeat |
41
|
4 |
|
if ($comparison[$candidate_key]['worst_pairwise_defeat_margin'] === null || $comparison[$candidate_key]['worst_pairwise_defeat_margin'] < $defeat_margin) : |
42
|
|
|
|
43
|
4 |
|
$comparison[$candidate_key]['worst_pairwise_defeat_margin'] = $defeat_margin; |
44
|
|
|
|
45
|
|
|
endif; |
46
|
|
|
|
47
|
|
|
// Worst pairwise opposition |
48
|
4 |
View Code Duplication |
if ($comparison[$candidate_key]['worst_pairwise_opposition'] < $candidate_data['lose'][$opponentKey]) : |
|
|
|
|
49
|
|
|
|
50
|
4 |
|
$comparison[$candidate_key]['worst_pairwise_opposition'] = $candidate_data['lose'][$opponentKey]; |
51
|
|
|
endif; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
// for each Win, null, Lose |
55
|
4 |
|
if ( $opponentLose > $candidate_data['lose'][$opponentKey] ) : |
56
|
|
|
|
57
|
4 |
|
$comparison[$candidate_key]['win']++; |
58
|
4 |
|
$comparison[$candidate_key]['balance']++; |
59
|
|
|
|
60
|
4 |
|
elseif ( $opponentLose === $candidate_data['lose'][$opponentKey] ) : |
61
|
|
|
|
62
|
2 |
|
$comparison[$candidate_key]['null']++; |
63
|
|
|
|
64
|
|
|
else : |
65
|
|
|
|
66
|
4 |
|
$comparison[$candidate_key]['lose']++; |
67
|
4 |
|
$comparison[$candidate_key]['balance']--; |
68
|
|
|
|
69
|
4 |
|
$comparison[$candidate_key]['sum_defeat_margin'] += $defeat_margin; |
70
|
|
|
|
71
|
|
|
// Worst winning defeat |
72
|
4 |
View Code Duplication |
if ($comparison[$candidate_key]['worst_pairwise_defeat_winning'] < $candidate_data['lose'][$opponentKey]) : |
|
|
|
|
73
|
|
|
|
74
|
4 |
|
$comparison[$candidate_key]['worst_pairwise_defeat_winning'] = $candidate_data['lose'][$opponentKey]; |
75
|
|
|
|
76
|
|
|
endif; |
77
|
|
|
|
78
|
|
|
endif; |
79
|
|
|
|
80
|
|
|
endforeach; |
81
|
|
|
|
82
|
|
|
endforeach; |
83
|
|
|
|
84
|
4 |
|
return $comparison; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.