From 1723e5c01789f5240a148a4671492de6a7438abd Mon Sep 17 00:00:00 2001 From: Joe Slag Date: Fri, 4 May 2007 20:44:48 +0000 Subject: [PATCH] Condorcet votes can now properly handle votes expressing equivalency between candidates. Viz. [1, [2, 3], 4] indicates that options 2 and 3 are equally preferred, both coming after 1 and before 4. git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@27 1440c7f4-e209-0410-9a04-881b5eb134a8 --- NEWS | 4 ++++ lib/rubyvote/condorcet.rb | 27 +++++++++++++++------------ test/condorcet_test.rb | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index dafd72f..1021e4d 100644 --- a/NEWS +++ b/NEWS @@ -1,2 +1,6 @@ +2007-05-04: Condorcet votes can now properly handle votes expressing +equivalency between candidates. Viz. [1, [2, 3], 4] indicates that +options 2 and 3 are equally preferred, both coming after 1 and before 4. + 2007-04-22: PureCondorcetVote now returns a one-dimensional array from its #winners method, rather than an array within an array. diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb index cee10db..65c664d 100644 --- a/lib/rubyvote/condorcet.rb +++ b/lib/rubyvote/condorcet.rb @@ -51,9 +51,9 @@ class CondorcetVote < ElectionVote def tally_vote(vote=nil) - vote.each_with_index do |winner, index| + vote.each_with_index do |winners, index| if vote.flatten.length < @candidates.length - implied_losers = @candidates.select { |c| not vote.include?(c) } + implied_losers = @candidates.select { |c| not vote.flatten.include?(c) } vote.push(implied_losers) end if vote.length - 1 == index @@ -66,18 +66,21 @@ class CondorcetVote < ElectionVote place = [place] unless place.class == Array place.each do |loser| + winners = [winners] unless winners.class == Array + next if winners.include?(loser) + winners.each do |winner| + @votes[winner] = Hash.new unless @votes.has_key?(winner) + @votes[loser] = Hash.new unless @votes.has_key?(loser) + + if @votes[winner].has_key?(loser) + @votes[winner][loser] += 1 + else + @votes[winner][loser] = 1 + end - @votes[winner] = Hash.new unless @votes.has_key?(winner) - @votes[loser] = Hash.new unless @votes.has_key?(loser) - - if @votes[winner].has_key?(loser) - @votes[winner][loser] += 1 - else - @votes[winner][loser] = 1 + # make sure we have a comparable object + @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner ) end - - # make sure we have a comparable object - @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner ) end end end diff --git a/test/condorcet_test.rb b/test/condorcet_test.rb index aebc8f0..4607163 100644 --- a/test/condorcet_test.rb +++ b/test/condorcet_test.rb @@ -128,4 +128,24 @@ class TestCondorcetVote < Test::Unit::TestCase assert_equal [[65, 64], [63, 66]], vote.results end + def test_ssd_multiple_equivalent + vote_array = Array.new + vote_array << ['B', ['A', 'C'], 'D'] + vote_array << ['A', 'C'] + vote_array << [['E', 'D'], 'C'] + results = CloneproofSSDVote.new(vote_array).results + assert_equal 5, results.flatten.size + assert_equal [['A', 'C'], ['B', 'D'], ['E']], results + end + + def test_ssd_multiple_equivalent_2 + vote_array = Array.new + vote_array << ['B', ['A'], 'C'] + vote_array << ['B', ['C'], 'A'] + results = CloneproofSSDVote.new(vote_array).results + assert_equal 3, results.flatten.size + assert_equal [['B'], ['A', 'C']], results + end + + end -- 2.39.5