Count/Method/MinMax.pm

filter: Statementstmt 100% 108 / 108 Branchbran 96.1% 25 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 99.3% 151 / 152 CC 20 SCAR 15.7
CC
20
Cov
99%
ScoreMinMax
8.0
MinMax
7.0
_pairmatrixtable1
6.0
CRAP 4.8
« Count/IRV.pm ↑ Summary Count/Redact.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Vote::Count::Method::MinMax;
7  
8 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 extends 'Vote::Count';
11  
12 1 our $VERSION='1.10';
13  
14 =head1 NAME
15  
16 Vote::Count::Method::MinMax
17  
18 =head1 VERSION 1.10
19  
20 =cut
21  
22 # ABSTRACT: Methods in the MinMax Family.
23  
24 =pod
25  
26 =head1 SYNOPSIS
27  
28 my $MinMaxElection =
29 Vote::Count::Method::MinMax->new( 'BallotSet' => $ballotset );
30
31 # $method is one of: winning margin opposition
32 my $Winner = $MinMaxElection->MinMax( $method )->{'winner'};
33 say $MinMaxElection->logv();
34  
35 =head1 The MinMax Methods
36  
37 MinMax (also known as Minimax and Simpson-Kramer) uses a Pairwise comparison Matrix. Instead of looking at wins and losses as with Condorcet Methods, it scores each pairing, the choice with the lowest worst pairing score wins.
38  
39 =head2 The Three MinMax Scoring Rules
40  
41 =head3 Winning Votes ('winning')
42  
43 When the choice being scored loses, the votes for the winner in the pairing are scored. When the choice wins or ties the pairing is scored as 0.
44  
45 This scoring method meets the Condorcet Winner, but not the Smith, Condorcet Loser or Later Harm Criteria.
46  
47 =head3 Margin ('margin')
48  
49 When the choice being scored loses, the votes for the winner minus the votes for that choice in the pairing are scored. When the choice wins or ties the pairing is scored as 0.
50  
51 This scoring method meets the Condorcet Winner, but not the Smith, Condorcet Loser or Later Harm Criteria.
52  
53 =head3 Opposition ('opposition')
54  
55 The votes for the other choice in the pairing are scored regardless of whether the choice won or lost.
56  
57 This scoring method is claimed to meet the Later Harm Criteria, but fails Condorcet Winner and Smith.
58  
59 =head2 Tie Breaker
60  
61 As a Tie Breaker it is recommended to use the next worst pairing score. Because it follows the method and should resolve well, this Tie Breaker is implemented by Vote::Count within the MinMax method itself. If it returns a tie your implementation can apply another method like Modified Grand-Junction.
62  
63 =cut
64  
65 1 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 1 ▶use Vote::Count::TextTableTiny qw/generate_table/;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 # use Data::Dumper;
71  
72 =pod
73  
74 =head2 ScoreMinMax
75  
76 Generate hashref scoring according the requested $method which is one of three scoring rules: 'winning', 'margin', 'opposition'.
77  
78 my $scores = $MinMaxElection->ScoreMinMax( $method );
79  
80 =cut
81  
82 ▶sub ScoreMinMax ( $self, $method ) {
Subroutine ScoreMinMax: called CC=8 SCAR=20.8
Pod documented
83 16 my $scores = {};
84 # Always grab the matrix by calling PairMatrix,
85 # build is lazy.
86 16 my $Matrix = $self->PairMatrix()->{'Matrix'};
87 16 my @choices = sort ( keys $self->Active()->%* );
88 16 for my $Choice (@choices) {
89 97 my @ChoiceLoss = ();
90 97 LOOPMMMO: for my $Opponent (@choices) {
91 643 ▶ next LOOPMMMO if $Opponent eq $Choice;
Branch: 1 branch
BranchTrueFalseTotal
if $Opponent eq $Choice 97 546 643
92 546 my $M = $Matrix->{$Choice}{$Opponent};
93 546 my $S = undef;
94 546 ▶ if ( $method eq 'opposition' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'opposition') { } 274 272 546
elsif ($M->{'winner'} eq $Opponent) { } 136 136 272
95 274 $S = $M->{$Opponent};
96 }
97 elsif ( $M->{'winner'} eq $Opponent ) {
98 136 ▶ $S = $M->{$Opponent} if $method eq 'winning';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "winning" 68 68 136
99 136 ▶ $S = $M->{$Opponent} - $M->{$Choice} if $method eq 'margin';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "margin" 68 68 136
100 }
101 else {
102 136 $S = 0;
103 }
104 546 $scores->{$Choice}{$Opponent} = $S;
105 # there was a bug where sometimes @ChoiceLoss was sorted
106 # alphanumerically. resolution force the sort to be numeric.
107 546 push @ChoiceLoss, ( $S );
108 } # LOOPMMMO:
109 $scores->{$Choice}{score}
110 97 = [ reverse sort { $a <=> $b } @ChoiceLoss ];
111 }
112 16 return $scores;
113 }
114  
115 ▶sub _pairmatrixtable1 ( $I, $scores ) {
Subroutine _pairmatrixtable1: called CC=6 SCAR=17.9
116 8 my @rows = ( [qw/Choice Choice Votes Opponent Votes Score/] );
117 8 my @choices = sort ( keys $I->Active()->%* );
118 8 my $Matrix = $I->PairMatrix()->{'Matrix'};
119 8 for my $Choice (@choices) {
120 47 push @rows, [$Choice];
121 47 for my $Opponent (@choices) {
122 305 my $Cstr = $Choice;
123 305 my $Ostr = $Opponent;
124 305 ▶ next if $Opponent eq $Choice;
Branch: 1 branch
BranchTrueFalseTotal
if $Opponent eq $Choice 47 258 305
125 258 my $CVote = $Matrix->{$Choice}{$Opponent}{$Choice};
126 258 my $OVote = $Matrix->{$Choice}{$Opponent}{$Opponent};
127 258 ▶ if ( $Matrix->{$Choice}{$Opponent}{'winner'} eq $Choice ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Matrix->{$Choice}{$Opponent}{"winner"} eq $Choice) 127 131 258
128 127 $Cstr = "**$Cstr**";
129 }
130 258 ▶ if ( $Matrix->{$Choice}{$Opponent}{'winner'} eq $Opponent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Matrix->{$Choice}{$Opponent}{"winner"} eq $Opponent) 127 131 258
131 127 $Ostr = "**$Ostr**";
132 }
133 258 my $Score = $scores->{$Choice}{$Opponent};
134 258 push @rows, [ ' ', $Cstr, $CVote, $Ostr, $OVote, $Score ];
135 }
136 }
137 8 return generate_table( rows => \@rows );
138 }
139  
140 ▶sub _pairmatrixtable2 ( $I, $scores ) {
Subroutine _pairmatrixtable2: called CC=2 SCAR=6.9
141 8 my @rows = ( [qw/Choice Scores/] );
142 8 my @choices = sort ( keys $I->Active()->%* );
143 8 for my $Choice (@choices) {
144 47 my $scores = join ', ', ( $scores->{$Choice}{'score'}->@* );
145 47 push @rows, [ $Choice, $scores ];
146 }
147 8 return generate_table( rows => \@rows );
148 }
149  
150 =pod
151  
152 =head2 MinMaxPairingVotesTable
153  
154 Generate a formatted table of the Pairing Matrix from a set of scores generated by ScoreMinMax.
155  
156 say $MinMaxElection->MinMaxPairingVotesTable( $scores );
157  
158 =cut
159  
160 ▶sub MinMaxPairingVotesTable ( $I, $scores ) {
Subroutine MinMaxPairingVotesTable: called CC=1 SCAR=0.0
Pod documented
161 8 my $table1 = $I->_pairmatrixtable1($scores);
162 8 my $table2 = $I->_pairmatrixtable2($scores);
163 8 return "\n$table1\n\n$table2\n";
164 }
165  
166 =pod
167  
168 =head2 MinMax
169  
170 Run and log the election with MinMax according to scoring $method: 'winning', 'margin', 'opposition'.
171  
172 my $result = $MinMaxElection->MinMax( $method );
173  
174 The returned value is a HashRef:
175  
176 { 'tie' => true or false value,
177 'winner' => will be false if tie is true --
178 otherwise the winning choice.
179 # tied is only present when tie is true.
180 'tied' => [ array ref of tied choices ],
181 }
182  
183 =cut
184  
185 ▶sub MinMax ( $self, $method ) {
Subroutine MinMax: called CC=7 SCAR=19.5
Pod documented
186 7 my $score = $self->ScoreMinMax($method);
187 7 my @active = $self->GetActiveList();
188 7 $self->logt( "MinMax $method Choices: ", join( ', ', @active ) );
189 7 $self->logv( $self->MinMaxPairingVotesTable($score) );
190 7 my $winner = '';
191 7 my @tied = ();
192 7 my $round = 0;
193 # round inited to 0. The 7th round is 6. round increments at
194 # end of the loop. this sets correct number of rounds.
195 7 my $roundlimit = scalar(@active) -1;
196 7 ▶ LOOPMINMAX: while ( $round < $roundlimit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($round < $roundlimit) 10 0 10
197 # start with $bestscore larger than any possible score
198 10 my $bestscore = $self->VotesCast() + 1;
199 10 my @hasbest = ();
200 10 for my $a (@active) {
201 49 my $this = $score->{$a}{'score'}[$round];
202 49 ▶ if ( $this == $bestscore ) { push @hasbest, $a }
Branch: 2 branches
BranchTrueFalseTotal
if ($this == $bestscore) { } 4 45 49
elsif ($this < $bestscore) { } 22 23 45
203 elsif ( $this < $bestscore ) {
204 22 $bestscore = $this;
205 22 @hasbest = ($a);
206 }
207 }
208 10 ▶ if ( scalar(@hasbest) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @hasbest == 1) 6 4 10
209 6 $winner = shift @hasbest;
210 6 $self->logt("Winner is $winner.");
211 6 return { 'tie' => 0, 'winner' => $winner };
212 }
213 # only choices that are tied continue to tie breaking.
214 4 @active = @hasbest;
215 # if this is the last round @tied must be set.
216 4 @tied = @hasbest;
217 4 ▶ if( $bestscore == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bestscore == 0) 1 3 4
218 1 $self->logt(
219 "Tie between @tied. Best Score is 0. No more Tiebreakers available." );
220 1 last LOOPMINMAX;
221 }
222 3 $round++;
223 3 $self->logt(
224 "Tie between @tied. Best Score is $bestscore. Going to Tiebreaker Round $round."
225 );
226 }
227 1 $self->logt( "Tied: " . join( ', ', @tied ) );
228 1 return { 'tie' => 1, 'tied' => \@tied, 'winner' => 0 };
229 }
230  
231 =pod
232  
233 =head2 Floor Rules
234  
235 It is recommended to use a low Floor or no Floor Rule at all.
236  
237 This method specifies that the scores from less worst pairings be used as the tie breaker, removing inconsequential choices can affect both the resolveability of the tie breaker and the outcome. Unlike IRV where the presence of inconsequential choices can be seen as a randomizing factor, and their bulk removal as improving the consistency of the method, there is no benefit gained by this method from a Floor Rule.
238  
239 =cut
240  
241 1 1;
242  
243 #FOOTER
244  
245 =pod
246  
247 BUG TRACKER
248  
249 L<https://github.com/brainbuz/Vote-Count/issues>
250  
251 AUTHOR
252  
253 John Karr (BRAINBUZ) brainbuz@cpan.org
254  
255 CONTRIBUTORS
256  
257 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
258  
259 LICENSE
260  
261 This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>.
262  
263 =cut
« Count/IRV.pm ↑ Summary Count/Redact.pm »