Count/IRV.pm

filter: Statementstmt 100% 81 / 81 Branchbran 95.8% 23 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 99.1% 119 / 120 CC 16 SCAR 15.2
CC
16
Cov
99%
_IRVDO
9.0
_ResolveTie
5.0
RunIRV
3.0
CRAP 4.6
« Count/Redact.pm ↑ Summary Count/Score.pm »
1 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 39 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Vote::Count::IRV;
7  
8 39 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 39 with 'Vote::Count::TopCount';
12 39 with 'Vote::Count::TieBreaker';
13  
14 39 ▶use Storable 3.15 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 39 our $VERSION='2.02';
17  
18 =head1 NAME
19  
20 Vote::Count::IRV
21  
22 =head1 VERSION 2.02
23  
24 =cut
25  
26 # ABSTRACT: IRV Method for Vote::Count
27  
28 39 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 39 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 #use Data::Dumper;
31 # use Data::Printer;
32  
33 ▶sub _ResolveTie ( $self, $active, $tiebreaker, @tiedchoices ) {
Subroutine _ResolveTie: called CC=5 SCAR=16.1
34 179 ▶ return @tiedchoices if @tiedchoices == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @tiedchoices == 1 135 44 179
35 my %high =
36 44 map { $_ => 1 } $self->TieBreaker( $tiebreaker, $active, @tiedchoices );
37 44 ▶ if ( defined $self->{'last_tiebreaker'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"last_tiebreaker"}) 5 39 44
38 5 $self->logt( $self->{'last_tiebreaker'}{'terse'} );
39 5 $self->logv( $self->{'last_tiebreaker'}{'verbose'} );
40 5 $self->{'last_tiebreaker'} = undef;
41 }
42 44 ▶ if ( @tiedchoices == scalar( keys %high ) ) { return @tiedchoices }
Branch: 1 branch
BranchTrueFalseTotal
if (@tiedchoices == scalar keys %high) 11 33 44
43 # tiebreaker returns winner, we want losers!
44 # use map to remove winner(s) from @tiedchoices.
45 # warning about sort interpreted as function fixed
46 my @low = sort map {
47 33 ▶ if ( $high{$_} ) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($high{$_}) { } 18 70 88
48 70 else { $_ }
49 } @tiedchoices;
50 33 return @low;
51 }
52  
53 48 ▶sub RunIRV ( $self, $active = undef, $tiebreaker = undef ) {
Subroutine RunIRV: called CC=3 SCAR=11.0
Pod documented
54 48 $self->_IRVDO( active => $active, tiebreaker => $tiebreaker );
55 }
56  
57 ▶sub RunBTRIRV ( $self, %args ) {
Subroutine RunBTRIRV: called CC=2 SCAR=6.9
Pod documented
58 4 ▶ my $ranking2 = $args{'ranking2'} ? $args{'ranking2'} : 'precedence';
Branch: 1 branch
BranchTrueFalseTotal
$args{'ranking2'} ? : 1 3 4
59 4 $self->_IRVDO( 'btr' => 1, ranking2 => $ranking2 );
60 }
61  
62 # RunIRV needed a new argument and was a long established method,
63 # so now it hands everything off to this private method that uses
64 # named arguments.
65 ▶sub _IRVDO ( $self, %args ) {
Subroutine _IRVDO: called CC=9 SCAR=22.0
66 52 local $" = ', ';
67 52 ▶ my $active = defined $args{'active'} ? dclone $args{'active'} : dclone $self->Active() ;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'active'} ? : 34 18 52
68 52 my $tiebreaker = do {
69 52 ▶ if ( defined $args{'tiebreaker'} ) { $args{'tiebreaker'} }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'tiebreaker'}) { } 39 13 52
elsif (defined $self->TieBreakMethod) { } 5 8 13
70 5 elsif ( defined $self->TieBreakMethod() ) { $self->TieBreakMethod() }
71 8 else { 'all' }
72 };
73 52 my $roundctr = 0;
74 52 my $maxround = scalar( keys %{$active} );
75 $self->logt( "Instant Runoff Voting",
76 52 'Choices: ', join( ', ', ( sort keys %{$active} ) ) );
77 # forever loop normally ends with return from $majority
78 # a tie should be detected and also generate a
79 # return from the else loop.
80 # if something goes wrong roundcountr/maxround
81 # will generate exception.
82 IRVLOOP:
83 52 until (0) {
84 251 $roundctr++;
85 251 ▶ die "IRVLOOP infinite stopped at $roundctr" if $roundctr > $maxround;
Branch: 1 branch
BranchTrueFalseTotal
if $roundctr > $maxround 0 251 251
86 251 my $round = $self->TopCount($active);
87 251 $self->logv( '---', "IRV Round $roundctr", $round->RankTable() );
88 251 my $majority = $self->EvaluateTopCountMajority($round);
89 251 ▶ if ( defined $majority->{'winner'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $majority->{'winner'}) { } 44 207 251
elsif ($args{'btr'}) { } 28 179 207
90 44 return $majority;
91 }
92 elsif ( $args{'btr'}) {
93 my $br = $self->BottomRunOff(
94 28 'active' => $active, 'ranking2' => $args{'ranking2'} );
95 28 $self->logv( $br->{'runoff'});
96 28 $self->logt( "Eliminating: ${\ $br->{'eliminate'} }" );
97 28 delete $active->{ $br->{'eliminate'} };
98 }
99 else { #--
100 179 my @bottom = $self->_ResolveTie( $active, $tiebreaker, $round->ArrayBottom()->@* );
101 179 ▶ if ( scalar(@bottom) == scalar( keys %{$active} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @bottom == scalar keys %{$active;}) 8 171 179
102 # if there is a tie at the end, the finalists should
103 # be both top and bottom and the active set.
104 8 $self->logt( "Tied: @bottom" );
105 8 return { tie => 1, tied => \@bottom, winner => 0 };
106 }
107 171 $self->logt( "Eliminating: @bottom" );
108 171 for my $b (@bottom) {
109 227 delete $active->{$b};
110 }
111 } #--
112 }
113 }
114  
115 39 1;
116  
117 =pod
118  
119 =head1 IRV
120  
121 Implements Instant Runoff Voting for Vote::Count.
122  
123 =head1 SYNOPSIS
124  
125 use Vote::Count::Method;
126 use Vote::Count::ReadBallots 'read_ballots';
127  
128 my $Election = Vote::Count::->new(
129 BallotSet => read_ballots('%path_to_my_ballots'),
130 TieBreakMethod => 'grandjunction');
131  
132 my $result = $Election->RunIRV();
133 my $winner = $result->{'winner'};
134  
135 say $Election->logv(); # Print the full Log.
136  
137 =head1 Method Summary
138  
139 Instant Runoff Voting Looks for a Majority Winner. If one isn't present the choice with the lowest Top Count is removed.
140  
141 Instant Runoff Voting is easy to count by hand and meets the Later Harm and Condorcet Loser Criteria. It, unfortunately, fails a large number of consistency criteria; the order of candidate dropping matters and small changes to the votes of non-winning choices that result in changes to the dropping order can change the outcome.
142  
143 Instant Runoff Voting is also known as Alternative Vote and as the Hare Method.
144  
145 =head2 Tie Handling
146  
147 There is no standard accepted method for IRV tie resolution, Eliminate All is a common one and the default.
148  
149 Returns a tie when all of the remaining choices are in a tie.
150  
151 An optional value to RunIRV is to specify tiebreaker, see L<Vote::Count::TieBreaker>.
152  
153 =head2 RunIRV
154  
155 $Election->RunIRV();
156 $Election->RunIRV( $active )
157 $Election->RunIRV( $active, 'approval' )
158  
159 Runs IRV on the provided Ballot Set. Takes an optional parameter of $active which is a hashref for which the keys are the currently active choices.
160  
161 Returns results in a hashref which will be the results of Vote::Count::TopCount->EvaluateTopCountMajority, if there is no winner hash will instead be:
162  
163 tie => [true or false],
164 tied => [ array of tied choices ],
165 winner => a false value
166  
167 Supports the Vote::Count logt, logv, and logd methods for providing details of the method.
168  
169 =head1 Bottom Two Runoff IRV
170  
171 B<Bottom Two Runoff IRV> is the simplest modification to IRV which meets the Condorcet Winner Criteria. Instead of eliminating the low choice, the lowest two choices enter a virtual runoff, eliminating the loser. This is the easiest possible Hand Count Condorcet method, there will always be fewer pairings than choices. As a Condorcet method it fails Later No Harm.
172  
173 BTR IRV will only eliminate a member of the Smith Set when both members of the runoff are in it, so it can never eliminate the final member of the Smith Set, and is thus Smith compliant.
174  
175 =head2 RunBTRIRV
176  
177 my $result = $Election->RunBTRIRV();
178 my $result = $Election->RunBTRIRV( 'ranking2' => 'Approval');
179  
180 Choices are ordered by TopCount, ties for position are decided by Precedence. It is mandatory that either the TieBreakMethod is Precedence or TieBreakerFallBackPrecedence is True. The optional ranking2 option will use a second method before Precedence, see UnTieList in L<Vote::Count::TieBreaker|Vote::Count::TieBreaker/UnTieList>.
181  
182 The returned values and logging are the same as for RunIRV.
183  
184 =cut
185  
186 #FOOTER
187  
188 =pod
189  
190 BUG TRACKER
191  
192 L<https://github.com/brainbuz/Vote-Count/issues>
193  
194 AUTHOR
195  
196 John Karr (BRAINBUZ) brainbuz@cpan.org
197  
198 CONTRIBUTORS
199  
200 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
201  
202 LICENSE
203  
204 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>.
205  
206 SUPPORT
207  
208 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
209  
210 =cut
211  
« Count/Redact.pm ↑ Summary Count/Score.pm »