Count/Charge/Cascade.pm

filter: Statementstmt 98.0% 148 / 151 Branchbran 89.4% 34 / 38 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 25 / 25 Podpod 0.0% 0 / 5 totaltotal 94.7% 217 / 229 CC 29 SCAR 16.7
CC
29
Cov
96%
_preEstimate
12.0
_chargeInsight
8.2
CalcCharge
5.0
CRAP 5.3
« Count/ReadBallots.pm ↑ Summary Count/Method/MinMax.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Vote::Count::Charge::Cascade;
6 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 extends 'Vote::Count::Charge';
9 # with 'Vote::Count::Charge::NthApproval';
10  
11 4 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Storable 3.15 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Mojo::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Sort::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Vote::Count::Helper::FullCascadeCharge;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 our $VERSION='2.02';
26  
27 =head1 NAME
28  
29 Vote::Count::Charge::Cascade
30  
31 =head1 VERSION 2.02
32  
33 =cut
34  
35 4 has 'VoteValue' => (
36 is => 'ro',
37 isa => 'Int',
38 default => 100000,
39 );
40  
41 4 has 'IterationLog' => (
42 is => 'rw',
43 isa => 'Str',
44 required => 0,
45 );
46  
47 4 has 'EstimationRule' => (
48 is => 'ro',
49 isa => 'Str',
50 default => 'estimate',
51 );
52  
53 4 has 'EstimationFresh' => (
54 is => 'ro',
55 isa => 'Bool',
56 default => 0,
57 );
58  
59 4 has 'TieBreakMethod' => (
60 is => 'rw',
61 isa => 'Str',
62 default => ''
63 );
64  
65 sub BUILD {
66 13 ▶ my $I = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
67 # $I->TieBreakMethod('precedence');
68 13 $I->TieBreakerFallBackPrecedence(1);
69 13 $I->{'roundstatus'} = { 0 => {} };
70 13 $I->{'currentround'} = 0;
71 # to hold the last charged values for elected choices.
72 13 $I->{'lastcharge'} = {};
73 }
74  
75 4 our $coder = Cpanel::JSON::XS->new->ascii->pretty;
76  
77 7 ▶sub Round($I) { return $I->{'currentround'}; }
Subroutine Round: called CC=1 SCAR=0.0
Pod undocumented
78  
79 # quota and charge of from previous round!
80 11 ▶sub NewRound ( $I, $quota = 0, $charge = {} ) {
Subroutine NewRound: called CC=4 SCAR=13.9
Pod undocumented
81 11 $I->TopCount();
82 11 my $round = ++$I->{'currentround'};
83 11 $I->{'roundstatus'}{ $round - 1 } = {
84 'charge' => $charge,
85 'quota' => $quota,
86 };
87 11 ▶ if ( keys $charge->%* ) { $I->{'lastcharge'} = $charge }
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$charge) 5 6 11
88 11 return $round;
89 }
90  
91 ▶sub _preEstimate ( $I, $quota, @elected ) {
Subroutine _preEstimate: called CC=12 SCAR=24.9
92 13 my $estrule = $I->EstimationRule();
93 13 ▶ my $lastround = $I->{'currentround'} ? $I->{'currentround'} - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$I->{'currentround'} ? : 8 5 13
94 13 my $lastcharge = $I->{'lastcharge'};
95 13 my $unw = $I->LastTopCountUnWeighted();
96 13 ▶ die 'LastTopCountUnWeighted failed' unless ( keys $unw->%* );
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$unw 1 12 13
97 12 my %estimate = ();
98 12 my %caps = ();
99 12 ▶ if ( $I->EstimationFresh && $I->EstimationRule eq 'estimate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->EstimationFresh and $I->EstimationRule eq "estimate") 1 11 12
Condition100%3/3
$I->EstimationFresh and $I->EstimationRule eq "estimate"
!ll&&!rl&&r
831
MC/DC100%2/2
$I->EstimationFresh and $I->EstimationRule eq "estimate"
$I->EstimationFresh $I->EstimationRule eq "estimate"
Truth table100%3/3
A and B
A= $I->EstimationFreshB= $I->EstimationRule eq "estimate"
ABresult
0X0
100
111
100 1 die "Fresh Estimation is not compatible with EstimationRule estimate, because prior winners are not in current top count!";
101 }
102 11 for my $e (@elected) {
103 25 ▶ if ( $I->{'lastcharge'}{$e} && ! $I->EstimationFresh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->{'lastcharge'}{$e} and not $I->EstimationFresh) { } 5 20 25
Condition100%3/3
$I->{"lastcharge"}{$e} and not $I->EstimationFresh
!ll&&!rl&&r
1465
MC/DC100%2/2
$I->{"lastcharge"}{$e} and not $I->EstimationFresh
$I->{"lastcharge"}{$e} not $I->EstimationFresh
Truth table100%3/3
A and not(B)
A= $I->{"lastcharge"}{$e}B= not $I->EstimationFresh
ABresult
0X0
100
111
104 5 $estimate{$e} = $I->{'lastcharge'}{$e};
105 5 $caps{$e} = $I->{'lastcharge'}{$e};
106 }
107 else {
108 20 ▶ if ($estrule eq 'estimate') { $estimate{$e} = int( $quota / $unw->{$e} ) }
Branch: 4 branches
BranchTrueFalseTotal
if ($estrule eq 'estimate') { } 13 7 20
elsif ($estrule eq 'votevalue') { } 3 4 7
elsif ($estrule eq 'zero') { } 2 2 4
elsif ($estrule eq 'halfvalue') { } 2 0 2
109 3 elsif ( $estrule eq 'votevalue' ) { $estimate{$e} = $I->VoteValue }
110 2 elsif ( $estrule eq 'zero') { $estimate{$e} = 0 }
111 2 elsif ( $estrule eq 'halfvalue' ){ $estimate{$e} = int( $I->VoteValue / 2 ) }
112 20 $caps{$e} = $I->VoteValue;
113 }
114 }
115 11 return ( \%estimate, \%caps );
116 }
117  
118 # Must move directly to charge after this
119 # --- if another topcount happens estimate will crash!
120 ▶sub QuotaElectDo ( $I, $quota ) {
Subroutine QuotaElectDo: called CC=3 SCAR=11.0
Pod undocumented
121 3 my %TC = $I->TopCount()->RawCount()->%*;
122 3 my @Electable = ();
123 3 for my $C ( keys %TC ) {
124 13 ▶ if ( $TC{$C} >= $quota ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TC{$C} >= $quota) 4 9 13
125 4 $I->Elect($C);
126 4 push @Electable, $C;
127 }
128 }
129 3 return @Electable;
130 }
131  
132 # Produce a better estimate than the previous by running
133 # FullCascadeCharge of the last estimate. Clones a copy of
134 # Ballots for the Cascade Charge.
135 ▶sub _chargeInsight ( $I, $quota, $est, $cap, $bottom, $freeze, @elected ) {
Subroutine _chargeInsight: called CC=8 SCAR=21.1
136 29 my $active = $I->GetActive();
137 29 my %estnew = ();
138 # make sure a new freeze is applied before charge evaluation.
139 29 for my $froz ( keys $freeze->%* ) {
140 70 ▶ $est->{$froz} = $freeze->{$froz} if $freeze->{$froz};
Branch: 1 branch
BranchTrueFalseTotal
if $freeze->{$froz} 0 70 70
141 }
142 29 my %elect = map { $_ => 1 } (@elected);
143 29 my $B = dclone $I->GetBallots();
144 29 my $charge =
145 FullCascadeCharge( $B, $quota, $est, $active, $I->VoteValue() );
146 29 LOOPINSIGHT: for my $E (@elected) {
147 76 ▶ if ( $freeze->{$E} ) { # if frozen stay frozen.
Branch: 2 branches
BranchTrueFalseTotal
if ($freeze->{$E}) { } 0 76 76
elsif ($charge->{$E}{'surplus'} >= 0) { } 43 33 76
148 0 $estnew{$E} = $freeze->{$E};
149 0 next LOOPINSIGHT;
150 }
151 elsif ( $charge->{$E}{'surplus'} >= 0 ) {
152 $estnew{$E} =
153 43 $est->{$E} - int( $charge->{$E}{'surplus'} / $charge->{$E}{'count'} );
154 }
155 else {
156 $estnew{$E} = $est->{$E} -
157 33 ( int( $charge->{$E}{'surplus'} / $charge->{$E}{'count'} ) ) + 1 ;
158 }
159 76 ▶ $estnew{$E} = $cap->{$E} if $cap->{$E} < $estnew{$E}; # apply cap.
Branch: 1 branch
BranchTrueFalseTotal
if $cap->{$E} < $estnew{$E} 4 72 76
160 $estnew{$E} = $bottom->{$E}
161 76 ▶ if $bottom->{$E} > $estnew{$E}; # apply bottom.
Branch: 1 branch
BranchTrueFalseTotal
if $bottom->{$E} > $estnew{$E} 1 75 76
162 }
163 29 return { 'result' => $charge, 'estimate' => \%estnew };
164 }
165  
166 ▶sub _write_iteration_log ( $I, $round, $data ) {
Subroutine _write_iteration_log: called CC=2 SCAR=6.9
167 5 ▶ if( $I->IterationLog() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->IterationLog) 2 3 5
168 2 my $jsonpath = $I->IterationLog() . ".$round.json";
169 2 my $yamlpath = $I->IterationLog() . ".$round.yaml";
170 2 path( $jsonpath )->spew( $coder->encode( $data ) );
171 2 path( $yamlpath )->spew( Dump $data );
172 }
173 }
174  
175 ▶sub CalcCharge ( $I, $quota ) {
Subroutine CalcCharge: called CC=5 SCAR=16.1
Pod undocumented
176 6 my @elected = $I->Elected();
177 6 my $round = $I->Round();
178 6 my $estimates = {};
179 6 my $iteration = 0;
180 6 my $freeze = { map { $_ => 0 } @elected };
181 6 my $bottom = { map { $_ => 0 } @elected };
182 6 my ( $estimate, $cap ) = _preEstimate( $I, $quota, @elected );
183 5 $estimates->{$iteration} = $estimate;
184 5 my $done = 0;
185 5 my $charged = undef ; # the last value from loop is needed for log.
186 5 ▶ until ( $done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 26 5 31
187 26 ++$iteration;
188 26 ▶ if ( $iteration > 100 ) { die "Exceeded Iteration Limit!\n"}
Branch: 1 branch
BranchTrueFalseTotal
if ($iteration > 100) 0 26 26
189 # for ( $estimate, $cap, $bottom, $freeze, @elected ) { warn Dumper $_}
190 $charged =
191 26 _chargeInsight( $I, $quota, $estimate, $cap, $bottom, $freeze,
192 @elected );
193 26 $estimate = $charged->{'estimate'};
194 26 $estimates->{$iteration} = $charged->{'estimate'};
195 26 $done = 1;
196 26 for my $V (@elected) {
197 70 my $est1 = $estimates->{$iteration}{$V};
198 70 my $est2 = $estimates->{ $iteration - 1 }{$V};
199 70 ▶ if ( $est1 != $est2 ) { $done = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($est1 != $est2) 35 35 70
200 }
201 }
202 _write_iteration_log( $I, $round, {
203 estimates => $estimates,
204 quota => $quota,
205 charge => $estimate,
206 iterations => $iteration,
207 5 detail => $charged->{'result'} } );
208  
209 $I->STVEvent( {
210 round => $round,
211 quota => $quota,
212 charge => $estimate,
213 iterations => $iteration,
214 5 detail => $charged->{'result'}
215 } );
216 5 return $estimate;
217 }
218  
219 4 __PACKAGE__->meta->make_immutable;
220 4 1;
221  
222 #FOOTER
223  
224 =pod
225  
226 BUG TRACKER
227  
228 L<https://github.com/brainbuz/Vote-Count/issues>
229  
230 AUTHOR
231  
232 John Karr (BRAINBUZ) brainbuz@cpan.org
233  
234 CONTRIBUTORS
235  
236 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
237  
238 LICENSE
239  
240 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>.
241  
242 SUPPORT
243  
244 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
245  
246 =cut
247  
« Count/ReadBallots.pm ↑ Summary Count/Method/MinMax.pm »