9 binmode STDIN, 'encoding(UTF-8)';
10 binmode STDOUT, 'encoding(UTF-8)';
11 binmode STDERR, 'encoding(UTF-8)';
20 my $choname = shift @_;
21 my $cho = $choices{$choname} = { @_, Index => (scalar @choices) };
22 push @choices, $choname;
31 if (m/^([A-Z]+)\s*\=\s*(\S.*)$/) {
32 my ($choname, $desc) = ($1,$2);
33 my $cho = addchoice($choname, Desc => $desc);
34 if ($desc =~ m/\[(\d+):(\d+)\]/) {
35 $cho->{Smaj} = [$1,$2];
36 } elsif ($desc =~ m/\[default\]/) {
39 } elsif (m/^V:\s+(\S+)\s+(\S.*)/) {
40 push @invotes, [ $1, $2 ];
41 } elsif (m/^quorum = ([0-9.]+)$/) {
48 $defcho ||= $choices{FD};
50 $defcho = addchoice('FD', Desc => "Further Discussion");
52 my $defi = $defcho->{Index};
53 die "FD has smaj?!" if $defcho->{Smaj};
56 # $vab[$ia][$ib] = V(A,B)
57 # Actually, it's a list of voters who prefer A to B (A.6(3)(1))
59 # Go through the voters and construct V(A,B)
61 print "\nPreference matrix\n";
63 foreach my $iv (@invotes) {
64 my ($votestr,$voter) = @$iv;
66 length $votestr eq @choices or die "wrong vote vector length";
67 my @vs = split //, $votestr;
68 foreach my $ix (0..$#vs) {
72 } elsif ($vchr =~ m/[0-9a-z]/) {
73 $vs[$ix] = ord($vchr);
78 foreach my $ia (0..$#vs) {
79 foreach my $ib ($ia+1..$#vs) {
82 if ($va < $vb) { push @{ $vab[$ia][$ib] }, $voter }
83 elsif ($vb < $va) { push @{ $vab[$ib][$ia] }, $voter }
87 die "voter $voter $@" if $@;
90 our @ch = map { $choices{$_} } @choices;
92 # Print the counts V(A,B)
93 foreach my $iy (-2..$#ch) {
94 foreach my $ix (-2..$#ch) {
103 } elsif ($ix==-2 && $iy==-2) {
106 printf "%5s ", $choices[$ix];
108 printf "%5s ", $choices[$iy];
110 my $v = \( $vab[$iy][$ix] );
113 printf "%5d ", (scalar @$$v);
124 print "dropping $choices[$i]: $why\n";
125 $ch[$i]{Dropped} = $why;
128 print "\nQuorum A.6(2) (quorum is $quorum)\n";
130 foreach my $i (0..$#choices) {
131 next if $ch[$i]{Dropped};
133 my $v = $vab[$i][$defi];
134 next if $v >= $quorum;
135 drop $i, "quorum ($v < $quorum)";
138 print "\nMajority ratio A.6(3)\n";
140 foreach my $i (0..$#choices) {
141 next if $ch[$i]{Dropped};
143 my $majr = $ch[$i]{Smaj};
144 $majr ||= [1,1]; # A.6(3)(3)
145 my $vad = scalar @{ $vab[$i][$defi] };
146 my $vda = scalar @{ $vab[$defi][$i] };
147 next if $vad * $majr->[1] > $vda * $majr->[0];
148 drop $i, "majority ratio ($vad * $majr->[1] <= $vda * $majr->[0])";
151 print "\nDefeats A.6(4)\n";
153 my $defeats = Graph::Directed->new;
156 return grep { !$ch[$_]{Dropped} } (0..$#ch);
159 foreach my $ia (chremain()) {
160 $defeats->add_vertex($choices[$ia]);
161 foreach my $ib (chremain()) {
162 my $vab = scalar @{ $vab[$ia][$ib] };
163 my $vba = scalar @{ $vab[$ib][$ia] };
164 next unless $vab > $vba;
165 my $diff = $vab - $vba;
166 print "defeat: $choices[$ia] beats $choices[$ib]",
167 " ($vab > $vba: $diff)\n";
168 $defeats->add_edge($choices[$ia],$choices[$ib]);
174 my $v = $vab[ $choices{$ca}{Index} ][ $choices{$cb}{Index} ];
180 my ($def1,$def2) = @_;
181 my ($ca,$cx) = @$def1;
182 my ($cb,$cy) = @$def1;
183 return 1 if chvab($ca, $cx) < chvab($cb, $cy);
184 return 1 if chvab($ca, $cx) == chvab($cb, $cy)
185 && chvab($cx, $ca) > chvab($cy, $cb);
194 print "defeats graph: $defeats\n";
196 print "\nTransitive closure A.6(5)\n";
198 my $tdefeats = $defeats->transitive_closure();
200 print "closure graph: $tdefeats\n";
202 print "\nSchwartz set A.6(6)\n";
204 $schwartz = $defeats->copy();
206 foreach my $ia (chremain()) {
207 foreach my $ib (chremain()) {
208 next if $tdefeats->has_edge($choices[$ia],$choices[$ib]);
209 next if !$tdefeats->has_edge($choices[$ib],$choices[$ia]);
210 print "not in Schwartz set: $choices[$ia] because $choices[$ib]\n";
211 $schwartz->delete_vertex($choices[$ia]);
216 print "\nDropping weakest defeats A.6(7)\n";
220 foreach my $edge ($schwartz->edges()) {
221 # print " considering @$edge\n";
223 # no weakest edges yet
224 } elsif (weaker($edge, $weakest[0])) {
225 # this edge is weaker than previous weakest, start new set
227 } elsif (weaker($weakest[0], $edge)) {
228 # weakest edge is weaker than this one, ignore this one
231 # weakest edge is exactly as weak as this one, add this one
233 push @weakest, $edge;
236 last unless @weakest;
239 printf "weakest defeats %d > %d\n",
240 chvab($w->[0], $w->[1]),
241 chvab($w->[1], $w->[0]);
242 foreach my $weakest (@weakest) {
243 my ($ca,$cb) = @$weakest;
244 print "weakest defeat $ca > $cb\n";
245 $defeats->delete_edge($ca,$cb);
248 print "defeats within the Schwartz set, round again\n";
251 print "no defeats within the Schwartz set\n";
252 print "final schwartz set:\n\n";
254 if ($schwartz->vertices() == 1) {
255 print "WINNER IS:\n";
257 print "WINNER IS ONE OF (CASTING VOTE DECIDES):\n";
260 printf " %-5s %s\n", $_, $choices{$_}{Desc}
261 foreach ($schwartz->vertices());