chiark / gitweb /
stv: correct surplus transfer value
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 1 Aug 2016 15:28:44 +0000 (16:28 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 1 Aug 2016 15:28:44 +0000 (16:28 +0100)
stv

diff --git a/stv b/stv
index fbd7b41050b18f535931223c8b7e0d8534c9f095..111b2bde21bbb8f95f4b82866b5ef495b63b013f 100755 (executable)
--- a/stv
+++ b/stv
@@ -140,7 +140,7 @@ for (;;) {
     my $placesremain = $places - @elected;
 
     unless ($placesremain > 0) {
     my $placesremain = $places - @elected;
 
     unless ($placesremain > 0) {
-       printf "Complete.\n";
+       printf "Complete: @elected\n";
        last;
     }
 
        last;
     }
 
@@ -194,6 +194,8 @@ for (;;) {
 
     my $need_to_transfer_surplus = 1;
 
 
     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)
     # Look for people to elect.
     # We elect as many as we can, rather than recomputing the (lower) quota
     # (ERS rules 5.4.9)
@@ -206,17 +208,43 @@ for (;;) {
        printf "%7s %10s ***************\n", 'ELECTED', $s->{Cand};
        push @elected, $s->{Cand};
 
        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} }) {
        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} };
 
        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};
 
        $things_update->();
        printf "%7s %10s %s\n", 'surplus', $s->{Cand}, pr $s->{Total};