Count/Method/WIGM.pm

filter: Statementstmt 97.5% 118 / 121 Branchbran 80.0% 16 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 1 totaltotal 95.0% 152 / 160 CC 17 SCAR 10.6
CC
17
Cov
95%
WIGRun
6.0
_WIGRound
4.0
_WIGElect
3.1
CRAP 2.9
« Count/RankCount.pm ↑ Summary Count/Method/STAR.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Vote::Count::Method::WIGM;
6 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 extends 'Vote::Count::Charge';
9  
10 2 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Storable 3.15 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Sort::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our $VERSION='2.02';
19  
20 =head1 NAME
21  
22 Vote::Count::Method::WIGM
23  
24 =head1 VERSION 2.02
25  
26 =cut
27  
28 # ABSTRACT: An implementation of WIGM STV using Vote::Charge.
29  
30 =pod
31  
32 =head1 SYNOPSIS
33  
34 use Vote::Count::Method::WIGM;
35 use Vote::Count::ReadBallots 'read_ballots';
36  
37 my $ballotset = read_ballots('t/data/Scotland2012/Cumbernauld_South.txt');
38 my $Cumbernauld = Vote::Count::Method::WIGM->new(
39 Seats => 4,
40 BallotSet => $ballotset,
41 VoteValue => 100000, # default
42 LogTo => '/tmp/cumbernauld_south_2012',
43 );
44  
45 # Run the Election
46 $D->WIGRun();
47 # Write the Human Readable Logs
48 $D->WriteLog();
49 # Write the Events in JSON and YAML
50 $D->WriteSTVEvent();
51  
52 =head1 Description
53  
54 Implements Weighted Improved Gregory Single Transferable Vote based on Scotland's rules.
55  
56 =head1 WIGRun
57  
58 Run and log the Election.
59  
60 =head2 Implementation Notes
61  
62 The Scottish Rules specify 5 decimal places, a weight of 100,000 is used which is equivalent.
63  
64 When more than one choice is Pending the rules call for a full Stage to elect each of them. Pending Choices cannot recieve additional votes, this implementation elects, charges, and rebates the Pending Choices, then starts a new Round. The final result will be the same, but Vote::Count::Method::WIGM rounds will not always match the stages of the Hand Count rules.
65  
66 =head1 Experimental
67  
68 Small discrepencies with the stages data available for testing have been seen, which are likely to be rounding issues. Until further review can be taken, this code should be considered a preview.
69  
70 =head1 The Rules
71  
72 L<The Official Rules|http://www.opsi.gov.uk/legislation/scotland/ssi2007/ssi_20070042_en.pdf>
73  
74 =cut
75  
76 2 has 'VoteValue' => (
77 is => 'ro',
78 isa => 'Int',
79 default => 100000,
80 );
81  
82 4 ▶sub _SetWIGQuota ( $I, $ballots = 0 ) {
Subroutine _SetWIGQuota: called CC=3 SCAR=11.0
83 4 ▶ $ballots = $I->VotesCast() unless $ballots;
Branch: 1 branch
BranchTrueFalseTotal
unless $ballots 3 1 4
84 4 my $denominator = $I->Seats() + 1;
85 4 my $q = 1 + int( $ballots / $denominator );
86 4 return ( $q * $I->VoteValue );
87 }
88  
89 # If a floor is applied there may be abandoned ballots,
90 # _WIGStart will remove these from the quota.
91  
92 ▶sub _format_round_result ( $rslt, $topcount, $votevalue ) {
Subroutine _format_round_result: called CC=2 SCAR=6.9
93 12 my $top = dclone $topcount;
94 12 my $rawcount = $top->{'rawcount'};
95 12 for my $t ( keys $rawcount->%* ) {
96 54 my $folded = $rawcount->{$t} / $votevalue;
97 54 $rawcount->{$t} = "$folded ($rawcount->{$t})";
98 }
99 # die Dumper $rawcount;
100 12 my $tmpl = q|% use feature 'postderef';
101 % my @pending = $rslt->{'pending'}->@*;
102 ## Round: <%= $rslt->{'round'} %>
103  
104 <%= $tc->RankTable() %>
105 % if ( @pending ) {
106 ### Winners:
107  
108 % for my $p ( @pending ) {
109 *<%= $p %>*: <%= $rslt->{'winvotes'}{$p} / $votevalue %> (<%= $rslt->{'winvotes'}{$p} %>)
110  
111 % }
112 % } else {
113 ### No Winners
114  
115 % }|;
116 12 my $mt = Mojo::Template->new;
117 12 return $mt->vars(1)->render( $tmpl, {rslt => $rslt, tc => $top, votevalue => $votevalue });
118 }
119  
120 ▶sub _WIGStart ( $I ) {
Subroutine _WIGStart: called CC=2 SCAR=6.9
121 1 my $seats = $I->Seats();
122 1 my $ballots_cast = $I->VotesCast();
123 1 $I->TopCount(); # Set the topchoice values on the ballots.
124 1 my $abandoned = $I->CountAbandoned()->{'count_abandoned'};
125 1 my $ballots_valid = $ballots_cast - $abandoned;
126 1 ▶ my $abandonmsg =
Branch: 1 branch
BranchTrueFalseTotal
$abandoned ? : 0 1 1
127 $abandoned
128 ? "\nThere are $ballots_cast ballots. \n$abandoned ballots have no choices and will be disregarded"
129 : '';
130 1 my $quota = $I->_SetWIGQuota($ballots_valid);
131 1 $I->logt(
132 qq/Using Weighted Inclusive Gregory, Scottish Rules. $abandonmsg
133 Seats to Fill: $seats
134 Ballots Counted for Quota: $ballots_valid
135 Quota: $quota;
136 /
137 );
138 1 my $event = { ballots => $ballots_valid, quota => $quota };
139 1 $I->STVEvent($event);
140 1 return $event;
141 }
142  
143 ▶sub _WIGRound ( $I, $quota ) {
Subroutine _WIGRound: called CC=4 SCAR=13.9
144 12 my $round_num = $I->NextSTVRound();
145 12 my $round = $I->TopCount();
146 12 my $roundcnt = $round->RawCount();
147 12 my @choices = $I->GetActiveList();
148 12 my %rndvotes = ();
149 12 my $leader = $round->Leader()->{'winner'};
150 12 my $votes4leader = $round->RawCount->{$leader};
151 12 ▶ my $pending = $votes4leader >= $quota ? $leader : '';
Branch: 1 branch
BranchTrueFalseTotal
$votes4leader >= $quota ? : 5 7 12
152  
153 12 for my $C (@choices ) {
154 54 ▶ if( $roundcnt->{ $C} >= $quota ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roundcnt->{$C} >= $quota) 9 45 54
155 9 $rndvotes{ $C } = $roundcnt->{ $C } ;
156 }
157 }
158 12 my @pending = sort_hash( \%rndvotes, 'numeric', 'desc' );
159  
160 my $rslt = {
161 pending => \@pending,
162 winvotes => \%rndvotes,
163 quota => $quota,
164 round => $round_num,
165 allvotes => $round->RawCount(),
166 lowest => $round->ArrayBottom()->[0],
167 12 noncontinuing => $I->CountAbandoned()->{'value_abandoned'},
168 };
169 12 $I->STVEvent($rslt);
170 12 $I->logv( _format_round_result( $rslt, $round, $I->VoteValue() ) );
171 12 return ($rslt);
172 }
173  
174 ▶sub _WIGElect ( $I, $chrg ) {
Subroutine _WIGElect: called CC=3 SCAR=11.4
175 7 my $choice = $chrg->{'choice'};
176 7 my $refund = int( $chrg->{'surplus'} / $chrg->{'cntchrgd'} );
177 7 my $refunded = 0;
178 7 for my $b ( $chrg->{'ballotschrgd'}->@* ) {
179 1047 my $ballot = $I->BallotSet()->{'ballots'}{$b};
180 1047 ▶ if ( $ballot->{'charged'}{$choice} < $refund )
Branch: 1 branch
BranchTrueFalseTotal
if ($ballot->{'charged'}{$choice} < $refund) { } 0 1047 1047
181 {
182 0 $ballot->{'votevalue'} += $ballot->{'charged'}{$choice};
183 0 $refunded += $ballot->{'charged'}{$choice} * $ballot->{'count'};
184 0 $ballot->{'charged'}{$choice} = 0;
185 } else
186 {
187 1047 $ballot->{'votevalue'} += $refund;
188 1047 $refunded += $ballot->{'count'} * $refund;
189 1047 $ballot->{'charged'}{$choice} -= $refund;
190 }
191 }
192 my $candvotes =
193 7 $I->GetChoiceStatus( $choice )->{'votes'} - $refunded;
194 7 $I->SetChoiceStatus( $choice, { votes => $candvotes } );
195 7 $I->Elect( $choice );
196 }
197  
198 ▶sub _wigcomplete ( $I, $_WIGRound ) {
Subroutine _wigcomplete: called CC=3 SCAR=11.0
199 10 my @choices = $I->GetActiveList();
200 10 my $choiceremain = scalar @choices;
201 10 my $numelected = scalar( $I->{'elected'}->@* );
202 10 my $openseats = $I->Seats() - $numelected;
203 10 ▶ if ( $choiceremain <= $openseats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($choiceremain <= $openseats) 1 9 10
204 1 $I->logv( "Electing all Remaining Choices: @choices.\n");
205 1 for my $C (@choices) {
206 1 my $cvotes = $_WIGRound->{'allvotes'}{$C};
207 1 $I->Elect($C);
208 1 $I->SetChoiceStatus( $C, { votes => $cvotes } );
209 }
210 1 return 1;
211 }
212 9 return 0;
213 }
214  
215 ▶sub WIGRun ( $I ) {
Subroutine WIGRun: called CC=6 SCAR=17.9
Pod undocumented
216 1 my $pre_rslt = $I->_WIGStart();
217 1 my $quota = $pre_rslt->{'quota'};
218 1 my $seats = $I->Seats();
219  
220 WIGDOROUNDLOOP:
221 1 ▶ while ( $I->Elected() < $seats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->Elected < $seats) 6 0 6
222 6 my $rnd = $I->_WIGRound($quota);
223 6 ▶ last WIGDOROUNDLOOP if _wigcomplete ( $I, $rnd );
Branch: 1 branch
BranchTrueFalseTotal
if _wigcomplete($I, $rnd) 0 6 6
224 6 my @pending = $rnd->{'pending'}->@*;
225 6 ▶ if ( scalar(@pending)){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pending) { } 2 4 6
226 2 for my $pending (@pending) {
227 3 my $chrg = $I->Charge( $pending, $quota );
228 3 $I->_WIGElect($chrg);
229 }
230 } else {
231 4 $I->logv( "Eliminating low choice: $rnd->{'lowest'}\n");
232 4 $I->Defeat($rnd->{'lowest'});
233 4 ▶ last WIGDOROUNDLOOP if _wigcomplete ( $I, $rnd );
Branch: 1 branch
BranchTrueFalseTotal
if _wigcomplete($I, $rnd) 1 3 4
234 }
235 }
236 1 my @elected = $I->Elected();
237 1 $I->STVEvent( { winners => \@elected });
238 1 $I->logt( "Winners: " . join( ', ', @elected ));
239 }
240  
241 2 __PACKAGE__->meta->make_immutable;
242 2 1;
243  
244 #FOOTER
245  
246 =pod
247  
248 BUG TRACKER
249  
250 L<https://github.com/brainbuz/Vote-Count/issues>
251  
252 AUTHOR
253  
254 John Karr (BRAINBUZ) brainbuz@cpan.org
255  
256 CONTRIBUTORS
257  
258 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
259  
260 LICENSE
261  
262 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>.
263  
264 SUPPORT
265  
266 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
267  
268 =cut
269  
« Count/RankCount.pm ↑ Summary Count/Method/STAR.pm »