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.022;
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='1.10';
13  
14 =head1 NAME
15  
16 Vote::Count::Method::CondorcetDropping
17  
18 =head1 VERSION 1.10
19  
20 =cut
21  
22 # ABSTRACT: Methods which use simple dropping rules to resolve a Winnerless Condorcet Matrix.
23  
24 =pod
25  
26 =head1 SYNOPSIS
27  
28 my $CondorcetElection =
29 Vote::Count::Method::CondorcetDropping->new(
30 'BallotSet' => $ballotset ,
31 'DropStyle' => 'all',
32 'DropRule' => 'topcount',
33 );
34  
35 my $Winner = $CondorcetElection->RunCondorcetDropping( $SmithSet )->{'winner'};
36  
37 =head1 Condorcet Dropping Methods
38  
39 This module implements dropping methodologies for resolving a Condorcet Matrix with no Winner. Dropping Methodologies apply a rule to either all remaining choices or to those with the least wins to select a choice for elimination.
40  
41 =head2 Basic Dropping Methods
42  
43 Supported Dropping Methods are: 'borda' count (with all the attendant weighting issues), 'approval', 'topcount' ('plurality'), and 'greatestloss'.
44  
45 =head2 Option SkipLoserDrop
46  
47 Normally RunCondorcetDropping eliminates Condorcet Losers whenever they are discovered. However, the Benham method, which is probably the most widely used method that uses simple dropping, only considers Condorcet Winners.
48  
49 =cut
50  
51 4 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 # use List::Util qw( min max );
53 # use YAML::XS;
54  
55 4 ▶use Vote::Count::Matrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 # use Try::Tiny;
58 # use Text::Table::Tiny 'generate_table';
59 # use Data::Dumper;
60  
61 4 has 'Matrix' => (
62 isa => 'Object',
63 is => 'ro',
64 lazy => 1,
65 builder => '_newmatrix',
66 );
67  
68 # DropStyle: whether to apply drop rule against
69 # all choices ('all') or the least winning ('leastwins').
70 4 has 'DropStyle' => (
71 isa => 'Str',
72 is => 'ro',
73 default => 'leastwins',
74 );
75  
76 4 has 'DropRule' => (
77 isa => 'Str',
78 is => 'ro',
79 default => 'plurality',
80 );
81  
82 4 has 'SkipLoserDrop' => (
83 isa => 'Int',
84 is => 'ro',
85 default => 0,
86 );
87  
88 30 ▶sub GetRound ( $self, $active, $roundnum = '' ) {
Subroutine GetRound: called CC=6 SCAR=17.9
Pod undocumented
89 30 my $rule = lc( $self->DropRule() );
90 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
91 17 return $self->TopCount($active);
92 }
93 elsif ( $rule eq 'approval' ) {
94 3 my $round = $self->Approval($active);
95 3 $self->logv( "Round $roundnum Approval Totals ", $round->RankTable() );
96 3 return $round;
97 }
98 elsif ( $rule eq 'borda' ) {
99 8 my $round = $self->Borda($active);
100 8 $self->logv( "Round $roundnum Borda Count ", $round->RankTable() );
101 8 return $round;
102 }
103 elsif ( $rule eq 'greatestloss' ) {
104 1 return $self->Matrix()->RankGreatestLoss($active);
105 }
106 else {
107 1 croak "undefined dropping rule $rule requested";
108 }
109 }
110  
111 ▶sub DropChoice ( $self, $round, @jeapardy ) {
Subroutine DropChoice: called CC=5 SCAR=16.1
Pod undocumented
112 9 my %roundvotes = $round->RawCount()->%*;
113 9 my @eliminate = ();
114 9 my $lowest = $round->CountVotes();
115 9 for my $j (@jeapardy) {
116 28 ▶ $lowest = $roundvotes{$j} if $roundvotes{$j} < $lowest;
Branch: 1 branch
BranchTrueFalseTotal
if $roundvotes{$j} < $lowest 17 11 28
117 }
118 9 for my $j (@jeapardy) {
119 28 ▶ if ( $roundvotes{$j} == $lowest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roundvotes{$j} == $lowest) 11 17 28
120 11 push @eliminate, $j;
121 }
122 }
123 9 return @eliminate;
124 }
125  
126 ▶sub _newmatrix ($self) {
Subroutine _newmatrix: called CC=1 SCAR=0.0
127 2 return Vote::Count::Matrix->new(
128 'BallotSet' => $self->BallotSet(),
129 Active => $self->Active()
130 );
131 }
132  
133 ▶sub _logstart ( $self, $active ) {
Subroutine _logstart: called CC=6 SCAR=18.5
134 10 my $dropdescription = 'Elimination Rule is Applied to All Active Choices.';
135 10 ▶ if ( $self->DropStyle eq 'leastwins' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->DropStyle eq "leastwins") 3 7 10
136 3 $dropdescription =
137 'Elimination Rule is Applied to only Choices with the Fewest Wins.';
138 }
139 10 my $rule = '';
140 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
141 7 $rule = "Drop the Choice With the Lowest TopCount.";
142 }
143 elsif ( $self->DropRule() eq 'approval' ) {
144 1 $rule = "Drop the Choice With the Lowest Approval.";
145 }
146 elsif ( $self->DropRule() eq 'borda' ) {
147 2 $rule = "Drop the Choice With the Lowest Borda Score.";
148 }
149 elsif ( $self->DropRule() eq 'greatestloss' ) {
150 0 $rule = "Drop the Choice With the Greatest Loss.";
151 }
152 else {
153 0 croak "undefined dropping rule $rule requested";
154 }
155 $self->logt( 'CONDORCET SEQUENTIAL DROPPING METHOD',
156 10 'CHOICES:', join( ', ', ( sort keys %{$active} ) ) );
157 10 $self->logv( "Elimination Rule: $rule", $dropdescription );
158 }
159  
160 10 ▶sub RunCondorcetDropping ( $self, $active = undef ) {
Subroutine RunCondorcetDropping: called CC=13 SCAR=25.7
Pod undocumented
161 10 ▶ unless ( defined $active ) { $active = $self->Active() }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $active) 9 1 10
162 10 my $roundctr = 0;
163 10 my $maxround = scalar( keys %{$active} );
164 10 $self->_logstart($active);
165 10 my $result = { tie => 0, tied => undef, winner => 0 };
166 DROPLOOP:
167 10 until (0) {
168 25 $roundctr++;
169 25 ▶ die "DROPLOOP infinite stopped at $roundctr" if $roundctr > $maxround;
Branch: 1 branch
BranchTrueFalseTotal
if $roundctr > $maxround 0 25 25
170 25 my $topcount = $self->TopCount($active);
171 25 my $round = $self->GetRound( $active, $roundctr );
172 25 $self->logv( '---', "Round $roundctr TopCount", $topcount->RankTable() );
173 25 my $majority = $self->EvaluateTopCountMajority($topcount);
174 25 ▶ if ( defined $majority->{'winner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $majority->{"winner"}) 7 18 25
175 7 $result->{'winner'} = $majority->{'winner'};
176 7 last DROPLOOP;
177 }
178 18 my $matrix = Vote::Count::Matrix->new(
179 'BallotSet' => $self->BallotSet,
180 'Active' => $active
181 );
182 18 $self->logv( '---', "Round $roundctr Pairings", $matrix->MatrixTable() );
183 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
184 18 ▶ if ($cw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cw) 2 16 18
185 2 my $wstr = "* Winner $cw *";
186 2 my $rpt = length($wstr);
187 2 $self->logt( '*' x $rpt, $wstr, '*' x $rpt );
188 2 $result->{'winner'} = $cw;
189 2 last DROPLOOP;
190 }
191 16 ▶ my $eliminated =
Branch: 1 branch
BranchTrueFalseTotal
$self->SkipLoserDrop ? : 1 15 16
192 $self->SkipLoserDrop()
193 ? { 'eliminations' => 0 }
194 : $matrix->CondorcetLoser();
195 16 ▶ if ( $eliminated->{'eliminations'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eliminated->{"eliminations"}) 7 9 16
196 # tracking active between iterations of matrix.
197 7 $active = $matrix->Active();
198 7 $self->logv( $eliminated->{'verbose'} );
199 # active changed, restart loop
200 7 next DROPLOOP;
201 }
202 9 my @jeapardy = ();
203 9 ▶ if ( $self->DropStyle eq 'leastwins' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->DropStyle eq 'leastwins') { } 3 6 9
204 3 @jeapardy = $matrix->LeastWins();
205 }
206 6 else { @jeapardy = keys %{$active} }
207 9 for my $goodbye ( $self->DropChoice( $round, @jeapardy ) ) {
208 11 delete $active->{$goodbye};
209 11 $self->logv("Eliminating $goodbye");
210 }
211 9 my @remaining = keys $active->%*;
212 9 ▶ if ( @remaining == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@remaining == 0) { } 0 9 9
elsif (@remaining == 1) { } 1 8 9
213 0 $self->logt(
214 "All remaining Choices would be eliminated, Tie between @jeapardy");
215 0 $result->{'tie'} = 1;
216 0 $result->{'tied'} = \@jeapardy;
217 0 last DROPLOOP;
218 }
219 elsif ( @remaining == 1 ) {
220 1 my $winner = $remaining[0];
221 1 $self->logt( "Only 1 choice remains.", "** WINNER : $winner **" );
222 1 $result->{'winner'} = $winner;
223 1 last DROPLOOP;
224 }
225 }; #infinite DROPLOOP
226 10 return $result;
227 }
228  
229 4 1;
230  
231 #FOOTER
232  
233 =pod
234  
235 BUG TRACKER
236  
237 L<https://github.com/brainbuz/Vote-Count/issues>
238  
239 AUTHOR
240  
241 John Karr (BRAINBUZ) brainbuz@cpan.org
242  
243 CONTRIBUTORS
244  
245 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
246  
247 LICENSE
248  
249 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>.
250  
251 =cut
« Count/TieBreaker.pm ↑ Summary Count/Method/CondorcetVsIRV.pm »