]> projects.mako.cc - rubyvote/blob - lib/rubyvote/condorcet.rb
96a103bd479c5bcf3e7d575cacd5d71f4afe8f78
[rubyvote] / lib / rubyvote / condorcet.rb
1 # election library -- a ruby library for elections
2 # copyright © 2005 MIT Media Lab and Benjamin Mako Hill
3
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.
8
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.
13
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
17 # 02110-1301, USA.
18
19 #################################################################
20 ## ==== condorcet.rb ====
21 ##
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 #################################################################
27
28 ##################################################################
29 ## CondorcetVote Classes and SubClasses
30 ##
31 ## The CondorcetVote class is subclassed by the PureCondorcetVote and
32 ## the CloneproofSSDVote classes but should not be used directly.
33
34 class CondorcetVote < ElectionVote
35
36   def initialize(votes=nil)
37     unless defined?(@candidates)
38       @candidates = Array.new
39       votes.each do |vote_row|
40         vote_row = vote_row.flatten if vote_row.class == Array
41         vote_row.each do |vote| 
42           @candidates << vote unless @candidates.include?(vote)
43         end
44       end
45     end
46     super(votes)
47   end
48
49   def tally_vote(vote=nil)
50
51     vote.each_with_index do |winner, index|
52       if vote.flatten.length < @candidates.length
53         implied_losers = @candidates.select { |c| not vote.include?(c) }
54         vote.push(implied_losers)
55       end
56       if vote.length - 1 == index
57         losers = []
58       else
59         losers = vote.last( vote.flatten.length - index )
60       end
61
62       losers.each do |place|
63         place = [place] unless place.class == Array
64         place.each do |loser|
65           
66           next if winner == loser
67
68           @votes[winner] = Hash.new unless @votes.has_key?(winner)
69           @votes[loser] = Hash.new unless @votes.has_key?(loser)
70
71           if @votes[winner].has_key?(loser)
72             @votes[winner][loser] += 1
73           else
74             @votes[winner][loser] = 1
75           end
76
77           # make sure we have a comparable object
78           @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner )
79         end
80       end
81     end
82   end
83
84   def result
85     top_result = resultFactory( self )
86     until @candidates.empty?
87       aResult = resultFactory( self )
88       top_result.full_results << aResult
89       filter_out(aResult)
90     end
91     top_result
92   end
93
94   protected
95   def verify_vote(vote=nil)
96     vote.instance_of?( Array ) and
97       vote == vote.uniq
98   end
99
100 end
101
102 class PureCondorcetVote < CondorcetVote
103   def resultFactory(init)
104     PureCondorcetResult.new(init)
105   end
106 end
107
108 class CloneproofSSDVote < CondorcetVote
109   def resultFactory(init)
110     CloneproofSSDResult.new(init)
111   end
112 end
113
114
115 ##################################################################
116 ## CondorcetResult Classes and SubClasses
117 ##
118 ## The CondorcetResult class is subclassed by the PureCondorcetResult
119 ## and the CloneproofSSDResult classes but should not be used
120 ## directly.
121
122 class CondorcetResult < ElectionResult
123   def initialize(voteobj=nil)
124     unless voteobj and voteobj.kind_of?( CondorcetVote )
125       raise ArgumentError, "You must pass a CondorcetVote array.", caller
126     end
127     super(voteobj)
128   end
129
130   protected
131   def defeats(candidates=nil, votes=nil)
132     candidates = @election.candidates unless candidates
133     votes = @election.votes unless votes
134
135     defeats = Array.new
136     candidates.each do |candidate|
137       candidates.each do |challenger|
138         next if candidate == challenger
139         if votes[candidate][challenger] > votes[challenger][candidate]
140           defeats << [ candidate, challenger ]
141         end
142       end
143     end
144
145     defeats
146   end
147
148 end
149
150 class PureCondorcetResult < CondorcetResult
151   def initialize(voteobj=nil)
152     super(voteobj)
153     self.condorcet()
154   end
155
156   protected
157   def condorcet
158     votes = @election.votes
159     candidates = @election.candidates
160
161     victors = Hash.new
162     candidates.each do |candidate|
163       victors[candidate] = Array.new
164     end
165
166     self.defeats.each do |pair|
167       winner, loser = *pair
168       victors[winner] << loser
169     end
170
171     winners = @election.candidates.find_all do |candidate|
172         victors[candidate].length == @election.candidates.length - 1
173     end
174
175     @winners << winners if winners.length == 1
176   end
177 end
178
179 class CloneproofSSDResult < CondorcetResult
180   def initialize(voteobj=nil)
181     super(voteobj)
182     @winners = self.cpssd()
183   end
184
185   protected
186   def cpssd
187     votes = @election.votes
188     candidates = *@election.candidates
189
190     def in_schwartz_set?(candidate, candidates, transitive_defeats)
191       candidates.each do |challenger|
192         next if candidate == challenger
193
194         if transitive_defeats.include?( [ challenger, candidate ] ) and
195             not transitive_defeats.include?( [ candidate, challenger ] )
196           return false
197         end
198       end
199       return true
200     end
201
202     loop do
203
204       # see the array with the standard defeats
205       transitive_defeats = self.defeats(candidates, votes)
206
207       candidates.each do |cand1|
208         candidates.each do |cand2|
209           candidates.each do |cand3|
210             if transitive_defeats.include?( [ cand2, cand1 ] ) and
211                 transitive_defeats.include?( [ cand1, cand3 ] ) and
212                 not transitive_defeats.include?( [ cand2, cand3 ] ) and
213                 not cand2 == cand3
214               transitive_defeats << [ cand2, cand3 ]
215             end
216           end
217         end
218       end
219
220       schwartz_set = Array.new
221       candidates.each do |candidate|
222         if in_schwartz_set?(candidate, candidates, transitive_defeats)
223           schwartz_set << candidate
224         end
225       end
226
227       candidates = schwartz_set
228
229       # look through the schwartz set now for defeats
230       defeats = self.defeats(candidates, votes)
231       
232       # it's a tie or there's only one option
233       break if defeats.length == 0
234
235       def is_weaker_defeat?( pair1, pair2 )
236         votes = @election.votes
237         if votes[pair1[0]][pair1[1]] > votes[pair2[0]][pair2[1]]
238           return true
239         elsif votes[pair1[0]][pair1[1]] == votes[pair2[0]][pair2[1]] and
240             votes[pair1[1]][pair1[0]] > votes[pair2[1]][pair2[0]]
241           return true
242         else
243           false
244         end
245       end
246       
247       defeats.sort! do |pair1, pair2|
248         if is_weaker_defeat?( pair1, pair2 ) 
249           +1
250         elsif is_weaker_defeat?( pair2, pair1 ) 
251           -1
252         else
253           0
254         end
255       end
256  
257       votes[defeats[0][0]][defeats[0][1]] = 0
258       votes[defeats[0][1]][defeats[0][0]] = 0
259       
260     end
261
262     return candidates
263   end
264
265 end

Benjamin Mako Hill || Want to submit a patch?