3 # Does STV according to
4 # http://www.rosenstiel.co.uk/stvrules/av/index.htm
12 use List::Util qw(sum0);
13 use List::MoreUtils qw(nsort_by);
23 our $places = shift @ARGV;
24 die unless $places eq ($places + 0);
26 open DEBUG, ">.stv.log" or die $!;
30 m/^(\w+) ([A-Z]+)$/ or die "$_ ?";
35 Prefs => [ split //, $prefs ],
37 push @allvotes, $vote;
42 confess unless defined $f;
43 return sprintf "%10.6f = %-10s", $f, $f;
48 push @{ $vote->{Log} }, "stage $stage: $m";
52 my ($cand, $why) = @_;
53 foreach my $vote (@allvotes) {
54 my $oldprefs = $vote->{Prefs};
55 my @prefs = grep { $_ ne $cand } $oldprefs;
56 next if @prefs == @$oldprefs;
57 votelog $vote, "crossed out candidate $cand: $why";
61 our @elected; # $elected[] = $candidate
63 our @unsorted = @allvotes;
66 # $sorted{$firstpref}{Votes} = [ $vote, ... ]
67 # $sorted{$firstpref}{Cand} = $firstpref
68 # $sorted{$firstpref}{Total} = $totalweight
69 our @surpluses; # values same as %sorted
71 our @exhausted; # votes
73 our %continuing; # $continuing{$candidate}=1
75 our @stagerecord; # $stagerecord[]{$candidate} = $total
77 foreach my $vote (@allvotes) {
78 $continuing{$_}=1 foreach @{ $vote->{Prefs} };
82 my ($how, @sorted) = @_;
83 return () unless @sorted;
85 my $eqtotal = $s->{Total};
87 while ($count < @sorted && $sorted[$count]{Total} == $eqtotal) {
88 printf "%7s %10s %s\n", $how, $sorted[$count]{Cand},
89 pr $sorted[$count]{Total};
92 return @sorted[ 0 .. $count-1 ];
95 sub historically_prefer ($@) {
96 my ($signum, @choices) = @_;
98 return $choices[0] if @choices < 2;
101 foreach my $sr (@stagerecord) {
102 my $d = $sr->{ $a->{Cand} } <=> $sr->{ $b->{Cand} };
103 return $d * $signum if $d;
108 @choices = sort $compare, @choices;
112 last unless $numequal >= @choices;
113 $b = $choices[$numequal];
114 last if $compare->();
117 die 'random choice unimplemented' if $numequal > 1;
121 foreach my $c (keys %continuing) {
131 printf "----- stage %d -----\n", $stage;
133 print DEBUG "#################### $stage ####################\n",
135 [ \@stagerecord, \@elected, \@unsorted, \%sorted, \@surpluses, \%continuing ],
136 [qw( _@stagerecord _@elected _@unsorted _%sorted _@surpluses _%continuing )]
139 my $placesremain = $places - @elected;
141 unless ($placesremain > 0) {
142 printf "Complete.\n";
146 while (my $vote = shift @unsorted) {
147 my ($firstpref) = grep { $continuing{$_} } @{ $vote->{Prefs} };
148 if (!defined $firstpref) {
149 votelog $vote, "ballot exhausted";
150 push @exhausted, $vote;
152 push @{ $sorted{$firstpref}{Votes} }, $vote;
155 foreach my $firstpref (sort keys %sorted) {
156 foreach my $vote (@{ $sorted{$firstpref}{Votes} }) {
157 votelog $vote, "counted $vote->{Weight} for $firstpref";
161 my $things_update = sub {
162 foreach my $s (@surpluses, values %sorted) {
163 $s->{Total} = sum0 map { $_->{Weight} } @{ $s->{Votes } };
165 @sorted = nsort_by { -$_->{Total} } values %sorted;
169 print DEBUG "SORTED\n", Dumper(\@sorted);
171 push @stagerecord, { map { ($_->{Cand}, $_->{Total}) } @sorted };
174 my $countvalid = sub {
176 foreach my $s (@$l) {
177 printf "%-7s %10s %s\n", $what, $s->{Cand}, pr $s->{Total};
178 $totalvalid += $s->{Total};
181 $countvalid->(\@sorted, '1stpref');
182 $countvalid->(\@surpluses, 'surplus');
184 printf "%7s %10s %s\n", 'TOTAL', '-----', pr $totalvalid;
186 unless ($totalvalid > 0) {
187 printf "No more votes!\n";
191 my $quota = $totalvalid / ($placesremain + 1);
192 printf "%7s %10s %s\n", 'quota', '', pr $quota;
194 my $need_to_transfer_surplus = 1;
196 # Look for people to elect.
197 # We elect as many as we can, rather than recomputing the (lower) quota
201 my $topvoters = $s->{Total};
202 my $surplus = $topvoters - $quota;
203 last unless $surplus > 0;
205 printf "%7s %10s ***************\n", 'ELECTED', $s->{Cand};
206 push @elected, $s->{Cand};
208 my $derate = $topvoters / $surplus;
209 printf "%7s %10s %s\n", 'derate', $s->{Cand}, pr $derate;
211 foreach my $vote (@{ $s->{Votes} }) {
212 votelog $vote, "elected $s->{Cand}, derated $derate";
213 $vote->{Weight} /= $derate;
216 delete $sorted{ $s->{Cand} };
217 delete $continuing{ $s->{Cand} };
220 printf "%7s %10s %s\n", 'surplus', $s->{Cand}, pr $s->{Total};
222 $need_to_transfer_surplus = 0;
223 # before actually transferring a surplus, we will consider
224 # eliminating, and then reconsider with a lower quota
227 my $deferredsurplus = sum0 map { $_->{Total} } @surpluses;
228 printf "%18s %s\n", 'deferred surplus', pr $deferredsurplus;
230 # Look for people to eliminate
231 # We eliminate before trying to transfer surpluses
233 my $elimvotebefore = 0;
237 if ($elimvotebefore) {
238 printf "%18s %s\n", 'elimination, sofar', pr $elimvotebefore;
239 } elsif (@surpluses) {
240 printf "%18s\n", 'elimination, maybe';
242 printf "%18s\n", 'elimination, starts';
245 my @elim = equalpiles 'elim?', reverse @sorted;
246 my $elimvotenow = sum0 map { $_->{Total} } @elim;
248 if (@surpluses || $elimvotebefore) {
250 if (@sorted == @elim) {
251 printf "%18s\n", 'no-elim (all-equal)';
254 my $nextup = $sorted[ $#sorted - @elim ];
255 printf "%7s %10s %s\n", 'nextup', $nextup->{Cand},
257 my $aheadby = $nextup->{Total} - ($elimvotenow + $elimvotebefore);
258 unless ($deferredsurplus <= $aheadby) {
260 printf "%18s %s\n", 'no-elim (nextup)', pr $aheadby;
267 (scalar keys %continuing) - (scalar @elim) < $placesremain;
269 # eliminate only one then, and try again
270 printf "elim-tie!\n";
271 @elim = historically_prefer -1, @elim;
274 foreach my $s (@elim) {
276 printf "%7s %10s %s\n", 'ELIM', $c, '----------';
277 my $votes = $s->{Votes};
278 votelog $_, "failed to stop $c elimination" foreach @$votes;
279 $elimvotebefore += $s->{Total};
280 delete $continuing{$c};
282 push @unsorted, @$votes;
286 $need_to_transfer_surplus = 0;
287 last if $elim_tie; # no more, then!
290 next unless $need_to_transfer_surplus;
292 @surpluses = nsort_by { $_->{Total} } @surpluses;
293 my @surplusxfer = equalpiles 'xfer?', @surpluses;
294 die unless @surplusxfer;
296 if (@surplusxfer > 1) {
297 @surplusxfer = historically_prefer +1, @surplusxfer;
300 my $s = $surplusxfer[0];
302 printf "%7s %10s\n", 'xfer', $c;
303 my $votes = $s->{Votes};
304 votelog $_, "surplus transferred" foreach @$votes;
305 @surpluses = grep { $_->{Cand} ne $c } @surpluses;
306 push @unsorted, @$votes;