3 # parses some input about ballots and computes the result according
4 # to Debian Constitution A.6.
6 # usage: .../compute [<input files>...]
17 binmode STDIN, 'encoding(UTF-8)';
18 binmode STDOUT, 'encoding(UTF-8)';
19 binmode STDERR, 'encoding(UTF-8)';
29 my $choname = shift @_;
30 my $cho = $choices{$choname} = {
31 @_, Index => (scalar @choices)
33 push @choices, $choname;
42 if (m/^([A-Z0-9]+)\s*\=\s*(\S.*)$/) {
43 my ($choname, $desc) = ($1,$2);
44 my $cho = addchoice($choname, Desc => $desc);
45 if ($desc =~ m/\[(\d+):(\d+)\]/) {
46 $cho->{Smaj} = [$1,$2];
47 } elsif ($desc =~ m/\[default\]/) {
50 } elsif (m/^V:\s+(\S+)\s+(\S.*)/) {
51 push @invotes_v, [ $1, $2 ];
52 } elsif (m/^(\S+)\s*\:\:\s*/) {
53 push @invotes_cc, [ $1, "$'" ];
54 } elsif (m/^quorum = ([0-9.]+)$/) {
56 } elsif (m/^nodefault$/) {
57 $defcho = { Index => -1 }
64 # $vab[$ia][$ib] = V(A,B)
65 # Actually, it's a list of voters who prefer A to B (A.6(3)(1))
67 # Go through the voters and construct V(A,B)
69 print "\nParsing \`simple' style ballots\n# devotee-tally-begin\n"
71 # actually, we pre-parse them into @invotes_v
72 # since we want to show them as a tally sheet anyway
74 foreach my $iv (@invotes_cc) {
76 foreach my $chn (m/\b\w+\b/g) {
77 next if $choices{$chn};
78 addchoice($chn, Desc => "($chn from voter ballot)");
82 foreach my $iv (@invotes_cc) {
83 my ($voter) = $iv->[0];
88 while (s{\(([^()]+)\)}{
89 my $x = $1; $x =~ s/[ =]+/=/g; $x;
93 print "# normalised $_ ($voter)\n";
95 my @ranks = (1000,) x @choices;
97 foreach (split /\s+/) {
99 my $cho = $choices{$_};
100 $cho or die "unknown choice $_ ($voter)";
101 my $ix = $cho->{Index};
106 my $vstr = join '', map {
108 $_ < 10 ? (sprintf "%d", $_) :
109 $_ < 36 ? (chr(ord('A') + $_ - 10)) : die
111 print "V: $vstr $voter\n";
112 push @invotes_v, [ $vstr, $voter ];
115 print "# devotee-tally-end\n"
118 print "\nDetermining default option\n";
120 if ($defcho && $defcho->{Index} > -1) {
121 print "default option was specified: $choices[$defcho->{Index}]\n";
123 print "no default option\n";
125 foreach my $try (qw(FD SQ NOTA)) {
126 $defcho = $choices{$try};
130 print "guessed default option: $choices[$defcho->{Index}]\n";
132 print "could not guess default option, assuming there is none\n";
136 my $defi = $defcho->{Index};
137 die "FD has smaj?!" if $defcho->{Smaj};
139 print "\nParsing devotee tally sheet ballots\n"
140 if @invotes_v > @invotes_cc;
142 foreach my $iv (@invotes_v) {
143 my ($votestr,$voter) = @$iv;
145 length $votestr eq @choices or die "wrong vote vector length";
146 my @vs = split //, $votestr;
147 foreach my $ix (0..$#vs) {
151 } elsif ($vchr =~ m/[0-9a-z]/) {
152 $vs[$ix] = ord($vchr);
157 foreach my $ia (0..$#vs) {
158 foreach my $ib ($ia+1..$#vs) {
161 if ($va < $vb) { push @{ $vab[$ia][$ib] }, $voter }
162 elsif ($vb < $va) { push @{ $vab[$ib][$ia] }, $voter }
166 die "voter $voter $@" if $@;
169 print "\nPreference matrix\n";
171 our @ch = map { $choices{$_} } @choices;
173 # Print the counts V(A,B)
174 foreach my $iy (-2..$#ch) {
175 foreach my $ix (-2..$#ch) {
184 } elsif ($ix==-2 && $iy==-2) {
187 printf "%5s ", $choices[$ix];
189 printf "%5s ", $choices[$iy];
191 my $v = \( $vab[$iy][$ix] );
194 printf "%5d ", (scalar @$$v);
205 print "dropping $choices[$i]: $why\n";
206 $ch[$i]{Dropped} = $why;
210 print "\nQuorum A.6(2) (quorum is $quorum)\n";
212 foreach my $i (0..$#choices) {
213 next if $ch[$i]{Dropped};
215 my $v = $vab[$i][$defi];
216 next if $v >= $quorum;
217 drop $i, "quorum ($v < $quorum)";
220 print "\nMajority ratio A.6(3)\n";
222 foreach my $i (0..$#choices) {
223 next if $ch[$i]{Dropped};
225 my $majr = $ch[$i]{Smaj};
226 $majr ||= [1,1]; # A.6(3)(3)
227 my $vad = scalar @{ $vab[$i][$defi] };
228 my $vda = scalar @{ $vab[$defi][$i] };
229 next if $vad * $majr->[1] > $vda * $majr->[0];
230 drop $i, "majority ratio ($vad:$vda <= $majr->[1]:$majr->[0])";
234 print "\nDefeats A.6(4)\n";
236 my $defeats = Graph::Directed->new;
239 return grep { !$ch[$_]{Dropped} } (0..$#ch);
242 foreach my $ia (chremain()) {
243 $defeats->add_vertex($choices[$ia]);
244 foreach my $ib (chremain()) {
245 my $vab = scalar @{ $vab[$ia][$ib] };
246 my $vba = scalar @{ $vab[$ib][$ia] };
247 next unless $vab > $vba;
248 my $diff = $vab - $vba;
249 print "defeat: $choices[$ia] beats $choices[$ib]",
250 " ($vab > $vba = +$diff)\n";
251 $defeats->add_edge($choices[$ia],$choices[$ib]);
257 my $v = $vab[ $choices{$ca}{Index} ][ $choices{$cb}{Index} ];
263 my ($def1,$def2) = @_;
264 my ($ca,$cx) = @$def1;
265 my ($cb,$cy) = @$def2;
266 return 1 if chvab($ca, $cx) < chvab($cb, $cy);
267 return 1 if chvab($ca, $cx) == chvab($cb, $cy)
268 && chvab($cx, $ca) > chvab($cy, $cb);
277 print "defeats graph: $defeats\n";
279 print "\nTransitive closure A.6(5)\n";
281 my $tdefeats = $defeats->transitive_closure();
283 # this makes the debugging output prettier
284 foreach my $ch (@choices) {
285 $tdefeats->delete_edge($ch,$ch);
287 print "closure graph: $tdefeats\n";
289 print "\nSchwartz set A.6(6)\n";
291 $schwartz = $defeats->copy();
293 foreach my $ia (chremain()) {
294 foreach my $ib (chremain()) {
295 next if $tdefeats->has_edge($choices[$ia],$choices[$ib]);
296 next if !$tdefeats->has_edge($choices[$ib],$choices[$ia]);
297 print "not in Schwartz set: $choices[$ia] because $choices[$ib]\n";
298 $schwartz->delete_vertex($choices[$ia]);
303 print "set: ", (join " ", $schwartz->vertices()), "\n";
305 print "\nDropping weakest defeats A.6(7)\n";
309 foreach my $edge ($schwartz->edges()) {
310 # print " considering @$edge\n";
312 # no weakest edges yet
313 } elsif (weaker($edge, $weakest[0])) {
314 # this edge is weaker than previous weakest, start new set
316 } elsif (weaker($weakest[0], $edge)) {
317 # weakest edge is weaker than this one, ignore this one
320 # weakest edge is exactly as weak as this one, add this one
322 push @weakest, $edge;
325 last unless @weakest;
328 printf "weakest defeats are %d > %d\n",
329 chvab($w->[0], $w->[1]),
330 chvab($w->[1], $w->[0]);
331 foreach my $weakest (@weakest) {
332 my ($ca,$cb) = @$weakest;
333 print "a weakest defeat is $ca > $cb\n";
334 $defeats->delete_edge($ca,$cb);
337 print "\nDefeats within the Schwartz set, go round again\n";
340 print "no defeats within the Schwartz set\n";
341 print "final schwartz set:\n\n";
343 if ($schwartz->vertices() == 1) {
344 print "WINNER IS:\n";
346 print "WINNER IS ONE OF (CASTING VOTE DECIDES):\n";
349 printf " %-5s %s\n", $_, $choices{$_}{Desc}
350 foreach ($schwartz->vertices());