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-Z]+)\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";
70 # actually, we pre-parse them into @invotes_v
71 # since we want to show them as a tally sheet anyway
73 foreach my $iv (@invotes_cc) {
75 foreach my $chn (m/\b\w+\b/g) {
76 next if $choices{$chn};
77 addchoice($chn, Desc => "($chn from voter ballot)");
81 foreach my $iv (@invotes_cc) {
82 my ($voter) = $iv->[0];
87 while (s{\(([^()]+)\)}{
88 my $x = $1; $x =~ s/[ =]+/=/g; $x;
92 print "# normalised $_ ($voter)\n";
94 my @ranks = (1000,) x @choices;
96 foreach (split /\s+/) {
98 my $cho = $choices{$_};
99 $cho or die "unknown choice $_ ($voter)";
100 my $ix = $cho->{Index};
105 my $vstr = join '', map {
107 $_ < 10 ? (sprintf "%d", $_) :
108 $_ < 36 ? (chr(ord('A') + $_ - 10)) : die
110 print "V: $vstr $voter\n";
111 push @invotes_v, [ $vstr, $voter ];
114 print "\nDetermining default option\n";
116 if ($defcho && $defcho->{Index} > -1) {
117 print "default option was specified: $choices[$defcho->{Index}]\n";
119 print "no default option\n";
121 foreach my $try (qw(FD SQ NOTA)) {
122 $defcho = $choices{$try};
126 print "guessed default option was: $choices[$defcho->{Index}]\n";
128 print "could not guess default option, assuming there is none\n";
132 my $defi = $defcho->{Index};
133 die "FD has smaj?!" if $defcho->{Smaj};
135 print "\nParsing devotee tally sheet ballots\n";
137 foreach my $iv (@invotes_v) {
138 my ($votestr,$voter) = @$iv;
140 length $votestr eq @choices or die "wrong vote vector length";
141 my @vs = split //, $votestr;
142 foreach my $ix (0..$#vs) {
146 } elsif ($vchr =~ m/[0-9a-z]/) {
147 $vs[$ix] = ord($vchr);
152 foreach my $ia (0..$#vs) {
153 foreach my $ib ($ia+1..$#vs) {
156 if ($va < $vb) { push @{ $vab[$ia][$ib] }, $voter }
157 elsif ($vb < $va) { push @{ $vab[$ib][$ia] }, $voter }
161 die "voter $voter $@" if $@;
164 print "\nPreference matrix\n";
166 our @ch = map { $choices{$_} } @choices;
168 # Print the counts V(A,B)
169 foreach my $iy (-2..$#ch) {
170 foreach my $ix (-2..$#ch) {
179 } elsif ($ix==-2 && $iy==-2) {
182 printf "%5s ", $choices[$ix];
184 printf "%5s ", $choices[$iy];
186 my $v = \( $vab[$iy][$ix] );
189 printf "%5d ", (scalar @$$v);
200 print "dropping $choices[$i]: $why\n";
201 $ch[$i]{Dropped} = $why;
205 print "\nQuorum A.6(2) (quorum is $quorum)\n";
207 foreach my $i (0..$#choices) {
208 next if $ch[$i]{Dropped};
210 my $v = $vab[$i][$defi];
211 next if $v >= $quorum;
212 drop $i, "quorum ($v < $quorum)";
215 print "\nMajority ratio A.6(3)\n";
217 foreach my $i (0..$#choices) {
218 next if $ch[$i]{Dropped};
220 my $majr = $ch[$i]{Smaj};
221 $majr ||= [1,1]; # A.6(3)(3)
222 my $vad = scalar @{ $vab[$i][$defi] };
223 my $vda = scalar @{ $vab[$defi][$i] };
224 next if $vad * $majr->[1] > $vda * $majr->[0];
225 drop $i, "majority ratio ($vad * $majr->[1] <= $vda * $majr->[0])";
229 print "\nDefeats A.6(4)\n";
231 my $defeats = Graph::Directed->new;
234 return grep { !$ch[$_]{Dropped} } (0..$#ch);
237 foreach my $ia (chremain()) {
238 $defeats->add_vertex($choices[$ia]);
239 foreach my $ib (chremain()) {
240 my $vab = scalar @{ $vab[$ia][$ib] };
241 my $vba = scalar @{ $vab[$ib][$ia] };
242 next unless $vab > $vba;
243 my $diff = $vab - $vba;
244 print "defeat: $choices[$ia] beats $choices[$ib]",
245 " ($vab > $vba = +$diff)\n";
246 $defeats->add_edge($choices[$ia],$choices[$ib]);
252 my $v = $vab[ $choices{$ca}{Index} ][ $choices{$cb}{Index} ];
258 my ($def1,$def2) = @_;
259 my ($ca,$cx) = @$def1;
260 my ($cb,$cy) = @$def2;
261 return 1 if chvab($ca, $cx) < chvab($cb, $cy);
262 return 1 if chvab($ca, $cx) == chvab($cb, $cy)
263 && chvab($cx, $ca) > chvab($cy, $cb);
272 print "defeats graph: $defeats\n";
274 print "\nTransitive closure A.6(5)\n";
276 my $tdefeats = $defeats->transitive_closure();
278 print "closure graph: $tdefeats\n";
280 print "\nSchwartz set A.6(6)\n";
282 $schwartz = $defeats->copy();
284 foreach my $ia (chremain()) {
285 foreach my $ib (chremain()) {
286 next if $tdefeats->has_edge($choices[$ia],$choices[$ib]);
287 next if !$tdefeats->has_edge($choices[$ib],$choices[$ia]);
288 print "not in Schwartz set: $choices[$ia] because $choices[$ib]\n";
289 $schwartz->delete_vertex($choices[$ia]);
294 print "set: ", (join " ", $schwartz->vertices()), "\n";
296 print "\nDropping weakest defeats A.6(7)\n";
300 foreach my $edge ($schwartz->edges()) {
301 # print " considering @$edge\n";
303 # no weakest edges yet
304 } elsif (weaker($edge, $weakest[0])) {
305 # this edge is weaker than previous weakest, start new set
307 } elsif (weaker($weakest[0], $edge)) {
308 # weakest edge is weaker than this one, ignore this one
311 # weakest edge is exactly as weak as this one, add this one
313 push @weakest, $edge;
316 last unless @weakest;
319 printf "weakest defeats are %d > %d\n",
320 chvab($w->[0], $w->[1]),
321 chvab($w->[1], $w->[0]);
322 foreach my $weakest (@weakest) {
323 my ($ca,$cb) = @$weakest;
324 print "a weakest defeat is $ca > $cb\n";
325 $defeats->delete_edge($ca,$cb);
328 print "\nDefeats within the Schwartz set, go round again\n";
331 print "no defeats within the Schwartz set\n";
332 print "final schwartz set:\n\n";
334 if ($schwartz->vertices() == 1) {
335 print "WINNER IS:\n";
337 print "WINNER IS ONE OF (CASTING VOTE DECIDES):\n";
340 printf " %-5s %s\n", $_, $choices{$_}{Desc}
341 foreach ($schwartz->vertices());