Count/Borda.pm

filter: Statementstmt 100% 80 / 80 Branchbran 100% 18 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.1% 16 / 17 Podpod 100% 1 / 1 totaltotal 99.1% 115 / 116 CC 17 SCAR 14.4
CC
17
Cov
100%
Borda
9.0
_dobordacount
5.0
_bordashrinkballot
4.0
CRAP 4.2
« Count/Redact.pm ↑ Summary Count/Score.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 25 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Vote::Count::Borda;
7  
8 25 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 25 our $VERSION='1.10';
11  
12 =head1 NAME
13  
14 Vote::Count::Borda
15  
16 =head1 VERSION 1.10
17  
18 =cut
19  
20 # ABSTRACT: Provides Borda Count to Vote::Count objects
21  
22 25 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 25 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 25 ▶use Vote::Count::RankCount;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 25 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 25 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 25 has 'bordaweight' => (
29 is => 'rw',
30 isa => 'CodeRef',
31 builder => '_buildbordaweight',
32 lazy => 1,
33 );
34  
35 25 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 25 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 201 ▶ my ( $x, $y ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
118 201 return ( $y + 1 - $x );
119 }
120 10 ▶}
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 24 my $newballots = {};
130 24 my %ballots = $BallotSet->{'ballots'}->%*;
131 24 for my $b ( keys %ballots ) {
132 164 my @newballot = ();
133 164 for my $item ( $ballots{$b}{'votes'}->@* ) {
134 388 ▶ try { if ( $active->{$item} ) { push @newballot, $item } }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($active->{$item}) 290 98 388
135 388 ▶ catch {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
136 }
137 164 ▶ if ( scalar(@newballot) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @newballot) 152 12 164
138 152 $newballots->{$b}{'votes'} = \@newballot;
139 $newballots->{$b}{'count'} =
140 152 $ballots{$b}->{'count'};
141 }
142 }
143 24 return $newballots;
144 }
145  
146 ▶sub _dobordacount ( $self, $BordaTable, $active ) {
Subroutine _dobordacount: called CC=5 SCAR=16.1
147 23 my $BordaCount = {};
148 23 my $weight = $self->bordaweight;
149 my $depth =
150 $self->bordadepth
151 ? $self->bordadepth
152 23 ▶ : scalar( keys %{$active} );
Branch: 1 branch
BranchTrueFalseTotal
$self->bordadepth ? : 1 22 23
153 23 for my $c ( keys $BordaTable->%* ) {
154 136 for my $rank ( keys $BordaTable->{$c}->%* ) {
155 219 ▶ $BordaCount->{$c} = 0 unless defined $BordaCount->{$c};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $BordaCount->{$c} 136 83 219
156 $BordaCount->{$c} +=
157 219 $BordaTable->{$c}{$rank} * $weight->( $rank, $depth );
158 }
159 }
160 23 return $BordaCount;
161 }
162  
163 23 ▶sub Borda ( $self, $active = undef ) {
Subroutine Borda: called CC=9 SCAR=22.0
Pod documented
164 23 my %BallotSet = $self->BallotSet()->%*;
165 23 my %ballots = ();
166 23 ▶ if ( defined $active ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $active) 19 4 23
167 19 ▶ die q/unrankdefault other than 0 is not compatible with overriding the
Branch: 1 branch
BranchTrueFalseTotal
if $self->unrankdefault 1 18 19
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 22 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 4 18 22
173 22 %ballots = %{ _bordashrinkballot( \%BallotSet, $active ) };
174 22 my %BordaTable = ( map { $_ => {} } keys( $active->%* ) );
175 BORDALOOPACTIVE:
176 22 for my $b ( keys %ballots ) {
177 145 my @votes = $ballots{$b}->{'votes'}->@* ;
178 145 my $bcount = $ballots{$b}->{'count'};
179 145 ▶ for ( my $i = 0 ; $i < scalar(@votes) ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @votes) 282 145 427
180 282 my $c = $votes[$i];
181 282 $BordaTable{$c}->{ $i + 1 } += $bcount;
182 }
183 }
184 22 my $BordaCounted = _dobordacount( $self, \%BordaTable, $active );
185 22 ▶ if ( $self->unrankdefault() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrankdefault) 2 20 22
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 22 return Vote::Count::RankCount->Rank($BordaCounted);
192 }
193  
194 25 1;
195  
196 #FOOTER
197  
198 =pod
199  
200 BUG TRACKER
201  
202 L<https://github.com/brainbuz/Vote-Count/issues>
203  
204 AUTHOR
205  
206 John Karr (BRAINBUZ) brainbuz@cpan.org
207  
208 CONTRIBUTORS
209  
210 Copyright 2020,2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
211  
212 LICENSE
213  
214 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>.
215  
216 =cut
« Count/Redact.pm ↑ Summary Count/Score.pm »