o Verify that all tests in test.rb are present in test/*_test.rb files,
[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 tally_vote(vote=nil)
37
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 )
42
43       losers.each do |loser|
44         next if winner == loser
45
46         @votes[winner] = Hash.new unless @votes.has_key?(winner)
47         @votes[loser] = Hash.new unless @votes.has_key?(loser)
48
49         if @votes[winner].has_key?(loser)
50           @votes[winner][loser] += 1
51         else
52           @votes[winner][loser] = 1
53         end
54
55         # make sure we have a comparable object
56         @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner )
57
58         @candidates << loser unless @candidates.include?( loser )
59       end
60
61       @candidates << winner unless @candidates.include?( winner )
62     end
63   end
64
65   protected
66   def verify_vote(vote=nil)
67     vote.instance_of?( Array ) and
68       vote == vote.uniq
69   end
70   
71 end
72
73 class PureCondorcetVote < CondorcetVote
74   def result
75     PureCondorcetResult.new( self )
76   end
77 end
78
79 class CloneproofSSDVote < CondorcetVote
80   def result
81     CloneproofSSDResult.new( self )
82   end
83 end
84
85
86 ##################################################################
87 ## CondorcetResult Classes and SubClasses
88 ##
89 ## The CondorcetResult class is subclassed by the PureCondorcetResult
90 ## and the CloneproofSSDResult classes but should not be used
91 ## directly.
92
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
97     end
98     super(voteobj)
99   end
100
101   protected
102   def defeats(candidates=nil, votes=nil)
103     candidates = @election.candidates unless candidates
104     votes = @election.votes unless votes
105
106     defeats = Array.new
107     candidates.each do |candidate|
108       candidates.each do |challenger|
109         next if candidate == challenger
110         if votes[candidate][challenger] > votes[challenger][candidate]
111           defeats << [ candidate, challenger ]
112         end
113       end
114     end
115
116     defeats
117   end
118
119 end
120
121 class PureCondorcetResult < CondorcetResult
122   def initialize(voteobj=nil)
123     super(voteobj)
124     self.condorcet()
125   end
126
127   protected
128   def condorcet
129     votes = @election.votes
130     candidates = @election.candidates
131
132     victors = Hash.new
133     candidates.each do |candidate|
134       victors[candidate] = Array.new
135     end
136
137     self.defeats.each do |pair|
138       winner, loser = *pair
139       victors[winner] << loser
140     end
141
142     winners = @election.candidates.find_all do |candidate|
143         victors[candidate].length == @election.candidates.length - 1
144     end
145
146     @winners << winners if winners.length == 1
147   end
148 end
149
150 class CloneproofSSDResult < CondorcetResult
151   def initialize(voteobj=nil)
152     super(voteobj)
153     @winners = self.cpssd()
154   end
155
156   protected
157   def cpssd
158     votes = @election.votes
159     candidates = *@election.candidates
160
161     def in_schwartz_set?(candidate, candidates, transitive_defeats)
162       candidates.each do |challenger|
163         next if candidate == challenger
164
165         if transitive_defeats.include?( [ challenger, candidate ] ) and
166             not transitive_defeats.include?( [ candidate, challenger ] )
167           return false
168         end
169       end
170       return true
171     end
172
173     loop do
174
175       # see the array with the standard defeats
176       transitive_defeats = self.defeats(candidates, votes)
177
178       candidates.each do |cand1|
179         candidates.each do |cand2|
180           candidates.each do |cand3|
181             if transitive_defeats.include?( [ cand2, cand1 ] ) and
182                 transitive_defeats.include?( [ cand1, cand3 ] ) and
183                 not transitive_defeats.include?( [ cand2, cand3 ] ) and
184                 not cand2 == cand3
185               transitive_defeats << [ cand2, cand3 ]
186             end
187           end
188         end
189       end
190
191       schwartz_set = Array.new
192       candidates.each do |candidate|
193         if in_schwartz_set?(candidate, candidates, transitive_defeats)
194           schwartz_set << candidate
195         end
196       end
197
198       candidates = schwartz_set
199
200       # look through the schwartz set now for defeats
201       defeats = self.defeats(candidates, votes)
202       
203       # it's a tie or there's only one option
204       break if defeats.length == 0
205
206       def is_weaker_defeat?( pair1, pair2 )
207         votes = @election.votes
208         if votes[pair1[0]][pair1[1]] > votes[pair2[0]][pair2[1]]
209           return true
210         elsif votes[pair1[0]][pair1[1]] == votes[pair2[0]][pair2[1]] and
211             votes[pair1[1]][pair1[0]] > votes[pair2[1]][pair2[0]]
212           return true
213         else
214           false
215         end
216       end
217       
218       defeats.sort! do |pair1, pair2|
219         if is_weaker_defeat?( pair1, pair2 ) 
220           +1
221         elsif is_weaker_defeat?( pair2, pair1 ) 
222           -1
223         else
224           0
225         end
226       end
227  
228       votes[defeats[0][0]][defeats[0][1]] = 0
229       votes[defeats[0][1]][defeats[0][0]] = 0
230       
231     end
232
233     return candidates
234   end
235
236 end

Benjamin Mako Hill || Want to submit a patch?