Passed
Branch dev-1.5.x (fb93e4)
by Boudry
04:14
created

DodgsonQuick   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 77
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 10 2
A getStats() 0 9 2
D computeDodgson() 0 44 9
1
<?php
2
/*
3
    Dodgson part of the Condorcet PHP Class
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
namespace Condorcet\Algo\Methods;
11
12
use Condorcet\Algo\Method;
13
use Condorcet\Algo\MethodInterface;
14
use Condorcet\Algo\Tools\PairwiseStats;
15
use Condorcet\Result;
16
17
// DODGSON Quick is an approximation for Dodgson method | https://www.maa.org/sites/default/files/pdf/cmj_ftp/CMJ/September%202010/3%20Articles/6%2009-229%20Ratliff/Dodgson_CMJ_Final.pdf
18
class DodgsonQuick extends Method implements MethodInterface
19
{
20
    // Method Name
21
    public const METHOD_NAME = ['Dodgson Quick','DodgsonQuick','Dodgson Quick Winner'];
22
23 1
    public function getResult () : Result
24
    {
25
        // Cache
26 1
        if ( $this->_Result !== null ) :
27 1
            return $this->_Result;
28
        endif;
29
30 1
        $this->computeDodgson();
31
32 1
        return $this->_Result;
33
    }
34
35 1
    protected function getStats () : array
36
    {
37 1
        $stats = [];
38
39 1
        foreach ($this->_Stats as $candidateKey => $dodgsonQuickValue) :
40 1
             $stats[(string) $this->_selfElection->getCandidateId($candidateKey)] = $dodgsonQuickValue;
41
        endforeach;
42
43 1
        return $stats;
44
    }
45
46
47
/////////// COMPUTE ///////////
48
49
    //:: DODGSON ALGORITHM. :://
50
51 1
    protected function computeDodgson () : void
52
    {
53 1
        $pairwise = $this->_selfElection->getPairwise(false);
54 1
        $HeadToHead = [];
55
56 1
        foreach ($pairwise as $candidateId => $CandidateStats) :
57 1
            foreach ($CandidateStats['lose'] as $opponentId => $CandidateLose) :
58 1
                if (($diff = $CandidateLose - $CandidateStats['win'][$opponentId]) >= 0) :
59 1
                    $HeadToHead[$candidateId][$opponentId] = $diff;
60
                endif;
61
            endforeach;
62
        endforeach;
63
64 1
        $dodgsonQuick = [];
65
66 1
        foreach ($HeadToHead as $candidateId => $CandidateTidemanScores) :
67 1
            $dodgsonQuick[$candidateId] = 0;
68
69 1
            foreach ($CandidateTidemanScores as $opponentId => $oneTidemanScore) :
70 1
                $dodgsonQuick[$candidateId] += ceil($oneTidemanScore / 2);
71
            endforeach;
72
        endforeach;
73 1
        asort($dodgsonQuick);
74
75 1
        $rank = 0;
76 1
        $result = [];
77
78 1
        if($basicCondorcetWinner = $this->_selfElection->getWinner(null)) :
79
            $result[++$rank][] = $this->_selfElection->getCandidateKey($basicCondorcetWinner);
80
        endif;
81
82 1
        $lastDodgsonQuickValue = null;
83
84 1
        foreach ($dodgsonQuick as $CandidateId => $dodgsonQuickValue) :
85 1
            if($lastDodgsonQuickValue === $dodgsonQuickValue) :
86
                $result[$rank][] = $CandidateId;
87
            else:
88 1
                $result[++$rank][] = $CandidateId;
89 1
                $lastDodgsonQuickValue = $dodgsonQuickValue;
90
            endif;
91
        endforeach;
92
93 1
        $this->_Stats = $dodgsonQuick;
0 ignored issues
show
Bug Best Practice introduced by
The property _Stats does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
94 1
        $this->_Result = $this->createResult($result);
95 1
    }
96
97
}
98