1 class InstantRunoffVote < ElectionVote
2 def initialize(votes=nil)
3 @candidates = Array.new
5 @candidates = vote.uniq if vote.uniq.length > candidates.length
8 @candidates.each do |candidate|
9 @votes[candidate] = [0, Hash.new] unless @votes.has_key?(candidate)
14 InstantRunoffResult.new(self, params)
20 candidate = votecopy.shift
21 votes[candidate] = [0, Hash.new] unless votes.has_key?(candidate)
22 votes[candidate][0] += 1
23 if votes[candidate][1].has_key?(votecopy)
24 votes[candidate][1][votecopy] += 1
26 votes[candidate][1][votecopy] = 1
30 def verify_vote(vote=nil)
31 vote.instance_of?( Array ) and
36 class InstantRunoffLogicVote < InstantRunoffVote
38 InstantRunoffLogicResult.new(self, params)
42 class InstantRunoffFirstRoundVote < InstantRunoffVote
44 InstantRunoffFirstRoundResult.new(self, params)
48 class InstantRunoffAllVote < InstantRunoffVote
50 InstantRunoffAllResult.new(self, params)
54 class InstantRunoffRandomVote < InstantRunoffVote
56 InstantRunoffRandomResult.new(self, params)
60 class InstantRunoffResult < ElectionResult
61 attr_reader :ranked_candidates
63 def initialize(voteobj=nil, params={})
64 unless voteobj and voteobj.kind_of?( InstantRunoffVote )
65 raise ArgumentError, "You must pass an InstantRunoffVote array.", caller
69 votes = @election.votes.clone
70 candidates = @election.candidates
71 votes_sum = votes.inject(0) {|n, value| n + value[1][0]}
72 @majority = votes_sum/2 + 1
73 @ranked_candidates = Array.new()
74 ranked_candidates = Array.new()
77 if params.has_key?('candidate_count')
78 apply_candidate_count(votes, params['candidate_count'])
80 if params.has_key?('vote_minimum')
81 apply_vote_minimum(votes, params['vote_minimum'])
83 if params.has_key?('percent_minimum')
84 apply_vote_minimum(votes, votes_sum * params['percent_minimum'])
86 if params.has_key?('percent_retention')
87 apply_retention(votes, votes_sum * params['percent_retention'])
91 ranked_candidates = votes.sort do |a, b|
93 end.collect {|i| i[0]}
94 @winners = ranked_candidates.find_all do |i|
95 votes[i][0] >= @majority
97 end while not self.winner? and next_round(votes, ranked_candidates)
98 @ranked_candidates.unshift(*ranked_candidates)
102 def apply_candidate_count(votes, candidate_count)
103 if votes.size > candidate_count
104 losers = votes.sort do |a, b|
106 end.collect {|i| i[0]}.last(votes.size - candidate_count)
107 @ranked_candidates.unshift(losers) unless losers.empty?
108 losers.each { |loser| remove_candidate(votes, loser) }
112 def apply_vote_minimum(votes, vote_minimum)
113 losers = votes.find_all do |i|
114 i[1][0] < vote_minimum
115 end.collect {|i| i[0]}
116 if losers.length == votes.size
119 @ranked_candidates.unshift(losers) unless losers.empty?
120 losers.each { |loser| remove_candidate(votes, loser) }
124 def apply_retention(votes, retention)
125 losers = votes.sort do |a, b|
127 end.collect {|i| i[0]}
129 while partial_sum < retention
130 partial_sum += votes[losers.shift][0]
132 @ranked_candidates.unshift(losers) unless losers.empty?
133 losers.each { |loser| remove_candidate(votes, loser) }
136 def next_round(votes, ranked_candidates)
137 loser = ranked_candidates[-1]
138 if votes.empty? or votes[loser][0] == votes[ranked_candidates[-2]][0]
141 @ranked_candidates.unshift(loser)
142 remove_candidate(votes, loser)
147 def remove_candidate(votes, loser)
148 votes.each_pair do |candidate, morevotes|
150 hash.each_pair do |vote, count|
156 votes[loser][1].each_pair do |vote, count|
157 candidate = vote.shift
158 votes[candidate][0] += count
159 if votes[candidate][1].has_key?(vote)
160 votes[candidate][1][vote] += count
162 votes[candidate][1][vote] = count
169 class InstantRunoffLogicResult < InstantRunoffResult
170 def next_round(votes, ranked_candidates)
171 losers = ranked_candidates.find_all do |i|
172 votes[i][0] == votes[ranked_candidates[-1]][0]
174 if losers.inject(0) {|n, loser| n + votes[loser][0]} >= @majority
177 @ranked_candidates.unshift(losers)
178 losers.each do |loser|
179 remove_candidate(votes, loser)
186 class InstantRunoffFirstRoundResult < InstantRunoffResult
187 def next_round(votes, ranked_candidates)
188 losers = ranked_candidates.find_all do |i|
189 votes[i][0] == votes[ranked_candidates[-1]][0]
191 loser = losers.sort do |a, b|
192 @election.votes[a][0] <=> @election.votes[b][0]
194 @ranked_candidates.unshift(loser)
195 remove_candidate(votes, loser)
199 class InstantRunoffAllResult < InstantRunoffResult
200 def next_round(votes, ranked_candidates)
201 losers = ranked_candidates.find_all do |i|
202 votes[i][0] == votes[ranked_candidates[-1]][0]
204 if losers.length == ranked_candidates.length
207 @ranked_candidates.unshift(losers)
208 losers.each do |loser|
209 remove_candidate(votes, loser)
216 class InstantRunoffRandomResult < InstantRunoffResult
217 def next_round(votes, ranked_candidates)
218 losers = ranked_candidates.find_all do |i|
219 votes[i][0] == votes[ranked_candidates[-1]][0]
221 loser = losers[rand(losers.length)]
222 @ranked_candidates.unshift(loser)
223 remove_candidate(votes, loser)