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