X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=appendix-a6.git;a=blobdiff_plain;f=stv;h=3c6160e979f27ffa7f026ed59d20a4b2dabf2710;hp=3942b6b175f488c6dbbedfd6338ded2560eb450c;hb=163ff5fe92d09360697b9ca48e792e50625f5c0d;hpb=be8af598391869fa6bb4cf9ccf630a09f6c2edd8 diff --git a/stv b/stv index 3942b6b..3c6160e 100755 --- a/stv +++ b/stv @@ -3,6 +3,19 @@ # Does STV according to # http://www.rosenstiel.co.uk/stvrules/av/index.htm +# Usage: +# ./stv [TIEBREAK ...] PLACES [INPUT-FILES ...] +# +# PLACES is the number of seats to be filled +# +# INPUT-FILES are in the format: +# VOTER PQRST +# where P, Q, R, etc. are candidates. Candidates +# must be single letters. +# +# Each TIEBREAK is +# P,Q,... + use strict; use utf8; use autodie; @@ -20,6 +33,25 @@ use List::MoreUtils qw(nsort_by); our $stage=0; our @allvotes; +our %tiebreak; # $tiebreak{ winningcand }{ losingcand } = 1 + +while (@ARGV && $ARGV[0] =~ m/[<>]/) { + my ($losers, $winners) = ($? eq '<' ? ($`, $') : ($', $`)); + my @losers = sort split /\,/, $losers; + my @winners = sort split /\,/, $winners; + die "@losers @winners" if @losers>1 && @winners>1; + my @all = sort @losers, @winners; + my $record = sub { + my ($how, $howers) = @_; + return unless @$howers == 1; + die "@all $how" if defined $tiebreak{$how}{"@all"}; + $tiebreak{$how}{"@all"} = $howers->[0]; + }; + $record->('winner', \@winners); + $record->('loser', \@losers); + shift @ARGV; +} + our $places = shift @ARGV; die unless $places eq ($places + 0); @@ -95,18 +127,36 @@ sub equalpiles ($@) { sub historically_prefer ($@) { my ($signum, @choices) = @_; + # $signum==+1 means choose candidates with more early preferences return $choices[0] if @choices < 2; my $compare = sub { foreach my $sr (@stagerecord) { - my $d = $sr->{ $a->{Cand} } <=> $sr->{ $b->{Cand} }; - return $d * $signum if $d; + my $d = ($sr->{ $a->{Cand} }//0) <=> ($sr->{ $b->{Cand} }//0); + return -$d * $signum if $d; } return 0; }; @choices = sort $compare @choices; + + print DEBUG "historically_prefer\n", + Data::Dumper->Dump([\@choices, \@stagerecord], + [qw(_@choices _@stagerecord)]); + + foreach my $s (@choices) { + my $c = $s->{Cand}; + printf " %6s %10s |", 'tiebrk', $c; + foreach my $sr (@stagerecord) { + my $p = pr($sr->{$c} // 0); + $p =~ s/\.0+\b//g; + $p =~ s/ //g; + print "$p|"; + } + print "\n"; + } + $a = $choices[0]; my $numequal = 1; for (;;) { @@ -116,7 +166,17 @@ sub historically_prefer ($@) { $numequal++; } - die 'random choice unimplemented' if $numequal > 1; + if ($numequal > 1) { + my $how = $signum > 0 ? 'winner' : 'loser'; + my @all = sort map { $_->{Cand} } @choices[ 0 .. $numequal-1 ]; + my $hower = $tiebreak{$how}{"@all"}; + if (defined $hower) { + printf "%7.7s %10s !!!!!!!!!!\n", "TIE".(uc $how), $hower; + return grep { $_->{Cand} eq $hower } @choices; + } + die "need tie break for $how between @all\n"; + } + return $choices[0]; } @@ -161,7 +221,7 @@ for (;;) { } my @sorted; my $things_update = sub { - foreach my $s (@surpluses, values %sorted) { + foreach my $s (values %sorted) { $s->{Total} = sum0 map { $_->{Weight} } @{ $s->{Votes } }; } @sorted = nsort_by { -$_->{Total} } values %sorted; @@ -205,6 +265,7 @@ for (;;) { my $topvoters = $s->{Total}; my $surplus = $topvoters - $quota; last unless $surplus >= 0; + last unless $placesremain; my @elect = equalpiles 'elect?', @sorted; @@ -226,6 +287,7 @@ for (;;) { push @newsurpluses, $s; delete $sorted{ $s->{Cand} }; delete $continuing{ $s->{Cand} }; + $placesremain--; } $things_update->(); @@ -245,15 +307,18 @@ for (;;) { grep { !!voteliveprefs $_ } @$votes; + my $derate = 1/1; printf "%7s %10s %s\n", 'xfrable', $s->{Cand}, pr $transferrable; if ($transferrable > $surplus) { - my $derate = $transferrable / $surplus; + $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; } } + $s->{Total} = $transferrable / $derate; + push @surpluses, $s; $things_update->(); @@ -346,4 +411,14 @@ for (;;) { push @unsorted, @$votes; } +open VL, ">.stv.votes" or die $!; +foreach my $vote (@allvotes) { + print VL "----------------------------------------\n"; + print VL "voter $vote->{Voter}\n"; + print VL "prefs ". (join " ", @{ $vote->{Prefs} }). "\n"; + print VL $_, "\n" foreach @{ $vote->{Log} }; +} +print VL "========================================\n"; +close VL; + print "done.\n";