Count/TopCount.pm

filter: Statementstmt 100% 104 / 104 Branchbran 96.1% 25 / 26 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 18 / 18 Podpod 80.0% 4 / 5 totaltotal 98.7% 156 / 158 CC 30 SCAR 17.5
CC
30
Cov
99%
_RangeTopCount
11.0
TopCountMajority
7.0
TopCount
6.0
CRAP 5.8
« Count/Matrix.pm ↑ Summary Count/ReadBallots.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  
5 39 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Vote::Count::TopCount;
8 39 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 39 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use Vote::Count::RankCount;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 ▶use Vote::Count::TextTableTiny 'generate_table';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 39 ▶use Math::BigRat try => 'GMP';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # ABSTRACT: TopCount and related methods for Vote::Count. Toolkit for vote counting.
19  
20 39 our $VERSION='2.02';
21  
22 =head1 NAME
23  
24 Vote::Count::TopCount
25  
26 =head1 VERSION 2.02
27  
28 =head1 Synopsis
29  
30 This Role is consumed by Vote::Count it provides TopCount and related Methods to Vote::Count objects.
31  
32 =head1 Definition of Top Count
33  
34 Top Count is tabulation of the Top Choice vote on each ballot. As choices are eliminated the first choice on some ballots will be removed, the next highest remaining choice becomes the Top Choice for that ballot. When all choices on a ballot are eliminated it becomes exhausted and is no longer counted.
35  
36 =head1 TopCount Methods
37  
38 =head2 TopCount
39  
40 Takes a hashref of active choices as an optional parameter, if one is not provided it uses the internal active list accessible via the ->Active() method, which itself defaults to the BallotSet's Choices list.
41  
42 Returns a L<RankCount|Vote::Count::RankCount> object containing the TopCount.
43  
44 TopCount supports both Ranked and Range Ballot Types.
45  
46 For RCV, TopCount respects weighting, 'votevalue' is defaulted to 1 by readballots. Integers or Floating point values may be used.
47  
48 =head2 LastTopCountUnWeighted
49  
50 Returns a hashref of the unweighted raw count from the last TopCount operation.
51  
52 =cut
53  
54 39 has 'LastTopCountUnWeighted' => (
55 is => 'rw',
56 isa => 'HashRef',
57 required => 0,
58 );
59  
60 14 ▶sub _RangeTopCount ( $self, $active = undef ) {
Subroutine _RangeTopCount: called CC=11 SCAR=24.0
61 14 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 2 12 14
62 14 my %topcount = ( map { $_ => Math::BigRat->new(0) } keys( $active->%* ) );
63 TOPCOUNTRANGEBALLOTS:
64 14 for my $b ( $self->BallotSet()->{'ballots'}->@* ) {
65 64 my $vv = dclone $b->{'votes'};
66 64 my %votes = $vv->%*;
67 64 for my $v ( keys %votes ) {
68 236 ▶ delete $votes{$v} unless defined $active->{$v};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active->{$v} 54 182 236
69 }
70 64 ▶ next TOPCOUNTRANGEBALLOTS unless keys %votes;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %votes 3 61 64
71 61 my $max = max( values %votes );
72 61 my @top = ();
73 61 for my $c ( keys %votes ) {
74 182 ▶ if ( $votes{$c} == $max ) { push @top, $c }
Branch: 1 branch
BranchTrueFalseTotal
if ($votes{$c} == $max) 64 118 182
75 }
76 61 my $topvalue = Math::BigRat->new( $b->{'count'} / scalar(@top) );
77 61 for (@top) { $topcount{$_} += $topvalue }
78 }
79 14 for my $k ( keys %topcount ) {
80 66 $topcount{$k} = $topcount{$k}->as_float(5)->numify();
81 }
82 14 return Vote::Count::RankCount->Rank( \%topcount );
83 }
84  
85 429 ▶sub _RCVTopCount ( $self, $active = undef ) {
Subroutine _RCVTopCount: called CC=6 SCAR=17.9
86 429 my %ballotset = $self->BallotSet()->%*;
87 429 my %ballots = ( $ballotset{'ballots'}->%* );
88 429 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 69 360 429
89 429 my %topcount = ( map { $_ => 0 } keys( $active->%* ) );
90 429 my %lasttopcount = ( map { $_ => 0 } keys( $active->%* ) );
91 TOPCOUNTBALLOTS:
92 429 for my $b ( keys %ballots ) {
93 # reset topchoice so that if there is none the value will be false.
94 45426 $ballots{$b}{'topchoice'} = 'NONE';
95 45426 my @votes = $ballots{$b}->{'votes'}->@*;
96 45426 for my $v (@votes) {
97 65582 ▶ if ( defined $topcount{$v} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $topcount{$v}) 43839 21743 65582
98 43839 $topcount{$v} += $ballots{$b}{'count'} * $ballots{$b}{'votevalue'};
99 43839 $lasttopcount{$v} += $ballots{$b}{'count'};
100 43839 $ballots{$b}{'topchoice'} = $v;
101 43839 next TOPCOUNTBALLOTS;
102 }
103 }
104 }
105 429 $self->LastTopCountUnWeighted( \%lasttopcount );
106 429 return Vote::Count::RankCount->Rank( \%topcount );
107 }
108  
109 444 ▶sub TopCount ( $self, $active = undef ) {
Subroutine TopCount: called CC=6 SCAR=17.9
Pod documented
110 # An STV method was performing a TopCount to reset the topchoices
111 # after elimination. Decided it was better to check here.
112 444 ▶ unless( keys( $self->Active()->%* ) or defined( $active) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$self->Active} or defined $active) 1 443 444
Condition100%3/3
keys %{$self->Active} or defined $active
l!l&&r!l&&!r
44211
MC/DC100%2/2
keys %{$self->Active} or defined $active
keys %{$self->Active} defined $active
Truth table100%3/3
A or B
A= keys %{$self->Active}B= defined $active
ABresult
1X1
011
000
113 1 return { 'error' => 'no active choices'};
114 }
115 443 ▶ if ( $self->BallotSet()->{'options'}{'rcv'} == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->BallotSet->{'options'}{'rcv'} == 1) { } 429 14 443
elsif ($self->BallotSet->{'options'}{'range'} == 1) { } 14 0 14
116 429 return $self->_RCVTopCount($active);
117 }
118 elsif ( $self->BallotSet()->{'options'}{'range'} == 1 ) {
119 14 return $self->_RangeTopCount($active);
120 }
121 }
122  
123 4 ▶sub topcount { TopCount(@_) }
Subroutine topcount: called CC=1 SCAR=0.0
Pod undocumented
124  
125 =head2 TopChoice
126  
127 Returns the Top Choice on a specific ballot from the last TopCount operation. The ballot is identified by it's key in the ballotset.
128  
129 $Election->TopCount();
130 my $top = $Election->TopChoice( 'FOO:BAZ:BAR:ZAB');
131  
132 =cut
133  
134 ▶sub TopChoice( $self, $ballot ) {
Subroutine TopChoice: called CC=1 SCAR=0.0
Pod documented
135 11822 return $self->BallotSet()->{ballots}{$ballot}{topchoice};
136 }
137  
138 =head2 TopCountMajority
139  
140 $self->TopCountMajority( $round_topcount )
141 or
142 $self->TopCountMajority( undef, $active_choices )
143  
144 Will find the majority winner from the results of a topcount, or alternately may be given undef and a hashref of active choices and will topcount the ballotset for just those choices and then find the majority winner.
145  
146 Returns a hashref of results. It will always include the votes in the round and the threshold for majority. If there is a winner it will also include the winner and winvotes.
147  
148 =cut
149  
150 308 ▶sub TopCountMajority ( $self, $topcount = undef, $active = undef ) {
Subroutine TopCountMajority: called CC=7 SCAR=19.5
Pod documented
151 308 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 306 2 308
152 308 ▶ unless ( defined $topcount ) { $topcount = $self->TopCount($active) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $topcount) 26 282 308
153 308 my $topc = $topcount->RawCount();
154 308 my $numvotes = $topcount->CountVotes();
155 308 my @choices = keys $topc->%*;
156 308 my $threshold = 1 + int( $numvotes / 2 );
157 308 for my $t (@choices) {
158 1493 ▶ if ( $topc->{$t} >= $threshold ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topc->{$t} >= $threshold) 56 1437 1493
159 return (
160 {
161 votes => $numvotes,
162 threshold => $threshold,
163 winner => $t,
164 56 winvotes => $topc->{$t}
165 }
166 );
167 }
168 }
169 # No winner
170 return (
171 {
172 252 votes => $numvotes,
173 threshold => $threshold,
174 }
175 );
176 }
177  
178 =head2 EvaluateTopCountMajority
179  
180 This method wraps TopCountMajority adding logging, the logging of which would be a lot of boiler plate in round oriented methods. It takes the same parameters and returns the same hashref.
181  
182 =cut
183  
184 298 ▶sub EvaluateTopCountMajority ( $self, $topcount = undef, $active = undef ) {
Subroutine EvaluateTopCountMajority: called CC=4 SCAR=13.9
Pod documented
185 298 my $majority = $self->TopCountMajority( $topcount, $active );
186 298 ▶ if ( $majority->{'winner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($majority->{"winner"}) 52 246 298
187 52 my $winner = $majority->{'winner'};
188 my $rows = [
189 [ 'Winner', $winner ],
190 [ 'Votes in Final Round', $majority->{'votes'} ],
191 [ 'Votes Needed for Majority', $majority->{'threshold'} ],
192 52 [ 'Winning Votes', $majority->{'winvotes'} ],
193 ];
194 52 $self->logt(
195 '---',
196 generate_table(
197 rows => $rows,
198 header_row => 0,
199 )
200 );
201 }
202 298 return $majority;
203 }
204  
205 =pod
206  
207 =head1 Top Counting Range Ballots
208  
209 Since Range Ballots often allow ranking choices equally, those equal votes need to be split. The other option is to have a rule that assigns an order among the tied choices in a conversion to Ranked Ballots. To prevent Rounding errors in the addition on large sets the fractions are added as Rational Numbers. The totals are converted to floating point numbers with a precision of 5 places for display.
210  
211 It is recommended to install Math::BigInt::GMP to improve performance on the Rational Number math used for Top Count on Range Ballots.
212  
213 =cut
214  
215 39 1;
216  
217 #FOOTER
218  
219 =pod
220  
221 BUG TRACKER
222  
223 L<https://github.com/brainbuz/Vote-Count/issues>
224  
225 AUTHOR
226  
227 John Karr (BRAINBUZ) brainbuz@cpan.org
228  
229 CONTRIBUTORS
230  
231 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
232  
233 LICENSE
234  
235 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>.
236  
237 SUPPORT
238  
239 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
240  
241 =cut
242  
« Count/Matrix.pm ↑ Summary Count/ReadBallots.pm »