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   19048 use strict;
  39         126  
  39         1231  
2 39     39   209 use warnings;
  39         101  
  39         944  
3 39     39   650 use 5.024;
  39         138  
4 39     39   210 use feature qw /postderef signatures/;
  39         84  
  39         3509  
5              
6              
7             use Moose::Role;
8 39     39   339  
  39         94  
  39         266  
9             # use Storable 3.15 qw(dclone);
10             # use Try::Tiny;
11              
12             our $VERSION='2.02';
13              
14             =head1 NAME
15              
16             Vote::Count::Score
17              
18             =head1 VERSION 2.02
19              
20             =cut
21              
22             # ABSTRACT: Provides Score Method for Range Ballots to Vote::Count objects
23              
24             no warnings 'experimental';
25 39     39   206383 use Vote::Count::RankCount;
  39         97  
  39         1587  
26 39     39   253 # use Try::Tiny;
  39         81  
  39         20371  
27             # use boolean;
28              
29             =pod
30              
31             =head1 Synopsis
32              
33             my $RangeElection = Vote::Count->new(
34             BallotSet => read_range_ballots('t/data/tennessee.range.json')
35             );
36             my $scored = $RangeElection->Score();
37              
38             =head1 Range Score Methods
39              
40             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.
41              
42             =head2 Score
43              
44             Returns a RankCount Object with the choices scored using the scores set by the voters, for Range Ballots.
45              
46             =cut
47              
48             my $depth = $self->BallotSet()->{'depth'};
49 11     11 1 1332 # my @Ballots = $self->BallotSet()->{'ballots'}->@*;
  11         15  
  11         18  
  11         14  
50 11         297 $active = $self->Active() unless defined $active;
51             my %scores = ( map { $_ => 0 } keys( $active->%* ) );
52 11 100       95 for my $ballot ( $self->BallotSet()->{'ballots'}->@* ) {
53 11         42 for my $choice ( keys %scores ) {
  56         111  
54 11         286 if ( defined $ballot->{'votes'}{$choice} ) {
55 48         113 $scores{$choice} += $ballot->{'count'} * $ballot->{'votes'}{$choice};
56 272 100       493 }
57 122         226 }
58             }
59             return Vote::Count::RankCount->Rank( \%scores );
60             }
61 11         113  
62             =head2 RangeBallotPair
63              
64             Used for pairings against Range Ballots. Used by Condorcet and STAR.
65              
66             where $I is a Vote::Count object created with a Range Ballot Set.
67              
68             my ( $countA, $countB ) = $I->RangeBallotPair( $A, $B) ;
69              
70             =cut
71              
72             my $countA = 0;
73             my $countB = 0;
74 147     147 1 1130 my $approval = $self->Approval();
  147         173  
  147         170  
  147         172  
  147         143  
75 147         165 for my $ballot ( $self->BallotSet()->{'ballots'}->@* ) {
76 147         159 my $prefA = $ballot->{'votes'}{$A} || 0;
77 147         284 my $prefB = $ballot->{'votes'}{$B} || 0;
78 147         3602 if ( $prefA > $prefB ) { $countA += $ballot->{'count'} }
79 860   100     1634 elsif ( $prefA < $prefB ) { $countB += $ballot->{'count'} }
80 860   100     1502 else { }
81 860 100       1319 }
  214 100       292  
82 211         287 return ( $countA, $countB );
83             }
84              
85 147         891 1;
86              
87             #FOOTER
88              
89             =pod
90              
91             BUG TRACKER
92              
93             L<https://github.com/brainbuz/Vote-Count/issues>
94              
95             AUTHOR
96              
97             John Karr (BRAINBUZ) brainbuz@cpan.org
98              
99             CONTRIBUTORS
100              
101             Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
102              
103             LICENSE
104              
105             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>.
106              
107             SUPPORT
108              
109             This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
110              
111             =cut
112