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 def tally_vote(vote=nil)
38 vote.each_with_index do |winner, index|
39 # only run through this if this *is* preferred to something
40 break if vote.length - 1 == index
41 losers = vote.last( vote.length - index )
43 losers.each do |loser|
44 next if winner == loser
46 @votes[winner] = Hash.new unless @votes.has_key?(winner)
47 @votes[loser] = Hash.new unless @votes.has_key?(loser)
49 if @votes[winner].has_key?(loser)
50 @votes[winner][loser] += 1
52 @votes[winner][loser] = 1
55 # make sure we have a comparable object
56 @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner )
58 @candidates << loser unless @candidates.include?( loser )
61 @candidates << winner unless @candidates.include?( winner )
66 def verify_vote(vote=nil)
67 vote.instance_of?( Array ) and
73 class PureCondorcetVote < CondorcetVote
75 PureCondorcetResult.new( self )
79 class CloneproofSSDVote < CondorcetVote
81 CloneproofSSDResult.new( self )
86 ##################################################################
87 ## CondorcetResult Classes and SubClasses
89 ## The CondorcetResult class is subclassed by the PureCondorcetResult
90 ## and the CloneproofSSDResult classes but should not be used
93 class CondorcetResult < ElectionResult
94 def initialize(voteobj=nil)
95 unless voteobj and voteobj.kind_of?( CondorcetVote )
96 raise ArgumentError, "You must pass a CondorcetVote array.", caller
101 def defeats(candidates=nil, votes=nil)
102 candidates = @election.candidates unless candidates
103 votes = @election.votes unless votes
106 candidates.each do |candidate|
107 candidates.each do |challenger|
108 next if candidate == challenger
109 if votes[candidate][challenger] > votes[challenger][candidate]
110 defeats << [ candidate, challenger ]
120 class PureCondorcetResult < CondorcetResult
121 def initialize(voteobj=nil)
128 votes = @election.votes
129 candidates = @election.candidates
132 candidates.each do |candidate|
133 victors[candidate] = Array.new
136 self.defeats.each do |pair|
137 winner, loser = *pair
138 victors[winner] << loser
141 winners = @election.candidates.find_all do |candidate|
142 victors[candidate].length == @election.candidates.length - 1
145 @winners = winners if winners.length == 1
149 class CloneproofSSDResult < CondorcetResult
150 def initialize(voteobj=nil)
152 @winners = self.cpssd()
157 votes = @election.votes
158 candidates = *@election.candidates
160 def in_schwartz_set?(candidate, candidates, transitive_defeats)
161 candidates.each do |challenger|
162 next if candidate == challenger
164 if transitive_defeats.include?( [ challenger, candidate ] ) and
165 not transitive_defeats.include?( [ candidate, challenger ] )
174 # see the array with the standard defeats
175 transitive_defeats = self.defeats(candidates, votes)
177 candidates.each do |cand1|
178 candidates.each do |cand2|
179 candidates.each do |cand3|
180 if transitive_defeats.include?( [ cand2, cand1 ] ) and
181 transitive_defeats.include?( [ cand1, cand3 ] ) and
182 not transitive_defeats.include?( [ cand2, cand3 ] ) and
184 transitive_defeats << [ cand2, cand3 ]
190 schwartz_set = Array.new
191 candidates.each do |candidate|
192 if in_schwartz_set?(candidate, candidates, transitive_defeats)
193 schwartz_set << candidate
197 candidates = schwartz_set
199 # look through the schwartz set now for defeats
200 defeats = self.defeats(candidates, votes)
202 # it's a tie or there's only one option
203 break if defeats.length == 0
205 def is_weaker_defeat?( pair1, pair2 )
206 votes = @election.votes
207 if votes[pair1[0]][pair1[1]] > votes[pair2[0]][pair2[1]]
209 elsif votes[pair1[0]][pair1[1]] == votes[pair2[0]][pair2[1]] and
210 votes[pair1[1]][pair1[0]] > votes[pair2[1]][pair2[0]]
217 defeats.sort! do |pair1, pair2|
218 if is_weaker_defeat?( pair1, pair2 )
220 elsif is_weaker_defeat?( pair2, pair1 )
227 votes[defeats[0][0]][defeats[0][1]] = 0
228 votes[defeats[0][1]][defeats[0][0]] = 0