File Coverage

blib/lib/Vote/Count/Score.pm
Criterion Covered Total %
statement 48 48 100.0
branch 8 8 100.0
condition 4 4 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 71 71 100.0


line stmt bran cond sub pod time code
1 39     39   20956 use strict;
  39         89  
  39         1235  
2 39     39   201 use warnings;
  39         77  
  39         956  
3 39     39   720 use 5.024;
  39         188  
4 39     39   199 use feature qw /postderef signatures/;
  39         73  
  39         3562  
5              
6             package Vote::Count::Score;
7              
8 39     39   251 use Moose::Role;
  39         93  
  39         291  
9              
10             # use Storable 3.15 qw(dclone);
11             # use Try::Tiny;
12              
13             our $VERSION='2.01';
14              
15             =head1 NAME
16              
17             Vote::Count::Score
18              
19             =head1 VERSION 2.01
20              
21             =cut
22              
23             # ABSTRACT: Provides Score Method for Range Ballots to Vote::Count objects
24              
25 39     39   193262 no warnings 'experimental';
  39         88  
  39         1719  
26 39     39   244 use Vote::Count::RankCount;
  39         75  
  39         19261  
27             # use Try::Tiny;
28             # use boolean;
29              
30             =pod
31              
32             =head1 Synopsis
33              
34             my $RangeElection = Vote::Count->new(
35             BallotSet => read_range_ballots('t/data/tennessee.range.json')
36             );
37             my $scored = $RangeElection->Score();
38              
39             =head1 Range Score Methods
40              
41             When Range (Cardinal) Ballots are used, it is simple and obvious to total the scores provided by the voters. This contrasts to the related Borda Method which assigns scores based on position on a Ranked Ballot.
42              
43             =head2 Score
44              
45             Returns a RankCount Object with the choices scored using the scores set by the voters, for Range Ballots.
46              
47             =cut
48              
49 11     11 1 1034 sub Score ( $self, $active = undef ) {
  11         18  
  11         15  
  11         13  
50 11         246 my $depth = $self->BallotSet()->{'depth'};
51             # my @Ballots = $self->BallotSet()->{'ballots'}->@*;
52 11 100       82 $active = $self->Active() unless defined $active;
53 11         32 my %scores = ( map { $_ => 0 } keys( $active->%* ) );
  56         94  
54 11         222 for my $ballot ( $self->BallotSet()->{'ballots'}->@* ) {
55 48         88 for my $choice ( keys %scores ) {
56 272 100       402 if ( defined $ballot->{'votes'}{$choice} ) {
57 122         172 $scores{$choice} += $ballot->{'count'} * $ballot->{'votes'}{$choice};
58             }
59             }
60             }
61 11         49 return Vote::Count::RankCount->Rank( \%scores );
62             }
63              
64             =head2 RangeBallotPair
65              
66             Used for pairings against Range Ballots. Used by Condorcet and STAR.
67              
68             where $I is a Vote::Count object created with a Range Ballot Set.
69              
70             my ( $countA, $countB ) = $I->RangeBallotPair( $A, $B) ;
71              
72             =cut
73              
74 147     147 1 1214 sub RangeBallotPair ( $self, $A, $B ) {
  147         163  
  147         167  
  147         173  
  147         144  
75 147         179 my $countA = 0;
76 147         148 my $countB = 0;
77 147         320 my $approval = $self->Approval();
78 147         3642 for my $ballot ( $self->BallotSet()->{'ballots'}->@* ) {
79 860   100     1557 my $prefA = $ballot->{'votes'}{$A} || 0;
80 860   100     1432 my $prefB = $ballot->{'votes'}{$B} || 0;
81 860 100       1328 if ( $prefA > $prefB ) { $countA += $ballot->{'count'} }
  241 100       317  
82 184         283 elsif ( $prefA < $prefB ) { $countB += $ballot->{'count'} }
83             else { }
84             }
85 147         931 return ( $countA, $countB );
86             }
87              
88             1;
89              
90             #FOOTER
91              
92             =pod
93              
94             BUG TRACKER
95              
96             L<https://github.com/brainbuz/Vote-Count/issues>
97              
98             AUTHOR
99              
100             John Karr (BRAINBUZ) brainbuz@cpan.org
101              
102             CONTRIBUTORS
103              
104             Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
105              
106             LICENSE
107              
108             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>.
109              
110             SUPPORT
111              
112             This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
113              
114             =cut
115