Created
August 10, 2012 04:02
-
-
Save istro/3310936 to your computer and use it in GitHub Desktop.
started tdd-ing schulze voting method... didn't get far, but had fun, stopped during refactoring so it's all broken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
describe "Schulze" do | |
before :each do | |
@ballots = [['q','f','b'],['f','b','q'],['q','f','b']] | |
@ballots2 = [['f','q','b'],['f','b','q'],['q','f','b']] | |
@schulze = Schulze.new @ballots | |
@schulze_twin = Schulze.new @ballots2 | |
end | |
it 'sits in his basement, and looks at ballots' do | |
@schulze.should be_an_instance_of Schulze | |
end | |
it 'makes up an empty table of pairwise preferences from the first ballot' do | |
@schulze.pairwise.values.each {|value| value.should eq 0} | |
@schulze.pairwise.keys.each {|key| key.should be_an_instance_of Array} | |
end | |
it 'looks at each ballot and increments each pairwise preference' do | |
@schulze.evaluate_yer_ballots | |
@schulze_twin.evaluate_yer_ballots | |
@schulze.pairwise.first.strength.should eq 2 | |
@schulze_twin.pairwise.first.strength.should eq 1 | |
end | |
it '(he) compares each pairwise preference against opposite of that pair, thus coming up with DIRECT PATHS!!!' | |
end | |
class Schulze | |
attr_reader :pairwise | |
def initialize(ballots) | |
@ballots = ballots | |
@pairwise = prep_pairwise | |
end | |
def prep_pairwise | |
@ballots.first.map do |option1| | |
@ballots.first.map do |option2| | |
Path.new({:from => option1, :to => option2, :strength => 0}) if option1 != option2 | |
end | |
end | |
end | |
def evaluate_yer_ballots | |
@ballots.each do |ballot| | |
@pairwise.each do |pair| | |
@pairwise[pair[0]]+=1 if (ballot.index(pair[0][0]) < ballot.index(pair[0][1])) | |
end | |
end | |
end | |
end | |
class Path | |
def initialize(opts = {}) | |
@from = opts.fetch[:from] | |
@to = opts.fetch[:to] | |
@strength = opts.fetch[:strength] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment