| 1 | 25 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 25 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 25 | ▶ | use 5.022; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 25 | ▶ | use feature qw /postderef signatures/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | package Vote::Count::IRV; | ||||||||||
| 7 | |||||||||||
| 8 | 25 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 25 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 25 | with 'Vote::Count::TopCount'; | |||||||||
| 12 | 25 | with 'Vote::Count::TieBreaker'; | |||||||||
| 13 | |||||||||||
| 14 | 25 | ▶ | use Storable 3.15 'dclone'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 25 | our $VERSION='1.10'; | |||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | Vote::Count::IRV | ||||||||||
| 21 | |||||||||||
| 22 | =head1 VERSION 1.10 | ||||||||||
| 23 | |||||||||||
| 24 | =cut | ||||||||||
| 25 | |||||||||||
| 26 | # ABSTRACT: IRV Method for Vote::Count | ||||||||||
| 27 | |||||||||||
| 28 | 25 | ▶ | no warnings 'experimental'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 25 | ▶ | use List::Util qw( min max ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | #use Data::Dumper; | ||||||||||
| 31 | |||||||||||
| 32 | ▶ | sub _ResolveTie ( $self, $active, $tiebreaker, @tiedchoices ) { | |||||||||
|
Subroutine
_ResolveTie: called CC=5 SCAR=16.1
| |||||||||||
| 33 | 179 | ▶ | return @tiedchoices if @tiedchoices == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | my %high = | ||||||||||
| 35 | 44 | map { $_ => 1 } $self->TieBreaker( $tiebreaker, $active, @tiedchoices ); | |||||||||
| 36 | 44 | ▶ | if ( defined $self->{'last_tiebreaker'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 5 | $self->logt( $self->{'last_tiebreaker'}{'terse'} ); | |||||||||
| 38 | 5 | $self->logv( $self->{'last_tiebreaker'}{'verbose'} ); | |||||||||
| 39 | 5 | $self->{'last_tiebreaker'} = undef; | |||||||||
| 40 | } | ||||||||||
| 41 | 44 | ▶ | if ( @tiedchoices == scalar( keys %high ) ) { return @tiedchoices } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | # tiebreaker returns winner, we want losers! | ||||||||||
| 43 | # use map to remove winner(s) from @tiedchoices. | ||||||||||
| 44 | # warning about sort interpreted as function fixed | ||||||||||
| 45 | my @low = sort map { | ||||||||||
| 46 | 33 | ▶ | if ( $high{$_} ) { } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 70 | else { $_ } | |||||||||
| 48 | } @tiedchoices; | ||||||||||
| 49 | 33 | return @low; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 48 | ▶ | sub RunIRV ( $self, $active = undef, $tiebreaker = undef ) { | ||||||||
|
Subroutine
RunIRV: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||
| 53 | # external $active should not be changed. | ||||||||||
| 54 | 48 | ▶ | if ( defined $active ) { $active = dclone $active } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | # Object's active is altered by IRV. | ||||||||||
| 56 | 14 | else { $active = dclone $self->Active() } | |||||||||
| 57 | 48 | ▶ | unless ( defined $tiebreaker ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 9 | ▶ | if ( defined $self->TieBreakMethod() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 1 | $tiebreaker = $self->TieBreakMethod(); | |||||||||
| 60 | } | ||||||||||
| 61 | else { | ||||||||||
| 62 | 8 | $tiebreaker = 'all'; | |||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | 48 | my $roundctr = 0; | |||||||||
| 66 | 48 | my $maxround = scalar( keys %{$active} ); | |||||||||
| 67 | $self->logt( "Instant Runoff Voting", | ||||||||||
| 68 | 48 | 'Choices: ', join( ', ', ( sort keys %{$active} ) ) ); | |||||||||
| 69 | # forever loop normally ends with return from $majority | ||||||||||
| 70 | # a tie should be detected and also generate a | ||||||||||
| 71 | # return from the else loop. | ||||||||||
| 72 | # if something goes wrong roundcountr/maxround | ||||||||||
| 73 | # will generate exception. | ||||||||||
| 74 | IRVLOOP: | ||||||||||
| 75 | 48 | until (0) { | |||||||||
| 76 | 219 | $roundctr++; | |||||||||
| 77 | 219 | ▶ | die "IRVLOOP infinite stopped at $roundctr" if $roundctr > $maxround; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 219 | my $round = $self->TopCount($active); | |||||||||
| 79 | 219 | $self->logv( '---', "IRV Round $roundctr", $round->RankTable() ); | |||||||||
| 80 | 219 | my $majority = $self->EvaluateTopCountMajority($round); | |||||||||
| 81 | 219 | ▶ | if ( defined $majority->{'winner'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 40 | return $majority; | |||||||||
| 83 | } | ||||||||||
| 84 | else { | ||||||||||
| 85 | 179 | my @bottom = | |||||||||
| 86 | $self->_ResolveTie( $active, $tiebreaker, $round->ArrayBottom()->@* ); | ||||||||||
| 87 | 179 | ▶ | if ( scalar(@bottom) == scalar( keys %{$active} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | # if there is a tie at the end, the finalists should | ||||||||||
| 89 | # be both top and bottom and the active set. | ||||||||||
| 90 | 8 | $self->logt( "Tied: " . join( ', ', @bottom ) ); | |||||||||
| 91 | 8 | return { tie => 1, tied => \@bottom, winner => 0 }; | |||||||||
| 92 | } | ||||||||||
| 93 | 171 | $self->logv( "Eliminating: " . join( ', ', @bottom ) ); | |||||||||
| 94 | 171 | for my $b (@bottom) { | |||||||||
| 95 | 227 | delete $active->{$b}; | |||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 25 | 1; | |||||||||
| 102 | |||||||||||
| 103 | =pod | ||||||||||
| 104 | |||||||||||
| 105 | =head1 IRV | ||||||||||
| 106 | |||||||||||
| 107 | Implements Instant Runoff Voting for Vote::Count. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 SYNOPSIS | ||||||||||
| 110 | |||||||||||
| 111 | use Vote::Count::Method; | ||||||||||
| 112 | use Vote::Count::ReadBallots 'read_ballots'; | ||||||||||
| 113 | |||||||||||
| 114 | my $Election = Vote::Count::->new( | ||||||||||
| 115 | BallotSet => read_ballots('%path_to_my_ballots'), | ||||||||||
| 116 | TieBreakMethod => 'grandjunction'); | ||||||||||
| 117 | |||||||||||
| 118 | my $result = $Election->RunIRV(); | ||||||||||
| 119 | my $winner = $result->{'winner'}; | ||||||||||
| 120 | |||||||||||
| 121 | say $Election->logv(); # Print the full Log. | ||||||||||
| 122 | |||||||||||
| 123 | =head1 Method Summary | ||||||||||
| 124 | |||||||||||
| 125 | Instant Runoff Voting Looks for a Majority Winner. If one isn't present the choice with the lowest Top Count is removed. | ||||||||||
| 126 | |||||||||||
| 127 | Instant Runoff Voting is easy to count by hand and meets the Later Harm and Condorcet Loser Criteria. It, unfortunately, fails a large number of consistency criteria; the order of candidate dropping matters and small changes to the votes of non-winning choices that result in changes to the dropping order can change the outcome. | ||||||||||
| 128 | |||||||||||
| 129 | Instant Runoff Voting is also known as Alternative Vote and as the Hare Method. | ||||||||||
| 130 | |||||||||||
| 131 | =head2 Tie Handling | ||||||||||
| 132 | |||||||||||
| 133 | There is no standard accepted method for IRV tie resolution, Eliminate All is a common one and the default. | ||||||||||
| 134 | |||||||||||
| 135 | Returns a tie when all of the remaining choices are in a tie. | ||||||||||
| 136 | |||||||||||
| 137 | An optional value to RunIRV is to specify tiebreaker, see TieBreaker. | ||||||||||
| 138 | |||||||||||
| 139 | =head2 RunIRV | ||||||||||
| 140 | |||||||||||
| 141 | $Election->RunIRV(); | ||||||||||
| 142 | |||||||||||
| 143 | $Election->RunIRV( $active ) | ||||||||||
| 144 | |||||||||||
| 145 | $Election->RunIRV( $active, 'approval' ) | ||||||||||
| 146 | |||||||||||
| 147 | Runs IRV on the provided Ballot Set. Takes an optional parameter of $active which is a hashref for which the keys are the currently active choices. | ||||||||||
| 148 | |||||||||||
| 149 | Returns results in a hashref which will be the results of Vote::Count::TopCount->EvaluateTopCountMajority, if there is no winner hash will instead be: | ||||||||||
| 150 | |||||||||||
| 151 | tie => [true or false], | ||||||||||
| 152 | tied => [ array of tied choices ], | ||||||||||
| 153 | winner => a false value | ||||||||||
| 154 | |||||||||||
| 155 | Supports the Vote::Count logt, logv, and logd methods for providing details of the method. | ||||||||||
| 156 | |||||||||||
| 157 | =head2 TieBreaker | ||||||||||
| 158 | |||||||||||
| 159 | Uses TieBreaker from the TieBreaker Role. The default is 'all', which is to not break ties. 'none' the default for the Matrix (Condorcet) Object should not be used for IRV. | ||||||||||
| 160 | |||||||||||
| 161 | All was chosen as the module default because it is Later Harm safe. Modified Grand Junction is the most resolvable and is the recommended option. | ||||||||||
| 162 | |||||||||||
| 163 | In the event that the tie-breaker returns a tie eliminate all that remain tied is used, unless that would eliminate all choices, in which case the election returns a tie. | ||||||||||
| 164 | |||||||||||
| 165 | =cut | ||||||||||
| 166 | |||||||||||
| 167 | #FOOTER | ||||||||||
| 168 | |||||||||||
| 169 | =pod | ||||||||||
| 170 | |||||||||||
| 171 | BUG TRACKER | ||||||||||
| 172 | |||||||||||
| 173 | L<https://github.com/brainbuz/Vote-Count/issues> | ||||||||||
| 174 | |||||||||||
| 175 | AUTHOR | ||||||||||
| 176 | |||||||||||
| 177 | John Karr (BRAINBUZ) brainbuz@cpan.org | ||||||||||
| 178 | |||||||||||
| 179 | CONTRIBUTORS | ||||||||||
| 180 | |||||||||||
| 181 | Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org. | ||||||||||
| 182 | |||||||||||
| 183 | LICENSE | ||||||||||
| 184 | |||||||||||
| 185 | 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>. | ||||||||||
| 186 | |||||||||||
| 187 | =cut | ||||||||||