1 # election library -- a ruby library for elections
2 # copyright © 2005 MIT Media Lab and Benjamin Mako Hill
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #################################################################
20 ## ==== condorcet.rb ====
22 ## This file contains Condorcet election methods. Currently this
23 ## includes a pure condorcet and a CloneproofSSD implementation
24 ## modeled after the Python-based Debian project election code and
25 ## that gives the same results in several tested corner cases.
26 #################################################################
28 ##################################################################
29 ## CondorcetVote Classes and SubClasses
31 ## The CondorcetVote class is subclassed by the PureCondorcetVote and
32 ## the CloneproofSSDVote classes but should not be used directly.
34 class CondorcetVote < ElectionVote
36 attr_accessor :results
38 def initialize(votes=nil)
39 unless defined?(@candidates)
40 @candidates = Array.new
41 votes.each do |vote_row|
42 vote_row = vote_row.flatten if vote_row.class == Array
43 vote_row.each do |vote|
44 @candidates << vote unless @candidates.include?(vote)
52 def tally_vote(vote=nil)
54 vote.each_with_index do |winner, index|
55 if vote.flatten.length < @candidates.length
56 implied_losers = @candidates.select { |c| not vote.include?(c) }
57 vote.push(implied_losers)
59 if vote.length - 1 == index
62 losers = vote.flatten.last( vote.flatten.length - index - 1)
65 losers.each do |place|
66 place = [place] unless place.class == Array
70 @votes[winner] = Hash.new unless @votes.has_key?(winner)
71 @votes[loser] = Hash.new unless @votes.has_key?(loser)
73 if @votes[winner].has_key?(loser)
74 @votes[winner][loser] += 1
76 @votes[winner][loser] = 1
79 # make sure we have a comparable object
80 @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner )
87 if @results.size < 2 && (not @candidates.empty?)
94 find_only_winner unless @winner
100 def verify_vote(vote=nil)
101 vote.instance_of?( Array ) and
106 find_only_winner unless @winner
107 until @candidates.empty?
108 aResult = resultFactory( self )
109 @results << aResult.winners
115 @winner = resultFactory( self )
116 @results << @winner.winners
122 class PureCondorcetVote < CondorcetVote
124 PureCondorcetResult.new(self)
128 class CloneproofSSDVote < CondorcetVote
129 def resultFactory(init)
130 CloneproofSSDResult.new(init)
136 ##################################################################
137 ## CondorcetResult Classes and SubClasses
139 ## The CondorcetResult class is subclassed by the PureCondorcetResult
140 ## and the CloneproofSSDResult classes but should not be used
143 class CondorcetResult < ElectionResult
144 def initialize(voteobj=nil)
145 unless voteobj and voteobj.kind_of?( CondorcetVote )
146 raise ArgumentError, "You must pass a CondorcetVote array.", caller
153 def defeats(candidates=nil, votes=nil)
154 candidates = @election.candidates unless candidates
155 votes = @election.votes unless votes
158 candidates = [candidates] unless candidates.class == Array
159 candidates.each do |candidate|
160 candidates.each do |challenger|
161 next if candidate == challenger
162 if votes[candidate][challenger] > votes[challenger][candidate]
163 defeats << [ candidate, challenger ]
173 class PureCondorcetResult < CondorcetResult
174 def initialize(voteobj=nil)
181 votes = @election.votes
182 candidates = @election.candidates
185 candidates.each do |candidate|
186 victors[candidate] = Array.new
189 self.defeats.each do |pair|
190 winner, loser = *pair
191 victors[winner] << loser
194 winners = @election.candidates.find_all do |candidate|
195 victors[candidate].length == @election.candidates.length - 1
198 @winners << winners if winners.length == 1
202 class CloneproofSSDResult < CondorcetResult
203 def initialize(voteobj=nil)
205 @winners = self.cpssd()
211 votes = @election.votes
212 candidates = *@election.candidates
214 def in_schwartz_set?(candidate, candidates, transitive_defeats)
215 candidates.each do |challenger|
216 next if candidate == challenger
218 if transitive_defeats.include?( [ challenger, candidate ] ) and
219 not transitive_defeats.include?( [ candidate, challenger ] )
228 # see the array with the standard defeats
229 transitive_defeats = self.defeats(candidates, votes)
230 defeats_hash = Hash.new
231 transitive_defeats.each { |td| defeats_hash[td] = 1 }
233 candidates = [candidates] unless candidates.class == Array
234 candidates.each do |cand1|
235 candidates.each do |cand2|
236 unless cand1 == cand2
237 candidates.each do |cand3|
238 if not cand2 == cand3 and
239 not cand1 == cand3 and
240 defeats_hash[[cand2, cand1]] and
241 defeats_hash[[cand1, cand3]] and
242 not defeats_hash[[cand2, cand3]]
243 transitive_defeats << [cand2, cand3]
244 defeats_hash[[cand2, cand3]] = 1
251 schwartz_set = Array.new
252 candidates.each do |candidate|
253 if in_schwartz_set?(candidate, candidates, transitive_defeats)
254 schwartz_set << candidate
258 candidates = schwartz_set
260 # look through the schwartz set now for defeats
261 defeats = self.defeats(candidates, votes)
263 # it's a tie or there's only one option
264 break if defeats.length == 0
266 def is_weaker_defeat?( pair1, pair2 )
267 votes = @election.votes
268 if votes[pair1[0]][pair1[1]] > votes[pair2[0]][pair2[1]]
270 elsif votes[pair1[0]][pair1[1]] == votes[pair2[0]][pair2[1]] and
271 votes[pair1[1]][pair1[0]] > votes[pair2[1]][pair2[0]]
278 defeats.sort! do |pair1, pair2|
279 if is_weaker_defeat?( pair1, pair2 )
281 elsif is_weaker_defeat?( pair2, pair1 )
288 votes[defeats[0][0]][defeats[0][1]] = 0
289 votes[defeats[0][1]][defeats[0][0]] = 0