Count/Method/STAR.pm

filter: Statementstmt 100% 63 / 63 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 100% 88 / 88 CC 8 SCAR 10.6
CC
8
Cov
100%
STAR
6.0
_best_two
3.0
BEGIN
1.0
CRAP 2.9
« Count/RankCount.pm ↑ Summary Count/Start.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Vote::Count::Method::STAR;
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 extends 'Vote::Count';
10  
11 1 our $VERSION='1.10';
12  
13 =head1 NAME
14  
15 Vote::Count::Method::STAR
16  
17 =head1 VERSION 1.10
18  
19 =cut
20  
21 # ABSTRACT: STAR Voting.
22  
23 =pod
24  
25 =head1 SYNOPSIS
26  
27 use Vote::Count::Method::STAR;
28  
29 my $tennessee = Vote::Count::Method::STAR->new(
30 BallotSet => read_range_ballots('t/data/tennessee.range.json'), );
31 my $winner = $tennessee->STAR() ;
32  
33 say $Election->logv();
34  
35 =head1 Description
36  
37 Implements the STAR method for resolving Range Ballots.
38  
39 =head1 Method Common Name: STAR (Score Then Automatic Runoff)
40  
41 Scores the Range Ballots, then holds a runoff between the two highest scored choices. The method is named for the acronym for Score Then Automatic Runoff.
42  
43 =head2 Function Name: STAR
44  
45 Conducts and Logs STAR. Returns the winner or 0 in the event of a tie.
46  
47 =head2 Criteria
48  
49 =head3 Simplicity
50  
51 The Range Ballot is more complex for voters than the Ranked Choice Ballot. The scoring and runoff are both very simple.
52  
53 =head3 Later Harm
54  
55 There is significantly less Later Harm with STAR than with other Borda methods. By ranking the preferred choice with the maximum score, and alternate choices very low, the voter is minimizing the later harm impact of those later choices. With 10 choices in regular Borda, the second choice would recieve 90% of the first choice's score, by ranking later choices at the bottom of the scale the impact is much lower.
56  
57 =head3 Condorcet Criteria
58  
59 STAR only meets the Condorcet Loser Criteria. The runoff prevents a Condorcet Loser from winning.
60  
61 STAR does not meet the Smith and Condorcet Winner Criteria.
62  
63 More information is needed to know if in practice it performs better than IRV.
64  
65 =head3 Consistency
66  
67 STAR should meet Monotonacity. Adding a non-winning choice will have no impact on the outcome unless they can score high enough to reach and lose the runoff phase. Clone handling is dependent on the behavior of the clone group supporters, if they rank the clones far apart, the clone that attracts later support from non-clone supporters is likely to not reach the runoff.
68  
69 More information is needed to know if Clone handling is good or poor in practice, and whether there is a significant consistency failure of some other type.
70  
71 =head2 Implementation Notes
72  
73 Beginning with version 1.08 the STAR() method returns a Hash Ref similar to other Vote::Count Methods. The key 'tie' is true for a tie false otherwise, the key 'winner' contains the winning choice or 0 if there is a tie. When there is a tie an additional key 'tied' contains an Array Ref of the tied choices.
74  
75 When more than 2 choices are in a tie for the automatic runoff STAR() returns them as a tie.
76  
77 =cut
78  
79 1 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 # use YAML::XS;
81  
82 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use List::Util qw( min max sum );
Subroutine BEGIN: called CC=1 SCAR=0.0
84 # use Data::Dumper;
85 1 ▶use Sort::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 # Similar needs will arise elsewhere. this method should be generalized
88 # and put in a shared role. the aability to resolve ties internally will
89 # also be desired.
90  
91 ▶sub _best_two ( $I, $scores ) {
Subroutine _best_two: called CC=3 SCAR=11.0
92 10 my %sv = $scores->RawCount()->%*;
93 10 my @order = sort_hash( 'desc', \%sv );
94 10 my @toptwo = ( shift @order, shift @order );
95 10 my %tied = ( map { $_ => $sv{$_} } @toptwo );
96 10 my $lastval = $sv{ $toptwo[1] };
97 10 ▶ while ( $sv{ $order[0] } == $lastval ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv{$order[0]} == $lastval) 3 10 13
98 3 my $tieit = shift @order;
99 3 $tied{$tieit} = $sv{tieit};
100 }
101 10 ▶ if ( scalar( keys %tied ) > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %tied > 2) 3 7 10
102 3 $I->logt(
103 "Unhandled Situation, there is a tie in determining the top two for Automatic Runoff."
104 );
105 3 $I->logt( join( ', ', ( sort keys %tied ) ) );
106 3 $I->logd( $scores->RankTable() );
107 # $I->logd( Dumper $I );
108 3 return ( keys %tied );
109 }
110 7 return @toptwo;
111 }
112  
113 8 ▶sub STAR ( $self, $active = undef ) {
Subroutine STAR: called CC=6 SCAR=17.9
Pod documented
114 8 ▶ $active = $self->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 5 3 8
115 8 my $scores = $self->Score($active);
116 8 $self->logv( $scores->RankTable() );
117 8 my @best_two = $self->_best_two($scores);
118 8 ▶ if ( scalar( @best_two ) > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @best_two > 2) 2 6 8
119 2 return { 'tie' => 1, 'winner' => 0, 'tied' => \@best_two };
120 }
121 6 my ( $A, $B ) = @best_two;
122 6 my ( $countA, $countB ) = $self->RangeBallotPair( $A, $B );
123 6 ▶ if ( $countA > $countB ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($countA > $countB) { } 4 2 6
elsif ($countA < $countB) { } 1 1 2
124 4 $self->logt("Automatic Runoff Winner: $A [ $A: $countA -- $B: $countB ]");
125 4 return { 'tie' => 0, 'winner' => $A };
126 }
127 elsif ( $countA < $countB ) {
128 1 $self->logt("Automatic Runoff Winner: $B [ $B: $countB -- $A: $countA ]");
129 1 return { 'tie' => 0, 'winner' => $B };
130 }
131 else {
132 1 $self->logt("Automatic Runoff TIE: [ $A: $countA -- $B: $countB ]");
133 1 return { 'tie' => 1, 'winner' => 0, 'tied' => [ $A, $B ] };
134 }
135 }
136  
137 1 1;
138  
139 #FOOTER
140  
141 =pod
142  
143 BUG TRACKER
144  
145 L<https://github.com/brainbuz/Vote-Count/issues>
146  
147 AUTHOR
148  
149 John Karr (BRAINBUZ) brainbuz@cpan.org
150  
151 CONTRIBUTORS
152  
153 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
154  
155 LICENSE
156  
157 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>.
158  
159 =cut
160  
« Count/RankCount.pm ↑ Summary Count/Start.pm »