o Test that elections with a single vote return the expected result.
[rubyvote] / 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
65   def filter_out(winner)
66     if winner.winners[0].class == Array
67       to_filter = winner.winners[0]
68     else
69       to_filter = [winner.winners[0]]
70     end
71     @candidates.delete_if {|x| to_filter.include?(x)}
72   end
73
74 end
75
76 class PluralityVote < ElectionVote
77   def result
78     PluralityResult.new(self)
79   end
80   
81   protected
82   def verify_vote(vote=nil)
83     vote.instance_of?( String )
84   end
85
86   def tally_vote(candidate)
87     if @votes.has_key?(candidate)
88       @votes[candidate] += 1
89     else
90       @votes[candidate] = 1
91       @candidates << candidate
92     end
93   end
94 end
95
96 class ApprovalVote < PluralityVote
97   def result
98     ApprovalResult.new(self)
99   end
100
101   protected
102   def verify_vote(vote=nil)
103     vote.instance_of?( Array ) and vote.length >= 1
104   end
105
106   def tally_vote(approvals)
107     approvals.each {|candidate| super(candidate)}
108   end
109 end
110
111
112 ##################################################################
113 ## Election Result Classes
114 ##
115
116 ## There classes are used to compute and report the results of an
117 ## election. In almost all cases, these will be returned by the
118 ## #results method of a corresponding ElectionVote subclass.
119
120 class ElectionResult
121   attr_reader :winners
122   attr_accessor :full_results
123
124   def initialize(voteobj=nil)
125     unless voteobj and voteobj.kind_of?( ElectionVote )
126       raise ArgumentError, "You must pass a ElectionVote array.", caller
127     end
128
129     @election = voteobj
130     @winners = Array.new
131     @full_results = Array.new
132   end
133
134   def winner
135     @winners[0] if @winners.length > 0
136   end
137
138   def winner?
139     @winners.length > 0
140   end
141
142   def get_full_results
143     @full_results.collect {|x| x.winners}
144   end
145
146 end
147
148 class PluralityResult < ElectionResult
149   attr_reader :ranked_candidates
150
151   def initialize(voteobj=nil)
152     super(voteobj)
153
154     votes = @election.votes
155     candidates = @election.candidates
156     
157     @ranked_candidates = votes.sort do |a, b|
158       b[1] <=> a[1]
159     end.collect {|a| a[0]}
160     
161     # winners are anyone who has the same number of votes as the
162     # first person
163     @winners = @ranked_candidates.find_all do |i|
164       votes[i] == votes[@ranked_candidates[0]]
165     end
166   end
167 end
168
169 # this class is complete because results for approval are computed
170 # identically to results from plurality
171 class ApprovalResult < PluralityResult
172 end
173   
174 class ElectionError < ArgumentError
175 end
176

Benjamin Mako Hill || Want to submit a patch?