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
+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.
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
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
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