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
21 require 'lib/rubyvote'
23 def print_winner(result)
25 puts "There is no winner."
26 elsif result.winners.length == 1
27 puts "The winner is %s" % result.winners[0]
29 puts "There is a tie between the following candidates: %s" % result.winners.join(", ")
34 puts "USING CONDORCET..."
35 puts "The winner should be: B"
37 vote_array = Array.new
38 3.times {vote_array << "ABC".split("")}
39 3.times {vote_array << "CBA".split("")}
40 2.times {vote_array << "BAC".split("")}
42 print_winner( PureCondorcetVote.new(vote_array).result )
46 puts "USING CloneProofSSD..."
47 puts "The winner should be: E"
49 vote_array = Array.new
50 5.times {vote_array << "ACBED".split("")}
51 5.times {vote_array << "ADECB".split("")}
52 8.times {vote_array << "BEDAC".split("")}
53 3.times {vote_array << "CABED".split("")}
54 7.times {vote_array << "CAEBD".split("")}
55 2.times {vote_array << "CBADE".split("")}
56 7.times {vote_array << "DCEBA".split("")}
57 8.times {vote_array << "EBADC".split("")}
59 print_winner( CloneproofSSDVote.new(vote_array).result )
63 puts "USING CloneProofSSD..."
64 puts "The winner should be: D"
66 vote_array = Array.new
67 5.times {vote_array << "ACBD".split("")}
68 2.times {vote_array << "ACDB".split("")}
69 3.times {vote_array << "ADCB".split("")}
70 4.times {vote_array << "BACD".split("")}
71 3.times {vote_array << "CBDA".split("")}
72 3.times {vote_array << "CDBA".split("")}
73 1.times {vote_array << "DACB".split("")}
74 5.times {vote_array << "DBAC".split("")}
75 4.times {vote_array << "DCBA".split("")}
77 print_winner( CloneproofSSDVote.new(vote_array).result )
81 puts "USING CloneProofSSD..."
82 puts "The winner should be: B(?)"
84 vote_array = Array.new
85 3.times {vote_array << "ABCD".split("")}
86 2.times {vote_array << "DABC".split("")}
87 2.times {vote_array << "DBCA".split("")}
88 2.times {vote_array << "CBDA".split("")}
90 print_winner( CloneproofSSDVote.new(vote_array).result )
96 puts "The winner should be: B"
98 vote_array = Array.new
99 3.times {vote_array << "ABC".split("")}
100 3.times {vote_array << "CBA".split("")}
101 2.times {vote_array << "BAC".split("")}
103 print_winner( BordaVote.new(vote_array).result )
107 puts "USING PLURALITY..."
108 puts "The winner should be: C"
110 vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
112 print_winner( PluralityVote.new(vote_array).result )
117 puts "USING APPROVAL..."
118 puts "The winner should be: A"
120 vote_array = Array.new
121 10.times {vote_array << "AB".split("")}
122 10.times {vote_array << "CB".split("")}
123 11.times {vote_array << "AC".split("")}
124 5.times {vote_array << "A".split("")}
126 print_winner( ApprovalVote.new(vote_array).result )
131 puts "The winner shold be: A"
133 vote_array = Array.new
134 142.times {vote_array << "ABCD".split("")}
135 26.times {vote_array << "BCDA".split("")}
136 15.times {vote_array << "CDBA".split("")}
137 17.times {vote_array << "DCBA".split("")}
139 print_winner( InstantRunoffVote.new(vote_array).result )
144 puts "The winner shold be: D"
146 vote_array = Array.new
147 42.times {vote_array << "ABCD".split("")}
148 26.times {vote_array << "BCDA".split("")}
149 15.times {vote_array << "CDBA".split("")}
150 17.times {vote_array << "DCBA".split("")}
152 print_winner( InstantRunoffVote.new(vote_array).result )
157 puts "The winner shold be: C"
159 vote_array = Array.new
160 42.times {vote_array << "ABCD".split("")}
161 26.times {vote_array << "ACBD".split("")}
162 15.times {vote_array << "BACD".split("")}
163 32.times {vote_array << "BCAD".split("")}
164 14.times {vote_array << "CABD".split("")}
165 49.times {vote_array << "CBAD".split("")}
166 17.times {vote_array << "ABDC".split("")}
167 23.times {vote_array << "BADC".split("")}
168 37.times {vote_array << "BCDA".split("")}
169 11.times {vote_array << "CADB".split("")}
170 16.times {vote_array << "CBDA".split("")}
171 54.times {vote_array << "ADBC".split("")}
172 36.times {vote_array << "BDCA".split("")}
173 42.times {vote_array << "CDAB".split("")}
174 13.times {vote_array << "CDBA".split("")}
175 51.times {vote_array << "DABC".split("")}
176 33.times {vote_array << "DBCA".split("")}
177 39.times {vote_array << "DCAB".split("")}
178 12.times {vote_array << "DCBA".split("")}
180 print_winner( InstantRunoffVote.new(vote_array).result )
184 puts "USING IRV LOGIC..."
185 puts "The winner shold be: B"
187 vote_array = Array.new
188 42.times {vote_array << "ABCD".split("")}
189 26.times {vote_array << "BCDA".split("")}
190 15.times {vote_array << "CDBA".split("")}
191 15.times {vote_array << "DCBA".split("")}
193 print_winner( InstantRunoffLogicVote.new(vote_array).result )
197 puts "USING RANGE..."
198 puts "The winner shold be: B"
200 vote_array = Array.new
201 42.times {vote_array << {:A => 10, :B => 5, :C => 2, :D => 1}}
202 26.times {vote_array << {:A => 1, :B => 10, :C => 5, :D => 2}}
203 15.times {vote_array << {:A => 1, :B => 2, :C => 10, :D => 5}}
204 17.times {vote_array << {:A => 1, :B => 2, :C => 5, :D => 10}}
206 print_winner( RangeVote.new(vote_array).result )