Count/Method/CondorcetVsIRV.pm

filter: Statementstmt 96.5% 141 / 146 Branchbran 90.0% 45 / 50 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 25 / 25 Podpod 25.0% 2 / 8 totaltotal 89.3% 218 / 244 CC 32 SCAR 19.1
CC
32
Cov
93%
_CVI_RedactRun
14.3
CondorcetVsIRV
11.4
_CVI_IRV
3.0
CRAP 6.8
« Count/Method/CondorcetDropping.pm ↑ Summary Count/TextTableTiny.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Vote::Count::Method::CondorcetVsIRV;
7 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 with 'Vote::Count::Log';
11  
12 2 ▶use Storable 3.15 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Vote::Count::ReadBallots qw/read_ballots write_ballots/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Vote::Count::Redact qw/RedactSingle RedactPair RedactBullet/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Vote::Count::Method::CondorcetIRV;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our $VERSION='1.10';
20  
21 # no warnings 'uninitialized';
22 2 ▶no warnings qw/experimental/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 NAME
25  
26 Vote::Count::Method::CondorcetVsIRV
27  
28 =head1 VERSION 1.10
29  
30 =cut
31  
32 # ABSTRACT: Condorcet versus IRV
33  
34 =pod
35  
36 =head1 SYNOPSIS
37  
38 use Vote::Count::Method::CondorcetVsIRV;
39  
40 my $Election = Vote::Count::Method::CondorcetVsIRV->new( ... );
41 my $result = $Election->CondorcetVsIRV();
42 or
43 my $Election = Vote::Count->new( TieBreakMethod => 'approval' );
44 my $result = $Election->CondorcetVsIRV( relaxed => 1 );
45 equivalent to default:
46 my $result = $Election->CondorcetVsIRV( relaxed => 0, smithsetirv => 0 );
47  
48 say $result->{'winner'};
49  
50 $Election->WriteAllLogs();
51  
52 Returns a HashRef with a key for winner.
53  
54 =head1 Method Common Name: Condorcet vs IRV
55  
56 Condorcet vs IRV Methods determine if the Condorcet Winner needed votes from the IRV winner; electing the Condorcet Winner if there was not a later harm violation, electing the IRV winner if there was. If there is no Condorcet Winner the IRV Winner is chosen.
57  
58 To determine if there was a violation the ballots of one or more choices are redacted, with later choice on those ballots removed.
59  
60 With these methods it is also possible to allow a tolerance for Later Harm.
61  
62 =head3 Double Redaction
63  
64 The Double Redaction method (default) measures the later harm effect between a Condorcet Winner and the IRV Winner.
65  
66 Considering the Margin of the Condorcet Winner over the IRV Winner and the number of votes needed by the Condorcet Winner from the IRV winner as measures of Preference for the Condorcet Winner and of Later Harm, it is also possible to establish a Later Harm Tolerance Threshold.
67  
68 The Relaxed Later Harm option will select the Condorcet Winner when their margin of victory over the IRV Winner is greater than the number of later votes they need from the IRV Winner to be a Condorcet Winner. Although not presently implemented a different ratio or percentage could be used.
69  
70 Because in most cases where the IRV and Condorcet winners are different there are Later Harm effects, without relaxed this method will almost always confirm the IRV winner.
71  
72 =head3 Simple (Single Redaction)
73  
74 This variation only redacts the ballots that choose the IRV Winner as their first choice. This gives the voters confidence that if their first choice wins by the later harm safe method, that their vote will not be used against that choice.
75  
76 The simplest form is:
77  
78 1. Determine the IRV Winner
79  
80 2. Treating the ballots cast with the IRV Winner as their first choice as ballots cast only for the IRV Winner, determine the Condorcet Winner.
81  
82 3. Elect the Condorcet Winner, if there is none, elect the IRV Winner.
83  
84 Unfortunately, this simplest form, in cases where more than one choice defeats the IRV Winner in pairing and later choices of the IRV Winner's ballots determine which becomes the Condorcet Winner, removes the supporters of the IRV Winner from the final decision.
85  
86 The form implemented by Vote::Count is:
87  
88 =over
89  
90 1. Determine both the IRV and Condorcet Winner. If they are the same, elect that choice. If there is no Condorcet Winner, elect the IRV Winner.
91  
92 2. Treating the ballots cast with the IRV Winner as their first choice as ballots cast for only the IRV Winner determine the Condorcet Winner.
93  
94 3. If there is a Condorcet Winner, elect the first Condorcet Winner, if there is none, elect the IRV Winner. (The redaction cannot make the IRV Winner a Condorcet Winner if it isn't already one).
95  
96 =cut
97  
98 =head1 Criteria
99  
100 The double redaction version is later harm safe if the relaxed option is not used. The simple version later harm protects first choice votes only, it also does not protect the first Condorcet Winner's votes at all.
101  
102 =head2 Simplicity
103  
104 The simple version does not require Condorcet Loop resolution, and thus can be considered to be on par with Benham for complexity, and like Benham is Hand Countable. The double redaction version is more complex, but is perhaps more valuable as an approach for measuring later harm.
105  
106 =head2 Later Harm
107  
108 This method meets Later Harm with the default strict option.
109  
110 The relaxed option allows a finite Later Harm effect.
111  
112 Using the TCA Floor Rule and or Smith Set IRV add small Later Harm effects.
113  
114 =head2 Condorcet Criteria
115  
116 This method only meets Condorcet Loser, when the IRV winner is chosen instead of the Condorcet Winner, the winner may be outside the Smith Set.
117  
118 =head2 Consistency
119  
120 Because this method chooses between the outcomes of two different methods, it is subject to the consistency failings of both. Given that Cloning is an important consistency issue in real elections, the clone handling should be an improvement over IRV.
121  
122 =head1 Implementation
123  
124 Details specific to this implementation.
125  
126 The Tie Breaker is defaulted to (modified) Grand Junction for resolvability. Any Tie Breaker supported by Vote::Count::TieBreaker may be used, 'all' and 'none' are not recommended.
127  
128 =head2 Function Name: CondorcetVsIRV
129  
130 Runs the election, returns a hashref containing the winner, similar to how other Vote::Count Methods such as RunIRV behave.
131  
132 =head3 Arguments for CondorcetVsIRV()
133  
134 =over
135  
136 =item* relaxed
137  
138 =item* simple
139  
140 =item* smithsetirv
141  
142 =back
143  
144 =head2 LogTo, LogPath, LogBaseName, LogRedactedTo
145  
146 The first three behave as normal Vote::Count::Log methods, except that the default is /tmp/condorcetvsirv.
147  
148 LogRedactedTo defaults to appending _redacted into the log names for the redacted election, it can be overridden by setting a value (which should be /path/basename) like LogTo.
149  
150 =head2 WriteLog WriteAllLogs
151  
152 WriteLog behaves normally, there is a log set for the CondorcetVSIRV object as well as child logs for the Election and RedactedElection, each of which has a set of logs for PairMatrix as well. WriteAllLogs will write all of these logs.
153  
154 =cut
155  
156 # LogTo over-writes role LogTo changing default filename.
157 2 has 'LogTo' => (
158 is => 'rw',
159 isa => 'Str',
160 default => '/tmp/condorcetvsirv',
161 );
162  
163 2 has 'LogRedactedTo' => (
164 is => 'lazy',
165 is => 'rw',
166 isa => 'Str',
167 builder => '_setredactedlog',
168 );
169  
170 ▶sub _setredactedlog ( $self ) {
Subroutine _setredactedlog: called CC=2 SCAR=6.9
171 # There is a bug with LogTo being uninitialized despite having a default
172 22 ▶ my $logto
Branch: 1 branch
BranchTrueFalseTotal
defined $self->LogTo ? : 10 12 22
173 = defined $self->LogTo()
174 ? $self->LogTo() . '_redacted'
175 : '/tmp/condorcetvsirv_redacted';
176 22 return $logto;
177 }
178  
179 2 has 'TieBreakMethod' => (
180 is => 'ro',
181 isa => 'Str',
182 default => 'grandjunction',
183 );
184  
185 2 has 'BallotSet' => ( is => 'ro', isa => 'HashRef', required => 1 );
186  
187 2 has 'Active' => (
188 is => 'rw',
189 isa => 'HashRef',
190 lazy => 1,
191 builder => '_InitialActive',
192 );
193  
194 22 ▶sub _InitialActive ( $I ) { return dclone $I->BallotSet()->{'choices'} }
Subroutine _InitialActive: called CC=1 SCAR=0.0
195  
196 ▶sub SetActive ( $I, $active ) {
Subroutine SetActive: called CC=2 SCAR=6.9
Pod undocumented
197 4 $I->{'Active'} = dclone $active;
198 4 $I->{'Election'}->SetActive($active);
199 4 ▶ if ( defined $I->{'RedactedElection'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $I->{"RedactedElection"}) 2 2 4
200 2 $I->{'RedactedElection'}->SetActive($active);
201 }
202 }
203  
204 ▶sub ResetActive ( $self ) {
Subroutine ResetActive: called CC=1 SCAR=0.0
Pod undocumented
205 1 my $new = dclone $self->BallotSet()->{'choices'};
206 1 $self->SetActive($new);
207 1 return $new;
208 }
209  
210 # sub ResetActive ( $self ) { return dclone $self->BallotSet()->{'choices'} }
211  
212 ▶sub _CVI_IRV ( $I, $active, $smithsetirv ) {
Subroutine _CVI_IRV: called CC=3 SCAR=11.0
213 19 my $WonIRV = undef;
214 19 my $irvresult = undef;
215 19 ▶ if ($smithsetirv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smithsetirv) { } 2 17 19
216 2 $irvresult = $I->SmithSetIRV( $I->TieBreakMethod() );
217 }
218 else {
219 17 $irvresult = $I->RunIRV( $active, $I->TieBreakMethod() );
220 }
221 19 $I->logd( 'IRV Result: ' . Dumper $irvresult );
222 19 ▶ return $irvresult->{'winner'} if $irvresult->{'winner'};
Branch: 1 branch
BranchTrueFalseTotal
if $irvresult->{"winner"} 18 1 19
223 1 $I->logt("IRV ended with a Tie.");
224 $I->logt(
225 1 "Active (Tied) Choices are: " . join( ', ', $irvresult->{'tied'} ) );
226 1 $I->SetActiveFromArrayRef( $irvresult->{'tied'} );
227 1 return 0;
228 }
229  
230 sub BUILD {
231 22 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
232 $self->{'Election'} = Vote::Count::Method::CondorcetIRV->new(
233 BallotSet => $self->BallotSet(),
234 TieBreakMethod => $self->TieBreakMethod(),
235 Active => $self->Active(),
236 BallotSetType => 'rcv',
237 22 LogTo => $self->{'LogTo'} . '_unredacted',
238 );
239 22 $self->{'RedactedElection'} = undef,;
240 }
241  
242 ▶sub WriteAllLogs ($I) {
Subroutine WriteAllLogs: called CC=1 SCAR=0.0
Pod documented
243 1 $I->WriteLog();
244 1 $I->Election()->WriteLog();
245 1 $I->RedactedElection()->WriteLog();
246 1 $I->Election()->PairMatrix()->WriteLog();
247 1 $I->RedactedElection()->PairMatrix()->WriteLog();
248 }
249  
250 35 ▶sub Election ($self) { return $self->{'Election'} }
Subroutine Election: called CC=1 SCAR=0.0
Pod undocumented
251  
252 15 ▶sub RedactedElection ( $self, $ballotset = undef, $active = undef ) {
Subroutine RedactedElection: called CC=3 SCAR=11.0
Pod undocumented
253 15 return $self->{'RedactedElection'};
254 }
255  
256 11 ▶sub CreateRedactedElection ( $self, $WonCondorcet, $WonIRV, $simpleflag=0 ) {
Subroutine CreateRedactedElection: called CC=3 SCAR=11.0
Pod undocumented
257 11 ▶ my $ballotset = $simpleflag
Branch: 1 branch
BranchTrueFalseTotal
$simpleflag ? : 3 8 11
258 ? RedactBullet ( $self->BallotSet(), $WonIRV )
259 : RedactPair( $self->BallotSet(), $WonCondorcet, $WonIRV );
260 11 $self->{'RedactedElection'} = Vote::Count->new(
261 BallotSet => $ballotset,
262 TieBreakMethod => $self->TieBreakMethod(),
263 Active => $self->Active(),
264 BallotSetType => 'rcv',
265 LogTo => $self->LogRedactedTo(),
266 );
267 $self->logd(
268 'Created Redacted Election.',
269 $self->{'RedactedElection'}->PairMatrix()->PairingVotesTable(),
270 11 $self->{'RedactedElection'}->PairMatrix()->MatrixTable(),
271 );
272 }
273  
274 ▶sub _CVI_RedactRun ( $I, $WonCondorcet, $WonIRV, $active, $options ) {
Subroutine _CVI_RedactRun: called CC=14 SCAR=26.6
275 my $smithsetirv
276 10 ▶ = $options->{'smithsetirv'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'smithsetirv'} ? : 0 10 10
277 10 ▶ my $relaxed = $options->{'relaxed'} ? $options->{'relaxed'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'relaxed'} ? : 2 8 10
278 10 ▶ my $simpleflag = $options->{'simple'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'simple'} ? : 3 7 10
279 10 my $E = $I->Election();
280 10 my $R = $I->RedactedElection();
281 10 my $ConfirmC = $R->PairMatrix->CondorcetWinner();
282 10 my $ConfirmI = _CVI_IRV( $R, $active, $smithsetirv );
283 10 ▶ if ( $ConfirmC ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ConfirmC) { } 9 1 10
284 9 ▶ if ( $simpleflag ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($simpleflag) { } 3 6 9
elsif ($ConfirmC eq $WonCondorcet or $ConfirmC eq $WonIRV) { } 3 3 6
285 3 $I->logt("Elected $WonCondorcet, Redacted Ballots had a Condorcet Winner.");
286 3 ▶ $I->logv("The Redacted Condorcet Winner was $ConfirmC.")
Branch: 1 branch
BranchTrueFalseTotal
if $ConfirmC ne $WonCondorcet 1 2 3
287 if ( $ConfirmC ne $WonCondorcet);
288 3 return $WonCondorcet ;
289 ▶ } elsif ( $ConfirmC eq $WonCondorcet or $ConfirmC eq $WonIRV ) {
Condition66%2/3
$ConfirmC eq $WonCondorcet or $ConfirmC eq $WonIRV
l!l&&r!l&&!r
303
MC/DC50%1/2
$ConfirmC eq $WonCondorcet or $ConfirmC eq $WonIRV
$ConfirmC eq $WonCondorcet $ConfirmC eq $WonIRV
Truth table66%2/3
A or B
A= $ConfirmC eq $WonCondorcetB= $ConfirmC eq $WonIRV
ABresult
1X1
011
000
290 3 $I->logt("Elected $ConfirmC, Redacted Ballots Condorcet Winner.");
291 3 return $ConfirmC;
292 }
293 } else {
294 1 $ConfirmI = _CVI_IRV( $R, $active, $smithsetirv );
295 1 ▶ if ( $ConfirmI eq $WonCondorcet or $ConfirmI eq $WonIRV ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ConfirmI eq $WonCondorcet or $ConfirmI eq $WonIRV) 0 1 1
Condition33%1/3
$ConfirmI eq $WonCondorcet or $ConfirmI eq $WonIRV
l!l&&r!l&&!r
001
MC/DC0%0/2
$ConfirmI eq $WonCondorcet or $ConfirmI eq $WonIRV
$ConfirmI eq $WonCondorcet $ConfirmI eq $WonIRV
Truth table33%1/3
A or B
A= $ConfirmI eq $WonCondorcetB= $ConfirmI eq $WonIRV
ABresult
1X1
011
000
296 0 $I->logt("Elected $ConfirmI, Redacted Ballots IRV Winner.");
297 0 return $ConfirmI;
298 }
299 }
300 4 ▶ $ConfirmC = 'NONE' unless $ConfirmC;
Branch: 1 branch
BranchTrueFalseTotal
unless $ConfirmC 1 3 4
301 4 $I->logt("Neither $WonCondorcet nor $WonIRV were confirmed.");
302 4 $I->logt(
303 "Redacted Ballots Winners: Condorcet = $ConfirmC, IRV = $ConfirmI");
304 4 ▶ if ($relaxed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed) 2 2 4
305 2 my $GreatestLoss = $R->PairMatrix()->GreatestLoss($WonCondorcet);
306 my $Margin = $E->PairMatrix()->GetPairResult( $WonCondorcet, $WonIRV )
307 2 ->{'margin'};
308 2 $I->logt(
309 "The margin of the Condorcet over the IRV winner was: $Margin");
310 2 $I->logt(
311 "$WonCondorcet\'s greatest loss with redacted ballots was $GreatestLoss."
312 );
313 2 ▶ if ( $Margin > $GreatestLoss ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Margin > $GreatestLoss) 1 1 2
314 1 $I->logt("Elected: $WonCondorcet");
315 1 return $WonCondorcet;
316 }
317 }
318 3 $I->logt("Elected: $WonIRV");
319 3 return $WonIRV;
320 }
321  
322 ▶sub CondorcetVsIRV ( $self, %args ) {
Subroutine CondorcetVsIRV: called CC=11 SCAR=24.4
Pod documented
323 21 my $E = $self->Election();
324 21 ▶ my $smithsetirv = defined $args{'smithsetirv'} ? $args{'smithsetirv'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'smithsetirv'} ? : 15 6 21
325 21 ▶ my $simpleflag = defined $args{'simple'} ? $args{'simple'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'simple'} ? : 9 12 21
326 21 my $active = $self->Active();
327 # check for majority winner.
328 21 my $majority = $E->EvaluateTopCountMajority()->{'winner'};
329 21 ▶ return $majority if $majority;
Branch: 1 branch
BranchTrueFalseTotal
if $majority 0 21 21
330 21 my $WonIRV = undef;
331 21 my $WonCondorcet = $E->PairMatrix()->CondorcetWinner();
332 21 ▶ if ($WonCondorcet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WonCondorcet) { } 13 8 21
333 13 $self->logt("Condorcet Winner is $WonCondorcet");
334 # Even if SmithSetIRV requested, it would return the condorcet winner
335 # We need to know if a different choice would win IRV.
336 13 $WonIRV = $E->RunIRV( $active, $E->TieBreakMethod() )->{'winner'};
337 }
338 else {
339 8 $self->logt("No Condorcet Winner");
340 8 $WonIRV = _CVI_IRV( $E, $active, $smithsetirv );
341 8 ▶ if ($WonIRV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WonIRV) { } 7 1 8
342 7 $self->logt("Electing IRV Winner $WonIRV");
343 7 return { 'winner' => $WonIRV };
344 }
345 else { ;
346 1 $self->logt("There is no Condorcet or IRV winner.");
347 1 return { 'winner' => 0 };
348 }
349 }
350  
351 # IRV private already logged tie, now return the false value.
352 # Edge case IRV tie with Condorcet Winner, I guess CW wins?
353 13 ▶ unless ($WonIRV) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($WonIRV) 1 12 13
354 1 ▶ if ($WonCondorcet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WonCondorcet) 1 0 1
355 1 $self->logt("Electing Condorcet Winner $WonCondorcet, IRV tied.");
356 1 return { 'winner' => $WonCondorcet};
357 }
358 0 return { 'winner' => 0 };
359 }
360 12 ▶ if ( $WonIRV eq $WonCondorcet ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WonIRV eq $WonCondorcet) 2 10 12
361 2 $self->logt("Electing $WonIRV the winner by both Condorcet and IRV.");
362 2 return { 'winner' => $WonIRV };
363 }
364 10 ▶ if ( $WonIRV and !$WonCondorcet ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WonIRV and not $WonCondorcet) 0 10 10
Condition33%1/3
$WonIRV and not $WonCondorcet
!ll&&!rl&&r
0100
MC/DC0%0/2
$WonIRV and not $WonCondorcet
$WonIRV not $WonCondorcet
Truth table33%1/3
A and not(B)
A= $WonIRVB= not $WonCondorcet
ABresult
0X0
100
111
365 0 $self->logt(
366 "Electing IRV Winner $WonIRV. There was no Condorcet Winner.");
367 0 return { 'winner' => $WonIRV };
368 }
369 10 $self->CreateRedactedElection( $WonCondorcet, $WonIRV, $simpleflag );
370 10 my $winner
371 = $self->_CVI_RedactRun( $WonCondorcet, $WonIRV, $active, \%args );
372 10 return { 'winner' => $winner };
373  
374 }
375  
376 2 1;
377  
378 #FOOTER
379  
380 =pod
381  
382 BUG TRACKER
383  
384 L<https://github.com/brainbuz/Vote-Count/issues>
385  
386 AUTHOR
387  
388 John Karr (BRAINBUZ) brainbuz@cpan.org
389  
390 CONTRIBUTORS
391  
392 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
393  
394 LICENSE
395  
396 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>.
397  
398 =cut
« Count/Method/CondorcetDropping.pm ↑ Summary Count/TextTableTiny.pm »