vote-schulze 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm --create use 1.9.2@schulze
2
+
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.0"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.6.0)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.6.0)
27
+ rcov
28
+ rspec (~> 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Christoph Grabo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # vote-schulze
2
+
3
+ This gem is a Ruby implementation of the Schulze voting method with help of the Floyd–Warshall algorithm, a type of the Condorcet voting methods.
4
+
5
+ Wikipedia:
6
+
7
+ * [Schulze method](http://en.wikipedia.org/wiki/Schulze_method) ([deutsch](http://de.wikipedia.org/wiki/Schulze-Methode))
8
+ * [Floyd–Warshall algorithm](http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm)
9
+
10
+ ## Install
11
+
12
+ ``` bash
13
+ gem install vote-schulze
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ``` ruby
19
+ require 'vote-schulze'
20
+ vs = SchulzeBasic.new
21
+ vs.load candidate_count, vote_list
22
+ vs.run
23
+ #=> [result as array]
24
+ ```
25
+
26
+ ## Contributing to vote-schulze
27
+
28
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
29
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
30
+ * Fork the project
31
+ * Use git-flow
32
+ * Start a feature/bugfix branch
33
+ * Commit and push until you are happy with your contribution
34
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
35
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
36
+
37
+ ## Copyright
38
+
39
+ Copyright (c) 2011 Christoph Grabo. See LICENSE.txt for
40
+ further details.
41
+
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "vote-schulze"
18
+ gem.homepage = "http://github.com/asaaki/vote-schulze"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Schulze method implementation in Ruby (Condorcet voting method)}
21
+ gem.description = %Q{This gem is a Ruby implementation of the Schulze voting method with help of the Floyd–Warshall algorithm, a type of the Condorcet voting methods.}
22
+ gem.email = "chris@dinarrr.com"
23
+ gem.authors = ["Christoph Grabo"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "vote-schulze #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
50
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+ # monkey patch for public element modification
3
+ class Matrix
4
+ def []=(i,j,v)
5
+ @rows[i][j] = v
6
+ end
7
+ end
8
+
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+ require 'matrix'
3
+ require 'matrix_ext'
4
+ autoload :Vote, 'vote'
5
+ SchulzeInput = Vote::Condorcet::Schulze::Input
6
+ SchulzeBasic = Vote::Condorcet::Schulze::Basic
7
+ SchulzeWinAndLost = Vote::Condorcet::Schulze::WinAndLost
8
+
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+ module Vote
3
+ autoload :Condorcet, 'vote/condorcet'
4
+ end
5
+
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+ module Vote
3
+ module Condorcet
4
+ autoload :Schulze, 'vote/condorcet/schulze'
5
+ end
6
+ end
7
+
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+ module Vote
3
+ module Condorcet
4
+ module Schulze
5
+ autoload :Input, 'vote/condorcet/schulze/input'
6
+ autoload :Basic, 'vote/condorcet/schulze/basic'
7
+ autoload :WinAndLost, 'vote/condorcet/schulze/win_and_lost'
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,124 @@
1
+ # encoding: UTF-8
2
+ require 'matrix'
3
+
4
+ module Vote
5
+ module Condorcet
6
+ module Schulze
7
+
8
+ class Basic
9
+ @candidate_count = 0
10
+ @vote_matrix = nil
11
+ @play_matrix = nil
12
+ @result_matrix = nil
13
+ @ranking = nil
14
+
15
+ def initialize candidate_count=0, vote_matrix=nil
16
+ if candidate_count > 0 && vote_matrix != nil
17
+ load
18
+ end
19
+ true
20
+ end
21
+
22
+ def load candidate_count, vote_matrix
23
+
24
+ @play_matrix = nil
25
+ @result_matrix = nil
26
+ @ranking = nil
27
+
28
+ @candidate_count = candidate_count
29
+
30
+ @vote_matrix = vote_matrix.is_a?(Vote::Condorcet::Schulze::Input) ? \
31
+ vote_matrix.matrix : \
32
+ Vote::Condorcet::Schulze::Input.new(
33
+ candidate_count,
34
+ vote_matrix
35
+ ).matrix
36
+
37
+ self
38
+ end
39
+
40
+ def vote_matrix
41
+ @vote_matrix
42
+ end
43
+
44
+ def play
45
+
46
+ @play_matrix ||= Matrix.scalar(@candidate_count,0)
47
+
48
+ # step 1: find matches with wins
49
+ @candidate_count.times do |i|
50
+ @candidate_count.times do |j|
51
+ next if i==j
52
+ if @vote_matrix[i,j] > @vote_matrix[j,i]
53
+ @play_matrix[i,j] = @vote_matrix[i,j]
54
+ end
55
+ end
56
+ end
57
+
58
+ #step 2: find strongest pathes
59
+ @candidate_count.times do |i|
60
+ @candidate_count.times do |j|
61
+ next if i==j
62
+ @candidate_count.times do |k|
63
+ next if i==k
64
+ next if j==k
65
+ @play_matrix[j,k] = [
66
+ @play_matrix[j,k],
67
+ [ @play_matrix[j,i], @play_matrix[i,k] ].min
68
+ ].max
69
+ end
70
+ end
71
+ end
72
+
73
+ true
74
+ end
75
+
76
+ def play_matrix
77
+ @play_matrix
78
+ end
79
+
80
+ def result
81
+ @result_matrix ||= Matrix.scalar(@candidate_count,0)
82
+
83
+ @result_matrix.each_with_index do |e,x,y|
84
+ next if x==y
85
+ @result_matrix[x,y] = e+1 if @play_matrix[x,y] > @play_matrix[y,x]
86
+ end
87
+
88
+ true
89
+ end
90
+
91
+ def result_matrix
92
+ @result_matrix
93
+ end
94
+
95
+ def rank
96
+
97
+ @ranking ||= Array.new(@candidate_count,0)
98
+
99
+ @result_matrix.row_vectors.each_with_index do |v,i|
100
+ v.to_a.map do |e|
101
+ @ranking[i] += e
102
+ end
103
+ end
104
+
105
+ true
106
+ end
107
+
108
+ def ranking_array
109
+ @ranking
110
+ end
111
+
112
+ def run
113
+ play
114
+ result
115
+ rank
116
+ @ranking
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+ end
123
+ end
124
+
@@ -0,0 +1,44 @@
1
+ # encoding: UTF-8
2
+ require 'matrix'
3
+
4
+ module Vote
5
+ module Condorcet
6
+ module Schulze
7
+
8
+ class Input
9
+
10
+ @vote_matrix = nil
11
+
12
+ def initialize candidate_count, vote_list
13
+ @vote_matrix = Matrix.scalar(candidate_count,0)
14
+ insert_vote_array(vote_list) if vote_list.is_a?(Array)
15
+ insert_vote_file(vote_list) if vote_list.is_a?(File)
16
+ end
17
+
18
+ def insert_vote_array va
19
+ va.each do |vote|
20
+ @vote_matrix.each_with_index do |e,x,y|
21
+ next if x==y
22
+ @vote_matrix[x,y] += 1 if vote[x]>vote[y]
23
+ end
24
+ end
25
+ end
26
+
27
+ def insert_vote_list vl
28
+ #...
29
+ end
30
+
31
+ def insert_vote_file vf
32
+ #...
33
+ end
34
+
35
+ def matrix
36
+ @vote_matrix
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+ module Vote
3
+ module Condorcet
4
+ module Schulze
5
+
6
+ class WinAndLost < Vote::Condorcet::Schulze::Basic
7
+ #...
8
+ end
9
+
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'vote-schulze'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "VoteSchulze" do
4
+ #it "fails" do
5
+ # fail "hey buddy, you should probably rename this file and start specing for real"
6
+ #end
7
+ pending
8
+ end
9
+
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vote-schulze
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christoph Grabo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-17 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &18950060 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.3.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *18950060
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &18949580 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *18949580
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &18949100 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.6.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *18949100
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &18948620 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *18948620
59
+ description: This gem is a Ruby implementation of the Schulze voting method with help
60
+ of the Floyd–Warshall algorithm, a type of the Condorcet voting methods.
61
+ email: chris@dinarrr.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files:
65
+ - LICENSE.txt
66
+ - README.md
67
+ files:
68
+ - .document
69
+ - .rspec
70
+ - .rvmrc
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - VERSION
77
+ - lib/matrix_ext.rb
78
+ - lib/vote-schulze.rb
79
+ - lib/vote.rb
80
+ - lib/vote/condorcet.rb
81
+ - lib/vote/condorcet/schulze.rb
82
+ - lib/vote/condorcet/schulze/basic.rb
83
+ - lib/vote/condorcet/schulze/input.rb
84
+ - lib/vote/condorcet/schulze/win_and_lost.rb
85
+ - spec/spec_helper.rb
86
+ - spec/vote-schulze_spec.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/asaaki/vote-schulze
89
+ licenses:
90
+ - MIT
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: -203018078703163748
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.6.2
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Schulze method implementation in Ruby (Condorcet voting method)
116
+ test_files: []