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"
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 "\nDetermining default option\n";
117 if ($defcho && $defcho->{Index} > -1) {
118 print "default option was specified: $choices[$defcho->{Index}]\n";
120 print "no default option\n";
122 foreach my $try (qw(FD SQ NOTA)) {
123 $defcho = $choices{$try};
127 print "guessed default option was: $choices[$defcho->{Index}]\n";
129 print "could not guess default option, assuming there is none\n";
133 my $defi = $defcho->{Index};
134 die "FD has smaj?!" if $defcho->{Smaj};
136 print "\nParsing devotee tally sheet ballots\n"
137 if @invotes_v > @invotes_cc;
139 foreach my $iv (@invotes_v) {
140 my ($votestr,$voter) = @$iv;
142 length $votestr eq @choices or die "wrong vote vector length";
143 my @vs = split //, $votestr;
144 foreach my $ix (0..$#vs) {
148 } elsif ($vchr =~ m/[0-9a-z]/) {
149 $vs[$ix] = ord($vchr);
154 foreach my $ia (0..$#vs) {
155 foreach my $ib ($ia+1..$#vs) {
158 if ($va < $vb) { push @{ $vab[$ia][$ib] }, $voter }
159 elsif ($vb < $va) { push @{ $vab[$ib][$ia] }, $voter }
163 die "voter $voter $@" if $@;
166 print "\nPreference matrix\n";
168 our @ch = map { $choices{$_} } @choices;
170 # Print the counts V(A,B)
171 foreach my $iy (-2..$#ch) {
172 foreach my $ix (-2..$#ch) {
181 } elsif ($ix==-2 && $iy==-2) {
184 printf "%5s ", $choices[$ix];
186 printf "%5s ", $choices[$iy];
188 my $v = \( $vab[$iy][$ix] );
191 printf "%5d ", (scalar @$$v);
202 print "dropping $choices[$i]: $why\n";
203 $ch[$i]{Dropped} = $why;
207 print "\nQuorum A.6(2) (quorum is $quorum)\n";
209 foreach my $i (0..$#choices) {
210 next if $ch[$i]{Dropped};
212 my $v = $vab[$i][$defi];
213 next if $v >= $quorum;
214 drop $i, "quorum ($v < $quorum)";
217 print "\nMajority ratio A.6(3)\n";
219 foreach my $i (0..$#choices) {
220 next if $ch[$i]{Dropped};
222 my $majr = $ch[$i]{Smaj};
223 $majr ||= [1,1]; # A.6(3)(3)
224 my $vad = scalar @{ $vab[$i][$defi] };
225 my $vda = scalar @{ $vab[$defi][$i] };
226 next if $vad * $majr->[1] > $vda * $majr->[0];
227 drop $i, "majority ratio ($vad:$vda <= $majr->[1]:$majr->[0])";
231 print "\nDefeats A.6(4)\n";
233 my $defeats = Graph::Directed->new;
236 return grep { !$ch[$_]{Dropped} } (0..$#ch);
239 foreach my $ia (chremain()) {
240 $defeats->add_vertex($choices[$ia]);
241 foreach my $ib (chremain()) {
242 my $vab = scalar @{ $vab[$ia][$ib] };
243 my $vba = scalar @{ $vab[$ib][$ia] };
244 next unless $vab > $vba;
245 my $diff = $vab - $vba;
246 print "defeat: $choices[$ia] beats $choices[$ib]",
247 " ($vab > $vba = +$diff)\n";
248 $defeats->add_edge($choices[$ia],$choices[$ib]);
254 my $v = $vab[ $choices{$ca}{Index} ][ $choices{$cb}{Index} ];
260 my ($def1,$def2) = @_;
261 my ($ca,$cx) = @$def1;
262 my ($cb,$cy) = @$def2;
263 return 1 if chvab($ca, $cx) < chvab($cb, $cy);
264 return 1 if chvab($ca, $cx) == chvab($cb, $cy)
265 && chvab($cx, $ca) > chvab($cy, $cb);
274 print "defeats graph: $defeats\n";
276 print "\nTransitive closure A.6(5)\n";
278 my $tdefeats = $defeats->transitive_closure();
280 print "closure graph: $tdefeats\n";
282 print "\nSchwartz set A.6(6)\n";
284 $schwartz = $defeats->copy();
286 foreach my $ia (chremain()) {
287 foreach my $ib (chremain()) {
288 next if $tdefeats->has_edge($choices[$ia],$choices[$ib]);
289 next if !$tdefeats->has_edge($choices[$ib],$choices[$ia]);
290 print "not in Schwartz set: $choices[$ia] because $choices[$ib]\n";
291 $schwartz->delete_vertex($choices[$ia]);
296 print "set: ", (join " ", $schwartz->vertices()), "\n";
298 print "\nDropping weakest defeats A.6(7)\n";
302 foreach my $edge ($schwartz->edges()) {
303 # print " considering @$edge\n";
305 # no weakest edges yet
306 } elsif (weaker($edge, $weakest[0])) {
307 # this edge is weaker than previous weakest, start new set
309 } elsif (weaker($weakest[0], $edge)) {
310 # weakest edge is weaker than this one, ignore this one
313 # weakest edge is exactly as weak as this one, add this one
315 push @weakest, $edge;
318 last unless @weakest;
321 printf "weakest defeats are %d > %d\n",
322 chvab($w->[0], $w->[1]),
323 chvab($w->[1], $w->[0]);
324 foreach my $weakest (@weakest) {
325 my ($ca,$cb) = @$weakest;
326 print "a weakest defeat is $ca > $cb\n";
327 $defeats->delete_edge($ca,$cb);
330 print "\nDefeats within the Schwartz set, go round again\n";
333 print "no defeats within the Schwartz set\n";
334 print "final schwartz set:\n\n";
336 if ($schwartz->vertices() == 1) {
337 print "WINNER IS:\n";
339 print "WINNER IS ONE OF (CASTING VOTE DECIDES):\n";
342 printf " %-5s %s\n", $_, $choices{$_}{Desc}
343 foreach ($schwartz->vertices());