X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=stv;h=fbd7b41050b18f535931223c8b7e0d8534c9f095;hb=0328306e06758d9fb43cec21611d044eda1acf09;hp=47984e80dc4bdb286f8d2f271d4e3701e7089b72;hpb=83ac3d9b8d6e365ca3f7083db8f37034456da0be;p=appendix-a6.git diff --git a/stv b/stv index 47984e8..fbd7b41 100755 --- a/stv +++ b/stv @@ -6,9 +6,10 @@ use strict; use utf8; use autodie; -#use bigrat; +use bigrat; +use Carp; use Data::Dumper; -use List::Util qw(sum); +use List::Util qw(sum0); use List::MoreUtils qw(nsort_by); # vote is @@ -27,7 +28,7 @@ open DEBUG, ">.stv.log" or die $!; while (<>) { next if m/^\w+$/; m/^(\w+) ([A-Z]+)$/ or die "$_ ?"; - my $prefs = $1; + my $prefs = $2; my $vote = { Voter => $1, Weight => 1.0, @@ -38,7 +39,8 @@ while (<>) { sub pr ($) { my ($f) = @_; - return sprintf "%10.6f=%-10s", $f, $f; + confess unless defined $f; + return sprintf "%10.6f = %-10s", $f, $f; } sub votelog ($$) { @@ -46,16 +48,6 @@ sub votelog ($$) { push @{ $vote->{Log} }, "stage $stage: $m"; } -sub filterout ($$) { - my ($cand, $why) = @_; - foreach my $vote (@allvotes) { - my $oldprefs = $vote->{Prefs}; - my @prefs = grep { $_ ne $cand } $oldprefs; - next if @prefs == @$oldprefs; - votelog $vote, "crossed out candidate $cand: $why"; - } -} - our @elected; # $elected[] = $candidate our @unsorted = @allvotes; @@ -72,6 +64,17 @@ our %continuing; # $continuing{$candidate}=1 our @stagerecord; # $stagerecord[]{$candidate} = $total +sub voteliveprefs ($) { + my ($vote) = @_; + grep { $continuing{$_} } @{ $vote->{Prefs} }; +} + +sub votelogfull ($$) { + my ($vote,$m) = @_; + votelog $vote, $m; + votelog $vote, ("continuing prefs: ". join ' ', voteliveprefs $vote); +} + foreach my $vote (@allvotes) { $continuing{$_}=1 foreach @{ $vote->{Prefs} }; } @@ -83,7 +86,8 @@ sub equalpiles ($@) { my $eqtotal = $s->{Total}; my $count = 0; while ($count < @sorted && $sorted[$count]{Total} == $eqtotal) { - printf "%7s %10s\n", $how, $sorted[$count]{Cand}; + printf "%7s %10s %s\n", $how, $sorted[$count]{Cand}, + pr $sorted[$count]{Total}; $count++; } return @sorted[ 0 .. $count-1 ]; @@ -115,17 +119,33 @@ sub historically_prefer ($@) { return $choices[0]; } +foreach my $c (keys %continuing) { + $sorted{$c} = { + Cand => $c, + Votes => [], + }; +} + for (;;) { $stage++; + printf "----- stage %d -----\n", $stage; + print DEBUG "#################### $stage ####################\n", Data::Dumper->Dump( [ \@stagerecord, \@elected, \@unsorted, \%sorted, \@surpluses, \%continuing ], [qw( _@stagerecord _@elected _@unsorted _%sorted _@surpluses _%continuing )] ); + my $placesremain = $places - @elected; + + unless ($placesremain > 0) { + printf "Complete.\n"; + last; + } + while (my $vote = shift @unsorted) { - my ($firstpref) = grep { $continuing{$_} } @{ $vote->{Prefs} }; + my ($firstpref) = voteliveprefs $vote; if (!defined $firstpref) { votelog $vote, "ballot exhausted"; push @exhausted, $vote; @@ -133,18 +153,21 @@ for (;;) { push @{ $sorted{$firstpref}{Votes} }, $vote; } } - $sorted{$_}{Cand} = $_ foreach keys %sorted; foreach my $firstpref (sort keys %sorted) { foreach my $vote (@{ $sorted{$firstpref}{Votes} }) { votelog $vote, "counted $vote->{Weight} for $firstpref"; - $sorted{$firstpref}{Total} += $vote->{Weight}; } } my @sorted; - my $sort_update = sub { + my $things_update = sub { + foreach my $s (@surpluses, values %sorted) { + $s->{Total} = sum0 map { $_->{Weight} } @{ $s->{Votes } }; + } @sorted = nsort_by { -$_->{Total} } values %sorted; }; - $sort_update->(); + $things_update->(); + + print DEBUG "SORTED\n", Dumper(\@sorted); push @stagerecord, { map { ($_->{Cand}, $_->{Total}) } @sorted }; @@ -159,22 +182,15 @@ for (;;) { $countvalid->(\@sorted, '1stpref'); $countvalid->(\@surpluses, 'surplus'); - printf "%7s %10s %s\n", 'TOTAL', '', pr $totalvalid; + printf "%7s %10s %s\n", 'TOTAL', '-----', pr $totalvalid; unless ($totalvalid > 0) { printf "No more votes!\n"; last; } - my $placesremain = $places - @elected; - - unless ($placesremain > 0) { - printf "Complete.\n"; - last; - } - my $quota = $totalvalid / ($placesremain + 1); - printf "%7s %10s %s\n", '', 'quota', pr $quota; + printf "%7s %10s %s\n", 'quota', '', pr $quota; my $need_to_transfer_surplus = 1; @@ -183,15 +199,15 @@ for (;;) { # (ERS rules 5.4.9) for (;;) { my $s = $sorted[0]; - my $topvoters = $s->{Weight}; + my $topvoters = $s->{Total}; my $surplus = $topvoters - $quota; last unless $surplus > 0; - printf "%7s %10s\n", 'ELECTED', $s->{Cand}; + printf "%7s %10s ***************\n", 'ELECTED', $s->{Cand}; push @elected, $s->{Cand}; my $derate = $topvoters / $surplus; - printf "%7s %10s\n", 'derate', $s->{Cand}, pr $derate; + printf "%7s %10s %s\n", 'derate', $s->{Cand}, pr $derate; foreach my $vote (@{ $s->{Votes} }) { votelog $vote, "elected $s->{Cand}, derated $derate"; @@ -201,31 +217,56 @@ for (;;) { delete $sorted{ $s->{Cand} }; delete $continuing{ $s->{Cand} }; - $sort_update->(); + $things_update->(); + printf "%7s %10s %s\n", 'surplus', $s->{Cand}, pr $s->{Total}; + $need_to_transfer_surplus = 0; # before actually transferring a surplus, we will consider # eliminating, and then reconsider with a lower quota } - my $deferredsurplus = sum map { $_->{Total} } @surpluses; - printf "%7s %10s %s\n", 'def.srp', 'total', pr $deferredsurplus; + my $deferredsurplus = sum0 map { $_->{Total} } @surpluses; + printf "%18s %s\n", 'deferred surplus', pr $deferredsurplus; # Look for people to eliminate # We eliminate before trying to transfer surpluses # ERS 5.2.5 + my $elimvotebefore = 0; for (;;) { last unless @sorted; - my @elim = equalpiles 'elim?', reverse @sorted; - my $elimvote = sum map { $_->{Total} } @elim; - my $elimvotetotal = $elimvote * scalar @elim; + if ($elimvotebefore) { + printf "%18s %s\n", 'elimination, sofar', pr $elimvotebefore; + } elsif (@surpluses) { + printf "%18s\n", 'elimination, maybe'; + } else { + printf "%18s\n", 'elimination, starts'; + } - if (@surpluses and $elimvotetotal > $deferredsurplus) { - printf "no-elim, un-defer\n"; - last; + my @elim = equalpiles 'elim?', reverse @sorted; + my $elimvotenow = sum0 map { $_->{Total} } @elim; + + if (@surpluses || $elimvotebefore) { + # rule 5.2.2 + if (@sorted == @elim) { + printf "%18s\n", 'no-elim (all-equal)'; + last; + } + my $nextup = $sorted[ $#sorted - @elim ]; + printf "%7s %10s %s\n", 'nextup', $nextup->{Cand}, + pr $nextup->{Total}; + my $aheadby = $nextup->{Total} - ($elimvotenow + $elimvotebefore); + unless ($deferredsurplus <= $aheadby) { + # rule 5.2.2 (b) + printf "%18s %s\n", 'no-elim (nextup)', pr $aheadby; + last; + } } - if ((scalar keys %continuing) - (scalar @elim) < $placesremain) { + my $elim_tie = + @elim > 1 && + (scalar keys %continuing) - (scalar @elim) < $placesremain; + if ($elim_tie) { # eliminate only one then, and try again printf "elim-tie!\n"; @elim = historically_prefer -1, @elim; @@ -233,21 +274,23 @@ for (;;) { foreach my $s (@elim) { my $c = $s->{Cand}; - printf "%7s %10s\n", 'ELIM', $c; + printf "%7s %10s %s\n", 'ELIM', $c, '----------'; my $votes = $s->{Votes}; - votelog $_, "failed to stop $c elimination" foreach @$votes; + votelogfull $_, "failed to stop $c elimination" foreach @$votes; + $elimvotebefore += $s->{Total}; delete $continuing{$c}; delete $sorted{$c}; push @unsorted, @$votes; } - $sort_update->(); + $things_update->(); $need_to_transfer_surplus = 0; + last if $elim_tie; # no more, then! } next unless $need_to_transfer_surplus; - @surpluses = nsort_by { $_->{Total} }, @surpluses; + @surpluses = nsort_by { $_->{Total} } @surpluses; my @surplusxfer = equalpiles 'xfer?', @surpluses; die unless @surplusxfer; @@ -259,7 +302,7 @@ for (;;) { my $c = $s->{Cand}; printf "%7s %10s\n", 'xfer', $c; my $votes = $s->{Votes}; - votelog $_, "surplus transferred" foreach @$votes; + votelogfull $_, "surplus transferred" foreach @$votes; @surpluses = grep { $_->{Cand} ne $c } @surpluses; push @unsorted, @$votes; }