Count/Floor.pm

filter: Statementstmt 100% 79 / 79 Branchbran 96.4% 27 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 99.2% 124 / 125 CC 20 SCAR 12.3
CC
20
Cov
99%
_FloorRnd
6.0
_DoFloor
4.0
ApprovalFloor
4.0
CRAP 3.4
« Count/Score.pm ↑ Summary Count/Approval.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::Floor;
7 25 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # use Data::Dumper;
10  
11 25 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 25 our $VERSION = '1.10';
14  
15 =head1 NAME
16  
17 Vote::Count::Floor
18  
19 =head1 VERSION 1.10
20  
21 =cut
22  
23 # ABSTRACT: Floor Rules for RCV elections.
24  
25 # load the roles providing the underlying ops.
26 25 with 'Vote::Count::Approval', 'Vote::Count::TopCount',;
27  
28 25 has 'FloorRounding' => (
29 is => 'rw',
30 isa => 'Str',
31 default => 'up'
32 );
33  
34 ▶sub _FloorRnd ( $I, $num ) {
Subroutine _FloorRnd: called CC=6 SCAR=17.9
35 30 ▶ if ( $I->FloorRounding eq 'down' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($I->FloorRounding eq 'down') { } 5 25 30
elsif ($I->FloorRounding eq 'up') { } 17 8 25
elsif ($I->FloorRounding eq 'round') { } 4 4 8
elsif ($I->FloorRounding eq 'nextint') { } 3 1 4
36 5 return int($num);
37 }
38 elsif ( $I->FloorRounding eq 'up' ) {
39 17 ▶ return int($num) if ( $num == int($num) );
Branch: 1 branch
BranchTrueFalseTotal
if $num == int $num 7 10 17
40 10 return int( $num + 1 );
41 }
42 elsif ( $I->FloorRounding eq 'round' ) {
43 4 return int( $num + 0.5 );
44 }
45 elsif ( $I->FloorRounding eq 'nextint' ) {
46 3 return int( $num + 1 );
47 }
48 1 else { die 'unknown FloorRounding method requested: ' . $I->FloorRounding }
49 }
50  
51 ▶sub _FloorMin ( $I, $floorpct ) {
Subroutine _FloorMin: called CC=2 SCAR=6.9
52 13 ▶ my $pct = $floorpct >= 1 ? $floorpct / 100 : $floorpct;
Branch: 1 branch
BranchTrueFalseTotal
$floorpct >= 1 ? : 12 1 13
53 13 return $I->_FloorRnd( $I->VotesCast() * $pct );
54 }
55  
56 ▶sub _DoFloor ( $I, $ranked, $cutoff ) {
Subroutine _DoFloor: called CC=4 SCAR=13.9
57 15 my @active = ();
58 15 my @remove = ();
59 15 for my $s ( keys $ranked->%* ) {
60 155 ▶ if ( $ranked->{$s} >= $cutoff ) { push @active, $s }
Branch: 1 branch
BranchTrueFalseTotal
if ($ranked->{$s} >= $cutoff) { } 83 72 155
61 else {
62 72 push @remove, $s;
63 72 $I->logv("Removing: $s: $ranked->{$s}, minimum is $cutoff.");
64 }
65 }
66 15 ▶ if (@remove) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remove) { } 14 1 15
67 14 $I->logt(
68 "Floor Rule Eliminated: ",
69 join( ', ', @remove ),
70 "Remaining: ", join( ', ', @active ),
71 );
72 }
73 else {
74 1 $I->logt('None Eliminated');
75 }
76 15 return { map { $_ => 1 } @active };
77 }
78  
79 # Approval Floor is Approval votes vs total
80 # votes cast -- not total of approval votes.
81 7 ▶sub ApprovalFloor ( $self, $floorpct = 5, $rangecutoff = 0 ) {
Subroutine ApprovalFloor: called CC=4 SCAR=13.9
Pod documented
82 7 my $votescast = $self->VotesCast();
83 7 $self->logt( "Applying Floor Rule of $floorpct\% "
84 . "Approval Count. vs Ballots Cast of $votescast." );
85 my $raw =
86 $self->BallotSetType() eq 'rcv'
87 7 ▶ ? do { $self->Approval(); $self->LastApprovalBallots() }
Branch: 1 branch
BranchTrueFalseTotal
$self->BallotSetType eq 'rcv' ? : 4 3 7
88 : $self->Approval( undef, $rangecutoff )->RawCount();
89 7 return $self->_DoFloor( $raw, $self->_FloorMin($floorpct) );
90 }
91  
92 3 ▶sub TopCountFloor ( $self, $floorpct = 2 ) {
Subroutine TopCountFloor: called CC=3 SCAR=11.0
Pod documented
93 3 $self->logt("Applying Floor Rule of $floorpct\% First Choice Votes.");
94 my $raw =
95 $self->BallotSetType() eq 'rcv'
96 3 ▶ ? do { $self->TopCount(); $self->LastTopCountUnWeighted() }
Branch: 1 branch
BranchTrueFalseTotal
$self->BallotSetType eq 'rcv' ? : 3 0 3
97 : $self->TopCount();
98 3 return $self->_DoFloor( $raw, $self->_FloorMin($floorpct) );
99 }
100  
101 6 ▶sub TCA ( $self, $floor = .5 ) {
Subroutine TCA: called CC=3 SCAR=11.0
Pod documented
102 6 ▶ if ( $floor > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($floor > 1) 1 5 6
103 1 my $m = "Floor value $floor is greater than 1";
104 1 $self->logt($m);
105 1 die "$m\n";
106 }
107 $self->logt(
108 5 'Applying Floor Rule: Approval Must at least ',
109 "$floor times the Most First Choice votes. "
110 );
111 5 my $tc = $self->TopCount();
112 # arraytop returns a list in case of tie.
113 5 my $winner = shift( $tc->ArrayTop->@* );
114 5 my $tcraw = $tc->RawCount()->{$winner};
115 5 my $cutoff = $self->_FloorRnd( $tcraw * $floor );
116 5 $self->logv( "The most first choice votes for any choice is $tcraw.",
117 "Cutoff will be $cutoff" );
118 5 return $self->_DoFloor( $self->Approval()->RawCount(), $cutoff );
119 }
120  
121 ▶sub ApplyFloor ( $self, $rule, @args ) {
Subroutine ApplyFloor: called CC=4 SCAR=13.9
Pod documented
122 4 my $newset = {};
123 4 ▶ if ( $rule eq 'ApprovalFloor' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rule eq 'ApprovalFloor') { } 1 3 4
elsif ($rule eq 'TopCountFloor') { } 1 2 3
elsif ($rule eq 'TCA') { } 1 1 2
124 1 $newset = $self->ApprovalFloor(@args);
125 }
126 elsif ( $rule eq 'TopCountFloor' ) {
127 1 $newset = $self->TopCountFloor(@args);
128 }
129 elsif ( $rule eq 'TCA' ) {
130 1 $newset = $self->TCA(@args);
131 }
132 else {
133 1 die "Bad rule provided to ApplyFloor, $rule";
134 }
135 3 $self->SetActive($newset);
136 3 return $newset;
137 }
138  
139 =head1 Floor Rules
140  
141 In real elections it is common to have choices with very little support, with write-ins there can be a large number of these choices, with iterative dropping like IRV it can take many rounds to work through them. A Floor Rule sets a criteria to remove the weakly supported choices early in a single operation.
142  
143 =head1 SYNOPSIS
144  
145 my $Election = Vote::Count->new( BallotSet => $someballotset );
146 my $ChoicesAfterFloor = $Election->ApprovalFloor();
147 $Election->SetActive( $ChoicesAfterFloor ); # To apply the floor
148 $Election->ApplyFloor( 'TopCountFloor', @options ); # One Step
149  
150 =head1 Rounding
151  
152 The default rounding is up. If a calculated cutoff is 11.2, the cutoff will become greater than or equal to 12. Set FloorRounding to 'down' to change this to round down for 11.9 to become 11. Set FloorRounding to 'round' to change this to round .5 or greater up. If the comparison needs to be Greater than, a FloorRounding of 'nextint' will use the next higher integer.
153  
154 # When creating the Election.
155 my $Election = Vote::Count->new( FloorRounding => 'round', ... );
156 # Before applying the floor.
157 $Election->FloorRounding( 'down');
158  
159 =head1 The Floor Methods
160  
161 All Methods in this Module apply a floor rule, log the eliminations and return the set of remaining choices as a HashRef. Use the ApplyFloor Method to immediately apply the results.
162  
163 =head2 ApplyFloor
164  
165 Takes as an argument the Method Name as a string of the rule to apply ( ApprovalFloor, TopCountFloor, TCA), followed by any optional arguments for the rule. Sets the Active Set as defined by that rule and returns the new Active Set as a hashref.
166  
167 # Apply a TopCount Floor of 10%.
168 my $newactive = $Election->ApplyFloor( 'TopCountFloor', 10);
169  
170 =head2 ApprovalFloor, TopCountFloor
171  
172 Requires a percent of votes cast in Approval or TopCount. The default is 5% for Approval and 2% for TopCount.
173  
174 # TopCountFloor with 3% threshold.
175 my $Floored = $Election->TopCountFloor( 3 );
176  
177 Both of these methods take an optional parameter which is the percentage for the floor. If the parameter is 1 or greater the parameter will be interpreted as a percentage, if it is less than 1 it will be interpreted as a decimal fraction, .1 and 10 will both result in a 10% floor.
178  
179 For Range Ballots using ApprovalFloor there is an additional optional value for cutoff that sets the score below which choices are not considered approved of.
180  
181 # Applies 5% floor with cutoff 5 (appropriate for Range 0-10)
182 my $active = $Range->ApprovalFloor( 5, 5 );
183  
184 =head2 TCA (TopCount-Approval)
185  
186 Aggressive but (effectively) safe for Condorcet Methods. It requires the Approval for a choice be at least half of the leading Top Count Vote.
187  
188 This rule takes an optional argument to change the floor from .5.
189  
190 # uses default of 1/2
191 my $active = $Election->TCA();
192 # requires approval equal leader
193 my $active = $Election->TCA( 1 );
194  
195 =head3 TCA Rule Validation and Implication
196  
197 If there is a Loop or Condorcet Winner, either it will be/include the Top Count Leader or it must be a choice which defeats the Top Count leader. To defeat the Top Count Leader a Choice's Approval must be greater than the Lead Top Count. To be able to defeat a choice it is necessary to have more than half of the approval of that choice. Thus to be able to defeat a choice which can defeat the Top Count Leader it will be necessary to have more than half of the Approval of a choice with an Approval greater than the lead Top Count.
198  
199 There is a small possibility for a situation with a deeply nested knotted result that this rule could eliminate a member of the Dominant Set. For the common simple dropping rules (Approval, Top Count, Greatest Loss, Borda) this choice would never win.
200  
201 For IRV any choice with an Approval that is not greater than the current TopCount of any other choice will always be eliminated prior to that choice. Unfortunately, with IRV any change to dropping order can alter the result. If it is used in IRV the Election Rules must specify it. Also because it is a high Approval based Floor, it can be construed as adding a small risk of Later Harm violation. If the reason for choosing IRV was Later Harm, then the only safe floor is a TopCount floor.
202  
203 =cut
204  
205 25 1;
206  
207 #FOOTER
208  
209 =pod
210  
211 BUG TRACKER
212  
213 L<https://github.com/brainbuz/Vote-Count/issues>
214  
215 AUTHOR
216  
217 John Karr (BRAINBUZ) brainbuz@cpan.org
218  
219 CONTRIBUTORS
220  
221 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
222  
223 LICENSE
224  
225 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>.
226  
227 =cut
« Count/Score.pm ↑ Summary Count/Approval.pm »