Skip to content

Instantly share code, notes, and snippets.

@istro
Created August 10, 2012 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save istro/3310936 to your computer and use it in GitHub Desktop.
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
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