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;
30 if (m/^([A-Z]+)\s*\=\s*(\S.*)$/) {
31 my ($choname, $desc) = ($1,$2);
32 my $cho = addchoice($choname, Desc => $desc);
33 if ($desc =~ m/\[(\d+):(\d+)\]/) {
34 $cho->{Smaj} = [$1,$2];
35 } elsif ($desc =~ m/\[default\]/) {
38 } elsif (m/^V:\s+(\S+)\s+(\S.*)/) {
39 push @invotes, [ $1, $2 ];
40 } elsif (m/^quorum = ([0-9.]+)$/) {
47 $defcho ||= $choices{FD};
49 $defcho = addchoice('FD', Desc => "Further Discussion");
51 my $defi = $defcho->{Index};
52 die "FD has smaj?!" if $defcho->{Smaj};
55 # $vab[$ia][$ib] = V(A,B)
56 # Actually, it's a list of voters who prefer A to B (A.6(3)(1))
58 # Go through the voters and construct V(A,B)
60 print "\nDefeats matrix\n";
62 foreach my $iv (@invotes) {
63 my ($votestr,$voter) = @$iv;
65 length $votestr eq @choices or die "wrong vote vector length";
66 my @vs = split //, $votestr;
67 foreach my $ix (0..$#vs) {
71 } elsif ($vchr =~ m/[0-9a-z]/) {
72 $vs[$ix] = ord($vchr);
77 foreach my $ia (0..$#vs) {
78 foreach my $ib ($ia+1..$#vs) {
81 if ($va < $vb) { push @{ $vab[$ia][$ib] }, $voter }
82 elsif ($vb < $va) { push @{ $vab[$ib][$ia] }, $voter }
86 die "voter $voter $@" if $@;
89 our @ch = map { $choices{$_} } @choices;
91 # Print the counts V(A,B)
92 foreach my $iy (-2..$#ch) {
93 foreach my $ix (-2..$#ch) {
102 } elsif ($ix==-2 && $iy==-2) {
105 printf "%5s ", $choices[$ix];
107 printf "%5s ", $choices[$iy];
109 my $v = \( $vab[$iy][$ix] );
112 printf "%5d ", (scalar @$$v);
123 print "dropping $choices[$i]: $why\n";
124 $ch[$i]{Dropped} = $why;
127 print "\nQuorum A.6(2) (quorum is $quorum)\n";
129 foreach my $i (0..$#choices) {
130 next if $ch[$i]{Dropped};
132 my $v = $vab[$i][$defi];
133 next if $v >= $quorum;
134 drop $i, "quorum ($v < $quorum)";
137 print "\nMajority ratio A.6(3)\n";
139 foreach my $i (0..$#choices) {
140 next if $ch[$i]{Dropped};
142 my $majr = $ch[$i]{Smaj};
143 $majr ||= [1,1]; # A.6(3)(3)
144 my $vad = scalar @{ $vab[$i][$defi] };
145 my $vda = scalar @{ $vab[$defi][$i] };
146 next if $vad * $majr->[1] > $vda * $majr->[0];
147 drop $i, "majority ratio ($vad * $majr->[1] <= $vda * $majr->[0])";
150 my $defeats = Graph::Directed->new; # A.6(4)
153 return grep { !$ch[$_]{Dropped} } (0..$#ch);
156 foreach my $ia (chremain()) {
157 $defeats->add_vertex($ia);
158 foreach my $ib (chremain()) {
159 my $vab = scalar @{ $vab[$ia][$ib] };
160 my $vba = scalar @{ $vab[$ib][$ia] };
161 next unless $vab > $vba;
162 print "defeat: $choices[$ia] beats $choices[$ib] ($vab > $vba)\n";
163 $defeats->add_vertex($ia,$ib);
169 my ($def1,$def2) = @_;
170 my ($ia,$ix) = @$def1;
171 my ($ib,$iy) = @$def1;
172 return 1 if $vab[$ia][$ix] < $vab[$ib][$iy];
173 return 1 if $vab[$ia][$ix] == $vab[$ib][$iy]
174 && $vab[$ix][$ia] > $vab[$iy][$ib];
183 print "defeats graph: $defeats\n";
185 print "\nTransitive closure A.6(5)\n";
187 my $tdefeats = $defeats->transitive_closure();
189 print "closure graph: $tdefeats\n";
191 print "\nSchwartz set A.6(6)\n";
193 $schwartz = $defeats->copy();
195 foreach my $ia (chremain()) {
196 foreach my $ib (chremain()) {
197 next if $tdefeats->has_edge($ia,$ib);
198 next if !$tdefeats->has_edge($ib,$ia);
199 print "not in Schwartz set: $choices[$ia] because $choices[$ib]\n";
200 $schwartz->delete_vertex($ia);
205 print "\nDropping weakest defeats A.6(7)\n";
209 foreach my $edge ($schwartz->edges()) {
211 # no weakest edges yet
212 } elsif (weaker($edge, $weakest[0])) {
213 # this edge is weaker than previous weakest, start new set
215 } elsif (weaker($weakest[0], $edge)) {
216 # weakest edge is weaker than this one, ignore this one
219 # weakest edge is exactly as weak as this one, add this one
221 push @weakest, $edge;
224 last unless @weakest;
226 printf "weakest defeats %d > %d",
227 (scalar @{ $vab[$_->[0]][$_->[1]] }),
228 (scalar @{ $vab[$_->[1]][$_->[0]] });
229 foreach my $weakest (@weakest) {
230 my ($ia,$ib) = @$weakest;
231 print "weakest defeat $choices[$ia] > $choices[$ib]\n";
232 $defeats->delete_edge($ia,$ib);
235 print "defeats within the Schwartz set, round again\n";
238 print "no defeats within the Schwartz set\n";
239 print "final schwartz set:\n\n";
241 if ($schwartz->vertices() == 1) {
242 print "WINNER IS:\n";
244 print "WINNER IS ONE OF (CASTING VOTE DECIDES):\n";
247 printf " %-5s %s\n", $choices[$_], $ch[$_]{Desc}
248 foreach ($schwartz->vertices());