Count/TieBreaker.pm

filter: Statementstmt 98.4% 195 / 198 Branchbran 94.2% 66 / 70 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 29 / 29 Podpod 62.5% 5 / 8 totaltotal 96.8% 308 / 318 CC 52 SCAR 20.3
CC
52
Cov
97%
TieBreaker
17.0
TieBreakerGrandJunction
12.0
UnTieList
8.0
CRAP 7.6
« Count/Method/Cascade.pm ↑ Summary Count/Method/CondorcetDropping.pm »
1 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 39 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 39 ▶use feature qw /postderef signatures switch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Vote::Count::TieBreaker;
8 39 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 39 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use List::Util qw( min max sum );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # use Data::Dumper;
14 # use Data::Printer;
15 39 ▶use Vote::Count::RankCount;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶use List::Util qw( min max sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 39 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 39 our $VERSION='2.02';
21  
22 =head1 NAME
23  
24 Vote::Count::TieBreaker
25  
26 =head1 VERSION 2.02
27  
28 =head1 Synopsis
29  
30 my $Election = Vote::Count->new(
31 'BallotSet' => $ballotsirvtie2,
32 'TieBreakMethod' => 'approval',
33 'TieBreakerFallBackPrecedence' => 0,
34 );
35  
36 =cut
37  
38 # ABSTRACT: TieBreaker object for Vote::Count. Toolkit for vote counting.
39  
40 =head1 TieBreakMethods
41  
42 =head2 TieBreakMethod argement to new
43  
44 'approval'
45 'topcount' [ of just tied choices ]
46 'topcount_active' [ currently active choices ]
47 'all' [ eliminate all tied choices ]
48 'borda' [ Borda Count to current Active set ]
49 'borda_all' [ includes all choices in Borda Count ]
50 'grandjunction' [ more resolveable than simple TopCount would be ]
51 'none' [ eliminate no choices ]
52 'precedence' [ requires also setting PrecedenceFile ]
53  
54 Approval, TopCount, and Borda may be passed in either lower case or in the CamelCase form of the method name. borda_all calculates the Borda Count with all choices which can yield a different result than just the current choices. If you want TopCount to use all of the choices, or a snapshot such as after a floor rule, generate a Precedence File and then use that with Precedence as the Tie Breaker.
55  
56 =head2 (Modified) Grand Junction
57  
58 The Grand Junction (also known as Bucklin) method is one of the simplest and easiest to Hand Count RCV resolution methods. Other than that, it is generally not considered a good method.
59  
60 Because it is simple, and nearly always resolves, except when ballots are perfectly matched up, it is a great TieBreaker. It is not Later Harm Safe, but heavily favors higher rankings.
61  
62 =head3 The (Standard) Grand Junction Method
63  
64 Only the Tie-Breaker variant is currently implemented in Vote::Count.
65  
66 =over
67  
68 =item 1
69  
70 Count the Ballots to determine the quota for a majority.
71  
72 =item 2
73  
74 Count the first choices and elect a choice which has a majority.
75  
76 =item 3
77  
78 If there is no winner add the second choices to the totals and elect the choice which has a majority (or the most votes if more than one choice reaches a majority).
79  
80 =item 4
81  
82 Keep adding the next rank to the totals until either there is a winner or all ballots are exhausted.
83  
84 =item 5
85  
86 When all ballots are exhausted the choice with the highest total wins.
87  
88 =back
89  
90 =head3 As a Tie Breaker
91  
92 The Tie Breaker Method is modified.
93  
94 Instead of Majority, any choice with a current total less than another is eliminated. This allows resolution of any number of choices in a tie.
95  
96 The winner is the last choice remaining.
97  
98 =head3 TieBreakerGrandJunction
99  
100 my $resolve = $Election->TieBreakerGrandJunction( $choice1, $choice2 [ $choice3 ... ] );
101 if ( $resolve->{'winner'}) { say "Tie Winner is $resolve->{'winner'}"}
102 elsif ( $resolve->{'tie'}) {
103 my @tied = $resolve->{'tied'}->@*;
104 say "Still tied between @tied."
105 }
106  
107 The Tie Breaking will be logged to the verbose log, any number of tied choices may be provided.
108  
109 =head2 Changing Tie Breakers
110  
111 When Changing Tie Breakers or Precedence Files, the PairMatrix is not automatically updated. To update the PairMatrix it is necessary to call the UpdatePairMatrix Method.
112  
113 =cut
114  
115 39 has 'TieBreakMethod' => (
116 is => 'rw',
117 isa => 'Str',
118 required => 0,
119 );
120  
121 # This is only used for the precedence tiebreaker and fallback!
122 39 has 'PrecedenceFile' => (
123 is => 'rw',
124 isa => 'Str',
125 required => 0,
126 trigger => \&_triggercheckprecedence,
127 );
128  
129 39 has 'TieBreakerFallBackPrecedence' => (
130 is => 'rw',
131 isa => 'Bool',
132 default => 0,
133 lazy => 0,
134 trigger => \&_triggercheckprecedence,
135 );
136  
137 289 ▶sub _triggercheckprecedence ( $I, $new, $old = undef ) {
Subroutine _triggercheckprecedence: called CC=3 SCAR=11.0
138 289 ▶ unless ( $I->PrecedenceFile() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($I->PrecedenceFile) 53 236 289
139 53 $I->PrecedenceFile('/tmp/precedence.txt');
140 53 $I->logt( "Generated FallBack TieBreaker Precedence Order: \n"
141 . join( ', ', $I->CreatePrecedenceRandom() ) );
142 }
143 289 $I->{'PRECEDENCEORDER'} = undef; # clear cached if the file changes.
144 }
145  
146 ▶sub TieBreakerGrandJunction ( $self, @tiedchoices ) {
Subroutine TieBreakerGrandJunction: called CC=12 SCAR=24.8
Pod documented
147 70 my $ballots = $self->BallotSet()->{'ballots'};
148 70 my %current = ( map { $_ => 0 } @tiedchoices );
149 70 my $deepest = 0;
150 70 for my $b ( keys $ballots->%* ) {
151 756 my $depth = scalar $ballots->{$b}{'votes'}->@*;
152 756 ▶ $deepest = $depth if $depth > $deepest;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $deepest 168 588 756
153 }
154 70 my $round = 1;
155 70 ▶ while ( $round <= $deepest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($round <= $deepest) 134 17 151
156 134 $self->logv("Tie Breaker Round: $round");
157 134 for my $b ( keys $ballots->%* ) {
158 1416 ▶ my $pick = $ballots->{$b}{'votes'}[ $round - 1 ] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pick = $ballots->{$b}{"votes"}[$round - 1] or next 392 1024 1416
159 1024 ▶ if ( defined $current{$pick} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current{$pick}) 227 797 1024
160 227 $current{$pick} += $ballots->{$b}{'count'};
161 }
162 }
163 134 my $max = max( values %current );
164 134 for my $c ( sort @tiedchoices ) {
165 308 $self->logv("\t$c: $current{$c}");
166 }
167 134 for my $c ( sort @tiedchoices ) {
168 308 ▶ if ( $current{$c} < $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current{$c} < $max) 76 232 308
169 76 delete $current{$c};
170 76 $self->logv("Tie Breaker $c eliminated");
171 }
172 }
173 134 @tiedchoices = ( sort keys %current );
174 134 ▶ if ( 1 == @tiedchoices ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @tiedchoices) 53 81 134
175 53 $self->logv("Tie Breaker Won By: $tiedchoices[0]");
176 53 return { 'winner' => $tiedchoices[0], 'tie' => 0, 'tied' => [] };
177 }
178 81 $round++;
179 }
180 17 ▶ if ( $self->TieBreakerFallBackPrecedence() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->TieBreakerFallBackPrecedence) { } 4 13 17
181 4 $self->logv('Applying Precedence fallback');
182 4 return $self->TieBreakerPrecedence(@tiedchoices);
183 }
184 else {
185 13 return { 'winner' => 0, 'tie' => 1, 'tied' => \@tiedchoices };
186 }
187 }
188  
189 =head1 TieBreaker
190  
191 Implements some basic methods for resolving ties. The default value for IRV is eliminate 'all', and the default value for Matrix is eliminate 'none'. 'all' is inappropriate for Matrix, and 'none' is inappropriate for IRV.
192  
193 my @keep = $Election->TieBreaker( $tiebreaker, $active, @tiedchoices );
194  
195 TieBreaker returns a list containing the winner, if the method is 'all' the list is empty, if 'none' the original @tiedchoices list is returned. If the TieBreaker is a tie there will be multiple elements.
196  
197 =head1 Breaking Ties With Precedence
198  
199 Since many existing Elections Rules call for Random, and Vote::Count does not accept Random as the result will be different bewtween runs, Precedence allows the Administrators of an election to randomly or arbitrarily determine who will win ties before running Vote::Count.
200  
201 The Precedence list takes the choices of the election one per line. Choices defeat any choice later than them in the list. When Precedence is used an additional attribute must be specified for the Precedence List.
202  
203 my $Election = Vote::Count->new(
204 BallotSet => read_ballots('somefile'),
205 TieBreakMethod => 'precedence',
206 PrecedenceFile => '/path/to/precedencefile');
207  
208 =head2 Precedence (Method)
209  
210 Returns a Vote::Count::RankCount object from the Precedence List. Takes a HashRef of an Active set as an optional argument, defaults to the Current Active Set.
211  
212 my $RankCountByPrecedence = $Election->Precedence();
213 my $RankCountByPrecedence = $Election->Precedence( $active );
214  
215 =head2 CreatePrecedenceRandom
216  
217 Creates a Predictable Psuedo Random Precedence file, and returns the list. Randomizes the choices using the number of ballots as the Random Seed for Perl's built in rand() function. For any given Ballot File, it will always return the same list. If the precedence filename argument is not given it defaults to '/tmp/precedence.txt'. This is the best solution to use where the Rules call for Random, in a large election the number of ballots cast will be sufficiently random, while anyone with access to Perl can reproduce the Precedence file.
218  
219 # Generate a random precedence file
220 my @precedence = Vote::Count->new( BallotSet => read_ballots('somefile') )
221 ->CreatePrecedenceRandom( '/tmp/precedence.txt');
222 # Create a new Election with it.
223 my $Election = Vote::Count->new( BallotSet => read_ballots('somefile'),
224 PrecedenceFile => '/tmp/precedence.txt', TieBreakMethod => 'Precedence' );
225  
226 =head2 TieBreakerFallBackPrecedence
227  
228 This optional argument enables or disables using precedence as a fallback if the primary tiebreaker cannot break the tie. Generates /tmp/precedence.txt using CreatePrecedenceRandom if no PrecedenceFile is specified. Default is off (0).
229  
230 TieBreakMethod must be defined and may not be all or none.
231  
232 =head2 UnTieList
233  
234 Sort a list in an order determined by a ranking method, sorted in Descending Order. The ranking must be a method that returns a RankCount object: Borda, TopCount, Precedence and Approval. If the tie is not resolved it will fall back to Precedence.
235  
236 my @orderedlosers = $Election->UnTieList(
237 'ranking1' => $Election->TieBreakMethod(), 'tied' => \@unorderedlosers );
238  
239 A second method may be provided.
240  
241 my @orderedlosers = $Election->UnTieList(
242 'ranking1' => 'TopCount', 'ranking2' => 'Borda', 'tied' => \@unorderedlosers );
243  
244 This method requires that Precedence be enabled either by having enabled TieBreakerFallBackPrecedence or by setting the TieBreakMethod to Precedence.
245  
246 =head2 UnTieActive
247  
248 Produces a precedence list of all the active choices in the election. Passes the ranking1 and ranking2 arguments to UnTieList and the Active Set as the list to untie.
249  
250 my @untiedset = $Election->UnTieActive( 'ranking1' => 'TopCount', 'ranking2' => 'Approval');
251  
252 =head1 TopCount > Approval > Precedence
253  
254 Top Count > Approval > Precedence produces a fully resolveable Tie Breaker that will almost never fall back to Precedence. It makes sense to the voters and limits Later Harm by putting Top Count first. The Precedence order should be determined before counting, the old fashioned coffee can is great for this, or use CreatePrecedenceRandom.
255  
256 To apply Top Count > Approval > Precedence you need to start with a random Precedence File, Untie the choices, and switch Precedence Files:
257  
258 use Path::Tiny;
259 my $Election = Vote::Count->new(
260 BallotSet => read_ballots($ballots),
261 PrecedenceFile => $initial,
262 TieBreakMethod => 'Precedence',
263 );
264 # Create the new Precedence
265 my @newbreaker = $Election->UnTieActive(
266 'ranking1' => 'TopCount',
267 'ranking2' => 'Approval'
268 );
269 local $" = ' > '; # set list separator to >
270 $Election->logv("Setting Tie Break Order to: @newbreaker");
271 local $" = "\n"; # set list separator to new line.
272 path($newprecedence)->spew("@newbreaker");
273 $Election->PrecedenceFile($newprecedence);
274 $Election->UpdatePairMatrix();
275  
276 =cut
277  
278 ▶sub _precedence_sort ( $I, @list ) {
Subroutine _precedence_sort: called CC=5 SCAR=16.1
279 312 my %ordered = ();
280 312 my $start = 0;
281 312 ▶ if ( defined $I->{'PRECEDENCEORDER'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $I->{'PRECEDENCEORDER'}) { } 287 25 312
282 287 %ordered = $I->{'PRECEDENCEORDER'}->%*;
283 }
284 else {
285 25 for ( split /\n/, path( $I->PrecedenceFile() )->slurp() ) {
286 253 $_ =~ s/\s//g; #strip out any accidental white space
287 253 $ordered{$_} = ++$start;
288 }
289 25 for my $c ( $I->GetChoices ) {
290 253 ▶ unless ( defined $ordered{$c} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ordered{$c}) 0 253 253
291 0 croak "Choice $c missing from precedence file\n";
292 }
293 }
294 25 $I->{'PRECEDENCEORDER'} = \%ordered;
295 }
296 312 my %L = map { $ordered{$_} => $_ } @list;
297 312 return ( map { $L{$_} } ( sort { $a <=> $b } keys %L ) );
298 }
299  
300 ▶sub TieBreakerPrecedence ( $I, @tiedchoices ) {
Subroutine TieBreakerPrecedence: called CC=1 SCAR=0.0
Pod undocumented
301 143 my @list = $I->_precedence_sort(@tiedchoices);
302 143 return { 'winner' => $list[0], 'tie' => 0, 'tied' => [] };
303 }
304  
305 88 ▶sub CreatePrecedenceRandom ( $I, $outfile = '/tmp/precedence.txt' ) {
Subroutine CreatePrecedenceRandom: called CC=4 SCAR=13.9
Pod documented
306 88 my @choices = $I->GetActiveList();
307 88 my %randomized = ();
308 88 srand( $I->BallotSet()->{'votescast'} );
309 88 ▶ while (@choices) {
Branch: 1 branch
BranchTrueFalseTotal
if (@choices) 709 88 797
310 709 my $next = shift @choices;
311 709 my $random = int( rand(1000000) );
312 709 ▶ if ( defined $randomized{$random} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $randomized{$random}) { } 0 709 709
313 # collision, this choice needs to do again.
314 0 unshift @choices, ($next);
315 }
316 else {
317 709 $randomized{$random} = $next;
318 }
319 }
320 my @precedence =
321 88 ( map { $randomized{$_} } sort { $a <=> $b } ( keys %randomized ) );
322 88 path($outfile)->spew( join( "\n", @precedence ) . "\n" );
323 88 $I->PrecedenceFile( $outfile );
324 88 return @precedence;
325 }
326  
327 ▶sub TieBreaker ( $I, $tiebreaker, $active, @tiedchoices ) {
Subroutine TieBreaker: called CC=17 SCAR=28.3
Pod undocumented
328 39 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
329 417 $tiebreaker = lc $tiebreaker;
330 417 ▶ if ( $tiebreaker eq 'none' ) { return @tiedchoices }
Branch: 1 branch
BranchTrueFalseTotal
if ($tiebreaker eq "none") 157 260 417
331 260 ▶ if ( $tiebreaker eq 'all' ) { return () }
Branch: 1 branch
BranchTrueFalseTotal
if ($tiebreaker eq "all") 17 243 260
332 243 my $choices_hashref = { map { $_ => 1 } @tiedchoices };
333 243 my $ranked = undef;
334 243 ▶ if ( $tiebreaker eq 'borda' ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($tiebreaker eq 'borda') { } 3 240 243
elsif ($tiebreaker eq 'borda_all') { } 3 237 240
elsif ($tiebreaker eq 'approval') { } 66 171 237
elsif ($tiebreaker eq 'topcount') { } 2 169 171
elsif ($tiebreaker eq 'topcount_active') { } 2 167 169
elsif ($tiebreaker eq 'grandjunction') { } 64 103 167
elsif ($tiebreaker eq 'precedence') { } 102 1 103
335 3 $ranked = $I->Borda($active);
336 }
337 elsif ( $tiebreaker eq 'borda_all' ) {
338 3 $ranked = $I->Borda( $I->BallotSet()->{'choices'} );
339 }
340 elsif ( $tiebreaker eq 'approval' ) {
341 66 $ranked = $I->Approval($choices_hashref);
342 }
343 elsif ( $tiebreaker eq 'topcount' ) {
344 2 $ranked = $I->TopCount($choices_hashref);
345 }
346 elsif ( $tiebreaker eq 'topcount_active' ) {
347 2 $ranked = $I->TopCount($active);
348 }
349 elsif ( $tiebreaker eq 'grandjunction' ) {
350 64 my $GJ = $I->TieBreakerGrandJunction(@tiedchoices);
351 64 ▶ if ( $GJ->{'winner'} ) { return $GJ->{'winner'} }
Branch: 2 branches
BranchTrueFalseTotal
if ($GJ->{'winner'}) { } 52 12 64
elsif ($GJ->{'tie'}) { } 12 0 12
352 12 elsif ( $GJ->{'tie'} ) { return $GJ->{'tied'}->@* }
353 0 else { croak "unexpected (or no) result from $tiebreaker!\n" }
354 }
355 elsif ( $tiebreaker eq 'precedence' ) {
356 # The one nice thing about precedence is that there is always a winner.
357 102 return $I->TieBreakerPrecedence(@tiedchoices)->{'winner'};
358 }
359 1 else { croak "undefined tiebreak method $tiebreaker!\n" }
360 76 my @highchoice = ();
361 76 my $highest = 0;
362 76 my $counted = $ranked->RawCount();
363 76 for my $c (@tiedchoices) {
364 178 ▶ if ( $counted->{$c} > $highest ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($counted->{$c} > $highest) { } 85 93 178
elsif ($counted->{$c} == $highest) { } 80 13 93
365 85 @highchoice = ($c);
366 85 $highest = $counted->{$c};
367 }
368 elsif ( $counted->{$c} == $highest ) {
369 80 push @highchoice, $c;
370 }
371 }
372 76 my $terse =
373 "Tie Breaker $tiebreaker: "
374 . join( ', ', @tiedchoices )
375 . "\nwinner(s): "
376 . join( ', ', @highchoice );
377 76 $I->{'last_tiebreaker'} = {
378 'terse' => $terse,
379 'verbose' => $ranked->RankTable(),
380 };
381 76 ▶ if ( @highchoice > 1 && $I->TieBreakerFallBackPrecedence() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@highchoice > 1 and $I->TieBreakerFallBackPrecedence) 36 40 76
Condition100%3/3
@highchoice > 1 and $I->TieBreakerFallBackPrecedence
!ll&&!rl&&r
221836
MC/DC100%2/2
@highchoice > 1 and $I->TieBreakerFallBackPrecedence
@highchoice > 1 $I->TieBreakerFallBackPrecedence
Truth table100%3/3
A and B
A= @highchoice > 1B= $I->TieBreakerFallBackPrecedence
ABresult
0X0
100
111
382 36 my $winner = $I->TieBreakerPrecedence(@tiedchoices)->{'winner'};
383 36 $I->{'last_tiebreaker'}{'terse'} .= "\nWinner by Precedence: $winner";
384 36 return ( $winner );
385 }
386 40 return (@highchoice);
387 }
388  
389 39 ▶sub Precedence ( $I, $active = undef ) {
Subroutine Precedence: called CC=3 SCAR=11.0
Pod documented
390 39 ▶ $active = $I->Active() unless defined $active;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $active 3 36 39
391 39 return Vote::Count::RankCount->newFromList(
392 $I->_precedence_sort( keys( $active->%* ) ) );
393 }
394  
395 20 ▶sub precedence { Precedence(@_) }
Subroutine precedence: called CC=1 SCAR=0.0
Pod undocumented
396  
397 ▶sub _shortuntie ( $I, $RC, @tied ) {
Subroutine _shortuntie: called CC=5 SCAR=16.1
398 44 my %T = map { $_ => $RC->{$_} } @tied;
399 44 my @order = ();
400 44 ▶ while ( keys %T ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %T) 101 44 145
401 101 my $best = min values %T;
402 101 my @leaders = ();
403 101 for my $leader ( keys %T ) {
404 275 ▶ push @leaders, $leader if $T{$leader} == $best;
Branch: 1 branch
BranchTrueFalseTotal
if $T{$leader} == $best 144 131 275
405 }
406 101 @leaders = $I->_precedence_sort(@leaders);
407 101 push @order, @leaders;
408 101 for (@leaders) { delete $T{$_} }
409 }
410 44 return @order;
411 }
412  
413 ▶sub UnTieList ( $I, %args ) {
Subroutine UnTieList: called CC=8 SCAR=20.8
Pod documented
414 39 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
415 58 ▶ unless ( $I->TieBreakerFallBackPrecedence()
Branch: 1 branch
BranchTrueFalseTotal
unless ($I->TieBreakerFallBackPrecedence or lc $I->TieBreakMethod eq "precedence") 3 55 58
Condition100%3/3
$I->TieBreakerFallBackPrecedence or lc $I->TieBreakMethod eq "precedence"
l!l&&r!l&&!r
18373
MC/DC100%2/2
$I->TieBreakerFallBackPrecedence or lc $I->TieBreakMethod eq "precedence"
$I->TieBreakerFallBackPrecedence lc $I->TieBreakMethod eq "precedence"
Truth table100%3/3
A or B
A= $I->TieBreakerFallBackPrecedenceB= lc $I->TieBreakMethod eq "precedence"
ABresult
1X1
011
000
416 or lc($I->TieBreakMethod) eq 'precedence' )
417 {
418 3 croak
419 "TieBreakerFallBackPrecedence must be enabled or TieBreakMethod must be precedence to use UnTieList [UnTieActive and BottomRunOff call it]";
420 }
421 55 my $ranking1 = $args{ranking1} ;
422 55 ▶ my $ranking2 = $args{ranking2} || 'Precedence';
Condition100%2/2
$args{'ranking2'} || 'Precedence'
l!l
4312
MC/DC100%1/1
$args{'ranking2'} || 'Precedence'
$args{'ranking2'}
Truth table100%2/2
A
A= $args{'ranking2'}
Aresult
11
00
423 55 my @tied = $args{tied}->@*;
424 55 my %tieactive = map { $_ => 1 } @tied;
425  
426 55 my @ordered = ();
427 55 ▶ return $I->_precedence_sort(@tied) if ( lc($ranking1) eq 'precedence' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $ranking1 eq "precedence" 3 52 55
428 52 ▶ my $RC1 = try { $I->$ranking1( \%tieactive )->HashByRank() }
Subroutine __ANON__: called CC=1 SCAR=0.0
429 catch {
430 2 ▶ my $mthstr = $ranking1 ? $ranking1 : "missing ranking1 . methods $ranking1 ? $ranking2 ";
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ranking1 ? : 1 1 2
431 2 croak "Unable to rank choices by $mthstr."
432 52 };
433 50 ▶ my $RC2 = try {$I->$ranking2( \%tieactive )->HashWithOrder() }
Subroutine __ANON__: called CC=1 SCAR=0.0
434 catch {
435 1 ▶ my $mthstr = $ranking2 ? $ranking2 : "missing ranking2 . methods $ranking1 ? $ranking2 ";
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ranking2 ? : 1 0 1
436 1 croak "Unable to rank choices by $mthstr."
437 50 };
438 49 for my $level ( sort { $a <=> $b } ( keys $RC1->%* ) ) {
439 181 my @l = @{ $RC1->{$level} };
440 181 my @suborder = ();
441 181 ▶ if ( 1 == $RC1->{$level}->@* ) { @suborder = @l }
Branch: 2 branches
BranchTrueFalseTotal
if (1 == @{$RC1->{$level}}) { } 111 70 181
elsif ($ranking2 eq 'precedence') { } 26 44 70
442 elsif ( $ranking2 eq 'precedence' ) {
443 26 @suborder = $I->_precedence_sort(@l);
444 }
445 else {
446 44 @suborder = $I->_shortuntie( $RC2, @l );
447 }
448 181 push @ordered, @suborder;
449 }
450 49 return @ordered;
451 }
452  
453 ▶sub UnTieActive ( $I, %ARGS ) {
Subroutine UnTieActive: called CC=1 SCAR=0.0
Pod documented
454 10 $ARGS{'tied'} = [ $I->GetActiveList() ];
455 10 $I->UnTieList( %ARGS );
456 }
457  
458 39 1;
459  
460 #FOOTER
461  
462 =pod
463  
464 BUG TRACKER
465  
466 L<https://github.com/brainbuz/Vote-Count/issues>
467  
468 AUTHOR
469  
470 John Karr (BRAINBUZ) brainbuz@cpan.org
471  
472 CONTRIBUTORS
473  
474 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
475  
476 LICENSE
477  
478 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>.
479  
480 SUPPORT
481  
482 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
483  
484 =cut
485  
« Count/Method/Cascade.pm ↑ Summary Count/Method/CondorcetDropping.pm »