Condorcet votes can now properly handle votes expressing equivalency
authorJoe Slag <joe@slagwerks.com>
Fri, 4 May 2007 20:44:48 +0000 (20:44 +0000)
committerJoe Slag <joe@slagwerks.com>
Fri, 4 May 2007 20:44:48 +0000 (20:44 +0000)
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
lib/rubyvote/condorcet.rb
test/condorcet_test.rb

diff --git a/NEWS b/NEWS
index dafd72f8562c1023993d6e3d9abc654b2011f6d4..1021e4d8723cb7a7fc9bb16c75fb9db96490db42 100644 (file)
--- 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.
 2007-04-22: PureCondorcetVote now returns a one-dimensional array from
 its #winners method, rather than an array within an array.
index cee10db7919dcd95f013778dd900d024fc4b26b9..65c664dd28a57417aa0a90395562289c8de0eed4 100644 (file)
@@ -51,9 +51,9 @@ class CondorcetVote < ElectionVote
 
   def tally_vote(vote=nil)
 
 
   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
       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
         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|
           
         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
           end
-
-          # make sure we have a comparable object
-          @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner )
         end
       end
     end
         end
       end
     end
index aebc8f02027e59f1984a2a44000cde66fb85619e..4607163a6fee8c96e8a3c6605693cbce1b6cca9d 100644 (file)
@@ -128,4 +128,24 @@ class TestCondorcetVote < Test::Unit::TestCase
     assert_equal [[65, 64], [63, 66]], vote.results
   end
 
     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
 end

Benjamin Mako Hill || Want to submit a patch?