legend = Hash.new
alldata, labels = get_positions_info(@election)
@election.results unless @election.condorcet_result || @election.ssd_result
+ ranked_candidates = @election.condorcet_result.ranked_candidates.flatten
names = Hash.new
candidates = @election.candidates.sort.collect {|candidate| candidate.id}
names[candidate]= (Candidate.find(candidate)).name
end
- ranked_candidates = @election.condorcet_result.ranked_candidates.flatten
ranked_candidates.each_with_index \
{|candidate, index| legend[names[candidate]] = alldata[index]}
send_data(*graph.output)
end
+ def plurality_pie
+ @election = Election.find(params[:id])
+ @election.results unless @election.plurality_result || @election.approval_result
+ votes = @election.votes.size
+ data = Hash.new
+ names = @election.names_by_id
+
+ @election.plurality_result.points.each do |candidate, votes|
+ data[names[candidate]] = votes
+ end
+
+ pie = GruffGraff.new ( :graph_type => Gruff::Pie,
+ :title => "Percentage of First Place Votes",
+ :data => data)
+ send_data(*pie.output)
+
+ end
+
private
def get_positions_info(election)
buckets = Hash.new
redirect_to :controller => 'site'
return
end
- @election.results
+ @results = @election.results
@candidates = {}
@election.candidates.each {|c| @candidates[c.id] = c}
end
belongs_to :user
validates_presence_of :name, :description
+ #validate that method is one of the listed election types
+
attr_reader :plurality_result
attr_reader :approval_result
attr_reader :condorcet_result
attr_reader :ssd_result
attr_reader :borda_result
-
+
require 'date'
+
+ def other_methods
+ if election_method
+ @other_methods = ELECTION_TYPES.reject {|i| i == election_method}
+ else
+ @other_methods = nil
+ end
+ @other_methods
+ end
def startdate
read_attribute( :startdate ) || Time.now
preference_tally << voter.vote.rankings.sort.collect \
{ |ranking| ranking.candidate.id }
end
+
@plurality_result = PluralityVote.new(plurality_tally).result
@approval_result = ApprovalVote.new(approval_tally).result
@condorcet_result = PureCondorcetVote.new(preference_tally).result
@ssd_result = CloneproofSSDVote.new(preference_tally).result
@borda_result = BordaVote.new(preference_tally).result
- #@runoff_result = InstantRunoffVote.new(preference_tally).result
- nil # to stay consistent
- end
+ { 'plurality' => @plurality_result,
+ 'approval' => @approval_result,
+ 'condorcet' => @condorcet_result,
+ 'ssd' => @ssd_result,
+ 'borda' => @borda_result }
+ end
def names_by_id
names = Hash.new
-<% candidates = @election.candidates.sort.collect {|candidate| candidate.id}-%>
+<% candidates = @election.condorcet_result.ranked_candidates.flatten -%>
<% voters = @election.voters.size %>
<% matrix = @election.condorcet_result.matrix %>
-<% names = Hash.new -%>
-<% candidates.each do |candidate| -%>
- <%names[candidate] = Candidate.find(candidate).name -%>
-<% end -%>
+<% victories = @election.condorcet_result.victories_and_ties %>
+<% names = @election.names_by_id %>
+
+
+<!-- This table shows how many times each choice was ranked above the other,
+ with percentages-->
<table class="voterbox">
<tr>
<td> </td>
<% candidates.each do |candidate| -%>
<th><%=h names[candidate] -%></th>
<% end -%>
-</tr>
+ </tr>
<% candidates.each do |winner| -%>
<tr>
</tr>
<% end -%>
</table>
+
+<!-- This table generates a margin of victory -->
+<table class="voterbox">
+ <% candidates.each do |victor| %>
+ <tr>
+ <th><%=h names[victor] %></th>
+ <% victories[victor].keys.each do |loser| %>
+ <% margin = victories[victor][loser]%>
+ <td><%=h names[loser] %>
+ <% if margin == 0%>
+ Tied!
+ <% else -%>
+ (<%= margin%>)
+ <% end -%>
+ </td>
+ <% end -%>
+ </tr>
+ <% end -%>
+</table>
+
--- /dev/null
+<div class="resultbox">
+<h3>Approval Result</h3>
+<p><font size="-1">(This algorithm assumes that top two choices are "approved.")</font></p>
+
+<div class="rbmoreinfo">
+<h4>About Approval Voting</h4>
+
+<p><%= link_to "Approval voting",
+"http://en.wikipedia.org/wiki/Approval_voting" %> is a voting system in
+which each voter can vote for as many or as few candidates as the voter
+chooses. Approval voting is a limited form of range voting, where the
+range that voters are allowed to express is extremely constrained:
+accept or not.</p>
+
+</div>
+
+</div>
\ No newline at end of file
--- /dev/null
+<div class="resultbox">
+<h3>Borda Count Results</h3>
+
+<div class="rbmoreinfo">
+<h4>About Borda Count</h4>
+
+<p><%= link_to "Borda count",
+"http://en.wikipedia.org/wiki/Borda_count" %>
+is an election method in which voters rank
+candidates in order of preference. The Borda count determines the winner
+of an election by giving each candidate a certain number of points
+corresponding to the position in which he or she is ranked by each
+voter. Once all votes have been counted the candidate with the most
+points is the winner.</p>
+</div>
+</div>
\ No newline at end of file
--- /dev/null
+<div class="resultbox">
+<h3>Simple Condorcet Results</h3>
+
+<div class="rbmoreinfo">
+<h4>About Simple Cordorcet Voting</h4>
+
+<p><%= link_to "Condorcet",
+"http://en.wikipedia.org/wiki/Condorcet_method" %> allows voters to rank
+candidates in order of preference. If there is a choice whom voters
+prefer to each other choice when compared to one at a time, that choice
+will be the winner.</p>
+
+<p>There is a family of Condorcet methods. This method is referred to as
+"Simple Condorcet" to distinguish it from the Schulze method which is
+another Condorcet system.</p>
+
+</div>
+</div>
\ No newline at end of file
--- /dev/null
+<div class="resultbox">
+<h3>Plurality Results</h3>
+
+<div class="rbmoreinfo">
+<h4>About Plurality Voting</h4>
+
+<p><%= link_to "Plurality voting",
+"http://en.wikipedia.org/wiki/Plurality_electoral_system" %> selects the
+winner who has received the most "number one" votes, regardless of
+whether or not he or she has a majority of votes.</p>
+
+<p>Plurality voting is also variously referred to as, "first past the
+post," "winner-take-all," "majoritarian" or "simple majority"
+voting.</p>
+
+</div>
+</div>
\ No newline at end of file
--- /dev/null
+<div class="resultbox">
+<h3>Instant Runoff (IRV) Results</h3>
+
+<div class="rbmoreinfo">
+<h4>About Instant Runoff Voting</h4>
+
+<p><%= link_to "Instant runoff voting",
+"http://en.wikipedia.org/wiki/Instant_Runoff_Voting" %> is an electoral
+system in which voters rank candidates in order of preference. In an IRV
+election, if no candidate receives an overall majority of first
+preferences the candidates with fewest votes are eliminated one by one,
+and their votes transferred according to their second and third
+preferences (and so on), until one candidate achieves a majority.</p>
+
+<p>Instant-runoff voting (IRV) is also known as the Alternative Vote (AV) and
+by several other names.</p>
+
+</div>
+
+</div>
\ No newline at end of file
--- /dev/null
+<div class="mainresultbox">
+<h3>Schulze Method Results</h3>
+
+<div class="rbmoreinfo">
+<h4>About the Schulze Method</h4>
+
+<p>The <%= link_to "Schulze method",
+"http://en.wikipedia.org/wiki/Schulze_method" %> is a preferential
+voting system. It is based on the Condorcet method but includes a set of
+methods for resolving "circular" defeats.</p>
+
+<p>The Schulze method is also known as Schwartz Sequential Dropping
+(SSD), Cloneproof Schwartz Sequential Dropping (CSSD), Beatpath Method,
+Beatpath Winner, Path Voting, and Path Winner.</p>
+</div>
+
+</div>
+++ /dev/null
-<% victories, tied = @election.condorcet_result.victories_and_ties %>
-<% names = @election.names_by_id %>
-<% %>
-<table class="voterbox">
- <% victories.keys.each do |victor| %>
- <tr>
- <th><%=h names[victor] %></th>
- <% victories[victor].keys.each do |loser| %>
- <td><%=h names[loser] %> (<%= victories[victor][loser] %>)</td>
- <% end -%>
- </tr>
- <% end -%>
-</table>
-
-
-
-
<h2>Winners</h2>
-<div class="mainresultbox">
-<h3>Schulze Method Results</h3>
-<%= render :partial => 'result', :object => @election.ssd_result %>
+<%= render :partial => 'result_' + @election.election_method,
+ :object => @results[@election.election_method] %>
-<div class="rbmoreinfo">
-<h4>About the Schulze Method</h4>
-<p>The <%= link_to "Schulze method",
-"http://en.wikipedia.org/wiki/Schulze_method" %> is a preferential
-voting system. It is based on the Condorcet method but includes a set of
-methods for resolving "circular" defeats.</p>
-
-<p>The Schulze method is also known as Schwartz Sequential Dropping
-(SSD), Cloneproof Schwartz Sequential Dropping (CSSD), Beatpath Method,
-Beatpath Winner, Path Voting, and Path Winner.</p>
-</div>
-
-</div>
-
-<div class="resultbox">
-<h3>Plurality Results</h3>
-<%= render :partial => 'result', :object => @election.plurality_result %>
-
-<div class="rbmoreinfo">
-<h4>About Plurality Voting</h4>
-
-<p><%= link_to "Plurality voting",
-"http://en.wikipedia.org/wiki/Plurality_electoral_system" %> selects the
-winner who has received the most "number one" votes, regardless of
-whether or not he or she has a majority of votes.</p>
-
-<p>Plurality voting is also variously referred to as, "first past the
-post," "winner-take-all," "majoritarian" or "simple majority"
-voting.</p>
-
-</div>
-</div>
-
-<div class="resultbox">
-<h3>Approval Result</h3>
-<p><font size="-1">(This algorithm assumes that top two choices are "approved.")</font></p>
-<%= render :partial => 'result', :object => @election.approval_result %>
-
-<div class="rbmoreinfo">
-<h4>About Approval Voting</h4>
-
-<p><%= link_to "Approval voting",
-"http://en.wikipedia.org/wiki/Approval_voting" %> is a voting system in
-which each voter can vote for as many or as few candidates as the voter
-chooses. Approval voting is a limited form of range voting, where the
-range that voters are allowed to express is extremely constrained:
-accept or not.</p>
-
-</div>
-
-</div>
-
-<div class="resultbox">
-<h3>Simple Condorcet Results</h3>
-<%= render :partial => 'result', :object => @election.condorcet_result %>
-
-<div class="rbmoreinfo">
-<h4>About Simple Cordorcet Voting</h4>
-
-<p><%= link_to "Condorcet",
-"http://en.wikipedia.org/wiki/Condorcet_method" %> allows voters to rank
-candidates in order of preference. If there is a choice whom voters
-prefer to each other choice when compared to one at a time, that choice
-will be the winner.</p>
-
-<p>There is a family of Condorcet methods. This method is referred to as
-"Simple Condorcet" to distinguish it from the Schulze method which is
-another Condorcet system.</p>
-
-</div>
-</div>
-
-<div class="resultbox">
-<h3>Borda Count Results</h3>
-<%= render :partial => 'result', :object => @election.borda_result %>
-
-<div class="rbmoreinfo">
-<h4>About Borda Count</h4>
-
-<p><%= link_to "Borda count",
-"http://en.wikipedia.org/wiki/Borda_count" %>
-is an election method in which voters rank
-candidates in order of preference. The Borda count determines the winner
-of an election by giving each candidate a certain number of points
-corresponding to the position in which he or she is ranked by each
-voter. Once all votes have been counted the candidate with the most
-points is the winner.</p>
-</div>
-</div>
-
-<div class="resultbox">
-<h3>Instant Runoff (IRV) Results</h3>
-
-<div class="rbmoreinfo">
-<h4>About Instant Runoff Voting</h4>
-
-<p><%= link_to "Instant runoff voting",
-"http://en.wikipedia.org/wiki/Instant_Runoff_Voting" %> is an electoral
-system in which voters rank candidates in order of preference. In an IRV
-election, if no candidate receives an overall majority of first
-preferences the candidates with fewest votes are eliminated one by one,
-and their votes transferred according to their second and third
-preferences (and so on), until one candidate achieves a majority.</p>
-
-<p>Instant-runoff voting (IRV) is also known as the Alternative Vote (AV) and
-by several other names.</p>
-
-</div>
+<h3> Other Voting Methods </h3>
+<% for result_type in @election.other_methods %>
+<%= render :partial => 'result_' + result_type,
+ :object => @results[result_type] %>
+<% end %>
-</div>
<div class="clearbox"></div>
<% end %>
</table>
-<%= render :partial => 'victories_ties' %>
-<%= render :partial => 'pref_table' %>
+<%= render :partial => 'pref_tables' %>
<%=image_tag( graph_url( :action => 'votes_per_interval', :id => @election ))%>
<br />
<%= image_tag( graph_url( :action => 'borda_bar', :id => @election ) ) %><br />
-<%= image_tag( graph_url( :action => 'choices_positions', :id => @election ) ) %>
+<%= image_tag( graph_url( :action => 'choices_positions', :id => @election ) ) %><br />
+<%= image_tag(graph_url( :action => 'plurality_pie', :id => @election ) )%>
require 'uniq_token'
require 'randarray'
-require 'rubyvote'
require 'gruff'
require 'sparklines'
+require 'rubyvote'
+ELECTION_TYPES = %w(ssd plurality approval condorcet borda)
+
class String
# alternate capitalization method that does not lowercase the rest of
# the string -- which is almost never what I want
enddate datetime NOT NULL,
active tinyint NOT NULL DEFAULT 0,
user_id int NULL,
+ election_method varchar(100) DEFAULT 'ssd',
`type` varchar(100) NOT NULL,
primary key (id),
constraint fk_user_election foreign key (user_id) references users(id)
end
create_table "elections", :force => true do |t|
- t.column "name", :string, :limit => 100, :default => "", :null => false
- t.column "description", :text, :default => "", :null => false
- t.column "anonymous", :integer, :limit => 4, :default => 1, :null => false
- t.column "startdate", :datetime
- t.column "enddate", :datetime, :null => false
- t.column "active", :integer, :limit => 4, :default => 0, :null => false
- t.column "user_id", :integer
- t.column "type", :string, :limit => 100, :default => "", :null => false
+ t.column "name", :string, :limit => 100, :default => "", :null => false
+ t.column "description", :text, :default => "", :null => false
+ t.column "anonymous", :integer, :limit => 4, :default => 1, :null => false
+ t.column "startdate", :datetime
+ t.column "enddate", :datetime, :null => false
+ t.column "active", :integer, :limit => 4, :default => 0, :null => false
+ t.column "user_id", :integer
+ t.column "election_method", :string, :limit => 100, :default => "ssd"
+ t.column "type", :string, :limit => 100, :default => "", :null => false
end
add_index "elections", ["user_id"], :name => "fk_user_election"
end
def victories_and_ties
- victors = Array.new
- ties = Array.new
- victories = Hash.new
+ victories_ties = {}
candidates = @matrix.keys.sort
candidates.each do |candidate|
candidates.each do |challenger|
next if candidate == challenger
diff = @matrix[candidate][challenger] - @matrix[challenger][candidate]
- if diff > 0
- victors << [candidate, challenger, diff]
- elsif diff == 0 && ties.include?([challenger, candidate]) == false
- ties << [candidate, challenger]
+ victories_ties[candidate] = {} unless victories_ties.key?(candidate)
+ if diff >= 0
+ victories_ties[candidate][challenger] = diff
end
end
- end
-
- victors.each do |list|
- if victories.has_key?(list[0])
- victories[list[0]][list[1]] = list[2]
- else
- victories[list[0]] = Hash.new
- victories[list[0]][list[1]] = list[2]
- end
end
- return victories, ties
+ return victories_ties
end
def ranked_candidates