trueskill-ranked 2.0 → 2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,254 @@
1
+ class Matrix
2
+
3
+ include Enumerable
4
+ @size=nil
5
+ @base=nil
6
+ def initialize(src=nil,width=nil,height=nil,options={})
7
+ if options.include? :func
8
+ f,src=options[:func],{}
9
+ @size=[width,height]
10
+ if width.nil?
11
+ @size[0]=Proc.new do |w|
12
+ @size[0]=w
13
+ end
14
+ end
15
+ if height.nil?
16
+ @size[1]=Proc.new do |h|
17
+ @size[1]=h
18
+ end
19
+ end
20
+ arr=Array(f.call(@size[0],@size[1]))
21
+ #pp arr
22
+ arr.each do|v,val|
23
+ src[[v[0],v[1]]]=val
24
+ end
25
+ width=@size[0]
26
+ height=@size[1]
27
+ end
28
+ if src.is_a? Array
29
+ unique_col_sizes=Set.new(src.map { |x| x.length })
30
+ if not unique_col_sizes.length==1
31
+ raise "Must be a rectangular range of numbers"
32
+ end
33
+ num=true
34
+ src.flatten.each { |x| if not x.is_a? Numeric then num=false end }
35
+ if num==false
36
+ raise "Must be a rectangular range of numbers"
37
+ end
38
+ two_dimensional_array=src
39
+ elsif src.is_a? Hash
40
+ if width.nil? or height.nil?
41
+ w,h=0,0
42
+ src.each_key do |x|
43
+ r=x[0]
44
+ c=x[1]
45
+ if width.nil?
46
+ w=[w,r+1].max
47
+ end
48
+ if height.nil?
49
+ h=[h,c+1].max
50
+ end
51
+ end
52
+ if width.nil?
53
+ width=w
54
+ end
55
+ if height.nil?
56
+ height=h
57
+ end
58
+ end
59
+ two_dimensional_array=[]
60
+ (0...height).each do |r|
61
+ row=[]
62
+ (0...width).each do |c|
63
+ if src.include? [r,c]
64
+ val=src[[r,c]]
65
+ else
66
+ val=0
67
+ end
68
+ row << val
69
+ end
70
+ two_dimensional_array << row
71
+ end
72
+ end
73
+ @base=two_dimensional_array
74
+ end
75
+
76
+ def width
77
+ return @base[0].length
78
+ end
79
+
80
+ def height
81
+ return @base.length
82
+ end
83
+ def each(&block)
84
+ return
85
+ end
86
+ def [](r,c)
87
+ return @base[r][c]
88
+ end
89
+ def []=(r,c,val)
90
+ @base[r][c]=val
91
+ end
92
+
93
+ def transpose
94
+ w,h=width,height
95
+ src={}
96
+ (0...w).each do |c|
97
+ (0...h).each do |r|
98
+ src[[c,r]]=@base[r][c]
99
+ end
100
+ end
101
+ return Matrix.new(src,h,w)
102
+ end
103
+ def minor(row_n,col_n)
104
+ w,h=width,height
105
+ if row_n < 0 or row_n >= w or col_n < 0 or col_n >= h
106
+ raise 'invalid row or column number'
107
+ end
108
+ two_dimensional_array=[]
109
+ (0...h).each do |r|
110
+ if r==row_n
111
+ next
112
+ end
113
+ row=[]
114
+ (0...w).each do |c|
115
+ if c==col_n
116
+ next
117
+ end
118
+ row << @base[r][c]
119
+ end
120
+ two_dimensional_array << row
121
+ end
122
+ return Matrix.new(two_dimensional_array)
123
+ end
124
+ def row(r)
125
+ @base[r]
126
+ end
127
+ def setrow(r,val)
128
+ @base[r]=val
129
+ end
130
+ def determinant
131
+ w,h=width,height
132
+ if not w==h
133
+ raise "Must be a square matrix"
134
+ end
135
+ tmp,rv=self.clone,1
136
+ (w-1).downto(1) do |c|
137
+ p=[]
138
+ (0..c).each {|r| p << [tmp[r,c].abs,r]}
139
+ pivot,r=p.max
140
+ pivot=tmp[r,c]
141
+ if pivot.nil?
142
+ return 0
143
+ end
144
+
145
+ val=tmp.row(r)
146
+ tmp.setrow(r,tmp.row(c))
147
+
148
+ tmp.setrow(c,val)
149
+ if not r==c
150
+ rv=-rv
151
+ end
152
+ rv*=pivot
153
+ fact=-1.0/pivot
154
+ (0...c).each do |r|
155
+ f=fact*tmp[r,c]
156
+ (0...c).each do |x|
157
+ tmp[r,x]+=f*tmp[c,x]
158
+ end
159
+ end
160
+ end
161
+ return rv*tmp[0,0]
162
+ end
163
+
164
+ def adjucate
165
+ w,h=width,height
166
+ if h==2
167
+ a,b=self[0,0],self[0,1]
168
+ c,d=self[1,0],self[1,1]
169
+ return Matrix.new([[d,-b],[-c,a]])
170
+ else
171
+ src={}
172
+ (0...height).each do |r|
173
+ (0...width).each do |c|
174
+ v=0
175
+ if (r+c) % 2==1
176
+ v=-1
177
+ else
178
+ v=1
179
+ end
180
+ src[[r,c]]=self.minor(r,c).determinant*v
181
+ end
182
+ end
183
+ return Matrix.new(src,w,h)
184
+ end
185
+ end
186
+
187
+ def inverse
188
+ if width==1 and height==1
189
+ return Matrix.new([[1.0/self[0,0]]])
190
+ else
191
+ return (1.0/self.determinant())*self.adjucate()
192
+ end
193
+ end
194
+
195
+
196
+ def coerce(other)
197
+ return self, other
198
+ end
199
+
200
+ def +(other)
201
+ w,h=width,height
202
+ if not (w== other.width and h==other.height)
203
+ raise "Must be same size"
204
+ end
205
+ src={}
206
+ (0...h).each do |r|
207
+ (0...w).each do |c|
208
+ src[[r,c]]=self[r,c]+other[r,c]
209
+ end
210
+ end
211
+ return Matrix.new(src,w,h)
212
+ end
213
+
214
+ def *(other)
215
+ if other.is_a? Matrix
216
+ w,h=other.width,height
217
+ #pp w
218
+ #pp h
219
+ #pp other.width
220
+ #pp other.height
221
+ if not width== other.height
222
+ raise "Must be same size"
223
+ end
224
+ src={}
225
+ (0...h).each do |r|
226
+ (0...w).each do |c|
227
+ lst=[]
228
+ #pp r
229
+ #pp c
230
+
231
+ (0...width).each { |x| lst << (self[r,x] * other[x,c])}
232
+ src[[r,c]]=lst.inject(:+)
233
+ end
234
+ end
235
+ return Matrix.new(src,w,h)
236
+ else
237
+ w,h=width,height
238
+ src={}
239
+ (0...h).each do |r|
240
+ (0...w).each do |c|
241
+ src[[r,c]]=other*self[r,c]
242
+ end
243
+ end
244
+ return Matrix.new(src,w,h)
245
+ end
246
+ end
247
+ def clone
248
+ rows=[]
249
+ @base.each do |x|
250
+ rows << x.clone
251
+ end
252
+ Matrix.new(rows)
253
+ end
254
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trueskill-ranked
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.0'
4
+ version: '2.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ cert_chain: []
13
13
  date: 2012-06-13 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: A improved TrueSkill with correct ranking order Now with Match Quality
16
- also
16
+ also fixed error with missing matrix
17
17
  email: kat.swales@nekokittygames.com
18
18
  executables: []
19
19
  extensions: []
@@ -33,6 +33,7 @@ files:
33
33
  - lib/TrueSkill/FactorGraph/Variable.rb
34
34
  - lib/TrueSkill/Mathematics/general.rb
35
35
  - lib/TrueSkill/Mathematics/guassian.rb
36
+ - lib/TrueSkill/Mathematics/matrix.rb
36
37
  homepage: https://github.com/nekosune/trueskill-ranked
37
38
  licenses: []
38
39
  post_install_message:
@@ -53,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
54
  version: '0'
54
55
  requirements: []
55
56
  rubyforge_project:
56
- rubygems_version: 1.8.11
57
+ rubygems_version: 1.8.24
57
58
  signing_key:
58
59
  specification_version: 3
59
60
  summary: Ranked TrueSkill