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 @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} };
}
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};
return $choices[0];
}
+foreach my $c (keys %continuing) {
+ $sorted{$c} = {
+ Cand => $c,
+ Votes => [],
+ };
+}
+
for (;;) {
$stage++;
[qw( _@stagerecord _@elected _@unsorted _%sorted _@surpluses _%continuing )]
);
+ my $placesremain = $places - @elected;
+
+ unless ($placesremain > 0) {
+ 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;
push @{ $sorted{$firstpref}{Votes} }, $vote;
}
}
- $sorted{$_}{Cand} = $_ foreach keys %sorted;
foreach my $firstpref (sort keys %sorted) {
- $sorted{$firstpref}{Total} = 0; # recount
foreach my $vote (@{ $sorted{$firstpref}{Votes} }) {
- votelog $vote, "counted $vote->{Weight} for $firstpref";
- $sorted{$firstpref}{Total} += $vote->{Weight};
+ votelog $vote, "counted for $firstpref ".pr $vote->{Weight};
}
}
my @sorted;
- my $sort_update = sub {
+ my $things_update = sub {
+ foreach my $s (@surpluses, values %sorted) {
+ $s->{Total} = sum0 map { $_->{Weight} } @{ $s->{Votes } };
+ }
@sorted = nsort_by { -$_->{Total} } values %sorted;
};
- $sort_update->();
+ $things_update->();
print DEBUG "SORTED\n", Dumper(\@sorted);
push @stagerecord, { map { ($_->{Cand}, $_->{Total}) } @sorted };
- my $totalvalid = 0;
+ my $totalvalid = 0/1;
my $countvalid = sub {
my ($l, $what) = @_;
foreach my $s (@$l) {
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 $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)
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} };
- $sort_update->();
+ $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};
+
$need_to_transfer_surplus = 0;
# before actually transferring a surplus, we will consider
# eliminating, and then reconsider with a lower quota
# Look for people to eliminate
# We eliminate before trying to transfer surpluses
# ERS 5.2.5
+ my $elimvotebefore = 0/1;
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 $elimvotetotal = sum0 map { $_->{Total} } @elim;
+ my $elimvotenow = sum0 map { $_->{Total} } @elim;
- if (@surpluses) {
+ if (@surpluses || $elimvotebefore) {
+ # rule 5.2.2
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};
- my $aheadby = $nextup->{Total} - $elimvotetotal;
+ my $aheadby = $nextup->{Total} - ($elimvotenow + $elimvotebefore);
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;
}
}
- 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;
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;
}
- $sort_update->();
+ $things_update->();
$need_to_transfer_surplus = 0;
+ last if $elim_tie; # no more, then!
}
next unless $need_to_transfer_surplus;
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;
}