Count/Method/CondorcetDropping.pm

filter: Statementstmt 94.9% 113 / 119 Branchbran 87.5% 35 / 40 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 92.1% 165 / 179 CC 27 SCAR 19.4
CC
27
Cov
93%
RunCondorcetDropping
13.1
_logstart
6.4
GetRound
6.0
CRAP 7.0
« Count/TieBreaker.pm ↑ Summary Count/Method/CondorcetVsIRV.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 4 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Vote::Count::Method::CondorcetDropping;
7  
8 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 extends 'Vote::Count';
11  
12 4 our $VERSION='2.02';
13  
14 =head1 NAME
15  
16 Vote::Count::Method::CondorcetDropping
17  
18 =head1 VERSION 2.02
19  
20 =cut
21  
22 # ABSTRACT: Methods which use simple dropping rules to resolve a Winner-less Condorcet Matrix.
23  
24 =pod
25  
26 =head1 Method Description for Simple Dropping
27  
28 Simple Dropping eliminates the I<weakest> choice until there is a Condorcet Winner. This method is simple and widely used.
29  
30 =head1 SYNOPSIS
31  
32 my $CondorcetElection =
33 Vote::Count::Method::CondorcetDropping->new(
34 'BallotSet' => $ballotset ,
35 'DropStyle' => 'all', # default = leastwins
36 'DropRule' => 'topcount', # default
37 'TieBreakMethod' => 'none', # default
38 );
39  
40 my $Winner = $CondorcetElection->RunCondorcetDropping( $ActiveSet )->{'winner'};
41  
42 =head1 RunCondorcetDropping
43  
44 Takes an optional parameter of an Active Set as a HashRef. Returns a HashRef with the standard result keys: winner, tie, tied. Writes details to the Vote::Count logs.
45  
46 =head1 Dropping Options
47  
48 =head2 DropStyle
49  
50 Set DropStyle to 'all' for dropping against all choices or 'leastwins' to only consider those choices.
51  
52 Default is leastwins.
53  
54 =head2 DropRule
55  
56 Determines the rule by which choices will be eliminated when there is no Condorcet Winner. Supported Dropping Rules are: 'borda' count (with all the attendant weighting issues), 'approval', 'topcount' ('plurality'), and 'greatestloss'.
57  
58 default is plurality (topcount)
59  
60 =head2 SkipLoserDrop
61  
62 Normally RunCondorcetDropping eliminates Condorcet Losers whenever they are discovered. Dropping Condorcet Losers will be skipped if set to 1.
63  
64 =head1 Benham
65  
66 This method modifies IRV by checking for a Condorcet Winner each round, and then drops the low choice as regular IRV. It is probably the most widely used Condorcet Method for Hand Counting because it does not require a full matrix. For each choice it is only required to determine if they lose to any of the other active choices. By Counting Approval at the beginning, it is often possible to determine that a choice will lose at least one pairing without conducting any pairings, then it is only necessary to check choices that possibly have no losses.
67  
68 This method is fairly simple, and meets Condorcet Winner/Loser, but fails LNH, and inherits IRV's failings on consistency. BTR-IRV is even easier to Hand Count and is Smith Compliant, Benham has no obvious advantage to it, other than having been used more widely in the past.
69  
70 The original method specified Random for Tie Breaker, which can be done in a reproducable manner with L<Precedence|Vote::Count::TieBreaker/Precedence>.
71  
72 The following example implements Benham, resolving ties with a precedence file generated using the number of ballots cast as the random seed.
73  
74 my $Benham = Vote::Count::Method::CondorcetDropping->new(
75 'BallotSet' => $someballotset,
76 'DropStyle' => 'all',
77 'DropRule' => 'topcount',
78 'SkipLoserDrop' => 1,
79 'TieBreakMethod' => 'precedence',
80 'PrecedenceFile' => '/tmp/benhamties.txt',
81 );
82 $Benham->CreatePrecedenceRandom( '/tmp/benhamties.txt' );
83 my $Result = $Benham->RunCondorcetDropping();
84  
85 =cut
86  
87 4 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 # use List::Util qw( min max );
89 # use YAML::XS;
90  
91 4 ▶use Vote::Count::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 # use Try::Tiny;
94 # use Data::Dumper;
95  
96 4 has 'Matrix' => (
97 isa => 'Object',
98 is => 'ro',
99 lazy => 1,
100 builder => '_newmatrix',
101 );
102  
103 # DropStyle: whether to apply drop rule against
104 # all choices ('all') or the least winning ('leastwins').
105 4 has 'DropStyle' => (
106 isa => 'Str',
107 is => 'ro',
108 default => 'leastwins',
109 );
110  
111 4 has 'DropRule' => (
112 isa => 'Str',
113 is => 'ro',
114 default => 'plurality',
115 );
116  
117 4 has 'SkipLoserDrop' => (
118 isa => 'Int',
119 is => 'ro',
120 default => 0,
121 );
122  
123 30 ▶sub GetRound ( $self, $active, $roundnum = '' ) {
Subroutine GetRound: called CC=6 SCAR=17.9
Pod undocumented
124 30 my $rule = lc( $self->DropRule() );
125 30 ▶ if ( $rule =~ m/(plurality|topcount)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($rule =~ /(plurality|topcount)/u) { } 17 13 30
elsif ($rule eq 'approval') { } 3 10 13
elsif ($rule eq 'borda') { } 8 2 10
elsif ($rule eq 'greatestloss') { } 1 1 2
126 17 return $self->TopCount($active);
127 }
128 elsif ( $rule eq 'approval' ) {
129 3 my $round = $self->Approval($active);
130 3 $self->logv( "Round $roundnum Approval Totals ", $round->RankTable() );
131 3 return $round;
132 }
133 elsif ( $rule eq 'borda' ) {
134 8 my $round = $self->Borda($active);
135 8 $self->logv( "Round $roundnum Borda Count ", $round->RankTable() );
136 8 return $round;
137 }
138 elsif ( $rule eq 'greatestloss' ) {
139 1 return $self->Matrix()->RankGreatestLoss($active);
140 }
141 else {
142 1 croak "undefined dropping rule $rule requested";
143 }
144 }
145  
146 ▶sub DropChoice ( $self, $round, @jeapardy ) {
Subroutine DropChoice: called CC=5 SCAR=16.1
Pod undocumented
147 9 my %roundvotes = $round->RawCount()->%*;
148 9 my @eliminate = ();
149 9 my $lowest = $round->CountVotes();
150 9 for my $j (@jeapardy) {
151 28 ▶ $lowest = $roundvotes{$j} if $roundvotes{$j} < $lowest;
Branch: 1 branch
BranchTrueFalseTotal
if $roundvotes{$j} < $lowest 15 13 28
152 }
153 9 for my $j (@jeapardy) {
154 28 ▶ if ( $roundvotes{$j} == $lowest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roundvotes{$j} == $lowest) 11 17 28
155 11 push @eliminate, $j;
156 }
157 }
158 9 return @eliminate;
159 }
160  
161 ▶sub _newmatrix ($self) {
Subroutine _newmatrix: called CC=1 SCAR=0.0
162 2 return Vote::Count::Matrix->new(
163 'BallotSet' => $self->BallotSet(),
164 Active => $self->Active()
165 );
166 }
167  
168 ▶sub _logstart ( $self, $active ) {
Subroutine _logstart: called CC=6 SCAR=18.5
169 10 my $dropdescription = 'Elimination Rule is Applied to All Active Choices.';
170 10 ▶ if ( $self->DropStyle eq 'leastwins' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->DropStyle eq "leastwins") 3 7 10
171 3 $dropdescription =
172 'Elimination Rule is Applied to only Choices with the Fewest Wins.';
173 }
174 10 my $rule = '';
175 10 ▶ if ( $self->DropRule() =~ m/(plurality|topcount)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->DropRule =~ /(plurality|topcount)/u) { } 7 3 10
elsif ($self->DropRule eq 'approval') { } 1 2 3
elsif ($self->DropRule eq 'borda') { } 2 0 2
elsif ($self->DropRule eq 'greatestloss') { } 0 0 0
176 7 $rule = "Drop the Choice With the Lowest TopCount.";
177 }
178 elsif ( $self->DropRule() eq 'approval' ) {
179 1 $rule = "Drop the Choice With the Lowest Approval.";
180 }
181 elsif ( $self->DropRule() eq 'borda' ) {
182 2 $rule = "Drop the Choice With the Lowest Borda Score.";
183 }
184 elsif ( $self->DropRule() eq 'greatestloss' ) {
185 0 $rule = "Drop the Choice With the Greatest Loss.";
186 }
187 else {
188 0 croak "undefined dropping rule $rule requested";
189 }
190 $self->logt( 'CONDORCET SEQUENTIAL DROPPING METHOD',
191 10 'CHOICES:', join( ', ', ( sort keys %{$active} ) ) );
192 10 $self->logv( "Elimination Rule: $rule", $dropdescription );
193 }
194  
195 10 ▶sub RunCondorcetDropping ( $self, $active = undef ) {
Subroutine RunCondorcetDropping: called CC=13 SCAR=25.7
Pod undocumented
196 10 ▶ unless ( defined $active ) { $active = $self->Active() }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $active) 9 1 10
197 10 my $roundctr = 0;
198 10 my $maxround = scalar( keys %{$active} );
199 10 $self->_logstart($active);
200 10 my $result = { tie => 0, tied => undef, winner => 0 };
201 DROPLOOP:
202 10 until (0) {
203 25 $roundctr++;
204 25 ▶ die "DROPLOOP infinite stopped at $roundctr" if $roundctr > $maxround;
Branch: 1 branch
BranchTrueFalseTotal
if $roundctr > $maxround 0 25 25
205 25 my $topcount = $self->TopCount($active);
206 25 my $round = $self->GetRound( $active, $roundctr );
207 25 $self->logv( '---', "Round $roundctr TopCount", $topcount->RankTable() );
208 25 my $majority = $self->EvaluateTopCountMajority($topcount);
209 25 ▶ if ( defined $majority->{'winner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $majority->{"winner"}) 7 18 25
210 7 $result->{'winner'} = $majority->{'winner'};
211 7 last DROPLOOP;
212 }
213 18 my $matrix = Vote::Count::Matrix->new(
214 'BallotSet' => $self->BallotSet,
215 'Active' => $active
216 );
217 18 $self->logv( '---', "Round $roundctr Pairings", $matrix->MatrixTable() );
218 18 ▶ my $cw = $matrix->CondorcetWinner() || 0;
Condition100%2/2
$matrix->CondorcetWinner || 0
l!l
216
MC/DC100%1/1
$matrix->CondorcetWinner || 0
$matrix->CondorcetWinner
Truth table100%2/2
A
A= $matrix->CondorcetWinner
Aresult
11
00
219 18 ▶ if ($cw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cw) 2 16 18
220 2 my $wstr = "* Winner $cw *";
221 2 my $rpt = length($wstr);
222 2 $self->logt( '*' x $rpt, $wstr, '*' x $rpt );
223 2 $result->{'winner'} = $cw;
224 2 last DROPLOOP;
225 }
226 16 ▶ my $eliminated =
Branch: 1 branch
BranchTrueFalseTotal
$self->SkipLoserDrop ? : 1 15 16
227 $self->SkipLoserDrop()
228 ? { 'eliminations' => 0 }
229 : $matrix->CondorcetLoser();
230 16 ▶ if ( $eliminated->{'eliminations'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eliminated->{"eliminations"}) 7 9 16
231 # tracking active between iterations of matrix.
232 7 $active = $matrix->Active();
233 7 $self->logv( $eliminated->{'verbose'} );
234 # active changed, restart loop
235 7 next DROPLOOP;
236 }
237 9 my @jeapardy = ();
238 9 ▶ if ( $self->DropStyle eq 'leastwins' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->DropStyle eq 'leastwins') { } 3 6 9
239 3 @jeapardy = $matrix->LeastWins();
240 }
241 6 else { @jeapardy = keys %{$active} }
242 9 for my $goodbye ( $self->DropChoice( $round, @jeapardy ) ) {
243 11 delete $active->{$goodbye};
244 11 $self->logv("Eliminating $goodbye");
245 }
246 9 my @remaining = keys $active->%*;
247 9 ▶ if ( @remaining == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@remaining == 0) { } 0 9 9
elsif (@remaining == 1) { } 1 8 9
248 0 $self->logt(
249 "All remaining Choices would be eliminated, Tie between @jeapardy");
250 0 $result->{'tie'} = 1;
251 0 $result->{'tied'} = \@jeapardy;
252 0 last DROPLOOP;
253 }
254 elsif ( @remaining == 1 ) {
255 1 my $winner = $remaining[0];
256 1 $self->logt( "Only 1 choice remains.", "** WINNER : $winner **" );
257 1 $result->{'winner'} = $winner;
258 1 last DROPLOOP;
259 }
260 }; #infinite DROPLOOP
261 10 return $result;
262 }
263  
264 4 1;
265  
266 #FOOTER
267  
268 =pod
269  
270 BUG TRACKER
271  
272 L<https://github.com/brainbuz/Vote-Count/issues>
273  
274 AUTHOR
275  
276 John Karr (BRAINBUZ) brainbuz@cpan.org
277  
278 CONTRIBUTORS
279  
280 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
281  
282 LICENSE
283  
284 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>.
285  
286 SUPPORT
287  
288 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
289  
290 =cut
291  
« Count/TieBreaker.pm ↑ Summary Count/Method/CondorcetVsIRV.pm »