Count/Borda.pm

filter: Statementstmt 100% 81 / 81 Branchbran 100% 18 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.4% 17 / 18 Podpod 50.0% 1 / 2 totaltotal 98.3% 117 / 119 CC 17 SCAR 14.2
CC
17
Cov
100%
Borda
9.0
_dobordacount
5.0
_bordashrinkballot
4.0
CRAP 4.1
« Count/Score.pm ↑ Summary Count/Charge.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::Borda;
7  
8 39 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 39 our $VERSION='2.02';
11  
12 =head1 NAME
13  
14 Vote::Count::Borda
15  
16 =head1 VERSION 2.02
17  
18 =cut
19  
20 # ABSTRACT: Provides Borda Count to Vote::Count objects
21  
22 39 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 39 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 39 ▶use Vote::Count::RankCount;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 39 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 39 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 39 has 'bordaweight' => (
29 is => 'rw',
30 isa => 'CodeRef',
31 builder => '_buildbordaweight',
32 lazy => 1,
33 );
34  
35 39 has 'bordadepth' => (
36 is => 'rw',
37 isa => 'Int',
38 default => 0,
39 );
40  
41 # Many real world Borda implmentations use 1
42 # for unranked default. The way unranked choices are valued
43 # relies on NonApproval (from Approval), which does not
44 # support overriding the Active Set. Because this is a low
45 # priority function the limitation is acceptable.
46 39 has 'unrankdefault' => (
47 is => 'rw',
48 isa => 'Int',
49 default => 0,
50 );
51  
52 =pod
53  
54 =head1 Synopsis
55  
56 my $RCV = Vote::Count->new(
57 BallotSet => read_ballots('t/data/data1.txt'),
58 bordadepth => 5
59 );
60 my $bordacount = $RCV->Borda();
61  
62 =head1 Borda Count
63  
64 Scores Choices based on their position on the Ballot. The first choice candidate gets a score equal to the number of choices, each lower choice receives 1 less.
65  
66 The Borda Count is trying to Cardinally value Preferential choices, for this reason where the Borda Count is an appropriate method it is a better to use a Range Ballot instead of Preferential so that the voters may assign the Cardinal values.
67  
68 =head1 Variations on the Borda Count
69  
70 One major criticism of the count is that when there are many choices the difference between a first and second choice becomes negligible. A large number of alternative weightings have been used to address this.
71  
72 =head2 Borda Depth (bordadepth parameter)
73  
74 One of the simpler variations is to fix the depth, when the depth is set to a certain number the weighting is as if the ballot had that many choices, and choices ranked lower than the depth are scored 0. If there are eight choices and a depth of 3, a first choice is worth 3, a 3rd 1, and later choices are ignored
75  
76 =head2 Borda Weight (bordaweight parameter)
77  
78 Some of the popular alternate weighting systems include:
79  
80 =over
81  
82 =item * different scaling such as 1/x where x is the position of the choice (1 is worth 1, 3 is 1/3).
83  
84 =item * Another popular alternative is to score for one less than the number of choices -- in a five choice race first is worth 4 and last is worth 0.
85  
86 =back
87  
88 When Creating a VoteCount object a custom Borda weight may be set by passing a coderef for bordaweight. The coderef takes two arguments. The first argument is the position of the choice in question. The second argument is optional for passing the depth of the ballot to the coderef. Some popular options such inversion (where choice $c becomes $c/1 then inverted to 1/$c) don't need to know the depth. In such cases the coderef should just ignore the second argument.
89  
90 my $testweight = sub {
91 my $x = int shift @_;
92 return $x ? 1/$x : 0 ;
93 };
94  
95 my $VC2 = Vote::Count->new(
96 BallotSet => read_ballots('t/data/data2.txt'),
97 bordaweight => $testweight,
98 );
99  
100 =head2 unrankdefault
101  
102 Jean-Charles de Borda expected voters to rank all available choices. When they fail to do this the unranked choices need to be handled. The default in Vote::Count is to score unranked choices as 0. However, it is also common to score them as 1. Vote::Count permits using any Integer for this valuation.
103  
104 my $VC2 = Vote::Count->new(
105 BallotSet => read_ballots('t/data/data2.txt'),
106 unrankdefault => 1,
107 );
108  
109 =head1 Method Borda
110  
111 Returns a RankCount Object with the scores per the weighting rule, for Ranked Choice Ballots. Optional Parameter is a hashref defining an active set.
112  
113 =cut
114  
115 sub _buildbordaweight {
116 return sub {
117 217 ▶ my ( $x, $y ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
118 217 return ( $y + 1 - $x );
119 }
120 11 ▶}
Subroutine _buildbordaweight: called CC=1 SCAR=0.0
121  
122 # Private Method _bordashrinkballot( $BallotSet, $active )
123  
124 # Takes a BallotSet and active list and returns a
125 # BallotSet reduced to only the active choices. When
126 # choices are removed later choices are promoted.
127  
128 ▶sub _bordashrinkballot ( $BallotSet, $active ) {
Subroutine _bordashrinkballot: called CC=4 SCAR=13.9
129 25 my $newballots = {};
130 25 my %ballots = $BallotSet->{'ballots'}->%*;
131 25 for my $b ( keys %ballots ) {
132 174 my @newballot = ();
133 174 for my $item ( $ballots{$b}{'votes'}->@* ) {
134 408 ▶ try { if ( $active->{$item} ) { push @newballot, $item } }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($active->{$item}) 310 98 408
135 408 ▶ catch {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
136 }
137 174 ▶ if ( scalar(@newballot) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @newballot) 162 12 174
138 162 $newballots->{$b}{'votes'} = \@newballot;
139 $newballots->{$b}{'count'} =
140 162 $ballots{$b}->{'count'};
141 }
142 }
143 25 return $newballots;
144 }
145  
146 ▶sub _dobordacount ( $self, $BordaTable, $active ) {
Subroutine _dobordacount: called CC=5 SCAR=16.1
147 24 my $BordaCount = {};
148 24 my $weight = $self->bordaweight;
149 my $depth =
150 $self->bordadepth
151 ? $self->bordadepth
152 24 ▶ : scalar( keys %{$active} );
Branch: 1 branch
BranchTrueFalseTotal
$self->bordadepth ? : 1 23 24
153 24 for my $c ( keys $BordaTable->%* ) {
154 148 for my $rank ( keys $BordaTable->{$c}->%* ) {
155 235 ▶ $BordaCount->{$c} = 0 unless defined $BordaCount->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $BordaCount->{$c} 148 87 235
156 $BordaCount->{$c} +=
157 235 $BordaTable->{$c}{$rank} * $weight->( $rank, $depth );
158 }
159 }
160 24 return $BordaCount;
161 }
162  
163 24 ▶sub Borda ( $self, $active = undef ) {
Subroutine Borda: called CC=9 SCAR=22.0
Pod documented
164 24 my %BallotSet = $self->BallotSet()->%*;
165 24 my %ballots = ();
166 24 ▶ if ( defined $active ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $active) 21 3 24
167 21 ▶ die q/unrankdefault other than 0 is not compatible with overriding the
Branch: 1 branch
BranchTrueFalseTotal
if $self->unrankdefault 1 20 21
168 Active Set. To fix this use the SetActive method to update the active
169 set, then call this (Borda) method without passing an active set./
170 if $self->unrankdefault();
171 }
172 23 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 3 20 23
173 23 %ballots = %{ _bordashrinkballot( \%BallotSet, $active ) };
174 23 my %BordaTable = ( map { $_ => {} } keys( $active->%* ) );
175 BORDALOOPACTIVE:
176 23 for my $b ( keys %ballots ) {
177 155 my @votes = $ballots{$b}->{'votes'}->@* ;
178 155 my $bcount = $ballots{$b}->{'count'};
179 155 ▶ for ( my $i = 0 ; $i < scalar(@votes) ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @votes) 302 155 457
180 302 my $c = $votes[$i];
181 302 $BordaTable{$c}->{ $i + 1 } += $bcount;
182 }
183 }
184 23 my $BordaCounted = _dobordacount( $self, \%BordaTable, $active );
185 23 ▶ if ( $self->unrankdefault() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrankdefault) 2 21 23
186 2 my $unranked = $self->NonApproval()->RawCount();
187 2 for my $u ( keys $unranked->%* ) {
188 16 $BordaCounted->{$u} += $unranked->{$u} * $self->unrankdefault()
189 }
190 }
191 23 return Vote::Count::RankCount->Rank($BordaCounted);
192 }
193  
194 3 ▶sub borda { Borda(@_) }
Subroutine borda: called CC=1 SCAR=0.0
Pod undocumented
195  
196 39 1;
197  
198 #FOOTER
199  
200 =pod
201  
202 BUG TRACKER
203  
204 L<https://github.com/brainbuz/Vote-Count/issues>
205  
206 AUTHOR
207  
208 John Karr (BRAINBUZ) brainbuz@cpan.org
209  
210 CONTRIBUTORS
211  
212 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
213  
214 LICENSE
215  
216 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>.
217  
218 SUPPORT
219  
220 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
221  
222 =cut
223  
« Count/Score.pm ↑ Summary Count/Charge.pm »