fc61840a8aec6172d498345b2b387f79bf4795ce
[selectricity] / lib / rubyvote / election.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 ## ==== election.rb ====
21 ##
22 ## This file contains the core ElectionVote and ElectionResults
23 ## classes and the most common and simple election methods including
24 ## plurality and approval voting.
25 #################################################################
26
27 ##################################################################
28 ## ElectionVote Classes and SubClasses
29 ##
30 ## There classes are used to store, verify, and "tally" (i.e. count
31 ## votes for the standard Election superclass and for the most common
32 ## types of elections.
33
34 class ElectionVote
35   attr_reader :votes
36   attr_reader :candidates
37
38   def initialize(votes=nil)
39     @votes = Hash.new unless defined?(@votes)
40     @candidates = Array.new unless defined?(@candidates)
41
42     if votes
43       if votes.instance_of?( Array )
44         votes.each do |vote|
45           self.tally_vote(vote) if self.verify_vote(vote)
46         end
47       else
48         raise ElectionError, "Votes must be in the form of an array.", caller
49       end
50     end
51   end
52
53   protected
54   # by default, this is set to look if the vote is defined. it should
55   # be overridden in each class
56   def verify_vote(vote=nil)
57     vote ? true : false
58   end
59
60   # by default, this does nothing. it must be redefined in any subclass
61   def tally_vote
62     self.verify_vote(vote)
63   end
64 end
65
66 class PluralityVote < ElectionVote
67   def result
68     PluralityResult.new(self)
69   end
70   
71   protected
72   def verify_vote(vote=nil)
73     vote ? true : false
74   end
75
76   def tally_vote(candidate)
77     if @votes.has_key?(candidate)
78       @votes[candidate] += 1
79     else
80       @votes[candidate] = 1
81       @candidates << candidate
82     end
83   end
84 end
85
86 class ApprovalVote < PluralityVote
87   def result
88     ApprovalResult.new(self)
89   end
90
91   protected
92   def verify_vote(vote=nil)
93     vote.instance_of?( Array ) and vote.length >= 1
94   end
95
96   def tally_vote(approvals)
97     approvals.each {|candidate| super(candidate)}
98   end
99 end
100
101
102 ##################################################################
103 ## Election Result Classes
104 ##
105
106 ## There classes are used to compute and report the results of an
107 ## election. In almost all cases, these will be returned by the
108 ## #results method of a corresponding ElectionVote subclass.
109
110 class ElectionResult
111   attr_reader :winners
112
113   def initialize(voteobj=nil)
114     unless voteobj and voteobj.kind_of?( ElectionVote )
115       raise ArgumentError, "You must pass a ElectionVote array.", caller
116     end
117
118     @election = voteobj
119     @winners = Array.new
120   end
121
122   def winner
123     @winners[0] if @winners.length > 0
124   end
125
126   def winner?
127     @winners.length > 0
128   end
129
130 end
131
132 class PluralityResult < ElectionResult
133   attr_reader :ranked_candidates
134
135   def initialize(voteobj=nil)
136     super(voteobj)
137
138     votes = @election.votes
139     candidates = @election.candidates
140     
141     @ranked_candidates = votes.sort do |a, b|
142       b[1] <=> a[1]
143     end.collect {|a| a[0]}
144     
145     # winners are anyone who has the same number of votes as the
146     # first person
147     @winners = @ranked_candidates.find_all do |i|
148       votes[i] == votes[@ranked_candidates[0]]
149     end
150   end
151 end
152
153 # this class is complete because results for approval are computed
154 # identically to results from plurality
155 class ApprovalResult < PluralityResult
156 end
157   
158 class ElectionError < ArgumentError
159 end
160

Benjamin Mako Hill || Want to submit a patch?