Count/Charge.pm

filter: Statementstmt 95.0% 229 / 241 Branchbran 83.8% 52 / 62 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.3% 42 / 45 Podpod 85.1% 23 / 27 totaltotal 91.3% 347 / 380 CC 50 SCAR 13.8
CC
50
Cov
92%
Defeated
12.0
STVFloor
10.1
Charge
6.0
CRAP 4.0
« Count/Borda.pm ↑ Summary Count.pm »
1 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use feature qw /postderef signatures switch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Vote::Count::Charge;
7 13 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 extends 'Vote::Count';
10  
11 13 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶no warnings 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 ▶use Sort::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 # use Storable 3.15 'dclone';
22  
23 13 our $VERSION='2.02';
24  
25 13 has 'Seats' => (
26 is => 'ro',
27 isa => 'Int',
28 required => 1,
29 );
30  
31 13 has 'FloorRule' => (
32 is => 'rw',
33 isa => 'Str',
34 default => '',
35 );
36  
37 13 has 'FloorThresshold' => (
38 is => 'ro',
39 isa => 'Num',
40 default => 0,
41 );
42  
43 13 my @choice_valid_states =
44 qw( elected pending defeated withdrawn active suspended );
45  
46 ▶sub _init_choice_status ( $I ) {
Subroutine _init_choice_status: called CC=5 SCAR=16.1
47 31 $I->{'choice_status'} = {};
48 31 $I->{'pending'} = [];
49 31 $I->{'elected'} = [];
50 31 $I->{'suspended'} = [];
51 31 $I->{'deferred'} = [];
52 31 $I->{'stvlog'} = [];
53 31 $I->{'stvround'} = 0;
54 31 for my $c ( $I->GetChoices() ) {
55 268 $I->{'choice_status'}->{$c} = {
56 state => 'hopeful',
57 votes => 0,
58 };
59 }
60 31 ▶ if ( $I->WithdrawalList ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->WithdrawalList) 1 30 31
61 1 for my $w (path( $I->WithdrawalList )->lines({ chomp => 1})) {
62 4 ▶ $I->Withdraw($w) if defined $I->{'choice_status'}{$w};
Branch: 1 branch
BranchTrueFalseTotal
if defined $I->{"choice_status"}{$w} 3 1 4
63 }
64 }
65 }
66  
67 # Default tie breaking to Precedence,
68 # Force Precedence as fallback, and generate reproducible precedence
69 # file if one isn't provided.
70 ▶sub _setTieBreaks ( $I ) {
Subroutine _setTieBreaks: called CC=4 SCAR=14.6
71 13 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 31 ▶ unless ( $I->TieBreakMethod() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($I->TieBreakMethod) 31 0 31
73 31 $I->logd('TieBreakMethod is undefined, setting to precedence');
74 31 $I->TieBreakMethod('precedence');
75 }
76 31 ▶ if ( $I->TieBreakMethod ne 'precedence' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->TieBreakMethod ne "precedence") 0 31 31
77 0 $I->logv( 'Ties will be broken by: '
78 . $I->TieBreakMethod
79 . ' with a fallback of precedence' );
80 0 $I->TieBreakerFallBackPrecedence(1);
81 }
82 31 ▶ unless ( stat $I->PrecedenceFile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (stat $I->PrecedenceFile) 31 0 31
83 31 my @order = $I->CreatePrecedenceRandom('/tmp/precedence.txt');
84 31 $I->PrecedenceFile('/tmp/precedence.txt');
85 31 $I->logv( "Order for Random Tie Breakers is: " . join( ", ", @order ) );
86 }
87 }
88  
89 ▶sub ResetVoteValue ($I) {
Subroutine ResetVoteValue: called CC=2 SCAR=6.9
Pod documented
90 32 my $ballots = $I->GetBallots();
91 32 for my $b ( keys $ballots->%* ) {
92 10510 $ballots->{$b}->{'votevalue'} = $I->VoteValue();
93 10510 $ballots->{$b}->{'topchoice'} = undef;
94 }
95 }
96  
97 0 ▶sub SeatsOpen ($I) { $I->Seats() - $I->Elected() }
Subroutine SeatsOpen: not called CC=1 SCAR=6.9
Pod documented
98  
99 sub BUILD {
100 32 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
101 32 ▶ unless ( $self->BallotSetType() eq 'rcv' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->BallotSetType eq "rcv") 1 31 32
102 1 croak "Charge only supports rcv Ballot Type";
103 }
104 31 $self->_setTieBreaks();
105 31 $self->ResetVoteValue();
106 31 $self->_init_choice_status();
107 31 $self->FloorRounding('down');
108 }
109  
110 =pod
111  
112 CountAbandoned
113  
114 =cut
115  
116 ▶sub CountAbandoned ($I) {
Subroutine CountAbandoned: called CC=5 SCAR=16.3
Pod undocumented
117 28 my @continuing = ( $I->Deferred(), $I->GetActiveList );
118 28 my $set = $I->GetBallots();
119 28 my %res = ( count_abandoned => 0, value_abandoned => 0, );
120 28 for my $k ( keys $set->%* ) {
121 17412 ▶ if ( $set->{$k}{'votevalue'} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set->{$k}{"votevalue"} == 0) 0 17412 17412
122 0 $res{count_abandoned} += $set->{$k}{'count'};
123 0 next;
124 }
125 17412 my $continue = 0;
126 17412 for my $c (@continuing) {
127 82806 $continue += ( grep /$c/, $set->{$k}{'votes'}->@* );
128 }
129 17412 ▶ unless ($continue) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($continue) 631 16781 17412
130 631 $res{count_abandoned} += $set->{$k}{'count'};
131 631 $res{value_abandoned} += $set->{$k}{'count'} * $set->{$k}{'votevalue'};
132 }
133 }
134 $res{message} =
135 28 "Votes with no Choice left: $res{count_abandoned}, Value: $res{value_abandoned}";
136 28 return \%res;
137 }
138  
139 29 ▶sub GetChoiceStatus ( $I, $choice = 0 ) {
Subroutine GetChoiceStatus: called CC=3 SCAR=11.0
Pod documented
140 29 ▶ if ($choice) { return $I->{'choice_status'}{$choice} }
Branch: 1 branch
BranchTrueFalseTotal
if ($choice) { } 24 5 29
141 5 else { return $I->{'choice_status'} }
142 }
143  
144 ▶sub SetChoiceStatus ( $I, $choice, $status ) {
Subroutine SetChoiceStatus: called CC=4 SCAR=14.0
Pod documented
145 12 ▶ if ( $status->{'state'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->{"state"}) 2 10 12
146 2 ▶ unless ( grep ( /^$status->{'state'}$/, @choice_valid_states ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$status->{"state"}$/u, @choice_valid_states) 0 2 2
147 0 croak "invalid state *$status->{'state'}* assigned to choice $choice";
148 }
149 2 $I->{'choice_status'}->{$choice}{'state'} = $status->{'state'};
150 }
151 12 ▶ if ( $status->{'votes'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->{"votes"}) 11 1 12
152 11 $I->{'choice_status'}->{$choice}{'votes'} = int $status->{'votes'};
153 }
154 }
155  
156 ▶sub VCUpdateActive ($I) {
Subroutine VCUpdateActive: called CC=3 SCAR=11.0
Pod documented
157 3 my $active = {};
158 3 for my $k ( keys $I->GetChoiceStatus()->%* ) {
159 24 ▶ $active->{$k} = 1 if $I->{'choice_status'}->{$k}{'state'} eq 'hopeful';
Branch: 1 branch
BranchTrueFalseTotal
if $I->{"choice_status"}{$k}{"state"} eq "hopeful" 5 19 24
160 # $active->{$k} = 1 if $I->{'choice_status'}->{$k}{'state'} eq 'pending';
161 }
162 3 $I->SetActive($active);
163 }
164  
165 ▶sub Elect ( $I, $choice ) {
Subroutine Elect: called CC=1 SCAR=0.0
Pod documented
166 39 delete $I->{'Active'}{$choice};
167 39 $I->{'choice_status'}->{$choice}{'state'} = 'elected';
168 39 $I->{'pending'} = [ grep ( !/^$choice$/, $I->{'pending'}->@* ) ];
169 39 push $I->{'elected'}->@*, $choice;
170 39 return $I->{'elected'}->@*;
171 }
172  
173 21 ▶sub Elected ($I) { return $I->{'elected'}->@* }
Subroutine Elected: called CC=1 SCAR=0.0
Pod documented
174  
175 ▶sub Defeat ( $I, $choice ) {
Subroutine Defeat: called CC=1 SCAR=0.0
Pod documented
176 23 delete $I->{'Active'}{$choice};
177 23 $I->{'choice_status'}->{$choice}{'state'} = 'defeated';
178 }
179  
180 ▶sub Defeated ($I) {
Subroutine Defeated: not called CC=3 SCAR=24.8
Pod documented
181 0 my @defeated = ();
182 0 for my $c ( keys $I->{'choice_status'}->%* ) {
183 0 ▶ if ( $I->{'choice_status'}{$c}{'state'} eq 'defeated') {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->{"choice_status"}{$c}{"state"} eq "defeated") 0 0 0
184 0 push @defeated, $c;
185 }
186 }
187 0 return sort(@defeated);
188 }
189  
190 ▶sub Withdrawn ($I) {
Subroutine Withdrawn: called CC=3 SCAR=11.0
Pod documented
191 12 my @withdrawn = ();
192 12 for my $c ( keys $I->{'choice_status'}->%* ) {
193 128 ▶ if ( $I->{'choice_status'}{$c}{'state'} eq 'withdrawn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->{"choice_status"}{$c}{"state"} eq "withdrawn") 25 103 128
194 25 push @withdrawn, $c;
195 }
196 }
197 12 return sort(@withdrawn);
198 }
199  
200 ▶sub Withdraw ( $I, $choice ) {
Subroutine Withdraw: called CC=1 SCAR=0.0
Pod documented
201 9 delete $I->{'Active'}{$choice};
202 9 $I->{'choice_status'}->{$choice}{'state'} = 'withdrawn';
203 9 return $I->Withdrawn();
204 }
205  
206 ▶sub Suspend ( $I, $choice ) {
Subroutine Suspend: called CC=2 SCAR=6.9
Pod documented
207 8 delete $I->{'Active'}{$choice};
208 8 $I->{'choice_status'}->{$choice}{'state'} = 'suspended';
209 8 ▶ unless ( grep ( /^$choice$/, $I->{'suspended'}->@* ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$choice$/u, @{$$I{"suspended"}}) 7 1 8
210 7 push $I->{'suspended'}->@*, $choice;
211 }
212 8 return $I->Suspended();
213 }
214  
215 ▶sub Suspended ($I ) {
Subroutine Suspended: called CC=1 SCAR=0.0
Pod documented
216 11 return $I->{'suspended'}->@*;
217 }
218  
219 ▶sub Defer ( $I, $choice ) {
Subroutine Defer: called CC=2 SCAR=7.0
Pod documented
220 2 delete $I->{'Active'}{$choice};
221 2 $I->{'choice_status'}->{$choice}{'state'} = 'deferred';
222 2 ▶ unless ( grep ( /^$choice$/, $I->{'deferred'}->@* ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$choice$/u, @{$$I{"deferred"}}) 2 0 2
223 2 push $I->{'deferred'}->@*, $choice;
224 }
225 2 return $I->Deferred();
226 }
227  
228 ▶sub Deferred ($I ) {
Subroutine Deferred: called CC=1 SCAR=0.0
Pod documented
229 31 return $I->{'deferred'}->@*;
230 }
231  
232 2 ▶sub Pending ( $I, $choice = undef ) {
Subroutine Pending: called CC=4 SCAR=13.9
Pod documented
233 2 ▶ if ($choice) {
Branch: 1 branch
BranchTrueFalseTotal
if ($choice) 1 1 2
234 1 ▶ unless ( grep /^$choice$/, $I->{'pending'}->@* ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$choice$/u, @{$$I{"pending"}}) 1 0 1
235 1 $I->{'choice_status'}->{$choice}{'state'} = 'pending';
236 1 push $I->{'pending'}->@*, $choice;
237 1 delete $I->{'Active'}{$choice};
238 }
239 }
240 2 return $I->{'pending'}->@*;
241 }
242  
243 ▶sub Reinstate ( $I, @choices ) {
Subroutine Reinstate: called CC=3 SCAR=11.0
Pod documented
244 # if no choices are given reinstate all.
245 4 ▶ @choices = ($I->{'suspended'}->@*, $I->{'deferred'}->@* ) unless @choices;
Branch: 1 branch
BranchTrueFalseTotal
unless @choices 1 3 4
246 4 my @reinstated = ();
247 REINSTATELOOP:
248 4 for my $choice (@choices) {
249 # I'm a fan of the give/when construct, but go to lengths not to use it
250 # because of past issues and that after 15 years it is still experimental.
251 6 given ($I->{'choice_status'}{$choice}{'state'}){
252 6 when ( 'suspended') { }
253 2 when ( 'deferred' ) { }
254 1 default { next REINSTATELOOP }
255 };
256 5 ($I->{'suspended'}->@*) = grep ( !/^$choice$/, $I->{'suspended'}->@* );
257 5 ($I->{'deferred'}->@*) = grep ( !/^$choice$/, $I->{'deferred'}->@* );
258 5 $I->{'choice_status'}->{$choice}{'state'} = 'hopeful';
259 5 $I->{'Active'}{$choice} = 1;
260 5 push @reinstated, $choice;
261 }
262 4 return @reinstated;
263 }
264  
265 22 ▶sub Charge ( $I, $choice, $quota, $charge=$I->VoteValue() ) {
Subroutine Charge: called CC=6 SCAR=17.9
Pod documented
266 22 my $charged = 0;
267 22 my $surplus = 0;
268 22 my @ballotschrgd = ();
269 22 my $cntchrgd = 0;
270 22 my $active = $I->Active();
271 22 my $ballots = $I->BallotSet()->{'ballots'};
272 # warn Dumper $ballots;
273 CHARGECHECKBALLOTS:
274 22 for my $B ( keys $ballots->%* ) {
275 11820 ▶ next CHARGECHECKBALLOTS if ( $I->TopChoice($B) ne $choice );
Branch: 1 branch
BranchTrueFalseTotal
if $I->TopChoice($B) ne $choice 9319 2501 11820
276 2501 my $ballot = $ballots->{$B};
277 2501 ▶ if ( $charge == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($charge == 0) { } 1 2500 2501
elsif ($ballot->{'votevalue'} >= $charge) { } 2344 156 2500
278 1 $charged += $ballot->{'votevalue'} * $ballot->{'count'};
279 1 $ballot->{'charged'}{$choice} = $ballot->{'votevalue'};
280 1 $ballot->{'votevalue'} = 0;
281 }
282 elsif ( $ballot->{'votevalue'} >= $charge ) {
283 2344 my $over = $ballot->{'votevalue'} - $charge;
284 2344 $charged += ( $ballot->{'votevalue'} - $over ) * $ballot->{'count'};
285 2344 $ballot->{'votevalue'} -= $charge;
286 2344 $ballot->{'charged'}{$choice} = $charge;
287 }
288 else {
289 156 $charged += $ballot->{'votevalue'} * $ballot->{'count'};
290 156 $ballot->{'charged'}{$choice} = $ballot->{'votevalue'};
291 156 $ballot->{'votevalue'} = 0;
292 }
293 2501 push @ballotschrgd, $B;
294 2501 $cntchrgd += $ballot->{'count'};
295 }
296 22 $I->{'choice_status'}->{$choice}{'votes'} += $charged;
297 22 $surplus = $I->{'choice_status'}->{$choice}{'votes'} - $quota;
298 22 $I->{'choice_status'}->{$choice}{'votes'} = $charged;
299 return (
300 {
301 22 choice => $choice,
302 surplus => $surplus,
303 ballotschrgd => \@ballotschrgd,
304 cntchrgd => $cntchrgd,
305 quota => $quota
306 }
307 );
308 }
309  
310 24 ▶sub STVEvent ( $I, $data = 0 ) {
Subroutine STVEvent: called CC=3 SCAR=11.0
Pod documented
311 24 ▶ return $I->{'stvlog'} unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 5 19 24
312 19 push $I->{'stvlog'}->@*, $data;
313 }
314  
315 ▶sub WriteSTVEvent( $I) {
Subroutine WriteSTVEvent: called CC=1 SCAR=0.0
Pod documented
316 2 my $jsonpath = $I->LogTo . '_stvevents.json';
317 2 my $yamlpath = $I->LogTo . '_stvevents.yaml';
318 # my $yaml = ;
319 2 my $coder = Cpanel::JSON::XS->new->ascii->pretty;
320 2 path($jsonpath)->spew( $coder->encode( $I->STVEvent() ) );
321 2 path($yamlpath)->spew( Dump $I->STVEvent() );
322 }
323  
324 0 ▶sub STVRound($I) { return $I->{'stvround'} }
Subroutine STVRound: not called CC=1 SCAR=6.9
Pod documented
325  
326 12 ▶sub NextSTVRound( $I) { return ++$I->{'stvround'} }
Subroutine NextSTVRound: called CC=1 SCAR=0.0
Pod documented
327  
328 ▶sub TCStats( $I ) {
Subroutine TCStats: called CC=1 SCAR=0.0
Pod undocumented
329 2 my $tc = $I->TopCount;
330 2 $tc->{'total_votes'} = $I->VotesCast;
331 2 $tc->{'total_vote_value'} = $tc->{'total_votes'} * $I->VoteValue;
332 2 $tc->{'abandoned'} = $I->CountAbandoned;
333 $tc->{'active_vote_value'} =
334 2 $tc->{'total_vote_value'} - $tc->{'abandoned'}{'value_abandoned'};
335 2 return $tc;
336 }
337  
338 2 ▶sub STVFloor ( $I, $action='Withdraw' ) {
Subroutine STVFloor: called CC=10 SCAR=23.1
Pod undocumented
339 2 ▶ if ( $I->FloorRule() && $I->FloorThresshold() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($I->FloorRule and $I->FloorThresshold) 2 0 2
Condition33%1/3
$I->FloorRule and $I->FloorThresshold
!ll&&!rl&&r
002
MC/DC0%0/2
$I->FloorRule and $I->FloorThresshold
$I->FloorRule $I->FloorThresshold
Truth table33%1/3
A and B
A= $I->FloorRuleB= $I->FloorThresshold
ABresult
0X0
100
111
340 2 ▶ $I->FloorRule('ApprovalFloor') if $I->FloorRule() eq 'Approval';
Branch: 1 branch
BranchTrueFalseTotal
if $I->FloorRule eq "Approval" 1 1 2
341 2 ▶ $I->FloorRule('TopCountFloor') if $I->FloorRule() eq 'TopCount';
Branch: 1 branch
BranchTrueFalseTotal
if $I->FloorRule eq "TopCount" 1 1 2
342 2 my @withdrawn =();
343 2 my $newactive =
344 $I->ApplyFloor(
345 $I->FloorRule(),
346 $I->FloorThresshold()
347 );
348 2 for my $choice (sort $I->GetChoices()) {
349 24 ▶ unless( $newactive->{$choice}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newactive->{$choice}) 9 15 24
350 9 $I->$action( $choice );
351 9 push @withdrawn, $choice;
352 }
353 }
354 2 @withdrawn = sort (@withdrawn);
355 2 my $done = $action;
356 2 ▶ $done = 'Withdrawn' if $action eq 'Withdraw';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "Withdraw" 1 1 2
357 2 ▶ $done = 'Defeated' if $action eq 'Defeat';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "Defeat" 1 1 2
358 2 return @withdrawn;
359 }
360 }
361  
362 9 ▶sub SetQuota ($I, $style='droop') {
Subroutine SetQuota: called CC=3 SCAR=11.0
Pod documented
363 9 my $abandoned = $I->CountAbandoned();
364 9 my $abndnvotes = $abandoned->{'value_abandoned'};
365 9 my $cast = $I->BallotSet->{'votescast'};
366 9 my $numerator = ( $cast * $I->VoteValue ) - $abndnvotes;
367 9 my $denominator = $I->Seats();
368 9 my $adjust = 0;
369 9 ▶ if ( $style eq 'droop' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "droop") 7 2 9
370 7 $denominator++;
371 7 $adjust = 1;
372 }
373 9 return ( $adjust + int( $numerator / $denominator ) );
374 }
375  
376 =head1 NAME
377  
378 Vote::Count::Charge
379  
380 =head1 VERSION 2.02
381  
382 =cut
383  
384 # ABSTRACT: Vote::Charge - implementation of STV.
385  
386 =pod
387  
388 =head1 SYNOPSIS
389  
390 my $E = Vote::Count::Charge->new(
391 Seats => 3,
392 VoteValue => 1000,
393 BallotSet => read_ballots('t/data/data1.txt', ) );
394  
395 $E->Elect('SOMECHOICE');
396 $E->Charge('SOMECHOICE', $quota, $perCharge );
397 say E->GetChoiceStatus( 'CARAMEL'),
398 > { state => 'withdrawn', votes => 0 }
399  
400 =head1 Vote Charge implementation of Surplus Transfer
401  
402 Vote Charge is how Vote::Count implements Surplus Transfer. The wording is chosen to make the concept more accessible to a general audience. It also uses integer math and imposes truncation as the rounding rule.
403  
404 Vote Charge describes the process of Single Transferable Vote as:
405  
406 The Votes are assigned a value, based on the number of seats and the total value of all of the votes, a cost is determined for electing a choice. The votes supporting that choice are then charged to pay that cost. The remainder of the value for the vote, if any, is available for the next highest choice of the vote.
407  
408 When value is transferred back to the vote, Vote Charge refers to it as a Rebate.
409  
410 Vote Charge uses integer math and truncates all remainders. Setting the Vote Value is equivalent to setting a number of decimal places, a Vote Value of 100,000 is the same as a 5 decimal place precision.
411  
412 =head1 Description
413  
414 This module provides methods that can be shared between Charge implementations and does not present a complete tool for conducting STV elections. Look at the Methods that have been implemented as part of Vote::Count.
415  
416 =head1 Candidate / Choices States
417  
418 Single Transferable Vote rules have more states than Active, Eliminated and Elected. Not all methods need all of the possible states. The SetChoiceStatus method is not linked to the underlying Vote::Count objects Active Set, the action methods: Elect, Defeat, Suspend, Defer, Reinstate, Withdraw do update the Active Set.
419  
420 Active choices are referred to as Hopeful. The normal methods for accessing the Active list are available. Although not prevented from doing so, STV Methods should not directly set the active list, but rely on methods that manipulate candidate state. The VCUpdateActive method will sync the Active set with the STV choice states corresponding to active.
421  
422 =over
423  
424 =item *
425  
426 hopeful: The default active state of a choice.
427  
428 =item *
429  
430 withdrawn: A choice that will be treated as not present.
431  
432 =item *
433  
434 defeated: A choice that will no longer be considered for election.
435  
436 =item *
437  
438 deferred and suspended:
439  
440 A choice that is temporarily removed from consideration. Suspended is treated the same as Defeated, but is eligible for reinstatement. Deferred is removed from the ActiveSet, but treated as present when calculating Quota and Non-Continuing Votes.
441  
442 =item *
443  
444 elected and pending:
445  
446 Elected and Pending choices are removed from the Active Set, but Pending choices are not yet considered elected. The Pending state is available to hold newly elected choices for a method that will not immediately complete processing their election.
447  
448 =back
449  
450 =head3 GetChoiceStatus
451  
452 When called with the argument of a Choice, returns a hashref with the keys 'state' and 'votes'. When called without argument returns a hashref with the Choices as keys, and the values a hashref with the 'state' and 'votes' keys.
453  
454 =head3 SetChoiceStatus
455  
456 Takes the arguments of a Choice and a hashref with the keys 'state' and 'votes'. This method does not keep the underlying active list in Sync. Use either the targeted methods such as Suspend and Defeat or use VCUpdateActive to force the update.
457  
458 =head3 VCUpdateActive
459  
460 Update the ActiveSet of the underlying Vote::Count object to match the set of Choices that are currently 'hopeful'.
461  
462 =head2 Elected and Pending
463  
464 In addition to Elected, there is a Pending State. Pending means a Choice has reached the Quota, but not completed its Charges and Rebates. The distinction is for the benefit of methods that need choices held in pending, both Pending and Elected choices are removed from the active set.
465  
466 =head3 Elect, Elected
467  
468 Set Choice as Elected. Elected returns the list of currently elected choices.
469  
470 =head3 Pending
471  
472 Takes an Choice to set as Pending. Returns the list of Pending Choices.
473  
474 =head2 Eliminated: Withdrawn, Defeated, or Suspended
475  
476 In methods that set the Quota only once, choices eliminated before setting Quota are Withdrawn and may result in null ballots that can be exluded. Choices eliminated after setting Quota are Defeated. Some rules bring eliminated Choices back in later Rounds, Suspended distinguishes those eligible to return.
477  
478 =head3 Defeat, Defer, Withdraw, Suspend
479  
480 Perform the corresponding action for a Choice.
481  
482 $Election->Defeat('MARMALADE');
483  
484 =head3 Defeated, Deferred, Withdrawn, Suspended
485  
486 Returns a list of choices in that state.
487  
488 =head3 Reinstate
489  
490 Will reinstate all currently suspended choices or may be given a list of suspended choices that will be reinstated.
491  
492 =head2 STVRound, NextSTVRound
493  
494 STVRound returns the current Round, NextSTVRound advances the Round Counter and returns the new Round number.
495  
496 =head2 STVEvent
497  
498 Takes a reference as argument to add that reference to an Event History. This needs to be done seperately from logI<x> because STVEvent holds a list of data references instead of readably formatted events.
499  
500 =head2 WriteSTVEvent
501  
502 Writes JSON and YAML logs (path based on LogTo) of the STVEvents.
503  
504 =head2 SetQuota
505  
506 Calculate the Hare or Droop Quota. After the Division the result is rounded down and 1 is added to the result. The default is the Droop Quota, but either C<'hare'> or C<'droop'> may be requested as an optional parameter.
507  
508 my $droopquota = $Election->SetQuota();
509 my $harequota = $Election->SetQuota('hare');
510  
511 The Hare formula is Active Votes divided by number of Seats. Droop adds 1 to the number of seats, and to the result after rounding, resulting in a lower quota. The Droop Quota is the smallest for which it is impossible for more choices than the number of seats to reach the quota.
512  
513 =head2 Charge
514  
515 Charges Ballots for election of choice, parameters are $choice, $quota and $charge (defaults to VoteValue ).
516  
517 =head2 ResetVoteValue
518  
519 Resets all Ballots to their initial Vote Value.
520  
521 =head2 SeatsOpen
522  
523 Calculate and return the number of seats remaining to fill.
524  
525 =cut
526  
527 13 __PACKAGE__->meta->make_immutable;
528 13 1;
529  
530 #FOOTER
531  
532 =pod
533  
534 BUG TRACKER
535  
536 L<https://github.com/brainbuz/Vote-Count/issues>
537  
538 AUTHOR
539  
540 John Karr (BRAINBUZ) brainbuz@cpan.org
541  
542 CONTRIBUTORS
543  
544 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
545  
546 LICENSE
547  
548 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>.
549  
550 SUPPORT
551  
552 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
553  
554 =cut
555  
« Count/Borda.pm ↑ Summary Count.pm »