Count/Approval.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 33.3% 1 / 3 totaltotal 95.7% 91 / 95 CC 15 SCAR 11.7
CC
15
Cov
97%
_approval_range_do
5.0
Approval
5.0
_approval_rcv_do
4.0
CRAP 3.2
« Count/Floor.pm ↑ Summary Count/RankCount.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::Approval;
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 39 our $VERSION='2.02';
14  
15 =head1 NAME
16  
17 Vote::Count::Approval
18  
19 =head1 VERSION 2.02
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 active set
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 =head2 Method LastApprovalBallots
45  
46 Returns a hashref of the unweighted raw count from the last Approval operation.
47  
48 =head2 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 =head3 Cutoff (Range Ballots Only)
53  
54 When counting Approval on Range Ballots an optional cutoff value may be provided as a second argument to the Approval Method. When doing so the Active Set argument must be provided. The Cutoff value is a score below which the ballot is considered to not approve of the choice.
55  
56 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.
57  
58 For Ranked Ballots the equivalent would be to provide a 'Nuetral Preference', for voters to indicate that any choices ranked lower should not be considered approved. This is not presently implemented, but may be at some point in the future.
59  
60 =cut
61  
62 39 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 116 my %approval = ( map { $_ => 0 } keys( $active->%* ) );
70 116 my %lastappcount = ( map { $_ => 0 } keys( $active->%* ) );
71 116 for my $b ( keys %{$ballots} ) {
72 2016 my @votes = $ballots->{$b}->{'votes'}->@*;
73 2016 for my $v (@votes) {
74 5988 ▶ if ( defined $approval{$v} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $approval{$v}) 3918 2070 5988
75 3918 $approval{$v} += $ballots->{$b}{'count'} * $ballots->{$b}{'votevalue'} ;
76 3918 $lastappcount{$v} += $ballots->{$b}{'count'};
77 }
78 }
79 }
80 116 $I->LastApprovalBallots( \%lastappcount );
81 116 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 157 my %approval = ( map { $_ => 0 } keys( $active->%* ) );
86 157 for my $b ( @{$ballots} ) {
87 916 APPROVALKEYSC: for my $c ( keys $b->{'votes'}->%* ) {
88 # croak "key is $c " . "value is $b->{'votes'}{$c}";
89 3364 ▶ next APPROVALKEYSC if $b->{'votes'}{$c} < $cutoff;
Branch: 1 branch
BranchTrueFalseTotal
if $b->{"votes"}{$c} < $cutoff 35 3329 3364
90 3329 ▶ $approval{$c} += $b->{'count'} if defined $approval{$c};
Branch: 1 branch
BranchTrueFalseTotal
if defined $approval{$c} 3236 93 3329
91 }
92 }
93 157 return Vote::Count::RankCount->Rank( \%approval );
94 }
95  
96 270 ▶sub Approval ( $self, $active = undef, $cutoff = 0 ) {
Subroutine Approval: called CC=5 SCAR=16.1
Pod undocumented
97 270 my %BallotSet = $self->BallotSet()->%*;
98 270 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 180 90 270
99 270 ▶ if ( $BallotSet{'options'}{'range'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BallotSet{'options'}{'range'}) { } 157 113 270
100 157 return _approval_range_do( $active, $BallotSet{'ballots'},
101 $cutoff );
102 }
103 else {
104 113 $self->_approval_rcv_do( $active, $BallotSet{'ballots'} );
105 }
106 }
107  
108 3 ▶sub approval { Approval(@_) }
Subroutine approval: called CC=1 SCAR=0.0
Pod undocumented
109  
110 ▶sub _non_approval_rcv_do ( $I, $ballots ) {
Subroutine _non_approval_rcv_do: called CC=2 SCAR=6.9
111 3 my $active = $I->Active();
112 3 my %nonapproval = ( map { $_ => 0 } keys( $active->%* ) );
113 3 my %approval = %{$I->_approval_rcv_do ( $active, $ballots )->RawCount()};
114 3 my $activevotes = $I->VotesActive();
115 3 for my $A ( keys %nonapproval) {
116 24 $nonapproval{ $A } = $activevotes - $approval{ $A };
117 }
118 3 return Vote::Count::RankCount->Rank( \%nonapproval );
119 }
120  
121 # For each choice in the active set counts ballots
122 3 ▶sub NonApproval ( $I, $cutoff = 0 ) {
Subroutine NonApproval: called CC=3 SCAR=11.4
Pod documented
123 3 my %BallotSet = $I->BallotSet()->%*;
124 3 my $ballots = $BallotSet{'ballots'};
125 3 ▶ if ( $BallotSet{'options'}{'rcv'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BallotSet{'options'}{'rcv'}) { } 3 0 3
126 3 return _non_approval_rcv_do( $I, $ballots );
127 }
128 else {
129 0 die "NonApproval currently implemented for RCV ballots."
130 }
131 }
132  
133 39 1;
134  
135 #FOOTER
136  
137 =pod
138  
139 BUG TRACKER
140  
141 L<https://github.com/brainbuz/Vote-Count/issues>
142  
143 AUTHOR
144  
145 John Karr (BRAINBUZ) brainbuz@cpan.org
146  
147 CONTRIBUTORS
148  
149 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
150  
151 LICENSE
152  
153 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>.
154  
155 SUPPORT
156  
157 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
158  
159 =cut
160  
« Count/Floor.pm ↑ Summary Count/RankCount.pm »