Count/Approval.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 95.6% 88 / 92 CC 15 SCAR 12.0
CC
15
Cov
97%
_approval_range_do
5.0
Approval
5.0
_approval_rcv_do
4.0
CRAP 3.3
« Count/Floor.pm ↑ Summary Count/RankCount.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::Approval;
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 25 our $VERSION='1.10';
14  
15 =head1 NAME
16  
17 Vote::Count::Approval
18  
19 =head1 VERSION 1.10
20  
21 =cut
22  
23 # ABSTRACT: Approval for Vote::Count. Toolkit for vote counting.
24  
25 =head1 Definition of Approval
26  
27 In Approval Voting, voters indicate which Choices they approve of indicating no preference. Approval can be infered from a Ranked Choice Ballot, by treating each ranked Choice as Approved.
28  
29 =head1 Method Approval
30  
31 Returns a RankCount object for the current Active Set taking an optional argument of an active list as a HashRef.
32  
33 my $Approval = $Election->Approval();
34 say $Approval->RankTable;
35  
36 # to specify the activeset
37 my $Approval = $Election->Approval( $activeset );
38  
39 # to specify a cutoff on Range Ballots
40 my $Approval = $Election->Approval( $activeset, $cutoff );
41  
42 For RCV, Approval respects weighting, 'votevalue' is defaulted to 1 by readballots. Integers or Floating point values may be used.
43  
44 =head3 LastApprovalBallots
45  
46 Returns a hashref of the unweighted raw count from the last Approval operation.
47  
48 =head1 Method NonApproval
49  
50 The opposite of Approval. Returns a RankCount object for the current Active Set of the non-exhausted ballots not supporting a choice. It does not have the option to provide an Active Set. Only available for Ranked Ballots.
51  
52 =head2 Cut Off (Range Ballots Only)
53  
54 When counting Approval on Range Ballots it is appropriate to set a threshold below which a choice is not considered to be supported by the voter, but indicated to represent a preference to even lower or unranked choices.
55  
56 A good value is half of the maximum possible score, however, the default action must be to treat all choices with a score as approved. With a Range of 0-10 a cutoff of 5 would be recommended, choices scored 4 or lower would not be counted for approval. If cutoff isn't provided it defaults to 0 producing the desired default behaviour.
57  
58 For Ranked Ballots the cutoff is ignored. If a cutoff is desired for Ranked Ballots a Neutral Preference Option should be included on the Ballot to indicate when subequent choices should not be considered Approved. This is not currently available in Vote::Count.
59  
60 =cut
61  
62 25 has 'LastApprovalBallots' => (
63 is => 'rw',
64 isa => 'HashRef',
65 required => 0,
66 );
67  
68 ▶sub _approval_rcv_do ( $I, $active, $ballots ) {
Subroutine _approval_rcv_do: called CC=4 SCAR=13.9
69 44 my %approval = ( map { $_ => 0 } keys( $active->%* ) );
70 44 my %lastappcount = ( map { $_ => 0 } keys( $active->%* ) );
71 44 for my $b ( keys %{$ballots} ) {
72 508 my @votes = $ballots->{$b}->{'votes'}->@*;
73 508 for my $v (@votes) {
74 1359 ▶ if ( defined $approval{$v} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $approval{$v}) 1022 337 1359
75 1022 $approval{$v} += $ballots->{$b}{'count'} * $ballots->{$b}{'votevalue'} ;
76 1022 $lastappcount{$v} += $ballots->{$b}{'count'};
77 }
78 }
79 }
80 44 $I->LastApprovalBallots( \%lastappcount );
81 44 return Vote::Count::RankCount->Rank( \%approval );
82 }
83  
84 ▶sub _approval_range_do ( $active, $ballots, $cutoff ) {
Subroutine _approval_range_do: called CC=5 SCAR=16.1
85 151 my %approval = ( map { $_ => 0 } keys( $active->%* ) );
86 151 for my $b ( @{$ballots} ) {
87 892 APPROVALKEYSC: for my $c ( keys $b->{'votes'}->%* ) {
88 # croak "key is $c " . "value is $b->{'votes'}{$c}";
89 3268 ▶ next APPROVALKEYSC if $b->{'votes'}{$c} < $cutoff;
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"votes"}{$c} < $cutoff 35 3233 3268
90 3233 ▶ $approval{$c} += $b->{'count'} if defined $approval{$c};
Branch: 1 branch
BranchTrueFalseTotal
if defined $approval{$c} 3140 93 3233
91 }
92 }
93 151 return Vote::Count::RankCount->Rank( \%approval );
94 }
95  
96 192 ▶sub Approval ( $self, $active = undef, $cutoff = 0 ) {
Subroutine Approval: called CC=5 SCAR=16.1
Pod undocumented
97 192 my %BallotSet = $self->BallotSet()->%*;
98 192 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 167 25 192
99 192 ▶ if ( $BallotSet{'options'}{'range'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BallotSet{'options'}{'range'}) { } 151 41 192
100 151 return _approval_range_do( $active, $BallotSet{'ballots'},
101 $cutoff );
102 }
103 else {
104 41 $self->_approval_rcv_do( $active, $BallotSet{'ballots'} );
105 }
106 }
107  
108 ▶sub _non_approval_rcv_do ( $I, $ballots ) {
Subroutine _non_approval_rcv_do: called CC=2 SCAR=6.9
109 3 my $active = $I->Active();
110 3 my %nonapproval = ( map { $_ => 0 } keys( $active->%* ) );
111 3 my %approval = %{$I->_approval_rcv_do ( $active, $ballots )->RawCount()};
112 3 my $activevotes = $I->VotesActive();
113 3 for my $A ( keys %nonapproval) {
114 24 $nonapproval{ $A } = $activevotes - $approval{ $A };
115 }
116 3 return Vote::Count::RankCount->Rank( \%nonapproval );
117 }
118  
119 # For each choice in the active set counts ballots
120 3 ▶sub NonApproval ( $I, $cutoff = 0 ) {
Subroutine NonApproval: called CC=3 SCAR=11.4
Pod undocumented
121 3 my %BallotSet = $I->BallotSet()->%*;
122 3 my $ballots = $BallotSet{'ballots'};
123 3 ▶ if ( $BallotSet{'options'}{'rcv'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BallotSet{'options'}{'rcv'}) { } 3 0 3
124 3 return _non_approval_rcv_do( $I, $ballots );
125 }
126 else {
127 0 die "NonApproval currently implemented for RCV ballots."
128 }
129 }
130  
131 25 1;
132  
133 #FOOTER
134  
135 =pod
136  
137 BUG TRACKER
138  
139 L<https://github.com/brainbuz/Vote-Count/issues>
140  
141 AUTHOR
142  
143 John Karr (BRAINBUZ) brainbuz@cpan.org
144  
145 CONTRIBUTORS
146  
147 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
148  
149 LICENSE
150  
151 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>.
152  
153 =cut
« Count/Floor.pm ↑ Summary Count/RankCount.pm »