1 # election library -- a ruby library for elections
2 # copyright © 2005 MIT Media Lab and Benjamin Mako Hill
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.
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.
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
19 #################################################################
20 ## ==== positional.rb ====
22 ## This file contains positional election methods. Currently only
23 ## includes an implementation of the Borda count election method.
24 #################################################################
26 ##################################################################
27 ## BordaVote and BordaResult Classes
29 ## These classes inherit from and/or are modeled after the classes in
30 ## election.rb and condorcet.rb
32 class BordaVote < ElectionVote
34 def initialize(votes=nil)
35 @candidates = Array.new
37 @candidates = vote.uniq if vote.uniq.length > candidates.length
43 points = candidates.length - 1
44 vote.each do |candidate|
45 #if the candidate exist, add the points, otherwise assign them
46 if @votes.has_key?(candidate)
47 @votes[candidate] += points
49 @votes[candidate] = points
55 def verify_vote(vote=nil)
56 vote.instance_of?( Array ) and
65 class BordaResult < ElectionResult
66 attr_reader :ranked_candidates
69 def initialize(voteobj=nil)
71 votes = @election.votes
73 @ranked_candidates = votes.sort do |a, b|
75 end.collect {|i| i[0]}
77 @winners = @ranked_candidates.find_all do |i|
78 votes[i] == votes[@ranked_candidates[0]]
81 @points = @election.votes