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
26 def print_winner(result)
28 puts "There is no winner."
29 elsif result.winners.length == 1
30 puts "The winner is %s" % result.winners[0]
32 puts "There is a tie between the following candidates: %s" % result.winners.join(", ")
37 puts "USING CONDORCET..."
38 puts "The winner should be: B"
40 vote_array = Array.new
41 3.times {vote_array << "ABC".split("")}
42 3.times {vote_array << "CBA".split("")}
43 2.times {vote_array << "BAC".split("")}
45 print_winner( PureCondorcetVote.new(vote_array).result )
49 puts "USING CloneProofSSD..."
50 puts "The winner should be: E"
52 vote_array = Array.new
53 5.times {vote_array << "ACBED".split("")}
54 5.times {vote_array << "ADECB".split("")}
55 8.times {vote_array << "BEDAC".split("")}
56 3.times {vote_array << "CABED".split("")}
57 7.times {vote_array << "CAEBD".split("")}
58 2.times {vote_array << "CBADE".split("")}
59 7.times {vote_array << "DCEBA".split("")}
60 8.times {vote_array << "EBADC".split("")}
62 print_winner( CloneproofSSDVote.new(vote_array).result )
66 puts "USING CloneProofSSD..."
67 puts "The winner should be: D"
69 vote_array = Array.new
70 5.times {vote_array << "ACBD".split("")}
71 2.times {vote_array << "ACDB".split("")}
72 3.times {vote_array << "ADCB".split("")}
73 4.times {vote_array << "BACD".split("")}
74 3.times {vote_array << "CBDA".split("")}
75 3.times {vote_array << "CDBA".split("")}
76 1.times {vote_array << "DACB".split("")}
77 5.times {vote_array << "DBAC".split("")}
78 4.times {vote_array << "DCBA".split("")}
80 print_winner( CloneproofSSDVote.new(vote_array).result )
84 puts "USING CloneProofSSD..."
85 puts "The winner should be: B(?)"
87 vote_array = Array.new
88 3.times {vote_array << "ABCD".split("")}
89 2.times {vote_array << "DABC".split("")}
90 2.times {vote_array << "DBCA".split("")}
91 2.times {vote_array << "CBDA".split("")}
93 print_winner( CloneproofSSDVote.new(vote_array).result )
99 puts "The winner should be: B"
101 vote_array = Array.new
102 3.times {vote_array << "ABC".split("")}
103 3.times {vote_array << "CBA".split("")}
104 2.times {vote_array << "BAC".split("")}
106 print_winner( BordaVote.new(vote_array).result )
110 puts "USING PLURALITY..."
111 puts "The winner should be: C"
113 vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
115 print_winner( PluralityVote.new(vote_array).result )
120 puts "USING APPROVAL..."
121 puts "The winner should be: A"
123 vote_array = Array.new
124 10.times {vote_array << "AB".split("")}
125 10.times {vote_array << "CB".split("")}
126 11.times {vote_array << "AC".split("")}
127 5.times {vote_array << "A".split("")}
129 print_winner( ApprovalVote.new(vote_array).result )
133 puts "USING RUNOFF..."
134 puts "The winner shold be: A"
136 vote_array = Array.new
137 142.times {vote_array << "ABCD".split("")}
138 26.times {vote_array << "BCDA".split("")}
139 15.times {vote_array << "CDBA".split("")}
140 17.times {vote_array << "DCBA".split("")}
142 print_winner( InstantRunoffVote.new(vote_array).result )
146 puts "USING RUNOFF..."
147 puts "The winner shold be: D"
149 vote_array = Array.new
150 42.times {vote_array << "ABCD".split("")}
151 26.times {vote_array << "BCDA".split("")}
152 15.times {vote_array << "CDBA".split("")}
153 17.times {vote_array << "DCBA".split("")}
155 print_winner( InstantRunoffVote.new(vote_array).result )
159 puts "USING RUNOFF..."
160 puts "The winner shold be: C"
162 vote_array = Array.new
163 42.times {vote_array << "ABCD".split("")}
164 26.times {vote_array << "ACBD".split("")}
165 15.times {vote_array << "BACD".split("")}
166 32.times {vote_array << "BCAD".split("")}
167 14.times {vote_array << "CABD".split("")}
168 49.times {vote_array << "CBAD".split("")}
169 17.times {vote_array << "ABDC".split("")}
170 23.times {vote_array << "BADC".split("")}
171 37.times {vote_array << "BCDA".split("")}
172 11.times {vote_array << "CADB".split("")}
173 16.times {vote_array << "CBDA".split("")}
174 54.times {vote_array << "ADBC".split("")}
175 36.times {vote_array << "BDCA".split("")}
176 42.times {vote_array << "CDAB".split("")}
177 13.times {vote_array << "CDBA".split("")}
178 51.times {vote_array << "DABC".split("")}
179 33.times {vote_array << "DBCA".split("")}
180 39.times {vote_array << "DCAB".split("")}
181 12.times {vote_array << "DCBA".split("")}
183 print_winner( InstantRunoffVote.new(vote_array).result )