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};
my $placesremain = $places - @elected;
unless ($placesremain > 0) {
- printf "Complete.\n";
+ printf "Complete: @elected\n";
last;
}
}
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;
push @stagerecord, { map { ($_->{Cand}, $_->{Total}) } @sorted };
- my $totalvalid = 0;
+ my $totalvalid = 0/1;
my $countvalid = sub {
my ($l, $what) = @_;
foreach my $s (@$l) {
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)
my $s = $sorted[0];
my $topvoters = $s->{Total};
my $surplus = $topvoters - $quota;
- last unless $surplus > 0;
+ last unless $surplus >= 0;
+
+ my @elect = equalpiles 'elect?', @sorted;
+
+ if (@elect > $placesremain) {
+ # oh my god
+ @elect = historically_prefer +1, @elect;
+ printf "%7s %10s\n", 'tie!', $elect[0]{Cand};
+ }
- printf "%7s %10s ***************\n", 'ELECTED', $s->{Cand};
- push @elected, $s->{Cand};
+ foreach $s (@elect) {
+ 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}";
+ }
- foreach my $vote (@{ $s->{Votes} }) {
- votelog $vote, "elected $s->{Cand}, derated $derate";
- $vote->{Weight} /= $derate;
+ $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;
- delete $sorted{ $s->{Cand} };
- delete $continuing{ $s->{Cand} };
$things_update->();
printf "%7s %10s %s\n", 'surplus', $s->{Cand}, pr $s->{Total};
# 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;