vote-schulze 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # vote-schulze
2
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.
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
4
 
5
5
  Wikipedia:
6
6
 
@@ -17,12 +17,45 @@ gem install vote-schulze
17
17
 
18
18
  ``` ruby
19
19
  require 'vote-schulze'
20
- vs = SchulzeBasic.new
21
- vs.load candidate_count, vote_list
22
- vs.run
23
- #=> [result as array]
20
+ vs = SchulzeBasic.do candidate_count, vote_list
21
+ vs.ranking_array
24
22
  ```
25
23
 
24
+ `SchulzeBasic.do` - SchulzeBasic is a short term for `Vote::Condorcet::Schulze::Basic` and `.do` is a method of this class!
25
+
26
+ Input:
27
+
28
+ * `candidate_count` Integer: number of candidates
29
+ * `vote_list` Array of Arrays: votes of each voter as weights `[ [A,B,C,...],[A,B,C,...],[A,B,C,...] ]`
30
+
31
+ preference order to weight example:
32
+
33
+ ```
34
+ voter => A D C B
35
+
36
+ weight => 4,1,2,3
37
+
38
+ A is on first position = highest prio == 4
39
+ B is on last position == 1
40
+ C is on third position == 2
41
+ D is on second position == 3
42
+ ```
43
+
44
+ Later versions will have an automatic Preference-to-Weight algorithm.
45
+ (Internally only integers are used for calculation of ranking.)
46
+
47
+ ### _SchulzeBasic_
48
+
49
+ It doesn't matter if you start counting at 0 (zero) or 1 (one).
50
+
51
+ Also it's not important, if you use jumps (like `1 3 5 9`).
52
+
53
+ Internally it will only check if candidate X > candidate Y
54
+
55
+ Output:
56
+
57
+ * `.ranking_array` Array: numbers of total wins for each candidate `[candidate A, candidate B, candidate C, ...]`
58
+
26
59
  ## Contributing to vote-schulze
27
60
 
28
61
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
@@ -36,6 +69,5 @@ vs.run
36
69
 
37
70
  ## Copyright
38
71
 
39
- Copyright (c) 2011 Christoph Grabo. See LICENSE.txt for
40
- further details.
72
+ Copyright (c) 2011 Christoph Grabo. See LICENSE.txt for further details.
41
73
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
data/lib/vote-schulze.rb CHANGED
@@ -1,7 +1,14 @@
1
1
  # encoding: UTF-8
2
+
3
+ # $:<<`pwd`.strip
4
+ # require 'vote-schulze'
5
+
2
6
  require 'matrix'
3
- require 'matrix_ext'
7
+ require 'vote'
8
+ require 'vote/matrix'
9
+
4
10
  autoload :Vote, 'vote'
11
+
5
12
  SchulzeInput = Vote::Condorcet::Schulze::Input
6
13
  SchulzeBasic = Vote::Condorcet::Schulze::Basic
7
14
  SchulzeWinAndLost = Vote::Condorcet::Schulze::WinAndLost
@@ -1,30 +1,17 @@
1
1
  # encoding: UTF-8
2
- require 'matrix'
3
2
 
4
3
  module Vote
5
4
  module Condorcet
6
5
  module Schulze
7
6
 
8
7
  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
8
+
9
+ def initialize
10
+ #self
20
11
  end
21
12
 
22
13
  def load candidate_count, vote_matrix
23
14
 
24
- @play_matrix = nil
25
- @result_matrix = nil
26
- @ranking = nil
27
-
28
15
  @candidate_count = candidate_count
29
16
 
30
17
  @vote_matrix = vote_matrix.is_a?(Vote::Condorcet::Schulze::Input) ? \
@@ -37,13 +24,11 @@ module Vote
37
24
  self
38
25
  end
39
26
 
40
- def vote_matrix
41
- @vote_matrix
42
- end
27
+ private
43
28
 
44
29
  def play
45
30
 
46
- @play_matrix ||= Matrix.scalar(@candidate_count,0)
31
+ @play_matrix = ::Matrix.scalar(@candidate_count,0).extend(Vote::Matrix)
47
32
 
48
33
  # step 1: find matches with wins
49
34
  @candidate_count.times do |i|
@@ -70,31 +55,26 @@ module Vote
70
55
  end
71
56
  end
72
57
 
73
- true
58
+ self
74
59
  end
75
60
 
76
- def play_matrix
77
- @play_matrix
78
- end
79
61
 
80
62
  def result
81
- @result_matrix ||= Matrix.scalar(@candidate_count,0)
63
+
64
+ @result_matrix = ::Matrix.scalar(@candidate_count,0).extend(Vote::Matrix)
82
65
 
83
66
  @result_matrix.each_with_index do |e,x,y|
84
67
  next if x==y
85
68
  @result_matrix[x,y] = e+1 if @play_matrix[x,y] > @play_matrix[y,x]
86
69
  end
87
70
 
88
- true
71
+ self
89
72
  end
90
73
 
91
- def result_matrix
92
- @result_matrix
93
- end
94
74
 
95
75
  def rank
96
76
 
97
- @ranking ||= Array.new(@candidate_count,0)
77
+ @ranking = Array.new(@candidate_count,0)
98
78
 
99
79
  @result_matrix.row_vectors.each_with_index do |v,i|
100
80
  v.to_a.map do |e|
@@ -102,23 +82,42 @@ module Vote
102
82
  end
103
83
  end
104
84
 
105
- true
85
+ self
106
86
  end
107
87
 
108
- def ranking_array
109
- @ranking
110
- end
88
+ public
111
89
 
112
90
  def run
