chiark / gitweb /
stv: correct surplus transfer value
[appendix-a6.git] / stv
diff --git a/stv b/stv
index a9636096ae9f90e1f1d58666bc2f5815360b7a04..111b2bde21bbb8f95f4b82866b5ef495b63b013f 100755 (executable)
--- a/stv
+++ b/stv
@@ -48,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;
@@ -74,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} };
 }
@@ -139,12 +140,12 @@ for (;;) {
     my $placesremain = $places - @elected;
 
     unless ($placesremain > 0) {
-       printf "Complete.\n";
+       printf "Complete: @elected\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;
@@ -193,6 +194,8 @@ for (;;) {
 
     my $need_to_transfer_surplus = 1;
 
+    my @newsurpluses;
+
     # Look for people to elect.
     # We elect as many as we can, rather than recomputing the (lower) quota
     # (ERS rules 5.4.9)
@@ -205,17 +208,43 @@ for (;;) {
        printf "%7s %10s ***************\n", 'ELECTED', $s->{Cand};
        push @elected, $s->{Cand};
 
-       my $derate = $topvoters / $surplus;
-       printf "%7s %10s %s\n", 'derate', $s->{Cand}, pr $derate;
-
        foreach my $vote (@{ $s->{Votes} }) {
-           votelog $vote, "elected $s->{Cand}, derated $derate";
-           $vote->{Weight} /= $derate;
+           votelog $vote, "elected $s->{Cand}";
        }
-       push @surpluses, $s;
+
+       $s->{Surplus} = $surplus;
+       push @newsurpluses, $s;
        delete $sorted{ $s->{Cand} };
        delete $continuing{ $s->{Cand} };
 
+       $things_update->();
+    }
+
+    foreach my $s (@newsurpluses) {
+       # calculate the transfer value of each surplus
+       # we do this simultaneously, but based on the number of
+       # continuing candidates (excluding all the ones elected already)
+       # ERS rule 5.3.3
+
+       my $votes = $s->{Votes};
+       my $surplus = $s->{Surplus};
+       my $transferrable =
+           sum0
+           map { $_->{Weight} }
+           grep { !!voteliveprefs $_ }
+           @$votes;
+
+       printf "%7s %10s %s\n", 'xfrable', $s->{Cand}, pr $transferrable;
+       if ($transferrable > $surplus) {
+           my $derate = $transferrable / $surplus;
+           printf "%7s %10s %s\n", 'derate', $s->{Cand}, pr $derate;
+           foreach my $vote (@{ $s->{Votes} }) {
+               votelog $vote, "part of surplus, derated ". pr $derate;
+               $vote->{Weight} /= $derate;
+           }
+       }
+       push @surpluses, $s;
+
        $things_update->();
        printf "%7s %10s %s\n", 'surplus', $s->{Cand}, pr $s->{Total};
 
@@ -275,7 +304,7 @@ for (;;) {
            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};
@@ -301,7 +330,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;
 }