3 # parses some input about ballots and computes the result according
4 # to Debian Constitution A.6.
6 # usage: .../compute [<input files>...]
7 # (uses stdin if none supplied)
9 # input files can be in any order and should contain lines like:
11 # <votername> :: <PREF>, (<PREF>=<PREF>), <PREF>, ....
12 # V: <tallyprefs> <votername>
13 # <ABBREV> = <description...>
14 # <ABBREV> = <description...> [<rat>:<io>]
15 # <ABBREV> = <description...> [default]
21 # <votername> is any non-whitespace characters
23 # <ABBREV> and <PREF> are abbreviations for choices on the ballot.
24 # They must be ASCII alphanumeric. Numeric preferences are
25 # not recommended as they are confusing (particularly when
26 # tally sheets are in use).
28 # It is best to avoid FD, SQ and NOTA except for the default
29 # option; if no default option is specified, it will default
30 # to one of these unless a "nodefault" line is found.
32 # In the "::" form, the commas are optional. Either ( ) or = may
33 # be used to indicate a set of equal preferences.
35 # <tallyprefs> is a tally sheet preference order: that is,
36 # a single uppercase base36 digit for each ballot choice,
37 # (in the order they were specified by "=" lines), giving
38 # the voter's preference rank for that option. The numbers
39 # need not be distinct or contiguous. "-" may be used for
40 # unranked preferences.
42 # <rat>:<io> is the majority ratio for the option
44 # <quorum> is the quorum
46 # and also allowed are
49 # blank lines } ignored
50 # lines starting with whitespace }
59 binmode STDIN, 'encoding(UTF-8)';
60 binmode STDOUT, 'encoding(UTF-8)';
61 binmode STDERR, 'encoding(UTF-8)';
71 my $choname = shift @_;
72 my $cho = $choices{$choname} = {
73 @_, Index => (scalar @choices)
75 push @choices, $choname;
84 if (m/^([A-Z0-9]+)\s*\=\s*(\S.*)$/) {
85 my ($choname, $desc) = ($1,$2);
86 my $cho = addchoice($choname, Desc => $desc);
87 if ($desc =~ m/\[(\d+):(\d+)\]/) {
88 $cho->{Smaj} = [$1,$2];
89 } elsif ($desc =~ m/\[default\]/) {
92 } elsif (m/^V:\s+(\S+)\s+(\S.*)/) {
93 push @invotes_v, [ $1, $2 ];
94 } elsif (m/^(\S+)\s*\:\:\s*/) {
95 push @invotes_cc, [ $1, "$'" ];
96 } elsif (m/^quorum = ([0-9.]+)$/) {
98 } elsif (m/^nodefault$/) {
99 $defcho = { Index => -1 }
106 # $vab[$ia][$ib] = V(A,B)
107 # Actually, it's a list of voters who prefer A to B (A.6(3)(1))
109 # Go through the voters and construct V(A,B)
111 print "\nParsing \`simple' style ballots\n# devotee-tally-begin\n"
113 # actually, we pre-parse them into @invotes_v
114 # since we want to show them as a tally sheet anyway
116 foreach my $iv (@invotes_cc) {
118 foreach my $chn (m/\b\w+\b/g) {
119 next if $choices{$chn};
120 addchoice($chn, Desc => "($chn from voter ballot)");
124 foreach my $iv (@invotes_cc) {
125 my ($voter) = $iv->[0];
130 while (s{\(([^()]+)\)}{
131 my $x = $1; $x =~ s/[ =]+/=/g; $x;
135 print "# normalised $_ ($voter)\n";
137 my @ranks = (1000,) x @choices;
139 foreach (split /\s+/) {
140 foreach (split /=/) {
141 my $cho = $choices{$_};
142 $cho or die "unknown choice $_ ($voter)";
143 my $ix = $cho->{Index};
148 my $vstr = join '', map {
150 $_ < 10 ? (sprintf "%d", $_) :
151 $_ < 36 ? (chr(ord('A') + $_ - 10)) : die
153 print "V: $vstr $voter\n";
154 push @invotes_v, [ $vstr, $voter ];
157 print "# devotee-tally-end\n"
160 print "\nDetermining default option\n";
162 if ($defcho && $defcho->{Index} > -1) {
163 print "default option was specified: $choices[$defcho->{Index}]\n";
165 print "no default option\n";
167 foreach my $try (qw(FD SQ NOTA)) {
168 $defcho = $choices{$try};
172 print "guessed default option: $choices[$defcho->{Index}]\n";
174 print "could not guess default option, assuming there is none\n";
178 my $defi = $defcho->{Index};
179 die "FD has smaj?!" if $defcho->{Smaj};
181 print "\nParsing devotee tally sheet ballots\n"
182 if @invotes_v > @invotes_cc;
184 foreach my $iv (@invotes_v) {
185 my ($votestr,$voter) = @$iv;
187 length $votestr eq @choices or die "wrong vote vector length";
188 my @vs = split //, $votestr;
189 foreach my $ix (0..$#vs) {
193 } elsif ($vchr =~ m/[0-9a-z]/) {
194 $vs[$ix] = ord($vchr);
199 foreach my $ia (0..$#vs) {
200 foreach my $ib ($ia+1..$#vs) {
203 if ($va < $vb) { push @{ $vab[$ia][$ib] }, $voter }
204 elsif ($vb < $va) { push @{ $vab[$ib][$ia] }, $voter }
208 die "voter $voter $@" if $@;
211 print "\nPreference matrix\n";
213 our @ch = map { $choices{$_} } @choices;
215 # Print the counts V(A,B)
216 foreach my $iy (-2..$#ch) {
217 foreach my $ix (-2..$#ch) {
226 } elsif ($ix==-2 && $iy==-2) {
229 printf "%5s ", $choices[$ix];
231 printf "%5s ", $choices[$iy];
233 my $v = \( $vab[$iy][$ix] );
236 printf "%5d ", (scalar @$$v);
247 print "dropping $choices[$i]: $why\n";
248 $ch[$i]{Dropped} = $why;
252 print "\nQuorum A.6(2) (quorum is $quorum)\n";
254 foreach my $i (0..$#choices) {
255 next if $ch[$i]{Dropped};
257 my $v = $vab[$i][$defi];
258 next if $v >= $quorum;
259 drop $i, "quorum ($v < $quorum)";
262 print "\nMajority ratio A.6(3)\n";
264 foreach my $i (0..$#choices) {
265 next if $ch[$i]{Dropped};
267 my $majr = $ch[$i]{Smaj};
268 $majr ||= [1,1]; # A.6(3)(3)
269 my $vad = scalar @{ $vab[$i][$defi] };
270 my $vda = scalar @{ $vab[$defi][$i] };
271 next if $vad * $majr->[1] > $vda * $majr->[0];
272 drop $i, "majority ratio ($vad:$vda <= $majr->[1]:$majr->[0])";
276 print "\nDefeats A.6(4)\n";
278 my $defeats = Graph::Directed->new;
281 return grep { !$ch[$_]{Dropped} } (0..$#ch);
284 foreach my $ia (chremain()) {
285 $defeats->add_vertex($choices[$ia]);
286 foreach my $ib (chremain()) {
287 my $vab = scalar @{ $vab[$ia][$ib] };
288 my $vba = scalar @{ $vab[$ib][$ia] };
289 next unless $vab > $vba;
290 my $diff = $vab - $vba;
291 print "defeat: $choices[$ia] beats $choices[$ib]",
292 " ($vab > $vba = +$diff)\n";
293 $defeats->add_edge($choices[$ia],$choices[$ib]);
299 my $v = $vab[ $choices{$ca}{Index} ][ $choices{$cb}{Index} ];
305 my ($def1,$def2) = @_;
306 my ($ca,$cx) = @$def1;
307 my ($cb,$cy) = @$def2;
308 return 1 if chvab($ca, $cx) < chvab($cb, $cy);
309 return 1 if chvab($ca, $cx) == chvab($cb, $cy)
310 && chvab($cx, $ca) > chvab($cy, $cb);
319 print "defeats graph: $defeats\n";
321 print "\nTransitive closure A.6(5)\n";
323 my $tdefeats = $defeats->transitive_closure();
325 # this makes the debugging output prettier
326 foreach my $ch (@choices) {
327 $tdefeats->delete_edge($ch,$ch);
329 print "closure graph: $tdefeats\n";
331 print "\nSchwartz set A.6(6)\n";
333 $schwartz = $defeats->copy();
335 foreach my $ia (chremain()) {
336 foreach my $ib (chremain()) {
337 next if $tdefeats->has_edge($choices[$ia],$choices[$ib]);
338 next if !$tdefeats->has_edge($choices[$ib],$choices[$ia]);
339 print "not in Schwartz set: $choices[$ia] because $choices[$ib]\n";
340 $schwartz->delete_vertex($choices[$ia]);
345 print "set: ", (join " ", $schwartz->vertices()), "\n";
347 print "\nDropping weakest defeats A.6(7)\n";
351 foreach my $edge ($schwartz->edges()) {
352 # print " considering @$edge\n";
354 # no weakest edges yet
355 } elsif (weaker($edge, $weakest[0])) {
356 # this edge is weaker than previous weakest, start new set
358 } elsif (weaker($weakest[0], $edge)) {
359 # weakest edge is weaker than this one, ignore this one
362 # weakest edge is exactly as weak as this one, add this one
364 push @weakest, $edge;
367 last unless @weakest;
370 printf "weakest defeats are %d > %d\n",
371 chvab($w->[0], $w->[1]),
372 chvab($w->[1], $w->[0]);
373 foreach my $weakest (@weakest) {
374 my ($ca,$cb) = @$weakest;
375 print "a weakest defeat is $ca > $cb\n";
376 $defeats->delete_edge($ca,$cb);
379 print "\nDefeats within the Schwartz set, go round again\n";
382 print "no defeats within the Schwartz set\n";
383 print "final schwartz set:\n\n";
385 if ($schwartz->vertices() == 1) {
386 print "WINNER IS:\n";
388 print "WINNER IS ONE OF (CASTING VOTE DECIDES):\n";
391 printf " %-5s %s\n", $_, $choices{$_}{Desc}
392 foreach ($schwartz->vertices());