113
91
  play
114
92
  result
115
93
  rank
94
+ end
95
+
96
+ def vote_matrix
97
+ @vote_matrix
98
+ end
99
+ def play_matrix
100
+ @play_matrix
101
+ end
102
+ def result_matrix
103
+ @result_matrix
104
+ end
105
+ def ranking_array
116
106
  @ranking
117
107
  end
118
108
 
119
- end
120
109
 
121
- end
122
- end
123
- end
110
+ # All-in-One class method to get a calculated SchulzeBasic object
111
+ def self.do candidate_count, vote_matrix
112
+ instance = new
113
+ instance.load candidate_count, vote_matrix
114
+ instance.run
115
+ instance
116
+ end
117
+
118
+ end #Basic
119
+
120
+ end #Schulze
121
+ end #Condorcet
122
+ end #Vote
124
123
 
@@ -1,5 +1,4 @@
1
1
  # encoding: UTF-8
2
- require 'matrix'
3
2
 
4
3
  module Vote
5
4
  module Condorcet
@@ -10,7 +9,7 @@ module Vote
10
9
  @vote_matrix = nil
11
10
 
12
11
  def initialize candidate_count, vote_list
13
- @vote_matrix = Matrix.scalar(candidate_count,0)
12
+ @vote_matrix = ::Matrix.scalar(candidate_count,0).extend(Vote::Matrix)
14
13
  insert_vote_array(vote_list) if vote_list.is_a?(Array)
15
14
  insert_vote_file(vote_list) if vote_list.is_a?(File)
16
15
  end
@@ -1,4 +1,5 @@
1
1
  # encoding: UTF-8
2
+
2
3
  module Vote
3
4
  module Condorcet
4
5
  module Schulze
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+
3
+ # extend matrix with method []=(i,j,v)
4
+ # usage: m = ::Matrix.scalar(size,value).extend(Vote::Matrix)
5
+
6
+ module Vote
7
+ module Matrix
8
+
9
+ def []=(i,j,v)
10
+ @rows[i][j] = v
11
+ end
12
+
13
+ end
14
+ end
15
+
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{vote-schulze}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Christoph Grabo"]
12
+ s.date = %q{2011-05-19}
13
+ s.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.}
14
+ s.email = %q{chris@dinarrr.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".rvmrc",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/vote-schulze.rb",
30
+ "lib/vote.rb",
31
+ "lib/vote/condorcet.rb",
32
+ "lib/vote/condorcet/schulze.rb",
33
+ "lib/vote/condorcet/schulze/basic.rb",
34
+ "lib/vote/condorcet/schulze/input.rb",
35
+ "lib/vote/condorcet/schulze/win_and_lost.rb",
36
+ "lib/vote/matrix.rb",
37
+ "spec/spec_helper.rb",
38
+ "spec/vote-schulze_spec.rb",
39
+ "vote-schulze.gemspec"
40
+ ]
41
+ s.homepage = %q{http://github.com/asaaki/vote-schulze}
42
+ s.licenses = ["MIT"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.6.2}
45
+ s.summary = %q{Schulze method implementation in Ruby (Condorcet voting method)}
46
+
47
+ if s.respond_to? :specification_version then
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
52
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
54
+ s.add_development_dependency(%q<rcov>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
59
+ s.add_dependency(%q<rcov>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
65
+ s.add_dependency(%q<rcov>, [">= 0"])
66
+ end
67
+ end
68
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vote-schulze
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-05-17 00:00:00.000000000 +02:00
12
+ date: 2011-05-19 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &18950060 !ruby/object:Gem::Requirement
17
+ requirement: &26515360 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 2.3.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *18950060
25
+ version_requirements: *26515360
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: bundler
28
- requirement: &18949580 !ruby/object:Gem::Requirement
28
+ requirement: &26514880 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.0.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *18949580
36
+ version_requirements: *26514880
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: jeweler
39
- requirement: &18949100 !ruby/object:Gem::Requirement
39
+ requirement: &26514400 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.6.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *18949100
47
+ version_requirements: *26514400
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rcov
50
- requirement: &18948620 !ruby/object:Gem::Requirement
50
+ requirement: &26513920 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *18948620
58
+ version_requirements: *26513920
59
59
  description: This gem is a Ruby implementation of the Schulze voting method with help
60
60
  of the Floyd–Warshall algorithm, a type of the Condorcet voting methods.
61
61
  email: chris@dinarrr.com
@@ -74,7 +74,6 @@ files:
74
74
  - README.md
75
75
  - Rakefile
76
76
  - VERSION
77
- - lib/matrix_ext.rb
78
77
  - lib/vote-schulze.rb
79
78
  - lib/vote.rb
80
79
  - lib/vote/condorcet.rb
@@ -82,8 +81,10 @@ files:
82
81
  - lib/vote/condorcet/schulze/basic.rb
83
82
  - lib/vote/condorcet/schulze/input.rb
84
83
  - lib/vote/condorcet/schulze/win_and_lost.rb
84
+ - lib/vote/matrix.rb
85
85
  - spec/spec_helper.rb
86
86
  - spec/vote-schulze_spec.rb
87
+ - vote-schulze.gemspec
87
88
  has_rdoc: true
88
89
  homepage: http://github.com/asaaki/vote-schulze
89
90
  licenses:
@@ -100,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
101
  version: '0'
101
102
  segments:
102
103
  - 0
103
- hash: -203018078703163748
104
+ hash: 2256632431419553553
104
105
  required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  none: false
106
107
  requirements:
data/lib/matrix_ext.rb DELETED
@@ -1,8 +0,0 @@
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
-