From: Ian Jackson Date: Mon, 1 Aug 2016 15:11:25 +0000 (+0100) Subject: stv: wip debug X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=appendix-a6.git;a=commitdiff_plain;h=79268be36a253e1396688eb9956e021bbfca53ef;hp=fa0279b26ffd9237060ac96f2a50e5e23daa246d stv: wip debug --- diff --git a/stv b/stv index 7059906..a963609 100755 --- a/stv +++ b/stv @@ -118,6 +118,13 @@ sub historically_prefer ($@) { return $choices[0]; } +foreach my $c (keys %continuing) { + $sorted{$c} = { + Cand => $c, + Votes => [], + }; +} + for (;;) { $stage++; @@ -129,6 +136,13 @@ for (;;) { [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} }; if (!defined $firstpref) { @@ -138,7 +152,6 @@ 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"; @@ -175,13 +188,6 @@ for (;;) { 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; @@ -224,31 +230,42 @@ for (;;) { # Look for people to eliminate # We eliminate before trying to transfer surpluses # ERS 5.2.5 + my $elimvotebefore = 0; for (;;) { last unless @sorted; - printf "%18s\n", 'elimination round'; + if ($elimvotebefore) { + printf "%18s %s\n", 'elimination, sofar', pr $elimvotebefore; + } elsif (@surpluses) { + printf "%18s\n", 'elimination, maybe'; + } else { + printf "%18s\n", 'elimination, starts'; + } my @elim = equalpiles 'elim?', reverse @sorted; - my $elimvotetotal = sum0 map { $_->{Total} } @elim; + my $elimvotenow = sum0 map { $_->{Total} } @elim; - if (@surpluses) { + if (@surpluses || $elimvotebefore) { + # rule 5.2.2 if (@sorted == @elim) { - printf "%18s\n", 'no-elim, un-defer, (all-equal)'; + 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} - $elimvotetotal; + my $aheadby = $nextup->{Total} - ($elimvotenow + $elimvotebefore); unless ($deferredsurplus <= $aheadby) { # rule 5.2.2 (b) - printf "%18s %s\n", 'no-elim, un-defer', pr $aheadby; + 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; @@ -259,6 +276,7 @@ for (;;) { printf "%7s %10s %s\n", 'ELIM', $c, '----------'; my $votes = $s->{Votes}; votelog $_, "failed to stop $c elimination" foreach @$votes; + $elimvotebefore += $s->{Total}; delete $continuing{$c}; delete $sorted{$c}; push @unsorted, @$votes; @@ -266,6 +284,7 @@ for (;;) { $things_update->(); $need_to_transfer_surplus = 0; + last if $elim_tie; # no more, then! } next unless $need_to_transfer_surplus;