a number of improvements
[rubyvote] / lib / rubyvote / positional.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 ## ==== positional.rb ====
21 ##
22 ## This file contains positional election methods. Currently only
23 ## includes an implementation of the Borda count election method.
24 #################################################################
25
26 ##################################################################
27 ## BordaVote and BordaResult Classes
28 ##
29 ## These classes inherit from and/or are modeled after the classes in
30 ## election.rb and condorcet.rb
31
32 # Borda is a positional voting system and, as a result, takes a list of
33 # ranked candidates and assigns points to each candidates based on their
34 # order. In Borda, there are *n* candidate and the first candidates is
35 # assigned *n* - 1 points and each subsequent candidate is assigned one
36 # less point. The candidate is assigned no points.
37 #
38 # Currently, all candidates should be ranked in each ballot.
39 #
40 # Example::
41 #
42 #   require 'positional'
43 #   vote_array = [ ["A", "B"],  ["B", "A"], ["B", "A"] ]
44 #   resultobject = BordaVote.new(vote_array).result
45 class BordaVote < ElectionVote
46
47   def initialize(votes=nil)
48     @candidates = Array.new
49     votes.each do |vote|
50       @candidates = vote.uniq if vote.uniq.length > candidates.length
51     end
52     super(votes)
53   end
54    
55   def tally_vote(vote)
56     points = candidates.length - 1
57     vote.each do |candidate|
58       #if the candidate exist, add the points, otherwise assign them
59       if @votes.has_key?(candidate)
60         @votes[candidate] += points
61       else
62         @votes[candidate] = points
63       end
64       points -= 1
65     end
66   end
67   
68   def verify_vote(vote=nil)
69     vote.instance_of?( Array ) and
70       vote == vote.uniq
71   end
72
73   def result
74     BordaResult.new(self)
75   end
76 end
77
78 class BordaResult < ElectionResult
79   attr_reader :ranked_candidates
80   attr_reader :points
81   
82   def initialize(voteobj=nil)
83     super(voteobj)
84     votes = @election.votes
85     
86     @ranked_candidates = votes.sort do |a, b|
87       b[1] <=> a[1]
88     end.collect {|i| i[0]}
89
90     @winners = @ranked_candidates.find_all do |i|
91       votes[i] == votes[@ranked_candidates[0]]
92     end
93     
94     @points = @election.votes
95   end
96
97 end

Benjamin Mako Hill || Want to submit a patch?