chiark / gitweb /
stv: fix historically_prefer
[appendix-a6.git] / stv
diff --git a/stv b/stv
index 111b2bde21bbb8f95f4b82866b5ef495b63b013f..3942b6b175f488c6dbbedfd6338ded2560eb450c 100755 (executable)
--- a/stv
+++ b/stv
@@ -84,7 +84,7 @@ sub equalpiles ($@) {
     return () unless @sorted;
     my $s = $sorted[0];
     my $eqtotal = $s->{Total};
-    my $count = 0;
+    my $count = 0/1;
     while ($count < @sorted && $sorted[$count]{Total} == $eqtotal) {
        printf "%7s %10s %s\n", $how, $sorted[$count]{Cand},
            pr $sorted[$count]{Total};
@@ -106,13 +106,14 @@ sub historically_prefer ($@) {
        return 0;
     };
 
-    @choices = sort $compare, @choices;
+    @choices = sort $compare @choices;
     $a = $choices[0];
-    my $numequal = 0;
+    my $numequal = 1;
     for (;;) {
-       last unless $numequal >= @choices;
+       last if $numequal >= @choices;
        $b = $choices[$numequal];
        last if $compare->();
+       $numequal++;
     }
 
     die 'random choice unimplemented' if $numequal > 1;
@@ -155,7 +156,7 @@ for (;;) {
     }
     foreach my $firstpref (sort keys %sorted) {
        foreach my $vote (@{ $sorted{$firstpref}{Votes} }) {
-           votelog $vote, "counted $vote->{Weight} for $firstpref";
+           votelog $vote, "counted for $firstpref ".pr $vote->{Weight};
        }
     }
     my @sorted;
@@ -171,7 +172,7 @@ for (;;) {
 
     push @stagerecord, { map { ($_->{Cand}, $_->{Total}) } @sorted };
 
-    my $totalvalid = 0;
+    my $totalvalid = 0/1;
     my $countvalid = sub {
        my ($l, $what) = @_;
        foreach my $s (@$l) {
@@ -203,19 +204,29 @@ for (;;) {
        my $s = $sorted[0];
        my $topvoters = $s->{Total};
        my $surplus = $topvoters - $quota;
-       last unless $surplus > 0;
+       last unless $surplus >= 0;
 
-       printf "%7s %10s ***************\n", 'ELECTED', $s->{Cand};
-       push @elected, $s->{Cand};
+       my @elect = equalpiles 'elect?', @sorted;
 
-       foreach my $vote (@{ $s->{Votes} }) {
-           votelog $vote, "elected $s->{Cand}";
+       if (@elect > $placesremain) {
+           # oh my god
+           @elect = historically_prefer +1, @elect;
+           printf "%7s %10s\n", 'tie!', $elect[0]{Cand};
        }
 
-       $s->{Surplus} = $surplus;
-       push @newsurpluses, $s;
-       delete $sorted{ $s->{Cand} };
-       delete $continuing{ $s->{Cand} };
+       foreach $s (@elect) {
+           printf "%7s %10s ***************\n", 'ELECTED', $s->{Cand};
+           push @elected, $s->{Cand};
+
+           foreach my $vote (@{ $s->{Votes} }) {
+               votelog $vote, "elected $s->{Cand}";
+           }
+
+           $s->{Surplus} = $surplus;
+           push @newsurpluses, $s;
+           delete $sorted{ $s->{Cand} };
+           delete $continuing{ $s->{Cand} };
+       }
 
        $things_update->();
     }
@@ -259,7 +270,7 @@ for (;;) {
     # Look for people to eliminate
     # We eliminate before trying to transfer surpluses
     # ERS 5.2.5
-    my $elimvotebefore = 0;
+    my $elimvotebefore = 0/1;
     for (;;) {
        last unless @sorted;