1 #!/usr/bin/ruby -I./lib
3 # election library -- a ruby library for elections
4 # copyright © 2005 MIT Media Lab and Benjamin Mako Hill
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 def print_winner(result)
27 puts "There is no winner."
28 elsif result.winners.length == 1
29 puts "The winner is %s" % result.winners[0]
31 puts "There is a tie between the following candidates: %s" % result.winners.join(", ")
36 puts "USING CONDORCET..."
37 puts "The winner should be: B"
39 vote_array = Array.new
40 3.times {vote_array << "ABC".split("")}
41 3.times {vote_array << "CBA".split("")}
42 2.times {vote_array << "BAC".split("")}
44 print_winner( PureCondorcetVote.new(vote_array).result )
48 puts "USING CloneProofSSD..."
49 puts "The winner should be: E"
51 vote_array = Array.new
52 5.times {vote_array << "ACBED".split("")}
53 5.times {vote_array << "ADECB".split("")}
54 8.times {vote_array << "BEDAC".split("")}
55 3.times {vote_array << "CABED".split("")}
56 7.times {vote_array << "CAEBD".split("")}
57 2.times {vote_array << "CBADE".split("")}
58 7.times {vote_array << "DCEBA".split("")}
59 8.times {vote_array << "EBADC".split("")}
61 print_winner( CloneproofSSDVote.new(vote_array).result )
65 puts "USING CloneProofSSD..."
66 puts "The winner should be: D"
68 vote_array = Array.new
69 5.times {vote_array << "ACBD".split("")}
70 2.times {vote_array << "ACDB".split("")}
71 3.times {vote_array << "ADCB".split("")}
72 4.times {vote_array << "BACD".split("")}
73 3.times {vote_array << "CBDA".split("")}
74 3.times {vote_array << "CDBA".split("")}
75 1.times {vote_array << "DACB".split("")}
76 5.times {vote_array << "DBAC".split("")}
77 4.times {vote_array << "DCBA".split("")}
79 print_winner( CloneproofSSDVote.new(vote_array).result )
83 puts "USING CloneProofSSD..."
84 puts "The winner should be: B(?)"
86 vote_array = Array.new
87 3.times {vote_array << "ABCD".split("")}
88 2.times {vote_array << "DABC".split("")}
89 2.times {vote_array << "DBCA".split("")}
90 2.times {vote_array << "CBDA".split("")}
92 print_winner( CloneproofSSDVote.new(vote_array).result )
98 puts "The winner should be: B"
100 vote_array = Array.new
101 3.times {vote_array << "ABC".split("")}
102 3.times {vote_array << "CBA".split("")}
103 2.times {vote_array << "BAC".split("")}
105 print_winner( BordaVote.new(vote_array).result )
109 puts "USING PLURALITY..."
110 puts "The winner should be: C"
112 vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
114 print_winner( PluralityVote.new(vote_array).result )
119 puts "USING APPROVAL..."
120 puts "The winner should be: A"
122 vote_array = Array.new
123 10.times {vote_array << "AB".split("")}
124 10.times {vote_array << "CB".split("")}
125 11.times {vote_array << "AC".split("")}
126 5.times {vote_array << "A".split("")}
128 print_winner( ApprovalVote.new(vote_array).result )