9 binmode STDIN, 'encoding(UTF-8)';
10 binmode STDOUT, 'encoding(UTF-8)';
11 binmode STDERR, 'encoding(UTF-8)';
21 my $choname = shift @_;
22 my $cho = $choices{$choname} = { @_, Index => (scalar @choices) };
23 push @choices, $choname;
32 if (m/^([A-Z]+)\s*\=\s*(\S.*)$/) {
33 my ($choname, $desc) = ($1,$2);
34 my $cho = addchoice($choname, Desc => $desc);
35 if ($desc =~ m/\[(\d+):(\d+)\]/) {
36 $cho->{Smaj} = [$1,$2];
37 } elsif ($desc =~ m/\[default\]/) {
40 } elsif (m/^V:\s+(\S+)\s+(\S.*)/) {
41 push @invotes_v, [ $1, $2 ];
42 } elsif (m/^(\S+)\s*\:\:\s*/) {
43 push @invotes_cc, [ $1, "$'" ];
44 } elsif (m/^quorum = ([0-9.]+)$/) {
51 $defcho ||= $choices{FD};
53 $defcho = addchoice('FD', Desc => "Further Discussion");
55 my $defi = $defcho->{Index};
56 die "FD has smaj?!" if $defcho->{Smaj};
59 # $vab[$ia][$ib] = V(A,B)
60 # Actually, it's a list of voters who prefer A to B (A.6(3)(1))
62 # Go through the voters and construct V(A,B)
64 print "\nPreference matrix\n";
66 foreach my $iv (@invotes_cc) {
68 foreach my $chn (m/\b\w+\b/g) {
69 $choices{$chn} or addchoice($chn, Desc => "($chn from voter ballot)");
73 foreach my $iv (@invotes_cc) {
74 my ($voter) = $iv->[0];
79 while (s{\(([^()]+)\)}{
80 my $x = $1; $x =~ s/[ =]+/=/g; $x;
84 print "# normalised $_ ($voter)\n";
86 my @ranks = (1000,) x @choices;
88 foreach (split /\s+/) {
90 my $cho = $choices{$_};
91 $cho or die "unknown option $_ ($voter)";
92 my $ix = $cho->{Index};
97 my $vstr = join '', map {
99 $_ < 10 ? (sprintf "%d", $_) :
100 $_ < 36 ? (chr(ord('A') + $_ - 10)) : die
102 print "V: $vstr $voter\n";
103 push @invotes_v, [ $vstr, $voter ];
106 foreach my $iv (@invotes_v) {
107 my ($votestr,$voter) = @$iv;
109 length $votestr eq @choices or die "wrong vote vector length";
110 my @vs = split //, $votestr;
111 foreach my $ix (0..$#vs) {
115 } elsif ($vchr =~ m/[0-9a-z]/) {
116 $vs[$ix] = ord($vchr);
121 foreach my $ia (0..$#vs) {
122 foreach my $ib ($ia+1..$#vs) {
125 if ($va < $vb) { push @{ $vab[$ia][$ib] }, $voter }
126 elsif ($vb < $va) { push @{ $vab[$ib][$ia] }, $voter }
130 die "voter $voter $@" if $@;
133 our @ch = map { $choices{$_} } @choices;
135 # Print the counts V(A,B)
136 foreach my $iy (-2..$#ch) {
137 foreach my $ix (-2..$#ch) {
146 } elsif ($ix==-2 && $iy==-2) {
149 printf "%5s ", $choices[$ix];
151 printf "%5s ", $choices[$iy];
153 my $v = \( $vab[$iy][$ix] );
156 printf "%5d ", (scalar @$$v);
167 print "dropping $choices[$i]: $why\n";
168 $ch[$i]{Dropped} = $why;
171 print "\nQuorum A.6(2) (quorum is $quorum)\n";
173 foreach my $i (0..$#choices) {
174 next if $ch[$i]{Dropped};
176 my $v = $vab[$i][$defi];
177 next if $v >= $quorum;
178 drop $i, "quorum ($v < $quorum)";
181 print "\nMajority ratio A.6(3)\n";
183 foreach my $i (0..$#choices) {
184 next if $ch[$i]{Dropped};
186 my $majr = $ch[$i]{Smaj};
187 $majr ||= [1,1]; # A.6(3)(3)
188 my $vad = scalar @{ $vab[$i][$defi] };
189 my $vda = scalar @{ $vab[$defi][$i] };
190 next if $vad * $majr->[1] > $vda * $majr->[0];
191 drop $i, "majority ratio ($vad * $majr->[1] <= $vda * $majr->[0])";
194 print "\nDefeats A.6(4)\n";
196 my $defeats = Graph::Directed->new;
199 return grep { !$ch[$_]{Dropped} } (0..$#ch);
202 foreach my $ia (chremain()) {
203 $defeats->add_vertex($choices[$ia]);
204 foreach my $ib (chremain()) {
205 my $vab = scalar @{ $vab[$ia][$ib] };
206 my $vba = scalar @{ $vab[$ib][$ia] };
207 next unless $vab > $vba;
208 my $diff = $vab - $vba;
209 print "defeat: $choices[$ia] beats $choices[$ib]",
210 " ($vab > $vba = +$diff)\n";
211 $defeats->add_edge($choices[$ia],$choices[$ib]);
217 my $v = $vab[ $choices{$ca}{Index} ][ $choices{$cb}{Index} ];
223 my ($def1,$def2) = @_;
224 my ($ca,$cx) = @$def1;
225 my ($cb,$cy) = @$def2;
226 return 1 if chvab($ca, $cx) < chvab($cb, $cy);
227 return 1 if chvab($ca, $cx) == chvab($cb, $cy)
228 && chvab($cx, $ca) > chvab($cy, $cb);
237 print "defeats graph: $defeats\n";
239 print "\nTransitive closure A.6(5)\n";
241 my $tdefeats = $defeats->transitive_closure();
243 print "closure graph: $tdefeats\n";
245 print "\nSchwartz set A.6(6)\n";
247 $schwartz = $defeats->copy();
249 foreach my $ia (chremain()) {
250 foreach my $ib (chremain()) {
251 next if $tdefeats->has_edge($choices[$ia],$choices[$ib]);
252 next if !$tdefeats->has_edge($choices[$ib],$choices[$ia]);
253 print "not in Schwartz set: $choices[$ia] because $choices[$ib]\n";
254 $schwartz->delete_vertex($choices[$ia]);
259 print "set: ", (join " ", $schwartz->vertices()), "\n";
261 print "\nDropping weakest defeats A.6(7)\n";
265 foreach my $edge ($schwartz->edges()) {
266 # print " considering @$edge\n";
268 # no weakest edges yet
269 } elsif (weaker($edge, $weakest[0])) {
270 # this edge is weaker than previous weakest, start new set
272 } elsif (weaker($weakest[0], $edge)) {
273 # weakest edge is weaker than this one, ignore this one
276 # weakest edge is exactly as weak as this one, add this one
278 push @weakest, $edge;
281 last unless @weakest;
284 printf "weakest defeats are %d > %d\n",
285 chvab($w->[0], $w->[1]),
286 chvab($w->[1], $w->[0]);
287 foreach my $weakest (@weakest) {
288 my ($ca,$cb) = @$weakest;
289 print "a weakest defeat is $ca > $cb\n";
290 $defeats->delete_edge($ca,$cb);
293 print "\nDefeats within the Schwartz set, go round again\n";
296 print "no defeats within the Schwartz set\n";
297 print "final schwartz set:\n\n";
299 if ($schwartz->vertices() == 1) {
300 print "WINNER IS:\n";
302 print "WINNER IS ONE OF (CASTING VOTE DECIDES):\n";
305 printf " %-5s %s\n", $_, $choices{$_}{Desc}
306 foreach ($schwartz->vertices());