Count/TopCount.pm

filter: Statementstmt 100% 103 / 103 Branchbran 96.1% 25 / 26 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 99.3% 154 / 155 CC 30 SCAR 17.7
CC
30
Cov
99%
_RangeTopCount
11.0
TopCountMajority
7.0
TopCount
6.0
CRAP 5.9
« Count/Matrix.pm ↑ Summary Count/IRV.pm »
1 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Vote::Count::TopCount;
8 25 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 25 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Vote::Count::RankCount;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Vote::Count::TextTableTiny 'generate_table';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 25 ▶use Math::BigRat try => 'GMP';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 25 ▶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 25 our $VERSION='1.10';
21  
22 =head1 NAME
23  
24 Vote::Count::TopCount
25  
26 =head1 VERSION 1.10
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 =head2 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 =head2 Method TopCount
37  
38 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.
39  
40 Returns a RankCount object containing the TopCount.
41  
42 TopCount supports both Ranked and Range Ballot Types.
43  
44 For RCV, TopCount respects weighting, 'votevalue' is defaulted to 1 by readballots. Integers or Floating point values may be used.
45  
46 =head3 LastTopCountUnWeighted
47  
48 Returns a hashref of the unweighted raw count from the last TopCount operation.
49  
50 =head3 Top Counting Range Ballots
51  
52 Since Range Ballots often allow ranking choices equally, those equal votes need to be split. 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 3 places. Three was arbitrarily chosen because it is reasonable for display, but precise enough as a truncation point to be unlikely to cause an error.
53  
54 It is recommended to install Math::BigInt::GMP to improve performance on the Rational Number math used for Top Count on Range Ballots.
55  
56 =cut
57  
58 25 has 'LastTopCountUnWeighted' => (
59 is => 'rw',
60 isa => 'HashRef',
61 required => 0,
62 );
63  
64 9 ▶sub _RangeTopCount ( $self, $active = undef ) {
Subroutine _RangeTopCount: called CC=11 SCAR=24.0
65 9 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 2 7 9
66 9 my %topcount = ( map { $_ => Math::BigRat->new(0) } keys( $active->%* ) );
67 TOPCOUNTRANGEBALLOTS:
68 9 for my $b ( $self->BallotSet()->{'ballots'}->@* ) {
69 44 my $vv = dclone $b->{'votes'};
70 44 my %votes = $vv->%*;
71 44 for my $v ( keys %votes ) {
72 156 ▶ delete $votes{$v} unless defined $active->{$v};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active->{$v} 38 118 156
73 }
74 44 ▶ next TOPCOUNTRANGEBALLOTS unless keys %votes;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %votes 3 41 44
75 41 my $max = max( values %votes );
76 41 my @top = ();
77 41 for my $c ( keys %votes ) {
78 118 ▶ if ( $votes{$c} == $max ) { push @top, $c }
Branch: 1 branch
BranchTrueFalseTotal
if ($votes{$c} == $max) 44 74 118
79 }
80 41 my $topvalue = Math::BigRat->new( $b->{'count'} / scalar(@top) );
81 41 for (@top) { $topcount{$_} += $topvalue }
82 }
83 9 for my $k ( keys %topcount ) {
84 50 $topcount{$k} = $topcount{$k}->as_float(3)->numify();
85 }
86 9 return Vote::Count::RankCount->Rank( \%topcount );
87 }
88  
89 307 ▶sub _RCVTopCount ( $self, $active = undef ) {
Subroutine _RCVTopCount: called CC=6 SCAR=17.9
90 307 my %ballotset = $self->BallotSet()->%*;
91 307 my %ballots = ( $ballotset{'ballots'}->%* );
92 307 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 17 290 307
93 307 my %topcount = ( map { $_ => 0 } keys( $active->%* ) );
94 307 my %lasttopcount = ( map { $_ => 0 } keys( $active->%* ) );
95 TOPCOUNTBALLOTS:
96 307 for my $b ( keys %ballots ) {
97 # reset topchoice so that if there is none the value will be false.
98 15154 $ballots{$b}{'topchoice'} = 'NONE';
99 15154 my @votes = $ballots{$b}->{'votes'}->@*;
100 15154 for my $v (@votes) {
101 18822 ▶ if ( defined $topcount{$v} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $topcount{$v}) 14428 4394 18822
102 14428 $topcount{$v} += $ballots{$b}{'count'} * $ballots{$b}{'votevalue'};
103 14428 $lasttopcount{$v} += $ballots{$b}{'count'};
104 14428 $ballots{$b}{'topchoice'} = $v;
105 14428 next TOPCOUNTBALLOTS;
106 }
107 }
108 }
109 307 $self->LastTopCountUnWeighted( \%lasttopcount );
110 307 return Vote::Count::RankCount->Rank( \%topcount );
111 }
112  
113 317 ▶sub TopCount ( $self, $active = undef ) {
Subroutine TopCount: called CC=6 SCAR=17.9
Pod documented
114 # An STV method was performing a TopCount to reset the topchoices
115 # after elimination. Decided it was better to check here.
116 317 ▶ unless( keys( $self->Active()->%* ) or defined( $active) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$self->Active} or defined $active) 1 316 317
Condition100%3/3
keys %{$self->Active} or defined $active
l!l&&r!l&&!r
31511
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
117 1 return { 'error' => 'no active choices'};
118 }
119 316 ▶ if ( $self->BallotSet()->{'options'}{'rcv'} == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->BallotSet->{'options'}{'rcv'} == 1) { } 307 9 316
elsif ($self->BallotSet->{'options'}{'range'} == 1) { } 9 0 9
120 307 return $self->_RCVTopCount($active);
121 }
122 elsif ( $self->BallotSet()->{'options'}{'range'} == 1 ) {
123 9 return $self->_RangeTopCount($active);
124 }
125 }
126  
127 =head3 TopChoice( $ballot_identifier )
128  
129 Returns the Top Choice on a ballot from the last TopCount operation
130  
131 =cut
132  
133 ▶sub TopChoice( $self, $ballot ) {
Subroutine TopChoice: called CC=1 SCAR=0.0
Pod documented
134 2 return $self->BallotSet()->{ballots}{$ballot}{topchoice};
135 }
136  
137 =head2 Method TopCountMajority
138  
139 $self->TopCountMajority( $round_topcount )
140 or
141 $self->TopCountMajority( undef, $active_choices )
142  
143 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.
144  
145 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.
146  
147 =cut
148  
149 276 ▶sub TopCountMajority ( $self, $topcount = undef, $active = undef ) {
Subroutine TopCountMajority: called CC=7 SCAR=19.5
Pod documented
150 276 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 274 2 276
151 276 ▶ unless ( defined $topcount ) { $topcount = $self->TopCount($active) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $topcount) 26 250 276
152 276 my $topc = $topcount->RawCount();
153 276 my $numvotes = $topcount->CountVotes();
154 276 my @choices = keys $topc->%*;
155 276 my $threshold = 1 + int( $numvotes / 2 );
156 276 for my $t (@choices) {
157 1295 ▶ if ( $topc->{$t} >= $threshold ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topc->{$t} >= $threshold) 52 1243 1295
158 return (
159 {
160 votes => $numvotes,
161 threshold => $threshold,
162 winner => $t,
163 52 winvotes => $topc->{$t}
164 }
165 );
166 }
167 }
168 # No winner
169 return (
170 {
171 224 votes => $numvotes,
172 threshold => $threshold,
173 }
174 );
175 }
176  
177 =head2 Method EvaluateTopCountMajority
178  
179 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.
180  
181 =cut
182  
183 266 ▶sub EvaluateTopCountMajority ( $self, $topcount = undef, $active = undef ) {
Subroutine EvaluateTopCountMajority: called CC=4 SCAR=13.9
Pod documented
184 266 my $majority = $self->TopCountMajority( $topcount, $active );
185 266 ▶ if ( $majority->{'winner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($majority->{"winner"}) 48 218 266
186 48 my $winner = $majority->{'winner'};
187 my $rows = [
188 [ 'Winner', $winner ],
189 [ 'Votes in Final Round', $majority->{'votes'} ],
190 [ 'Votes Needed for Majority', $majority->{'threshold'} ],
191 48 [ 'Winning Votes', $majority->{'winvotes'} ],
192 ];
193 48 $self->logt(
194 '---',
195 generate_table(
196 rows => $rows,
197 header_row => 0,
198 )
199 );
200 }
201 266 return $majority;
202 }
203  
204 25 1;
205 #FOOTER
206  
207 =pod
208  
209 BUG TRACKER
210  
211 L<https://github.com/brainbuz/Vote-Count/issues>
212  
213 AUTHOR
214  
215 John Karr (BRAINBUZ) brainbuz@cpan.org
216  
217 CONTRIBUTORS
218  
219 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
220  
221 LICENSE
222  
223 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>.
224  
225 =cut
« Count/Matrix.pm ↑ Summary Count/IRV.pm »