#filter_out and PureCondorcet weren't getting along well for sparse elections
authorJoe Slag <joe@slagwerks.com>
Tue, 20 Mar 2007 17:14:51 +0000 (17:14 +0000)
committerJoe Slag <joe@slagwerks.com>
Tue, 20 Mar 2007 17:14:51 +0000 (17:14 +0000)
git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@21 1440c7f4-e209-0410-9a04-881b5eb134a8

lib/rubyvote/condorcet.rb
lib/rubyvote/election.rb
test/condorcet_test.rb

index 340be92..3269261 100644 (file)
@@ -56,14 +56,13 @@ class CondorcetVote < ElectionVote
       if vote.length - 1 == index
         losers = []
       else
-        losers = vote.last( vote.flatten.length - index )
+        losers = vote.flatten.last( vote.flatten.length - index - 1)
       end
 
       losers.each do |place|
         place = [place] unless place.class == Array
         place.each do |loser|
           
-          next if winner == loser
 
           @votes[winner] = Hash.new unless @votes.has_key?(winner)
           @votes[loser] = Hash.new unless @votes.has_key?(loser)
@@ -82,13 +81,7 @@ class CondorcetVote < ElectionVote
   end
 
   def result
-    top_result = resultFactory( self )
-    until @candidates.empty?
-      aResult = resultFactory( self )
-      top_result.full_results << aResult
-      filter_out(aResult)
-    end
-    top_result
+    resultFactory( self )
   end
 
   protected
@@ -109,6 +102,17 @@ class CloneproofSSDVote < CondorcetVote
   def resultFactory(init)
     CloneproofSSDResult.new(init)
   end
+
+  def result
+    top_result = resultFactory( self )
+    until @candidates.empty?
+      aResult = resultFactory( self )
+      top_result.full_results << aResult
+      filter_out(aResult)
+    end
+    top_result
+  end
+
 end
 
 
index 4499233..4d51d78 100644 (file)
@@ -63,12 +63,7 @@ class ElectionVote
   end
 
   def filter_out(winner)
-    if winner.winners[0].class == Array
-      to_filter = winner.winners[0]
-    else
-      to_filter = [winner.winners[0]]
-    end
-    @candidates.delete_if {|x| to_filter.include?(x)}
+    @candidates.delete_if {|x| winner.winners.include?(x)}
   end
 
 end
index 82ff292..f0dd1cc 100644 (file)
@@ -98,4 +98,13 @@ class TestCondorcetVote < Test::Unit::TestCase
     assert_equal [[78]], result.get_full_results
   end
 
+  def test_ssd_sparse
+    vote_array = Array.new
+    vote_array << ['B', 'D']
+    vote_array << ['A', 'C']
+    vote_array << ['E', 'C']
+    result = CloneproofSSDVote.new(vote_array).result
+    assert_equal 5, result.get_full_results.flatten.size
+  end
+
 end

Benjamin Mako Hill || Want to submit a patch?