| 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::Matrix; | ||||||||||||||||||||||
| 7 | 25 | ▶ | use Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 25 | with | |||||||||||||||||||||
| 10 | 'Vote::Count::Common', | ||||||||||||||||||||||
| 11 | 'Vote::Count::Approval', | ||||||||||||||||||||||
| 12 | 'Vote::Count::Borda', | ||||||||||||||||||||||
| 13 | 'Vote::Count::Log', | ||||||||||||||||||||||
| 14 | 'Vote::Count::Score', | ||||||||||||||||||||||
| 15 | 'Vote::Count::TieBreaker', | ||||||||||||||||||||||
| 16 | ; | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | 25 | ▶ | use Vote::Count::RankCount; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | 25 | ▶ | no warnings 'experimental'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 21 | 25 | ▶ | use List::Util qw( min max sum ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 22 | 25 | ▶ | use Vote::Count::TextTableTiny qw/generate_table/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 23 | 25 | ▶ | use Sort::Hash; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 24 | 25 | ▶ | use Storable 3.15 'dclone'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | # use Try::Tiny; | ||||||||||||||||||||||
| 27 | #use Data::Dumper; | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | 25 | ▶ | use YAML::XS; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 25 | our $VERSION='1.10'; | |||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | =head1 NAME | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | Vote::Count::Matrix | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | =head1 VERSION 1.10 | ||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | # ABSTRACT: Condorcet Win Loss Matrix | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | 25 | has BallotSet => ( | |||||||||||||||||||||
| 44 | is => 'ro', | ||||||||||||||||||||||
| 45 | required => 1, | ||||||||||||||||||||||
| 46 | isa => 'HashRef', | ||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 25 | has TieBreakMethod => ( | |||||||||||||||||||||
| 50 | is => 'rw', | ||||||||||||||||||||||
| 51 | isa => 'Str', | ||||||||||||||||||||||
| 52 | required => 0, | ||||||||||||||||||||||
| 53 | default => 'none', | ||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||
| 55 | |||||||||||||||||||||||
| 56 | ▶ | sub _untie ( $I, $A, $B ) { | |||||||||||||||||||||
|
Subroutine
_untie: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 57 | 193 | my @untie = $I->TieBreaker( $I->TieBreakMethod(), $I->Active(), $A, $B ); | |||||||||||||||||||||
| 58 | 193 | ▶ | return $untie[0] if ( scalar(@untie) == 1 ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 59 | 158 | return 0; | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | ▶ | sub _pairwinner_rcv ( $ballots, $A, $B ) { | |||||||||||||||||||||
|
Subroutine
_pairwinner_rcv: called CC=5 SCAR=16.1
| |||||||||||||||||||||||
| 63 | 1985 | my $countA = 0; | |||||||||||||||||||||
| 64 | 1985 | my $countB = 0; | |||||||||||||||||||||
| 65 | FORVOTES: | ||||||||||||||||||||||
| 66 | 1985 | for my $b ( keys $ballots->%* ) { | |||||||||||||||||||||
| 67 | 50552 | for my $v ( values $ballots->{$b}{'votes'}->@* ) { | |||||||||||||||||||||
| 68 | 98355 | ▶ | if ( $v eq $A ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 69 | 14700 | $countA += $ballots->{$b}{'count'} * $ballots->{$b}{'votevalue'}; | |||||||||||||||||||||
| 70 | 14700 | next FORVOTES; | |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||
| 72 | elsif ( $v eq $B ) { | ||||||||||||||||||||||
| 73 | 19617 | $countB += $ballots->{$b}{'count'} * $ballots->{$b}{'votevalue'}; | |||||||||||||||||||||
| 74 | 19617 | next FORVOTES; | |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | } # FORVOTES | ||||||||||||||||||||||
| 78 | 1985 | return ( $countA, $countB ); | |||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | ▶ | sub _conduct_pair ( $I, $A, $B ) { | |||||||||||||||||||||
|
Subroutine
_conduct_pair: called CC=10 SCAR=23.0
| |||||||||||||||||||||||
| 82 | 2118 | my $ballots = $I->BallotSet()->{'ballots'}; | |||||||||||||||||||||
| 83 | 2118 | my $countA = 0; | |||||||||||||||||||||
| 84 | 2118 | my $countB = 0; | |||||||||||||||||||||
| 85 | 2118 | $I->logv("Pairing: $A vs $B"); | |||||||||||||||||||||
| 86 | 2118 | ▶ | if ( $I->BallotSet()->{'options'}{'range'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 87 | 133 | ( $countA, $countB ) = $I->RangeBallotPair( $A, $B ); | |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||
| 90 | 1985 | ( $countA, $countB ) = _pairwinner_rcv( $ballots, $A, $B ); | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | 2118 | my %retval = ( | |||||||||||||||||||||
| 93 | $A => $countA, | ||||||||||||||||||||||
| 94 | $B => $countB, | ||||||||||||||||||||||
| 95 | 'tie' => 0, | ||||||||||||||||||||||
| 96 | 'winner' => '', | ||||||||||||||||||||||
| 97 | 'loser' => '', | ||||||||||||||||||||||
| 98 | 'margin' => abs( $countA - $countB ) | ||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||
| 100 | 2118 | my $diff = $countA - $countB; | |||||||||||||||||||||
| 101 | # 0 : $countA == $countB | ||||||||||||||||||||||
| 102 | 2118 | ▶ | if ( $diff == 0 ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 103 | 193 | my $untied = $I->_untie( $A, $B ); | |||||||||||||||||||||
| 104 | 193 | ▶ | if ($untied) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 105 | 35 | ▶ | $diff = 1 if $untied eq $A; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 106 | 35 | ▶ | $diff = -1 if $untied eq $B; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||
| 109 | 2118 | ▶ | if ( $diff == 0 ) { | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 110 | 158 | $retval{'winner'} = ''; | |||||||||||||||||||||
| 111 | 158 | $retval{'tie'} = 1; | |||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||
| 113 | # $diff > 0 A won or won tiebreaker. | ||||||||||||||||||||||
| 114 | elsif ( $diff > 0 ) { | ||||||||||||||||||||||
| 115 | 927 | $retval{'winner'} = $A; | |||||||||||||||||||||
| 116 | 927 | $retval{'loser'} = $B; | |||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||
| 118 | # $diff < 0 B won or won tiebreaker. | ||||||||||||||||||||||
| 119 | elsif ( $diff < 0 ) { | ||||||||||||||||||||||
| 120 | 1033 | $retval{'winner'} = $B; | |||||||||||||||||||||
| 121 | 1033 | $retval{'loser'} = $A; | |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | 2118 | ▶ | if ( $retval{'winner'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 124 | 1960 | $I->logv("Winner: $retval{'winner'} ($A: $countA $B: $countB)"); | |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | 158 | else { $I->logv("Tie $A: $countA $B: $countB") } | |||||||||||||||||||||
| 127 | 2118 | return \%retval; | |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | sub BUILD { | ||||||||||||||||||||||
| 131 | 76 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||
| 132 | 76 | my $results = {}; | |||||||||||||||||||||
| 133 | 76 | my $ballotset = $self->BallotSet(); | |||||||||||||||||||||
| 134 | 76 | my @choices = keys $self->Active()->%*; | |||||||||||||||||||||
| 135 | 76 | ▶ | while ( scalar(@choices) ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 136 | 557 | my $A = shift @choices; | |||||||||||||||||||||
| 137 | 557 | for my $B (@choices) { | |||||||||||||||||||||
| 138 | 2115 | my $result = $self->_conduct_pair( $A, $B ); | |||||||||||||||||||||
| 139 | # Each result has two hash keys so it can be found without | ||||||||||||||||||||||
| 140 | # having to try twice or sort the names for a single key. | ||||||||||||||||||||||
| 141 | 2115 | $results->{$A}{$B} = $result; | |||||||||||||||||||||
| 142 | 2115 | $results->{$B}{$A} = $result; | |||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||
| 145 | 76 | $self->{'Matrix'} = $results; | |||||||||||||||||||||
| 146 | 76 | $self->logt( "# Matrix", $self->MatrixTable() ); | |||||||||||||||||||||
| 147 | 76 | $self->logv( "# Pairing Results", $self->PairingVotesTable() ); | |||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||
| 149 | |||||||||||||||||||||||
| 150 | ▶ | sub ScoreMatrix ( $self ) { | |||||||||||||||||||||
|
Subroutine
ScoreMatrix: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||
| 151 | 134 | my $scores = {}; | |||||||||||||||||||||
| 152 | 134 | my %active = $self->Active()->%*; | |||||||||||||||||||||
| 153 | 134 | for my $A ( keys %active ) { | |||||||||||||||||||||
| 154 | 808 | my $hasties = 0; | |||||||||||||||||||||
| 155 | 808 | $scores->{$A} = 0; | |||||||||||||||||||||
| 156 | 808 | for my $B ( keys %active ) { | |||||||||||||||||||||
| 157 | 6048 | ▶ | next if $B eq $A; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 158 | 5240 | ▶ | if ( $A eq $self->{'Matrix'}{$A}{$B}{'winner'} ) { $scores->{$A}++ } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 159 | 5240 | ▶ | if ( $self->{'Matrix'}{$A}{$B}{'tie'} ) { $hasties = .001 } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||
| 161 | 808 | ▶ | if ( $scores->{$A} == 0 ) { $scores->{$A} += $hasties } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||
| 163 | 134 | return $scores; | |||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | # return the choice with fewest wins in matrix. | ||||||||||||||||||||||
| 167 | ▶ | sub LeastWins ( $matrix ) { | |||||||||||||||||||||
|
Subroutine
LeastWins: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 168 | 3 | my @lowest = (); | |||||||||||||||||||||
| 169 | 3 | my %scored = $matrix->ScoreMatrix()->%*; | |||||||||||||||||||||
| 170 | 3 | my $lowscore = min( values %scored ); | |||||||||||||||||||||
| 171 | 3 | for my $A ( keys %scored ) { | |||||||||||||||||||||
| 172 | 10 | ▶ | if ( $scored{$A} == $lowscore ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 173 | 8 | push @lowest, $A; | |||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||
| 176 | 3 | return @lowest; | |||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | 17 | ▶ | sub CondorcetLoser ( $self, $nowins = 0 ) { | ||||||||||||||||||||
|
Subroutine
CondorcetLoser: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||
| 180 | 17 | my $unfinished = 1; | |||||||||||||||||||||
| 181 | 17 | my $wordy = "Removing Condorcet Losers\n"; | |||||||||||||||||||||
| 182 | 17 | my @eliminated = (); | |||||||||||||||||||||
| 183 | ▶ | my $loser = sub ( $score ) { | |||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||
| 184 | 184 | ▶ | if ($nowins) { return 1 if $score < 1 } | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 185 | 149 | ▶ | else { return 1 if $score == 0 } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 186 | 141 | return 0; | |||||||||||||||||||||
| 187 | 17 | }; | |||||||||||||||||||||
| 188 | CONDORCETLOSERLOOP: | ||||||||||||||||||||||
| 189 | 17 | ▶ | while ($unfinished) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 190 | 60 | $unfinished = 0; | |||||||||||||||||||||
| 191 | 60 | my $scores = $self->ScoreMatrix; | |||||||||||||||||||||
| 192 | 60 | my @alist = ( keys $self->Active()->%* ); | |||||||||||||||||||||
| 193 | # Check that tied choices at the top won't be | ||||||||||||||||||||||
| 194 | # eliminated. alist is looped over twice because we | ||||||||||||||||||||||
| 195 | # don't want to report the scores when the list is | ||||||||||||||||||||||
| 196 | # reduced to either a condorcet winner or tied situation. | ||||||||||||||||||||||
| 197 | 60 | for my $A (@alist) { | |||||||||||||||||||||
| 198 | 313 | ▶ | unless ( max( values $scores->%* ) ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 199 | 2 | last CONDORCETLOSERLOOP; | |||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||
| 202 | 58 | $wordy .= YAML::XS::Dump($scores); | |||||||||||||||||||||
| 203 | 58 | for my $A (@alist) { | |||||||||||||||||||||
| 204 | 184 | ▶ | if ( $loser->( $scores->{$A} ) ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 205 | 43 | push @eliminated, ($A); | |||||||||||||||||||||
| 206 | 43 | $wordy .= "Eliminationg Condorcet Loser: *$A*\n"; | |||||||||||||||||||||
| 207 | 43 | delete $self->{'Active'}{$A}; | |||||||||||||||||||||
| 208 | 43 | $unfinished = 1; | |||||||||||||||||||||
| 209 | 43 | next CONDORCETLOSERLOOP; | |||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||
| 213 | 17 | ▶ | my $elimstr = | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 214 | scalar(@eliminated) | ||||||||||||||||||||||
| 215 | ? "Eliminated Condorcet Losers: " . join( ', ', @eliminated ) . "\n" | ||||||||||||||||||||||
| 216 | : "No Condorcet Losers Eliminated\n"; | ||||||||||||||||||||||
| 217 | return { | ||||||||||||||||||||||
| 218 | 17 | verbose => $wordy, | |||||||||||||||||||||
| 219 | terse => $elimstr, | ||||||||||||||||||||||
| 220 | eliminated => \@eliminated, | ||||||||||||||||||||||
| 221 | eliminations => scalar(@eliminated), | ||||||||||||||||||||||
| 222 | }; | ||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||
| 224 | |||||||||||||||||||||||
| 225 | ▶ | sub CondorcetWinner( $self ) { | |||||||||||||||||||||
|
Subroutine
CondorcetWinner: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 226 | 65 | my $scores = $self->ScoreMatrix; | |||||||||||||||||||||
| 227 | 65 | my @choices = keys $scores->%*; | |||||||||||||||||||||
| 228 | # # if there is only one choice left they win. | ||||||||||||||||||||||
| 229 | # if ( scalar(@choices) == 1 ) { return $choices[0]} | ||||||||||||||||||||||
| 230 | 65 | my $mustwin = scalar(@choices) - 1; | |||||||||||||||||||||
| 231 | 65 | my $winner = ''; | |||||||||||||||||||||
| 232 | 65 | for my $c (@choices) { | |||||||||||||||||||||
| 233 | 434 | ▶ | if ( $scores->{$c} == $mustwin ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 234 | 32 | $winner .= $c; | |||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||
| 237 | 65 | return $winner; | |||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||
| 239 | |||||||||||||||||||||||
| 240 | ▶ | sub GreatestLoss ( $self, $A ) { | |||||||||||||||||||||
|
Subroutine
GreatestLoss: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||
| 241 | 26 | my $bigloss = 0; | |||||||||||||||||||||
| 242 | GREATESTLOSSLOOP: | ||||||||||||||||||||||
| 243 | 26 | for my $B ( $self->GetActiveList() ) { | |||||||||||||||||||||
| 244 | # for my $B ( keys $self->Active()->%* ) { | ||||||||||||||||||||||
| 245 | 290 | ▶ | next GREATESTLOSSLOOP if $B eq $A; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 246 | 264 | my %result = $self->{'Matrix'}{$A}{$B}->%*; | |||||||||||||||||||||
| 247 | # warn "$A : $B loser $result{'loser'} : margin $result{'margin'} $A: $result{$A} $B: $result{$B}"; | ||||||||||||||||||||||
| 248 | 264 | ▶ | if ( $result{'loser'} eq $A ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 249 | 111 | ▶ | $bigloss = $result{'margin'} if $result{'margin'} > $bigloss; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||
| 252 | 26 | return $bigloss; | |||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||
| 254 | |||||||||||||||||||||||
| 255 | 2 | ▶ | sub RankGreatestLoss ( $self, $active = undef ) { | ||||||||||||||||||||
|
Subroutine
RankGreatestLoss: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||
| 256 | 2 | my %loss = (); | |||||||||||||||||||||
| 257 | 2 | ▶ | $active = $self->Active() unless defined $active; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 258 | 2 | for my $A ( keys $active->%* ) { | |||||||||||||||||||||
| 259 | 21 | $loss{$A} = $self->GreatestLoss($A); | |||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||
| 261 | 2 | return Vote::Count::RankCount->Rank( \%loss ); | |||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||
| 263 | |||||||||||||||||||||||
| 264 | # reset active to choices | ||||||||||||||||||||||
| 265 | ▶ | sub ResetActive ( $self ) { | |||||||||||||||||||||
|
Subroutine
ResetActive: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 266 | 2 | $self->{'Active'} = dclone $self->BallotSet->{'choices'}; | |||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||
| 268 | |||||||||||||||||||||||
| 269 | ▶ | sub _getsmithguessforchoice ( $h, $matrix ) { | |||||||||||||||||||||
|
Subroutine
_getsmithguessforchoice: called CC=4 SCAR=13.9
| |||||||||||||||||||||||
| 270 | 74 | my @winners = ($h); | |||||||||||||||||||||
| 271 | 74 | for my $P ( keys $matrix->{$h}->%* ) { | |||||||||||||||||||||
| 272 | 561 | ▶ | if ( $matrix->{$h}{$P}{'winner'} eq $P ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 273 | 258 | push @winners, ($P); | |||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||
| 275 | elsif ( $matrix->{$h}{$P}{'tie'} ) { | ||||||||||||||||||||||
| 276 | 35 | push @winners, ($P); | |||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||
| 279 | 74 | return ( map { $_ => 1 } @winners ); | |||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||
| 281 | |||||||||||||||||||||||
| 282 | ▶ | sub GetPairResult ( $self, $A, $B ) { | |||||||||||||||||||||
|
Subroutine
GetPairResult: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 283 | 5 | return $self->{'Matrix'}{$A}{$B}; | |||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||
| 285 | |||||||||||||||||||||||
| 286 | ▶ | sub GetPairWinner ( $self, $A, $B ) { | |||||||||||||||||||||
|
Subroutine
GetPairWinner: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||
| 287 | 2 | my $winner = $self->{'Matrix'}{$A}{$B}{'winner'}; | |||||||||||||||||||||
| 288 | 2 | ▶ | return $winner if $winner; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 289 | 0 | return ''; | |||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||
| 291 | |||||||||||||||||||||||
| 292 | ▶ | sub SmithSet ( $self ) { | |||||||||||||||||||||
|
Subroutine
SmithSet: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 293 | 10 | my $matrix = $self->{'Matrix'}; | |||||||||||||||||||||
| 294 | 10 | my @alist = ( keys $self->Active()->%* ); | |||||||||||||||||||||
| 295 | 10 | my $sets = {}; | |||||||||||||||||||||
| 296 | 10 | my $setcounts = {}; | |||||||||||||||||||||
| 297 | # my $shortest = scalar(@list); | ||||||||||||||||||||||
| 298 | 10 | for my $h (@alist) { | |||||||||||||||||||||
| 299 | 72 | my %set = Vote::Count::Matrix::_getsmithguessforchoice( $h, $matrix ); | |||||||||||||||||||||
| 300 | 72 | $sets->{$h} = \%set; | |||||||||||||||||||||
| 301 | # the keys of setcounts are the counts | ||||||||||||||||||||||
| 302 | 72 | $setcounts->{ scalar( keys %set ) }{$h} = 1; | |||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||
| 304 | 10 | my $proposal = {}; | |||||||||||||||||||||
| 305 | 10 | my $minset = min( keys( $setcounts->%* ) ); | |||||||||||||||||||||
| 306 | 10 | for my $h ( keys $setcounts->{$minset}->%* ) { | |||||||||||||||||||||
| 307 | 18 | for my $k ( keys( $sets->{$h}->%* ) ) { | |||||||||||||||||||||
| 308 | 34 | $proposal->{$k} = 1; | |||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||
| 311 | 10 | SMITHLOOP: while (1) { | |||||||||||||||||||||
| 312 | 15 | my $cntchoice = scalar( keys $proposal->%* ); | |||||||||||||||||||||
| 313 | 15 | for my $h ( keys $proposal->%* ) { | |||||||||||||||||||||
| 314 | 46 | $proposal = { %{$proposal}, %{ $sets->{$h} } }; | |||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||
| 316 | # done when no choices get added on a pass through loop | ||||||||||||||||||||||
| 317 | 15 | ▶ | if ( scalar( keys $proposal->%* ) == $cntchoice ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 318 | 10 | last SMITHLOOP; | |||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||
| 321 | 10 | return $proposal; | |||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||
| 323 | |||||||||||||||||||||||
| 324 | # ScoreMatrix as a table. | ||||||||||||||||||||||
| 325 | ▶ | sub ScoreTable ( $self ) { | |||||||||||||||||||||
|
Subroutine
ScoreTable: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 326 | 1 | my $scores = $self->ScoreMatrix(); | |||||||||||||||||||||
| 327 | 1 | my @header = ( 'Choice', 'Score' ); | |||||||||||||||||||||
| 328 | 1 | my @rows = ( \@header ); | |||||||||||||||||||||
| 329 | 1 | for my $c ( sort_hash( $scores, 'numeric', 'desc' ) ) { | |||||||||||||||||||||
| 330 | # for my $c ( sort ( keys $scores->%* ) ) { | ||||||||||||||||||||||
| 331 | 12 | push @rows, [ $c, $scores->{$c} ]; | |||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||
| 333 | 1 | return generate_table( rows => \@rows ); | |||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||
| 335 | |||||||||||||||||||||||
| 336 | 107 | ▶ | sub MatrixTable ( $self, $options = {} ) { | ||||||||||||||||||||
|
Subroutine
MatrixTable: called CC=12 SCAR=25.1
Pod
documented
| |||||||||||||||||||||||
| 337 | 107 | my @header = ( 'Choice', 'Wins', 'Losses', 'Ties' ); | |||||||||||||||||||||
| 338 | # the options option was never fully implemented, it shows what the | ||||||||||||||||||||||
| 339 | # structure would be if one were or if I finished the feature. | ||||||||||||||||||||||
| 340 | # leaving the code in place even though its useless. | ||||||||||||||||||||||
| 341 | my $o_topcount = | ||||||||||||||||||||||
| 342 | 107 | ▶ | defined $options->{'topcount'} ? $options->{'topcount'} : 0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 343 | 107 | ▶ | push @header, 'Top Count' if $o_topcount; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 344 | 107 | my @active = sort ( keys $self->Active()->%* ); | |||||||||||||||||||||
| 345 | 107 | my @rows = ( \@header ); # [ 'Rank', 'Choice', 'TopCount']); | |||||||||||||||||||||
| 346 | 107 | for my $A (@active) { | |||||||||||||||||||||
| 347 | 733 | my $wins = 0; | |||||||||||||||||||||
| 348 | 733 | my $ties = 0; | |||||||||||||||||||||
| 349 | 733 | my $losses = 0; | |||||||||||||||||||||
| 350 | 733 | ▶ | my $topcount = $o_topcount ? $options->{'topcount'} : 0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 351 | MTNEWROW: | ||||||||||||||||||||||
| 352 | 733 | for my $B (@active) { | |||||||||||||||||||||
| 353 | 5989 | ▶ | if ( $A eq $B ) { next MTNEWROW } | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 354 | elsif ( $self->{'Matrix'}{$A}{$B}{'winner'} eq $A ) { | ||||||||||||||||||||||
| 355 | 2441 | $wins++; | |||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||
| 357 | elsif ( $self->{'Matrix'}{$A}{$B}{'winner'} eq $B ) { | ||||||||||||||||||||||
| 358 | 2441 | $losses++; | |||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||
| 360 | elsif ( $self->{'Matrix'}{$A}{$B}{'tie'} ) { | ||||||||||||||||||||||
| 361 | 374 | $ties++; | |||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||
| 364 | 733 | my @newrow = ( $A, $wins, $losses, $ties ); | |||||||||||||||||||||
| 365 | 733 | ▶ | push @newrow, $topcount if $o_topcount; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 366 | 733 | push @rows, \@newrow; | |||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||
| 368 | 107 | return generate_table( rows => \@rows ); | |||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||
| 370 | |||||||||||||||||||||||
| 371 | ▶ | sub PairingVotesTable ( $self ) { | |||||||||||||||||||||
|
Subroutine
PairingVotesTable: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 372 | 87 | my @rows = ( [qw/Choice Choice Votes Opponent Votes/] ); | |||||||||||||||||||||
| 373 | 87 | my @choices = sort ( keys $self->Active()->%* ); | |||||||||||||||||||||
| 374 | 87 | for my $Choice (@choices) { | |||||||||||||||||||||
| 375 | 627 | push @rows, [$Choice]; | |||||||||||||||||||||
| 376 | 627 | for my $Opponent (@choices) { | |||||||||||||||||||||
| 377 | 5293 | my $Cstr = $Choice; | |||||||||||||||||||||
| 378 | 5293 | my $Ostr = $Opponent; | |||||||||||||||||||||
| 379 | 5293 | ▶ | next if $Opponent eq $Choice; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 380 | 4666 | my $CVote = $self->{'Matrix'}{$Choice}{$Opponent}{$Choice}; | |||||||||||||||||||||
| 381 | 4666 | my $OVote = $self->{'Matrix'}{$Choice}{$Opponent}{$Opponent}; | |||||||||||||||||||||
| 382 | 4666 | ▶ | if ( $self->{'Matrix'}{$Choice}{$Opponent}{'winner'} eq $Choice ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 383 | 2149 | $Cstr = "**$Cstr**"; | |||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||
| 385 | 4666 | ▶ | if ( $self->{'Matrix'}{$Choice}{$Opponent}{'winner'} eq $Opponent ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 386 | 2149 | $Ostr = "**$Ostr**"; | |||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||
| 388 | 4666 | push @rows, [ ' ', $Cstr, $CVote, $Ostr, $OVote ]; | |||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||
| 391 | 87 | return generate_table( rows => \@rows ); | |||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||
| 393 | |||||||||||||||||||||||
| 394 | 25 | 1; | |||||||||||||||||||||
| 395 | |||||||||||||||||||||||
| 396 | #buildpod | ||||||||||||||||||||||
| 397 | |||||||||||||||||||||||
| 398 | =pod | ||||||||||||||||||||||
| 399 | |||||||||||||||||||||||
| 400 | =head1 Win-Loss Matrix | ||||||||||||||||||||||
| 401 | |||||||||||||||||||||||
| 402 | Condorcet Pairwise Methods require a Win-Loss Matrix. This object takes an RCV BallotSet with an optional Active list and returns the Matrix as an object. The object is capable of Scoring itself, Calculating a Smith Set, and identifying Condorcet Winners and Losers. | ||||||||||||||||||||||
| 403 | |||||||||||||||||||||||
| 404 | |||||||||||||||||||||||
| 405 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 406 | |||||||||||||||||||||||
| 407 | |||||||||||||||||||||||
| 408 | my $Matrix = | ||||||||||||||||||||||
| 409 | Vote::Count::Matrix->new( | ||||||||||||||||||||||
| 410 | 'BallotSet' => $myVoteCount->BallotSet() ); | ||||||||||||||||||||||
| 411 | my $Scores = $Matrix->ScoreMatrix(); | ||||||||||||||||||||||
| 412 | my %DominantSet = $Matrix->SmithSet()->%*; | ||||||||||||||||||||||
| 413 | my $CondorcetWinner = $Matrix->CondorcetWinner(); | ||||||||||||||||||||||
| 414 | |||||||||||||||||||||||
| 415 | |||||||||||||||||||||||
| 416 | |||||||||||||||||||||||
| 417 | =head1 Tie Breakers | ||||||||||||||||||||||
| 418 | |||||||||||||||||||||||
| 419 | A tie breaker may be specified by setting the Tie::Breaker attribute, see the Tie::Breaker module for more information. If using Range Ballots 'none' and 'approval' are the only currently supported options. | ||||||||||||||||||||||
| 420 | |||||||||||||||||||||||
| 421 | |||||||||||||||||||||||
| 422 | =head2 new | ||||||||||||||||||||||
| 423 | |||||||||||||||||||||||
| 424 | Parameters: | ||||||||||||||||||||||
| 425 | |||||||||||||||||||||||
| 426 | |||||||||||||||||||||||
| 427 | =head3 BallotSet (required) | ||||||||||||||||||||||
| 428 | |||||||||||||||||||||||
| 429 | A Ballot Set reference as generated by ReadBallots, which can be retrieved from a Vote::Count object via the ->BallotSet() method. | ||||||||||||||||||||||
| 430 | |||||||||||||||||||||||
| 431 | Both Ranked Choice and Range BallotSets are supported. | ||||||||||||||||||||||
| 432 | |||||||||||||||||||||||
| 433 | |||||||||||||||||||||||
| 434 | =head3 Active (optional) | ||||||||||||||||||||||
| 435 | |||||||||||||||||||||||
| 436 | A hash reference with active choices as the keys. The default value is all of the choices defined in the BallotSet. | ||||||||||||||||||||||
| 437 | |||||||||||||||||||||||
| 438 | |||||||||||||||||||||||
| 439 | =head3 Logging (optional) | ||||||||||||||||||||||
| 440 | |||||||||||||||||||||||
| 441 | Has the logging methods of L. | ||||||||||||||||||||||
| 442 | |||||||||||||||||||||||
| 443 | |||||||||||||||||||||||
| 444 | =head1 Methods | ||||||||||||||||||||||
| 445 | |||||||||||||||||||||||
| 446 | |||||||||||||||||||||||
| 447 | =head2 MatrixTable | ||||||||||||||||||||||
| 448 | |||||||||||||||||||||||
| 449 | Returns a MarkDown formatted table with the wins losses and ties for each Active Choice as text. | ||||||||||||||||||||||
| 450 | |||||||||||||||||||||||
| 451 | |||||||||||||||||||||||
| 452 | =head2 PairingVotesTable | ||||||||||||||||||||||
| 453 | |||||||||||||||||||||||
| 454 | Returns a MarkDown formatted table with the votes for all of the pairings. | ||||||||||||||||||||||
| 455 | |||||||||||||||||||||||
| 456 | |||||||||||||||||||||||
| 457 | =head2 GetPairResult ( $A, $B ) | ||||||||||||||||||||||
| 458 | |||||||||||||||||||||||
| 459 | Returns the results of the pairing of two choices as a hashref. | ||||||||||||||||||||||
| 460 | |||||||||||||||||||||||
| 461 | |||||||||||||||||||||||
| 462 | { | ||||||||||||||||||||||
| 463 | 'FUDGESWIRL' => 6, | ||||||||||||||||||||||
| 464 | 'loser' => "STRAWBERRY", | ||||||||||||||||||||||
| 465 | 'margin' => 2, | ||||||||||||||||||||||
| 466 | 'STRAWBERRY' => 4, | ||||||||||||||||||||||
| 467 | 'tie' => 0, | ||||||||||||||||||||||
| 468 | 'winner' => "FUDGESWIRL" | ||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||
| 470 | |||||||||||||||||||||||
| 471 | |||||||||||||||||||||||
| 472 | |||||||||||||||||||||||
| 473 | =head2 GetPairWinner ( $A, $B ) | ||||||||||||||||||||||
| 474 | |||||||||||||||||||||||
| 475 | Returns the winner of the pairing of two choices. If there is no Winner it returns a false value (empty string). | ||||||||||||||||||||||
| 476 | |||||||||||||||||||||||
| 477 | |||||||||||||||||||||||
| 478 | =head2 ScoreMatrix | ||||||||||||||||||||||
| 479 | |||||||||||||||||||||||
| 480 | Returns a HashRef of the choices and their Matrix Scores. The scoring is 1 for each win, 0 for losses and ties. In the event a choice has ties but no wins their score will be .001. Where N is the number of choices, a Condorcet Winner will have a score of N-1, a Condorcet Loser will have a score of 0. Since a choice with at least one tie but no wins is not defeated by all other choices they are not a Condorcet Loser, and thus those cases are scored with a near to zero value instead of 0. Methods that wish to treat no wins but tie case as a Condorcet Loser may test for a score less than 1. | ||||||||||||||||||||||
| 481 | |||||||||||||||||||||||
| 482 | |||||||||||||||||||||||
| 483 | =head2 ScoreTable | ||||||||||||||||||||||
| 484 | |||||||||||||||||||||||
| 485 | Returns the ScoreMatrix as a markdown compatible table. | ||||||||||||||||||||||
| 486 | |||||||||||||||||||||||
| 487 | |||||||||||||||||||||||
| 488 | =head2 LeastWins | ||||||||||||||||||||||
| 489 | |||||||||||||||||||||||
| 490 | Returns an array of the choice or choices with the fewest wins. | ||||||||||||||||||||||
| 491 | |||||||||||||||||||||||
| 492 | |||||||||||||||||||||||
| 493 | =head2 CondorcetLoser | ||||||||||||||||||||||
| 494 | |||||||||||||||||||||||
| 495 | Eliminates all Condorcet Losers from the Matrix Object's Active list. Returns a hashref. Takes an optional true false argument (default is false) to include choices that have tied but not won in the elimination. | ||||||||||||||||||||||
| 496 | |||||||||||||||||||||||
| 497 | |||||||||||||||||||||||
| 498 | { | ||||||||||||||||||||||
| 499 | verbose => 'verbose message', | ||||||||||||||||||||||
| 500 | terse => 'terse message', | ||||||||||||||||||||||
| 501 | eliminated => [ eliminated choices ], | ||||||||||||||||||||||
| 502 | eliminations => number of eliminated choices, | ||||||||||||||||||||||
| 503 | }; | ||||||||||||||||||||||
| 504 | |||||||||||||||||||||||
| 505 | |||||||||||||||||||||||
| 506 | |||||||||||||||||||||||
| 507 | =head2 CondorcetWinner | ||||||||||||||||||||||
| 508 | |||||||||||||||||||||||
| 509 | Returns either the Condorcet Winner or an empty string if there is none. | ||||||||||||||||||||||
| 510 | |||||||||||||||||||||||
| 511 | |||||||||||||||||||||||
| 512 | =head2 SmithSet | ||||||||||||||||||||||
| 513 | |||||||||||||||||||||||
| 514 | Finds the innermost Smith Set (Dominant Set). [ assistance in finding proof of the algorithm used would be appreciated so it could be correctly referenced in this documentation ]. A Dominant Set is a set which defeats all choices outside of that set. The inner Smith Set is the smallest possible Dominant Set. | ||||||||||||||||||||||
| 515 | |||||||||||||||||||||||
| 516 | Returns a hashref with the keys as the choices of the Smith Set. | ||||||||||||||||||||||
| 517 | |||||||||||||||||||||||
| 518 | |||||||||||||||||||||||
| 519 | =head2 ResetActive | ||||||||||||||||||||||
| 520 | |||||||||||||||||||||||
| 521 | Reset Active list to the choices list of the BallotSet. | ||||||||||||||||||||||
| 522 | |||||||||||||||||||||||
| 523 | |||||||||||||||||||||||
| 524 | =head2 GreatestLoss | ||||||||||||||||||||||
| 525 | |||||||||||||||||||||||
| 526 | Returns the greatest loss for a choice C<<< $MyMatrix->GreatestLoss( $A ) >>>. | ||||||||||||||||||||||
| 527 | |||||||||||||||||||||||
| 528 | |||||||||||||||||||||||
| 529 | =head2 RankGreatestLoss | ||||||||||||||||||||||
| 530 | |||||||||||||||||||||||
| 531 | Returns a RankCount object of the Greatest Loss for each choice. | ||||||||||||||||||||||
| 532 | |||||||||||||||||||||||
| 533 | =cut | ||||||||||||||||||||||
| 534 | |||||||||||||||||||||||
| 535 | #buildpod | ||||||||||||||||||||||
| 536 | |||||||||||||||||||||||
| 537 | #FOOTER | ||||||||||||||||||||||
| 538 | |||||||||||||||||||||||
| 539 | =pod | ||||||||||||||||||||||
| 540 | |||||||||||||||||||||||
| 541 | BUG TRACKER | ||||||||||||||||||||||
| 542 | |||||||||||||||||||||||
| 543 | L<https://github.com/brainbuz/Vote-Count/issues> | ||||||||||||||||||||||
| 544 | |||||||||||||||||||||||
| 545 | AUTHOR | ||||||||||||||||||||||
| 546 | |||||||||||||||||||||||
| 547 | John Karr (BRAINBUZ) brainbuz@cpan.org | ||||||||||||||||||||||
| 548 | |||||||||||||||||||||||
| 549 | CONTRIBUTORS | ||||||||||||||||||||||
| 550 | |||||||||||||||||||||||
| 551 | Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org. | ||||||||||||||||||||||
| 552 | |||||||||||||||||||||||
| 553 | LICENSE | ||||||||||||||||||||||
| 554 | |||||||||||||||||||||||
| 555 | 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>. | ||||||||||||||||||||||
| 556 | |||||||||||||||||||||||
| 557 | =cut | ||||||||||||||||||||||