chiark / gitweb /
stv: wip debug
[appendix-a6.git] / stv
diff --git a/stv b/stv
index 7059906ccb810b37f8cb06d58ab4253b8d33710d..fbd7b41050b18f535931223c8b7e0d8534c9f095 100755 (executable)
--- a/stv
+++ b/stv
@@ -48,16 +48,6 @@ sub votelog ($$) {
     push @{ $vote->{Log} }, "stage $stage: $m";
 }
 
     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;
 our @elected; # $elected[] = $candidate
 
 our @unsorted = @allvotes;
@@ -74,6 +64,17 @@ our %continuing; # $continuing{$candidate}=1
 
 our @stagerecord; # $stagerecord[]{$candidate} = $total
 
 
 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} };
 }
 foreach my $vote (@allvotes) {
     $continuing{$_}=1 foreach @{ $vote->{Prefs} };
 }
@@ -118,6 +119,13 @@ sub historically_prefer ($@) {
     return $choices[0];
 }
 
     return $choices[0];
 }
 
+foreach my $c (keys %continuing) {
+    $sorted{$c} = {
+        Cand => $c,
+        Votes => [],
+    };
+}
+
 for (;;) {
     $stage++;
 
 for (;;) {
     $stage++;
 
@@ -129,8 +137,15 @@ for (;;) {
 [qw( _@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) {
     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;
        if (!defined $firstpref) {
            votelog $vote, "ballot exhausted";
            push @exhausted, $vote;
@@ -138,7 +153,6 @@ for (;;) {
            push @{ $sorted{$firstpref}{Votes} }, $vote;
        }
     }
            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";
     foreach my $firstpref (sort keys %sorted) {
        foreach my $vote (@{ $sorted{$firstpref}{Votes} }) {
            votelog $vote, "counted $vote->{Weight} for $firstpref";
@@ -175,13 +189,6 @@ for (;;) {
        last;
     }
 
        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;
 
     my $quota = $totalvalid / ($placesremain + 1);
     printf "%7s %10s %s\n", 'quota', '', pr $quota;
 
@@ -224,31 +231,42 @@ for (;;) {
     # Look for people to eliminate
     # We eliminate before trying to transfer surpluses
     # ERS 5.2.5
     # Look for people to eliminate
     # We eliminate before trying to transfer surpluses
     # ERS 5.2.5
+    my $elimvotebefore = 0;
     for (;;) {
        last unless @sorted;
 
     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 @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) {
            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};
                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)
            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;
            }
        }
 
                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;
            # eliminate only one then, and try again
            printf "elim-tie!\n";
            @elim = historically_prefer -1, @elim;
@@ -258,7 +276,8 @@ for (;;) {
            my $c = $s->{Cand};
            printf "%7s %10s %s\n", 'ELIM', $c, '----------';
            my $votes = $s->{Votes};
            my $c = $s->{Cand};
            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;
            delete $continuing{$c};
            delete $sorted{$c};
            push @unsorted, @$votes;
@@ -266,6 +285,7 @@ for (;;) {
        
        $things_update->();
        $need_to_transfer_surplus = 0;
        
        $things_update->();
        $need_to_transfer_surplus = 0;
+       last if $elim_tie; # no more, then!
     }
     
     next unless $need_to_transfer_surplus;
     }
     
     next unless $need_to_transfer_surplus;
@@ -282,7 +302,7 @@ for (;;) {
     my $c = $s->{Cand};
     printf "%7s %10s\n", 'xfer', $c;
     my $votes = $s->{Votes};
     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;
 }
     @surpluses = grep { $_->{Cand} ne $c } @surpluses;
     push @unsorted, @$votes;
 }