4 ## Copyright (C) 2006 Daigo Moriwaki <daigo at debian dot org>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # This calculates rating scores of every players from CSA files, and outputs a
22 # yaml file (players.yaml) that Shogi Server can read.
25 # $ ./mk_rate . > players.yaml
27 # The conditions that games and players are rated as following:
28 # * Rated games, which were played by both rated players.
29 # * Rated players, who logged in the server with a name followed by a trip:
31 # * (Rated) players, who played more than $GAMES_LIMIT [15] (rated) games.
37 # Sample Commands to isntall prerequires will work for Debian.
40 # $ sudo aptitude install rubygems
42 # * Ruby bindings for the GNU Scientific Library (GSL)
43 # $ sudo aptitude install libgsl-ruby1.8
44 # Or, download it from http://rb-gsl.rubyforge.org/ .
46 # * RGL: Ruby Graph Library
47 # $ sudo gem install rgl
48 # Or, download it from http://rubyforge.org/projects/rgl/ .
55 require 'rgl/adjacency'
56 require 'rgl/connected_components'
58 #################################################
62 # Count out players who play less games than $GAMES_LIMIT
63 $GAMES_LIMIT = $DEBUG ? 0 : 15
70 # Holds the last time when a player gamed
71 $players_time = Hash.new { Time.at(0) }
74 #################################################
75 # Keeps the value of the lowest key
83 if @lowest.empty? || key < @lowest[0]
84 @lowest = [key, value]
97 #################################################
98 # Calculates rates of every player from a Win Loss GSL::Matrix
103 # The model of the win possibility is 1/(1 + 10^(-d/400)).
104 # The equation in this class is 1/(1 + e^(-Kd)).
105 # So, K should be calculated like this.
106 K = Math.log(10.0) / 400.0
108 # Convergence limit to stop Newton method.
110 # Stop Newton method after this iterations.
113 # Average rate among the players
122 # Calcurates the average of the vector.
124 def Rating.average(vector, mean=0.0)
125 sum = Array(vector).inject(0.0) {|sum, n| sum + n}
126 vector -= GSL::Vector[*Array.new(vector.size, sum/vector.size - mean)]
133 def initialize(win_loss_matrix)
137 when GSL::Matrix, GSL::Matrix::Int
146 attr_reader :rate, :n
150 (0...@size).collect {|k| yield k}
155 (0...@size).each {|k| yield k}
159 # The possibility that the player k will beet the player i.
162 1.0/(1.0 + exp(@rate[i]-@rate[k]))
166 # Most possible equation
173 sum += @n[k,i] * win_rate(i,k) - @n[i,k] * win_rate(k,i)
180 # / f0/R0 f0/R1 f0/R2 ... \
181 # dfk/dRj = | f1/R0 f1/R1 f1/R2 ... |
182 # \ f2/R0 f2/R1 f2/R2 ... /
188 sum += win_rate(i,k) * win_rate(k,i) * (@n[k,i] + @n[i,k])
192 sum = 2.0 * win_rate(j,k) * win_rate(k,j) * (@n[k,j] + @n[j,k])
198 # Jacobi matrix of the func().
204 (0...@size).collect do |k|
205 (0...@size).collect do |j|
213 # The initial value of the rate, which is of very importance for Newton
214 # method. This is based on my huristics; the higher the win probablity of
215 # a player is, the greater points he takes.
220 v = GSL::Vector[0, 0]
223 v += GSL::Vector[@n[k,i], @n[i,k]]
225 v.nrm2 < 1 ? 0 : v[0] / (v[0] + v[1])
227 rank = possibility.sort_index
228 @rate = player_vector do |k|
229 K*500 * (rank[k]+1) / @size
235 # Resets @rate as the higher the current win probablity of a player is,
236 # the greater points he takes.
239 @rate = @record.get || @rate
240 rank = @rate.sort_index
241 @rate = player_vector do |k|
242 K*@count*1.5 * (rank[k]+1) / @size
247 # mu is the deaccelrating parameter in Deaccelerated Newton method
248 def deaccelrate(mu, old_rate, a, old_f_nrm2)
249 @rate = old_rate - a * mu
250 if func_vector.nrm2 < (1 - mu / 4.0 ) * old_f_nrm2 then
254 @record.set(func_vector.nrm2, @rate)
258 $stderr.puts "mu: %f " % [mu] if $DEBUG
259 deaccelrate(mu*0.5, old_rate, a, old_f_nrm2)
263 # Main process to calculate ratings.
266 # Counter to stop the process.
267 # Calulation in Newton method may fall in an infinite loop
272 # Solve the equation:
274 # @rate_(n+1) = @rate_(n) - a
276 # f.nrm2 should approach to zero.
280 # $stderr.puts "j: %s" % [j.inspect] if $DEBUG
281 $stderr.puts "f: %s -> %f" % [f.to_a.inspect, f.nrm2] if $DEBUG
283 # GSL::Linalg::LU.solve or GSL::Linalg::HH.solve would be available instead.
284 a = GSL::Linalg::SV.solve(j, f)
285 a = self.class.average(a)
286 # $stderr.puts "a: %s -> %f" % [a.to_a.inspect, a.nrm2] if $DEBUG
288 # Deaccelerated Newton method
289 # GSL::Vector object should be immutable.
292 old_f_nrm2 = old_f.nrm2
293 deaccelrate(1.0, old_rate, a, old_f_nrm2)
294 @record.set(func_vector.nrm2, @rate)
296 $stderr.printf "|error| : %5.2e\n", a.nrm2 if $DEBUG
299 if @count > COUNT_MAX
300 $stderr.puts "Values seem to oscillate. Stopped the process."
301 $stderr.puts "f: %s -> %f" % [func_vector.to_a.inspect, func_vector.nrm2]
305 end while (a.nrm2 > ERROR_LIMIT * @rate.nrm2)
308 $stderr.puts "resolved f: %s -> %f" %
309 [func_vector.to_a.inspect, func_vector.nrm2] if $DEBUG
317 # Make the values of @rate finite.
320 @rate = @rate.collect do |a|
322 a.infinite? * AVERAGE_RATE * 100
330 # Flatten the values of @rate.
332 def average!(mean=0.0)
333 @rate = self.class.average(@rate, mean)
337 # Make the values of @rate integer.
340 @rate = @rate.collect do |a|
346 a.infinite? * AVERAGE_RATE * 100
352 #################################################
353 # Encapsulate a pair of keys and win loss matrix.
354 # - keys is an array of player IDs; [gps+123, foo+234, ...]
355 # - matrix holds games # where player i (row index) beats player j (column index).
356 # The row and column indexes match with the keys.
358 # This object should be immutable. If an internal state is being modified, a
359 # new object is always returned.
367 def self.mk_matrix(players)
368 keys = players.keys.sort
372 ((0...size).collect do |k|
374 p1_hash = players[p1]
375 ((0...size).collect do |j|
380 v = p1_hash[p2] || Vector[0,0]
385 return WinLossMatrix.new(keys, matrix)
388 def self.mk_win_loss_matrix(players)
389 obj = mk_matrix(players)
397 # an array of player IDs; [gps+123, foo+234, ...]
400 # matrix holds games # where player i (row index) beats player j (column index).
401 # The row and column indexes match with the keys.
404 def initialize(keys, matrix)
410 # Returns the size of the keys/matrix
421 # Removes a delete_index'th player and returns a new object.
423 def delete_row(delete_index)
425 (0...size).each do |i|
426 next if i == delete_index
427 row = @matrix.row(i).clone
428 row.delete_at(delete_index)
431 if copied_cols.size == 0
432 new_matrix = GSL::Matrix.new
434 new_matrix = GSL::Matrix[*copied_cols]
436 new_keys = @keys.clone
437 new_keys.delete_at(delete_index)
438 return WinLossMatrix.new(new_keys, new_matrix)
442 # Removes players in a rows; [1,3,5]
444 def delete_rows(rows)
446 rows.sort.reverse.each do |index|
447 obj = obj.delete_row(index)
453 # Removes players who do not pass a criteria to be rated, and returns a
457 $stderr.puts @keys.inspect if $DEBUG
458 $stderr.puts @matrix.inspect if $DEBUG
460 (0...size).each do |i|
465 if win < 1 || loss < 1 || win + loss < $GAMES_LIMIT
470 # The recursion ends if there is nothing to delete
471 return self if delete.empty?
473 new_obj = delete_rows(delete)
478 # Cuts self into connecting groups such as each player in a group has at least
479 # one game with other players in the group. Returns them as an array.
481 def connected_subsets
482 g = RGL::AdjacencyGraph.new
483 (0...size).each do |k|
484 (0...size).each do |i|
493 g.each_connected_component do |c|
496 new_keys << keys[v.to_s.to_i]
501 subsets = subsets.sort {|a,b| b.size <=> a.size}
503 result = subsets.collect do |keys|
506 ((0...keys.size).collect do |k|
507 p1 = @keys.index(keys[k])
508 ((0...keys.size).collect do |j|
512 p2 = @keys.index(keys[j])
517 WinLossMatrix.new(keys, matrix)
524 "size : #{@keys.size}" + "\n" +
525 @keys.inspect + "\n" +
532 #################################################
537 # After NHAFE_LIFE days value will get half.
538 # 0.693 is constant, where exp(0.693) ~ 0.5
544 Math::exp(-0.693/NHALF_LIFE*(days-7))
548 def _add_win_loss(winner, loser, time)
549 how_long_days = (Time.now - time)/(3600*24)
550 $players[winner] ||= Hash.new { GSL::Vector[0,0] }
551 $players[loser] ||= Hash.new { GSL::Vector[0,0] }
552 $players[winner][loser] += GSL::Vector[1.0*half_life(how_long_days),0]
553 $players[loser][winner] += GSL::Vector[0,1.0*half_life(how_long_days)]
556 def _add_time(player, time)
557 $players_time[player] = time if $players_time[player] < time
560 def add(black_mark, black_name, white_name, white_mark, time)
561 if black_mark == WIN_MARK && white_mark == LOSS_MARK
562 _add_win_loss(black_name, white_name, time)
563 elsif black_mark == LOSS_MARK && white_mark == WIN_MARK
564 _add_win_loss(white_name, black_name, time)
565 elsif black_mark == DRAW_MARK && white_mark == DRAW_MARK
568 raise "Never reached!"
570 _add_time(black_name, time)
571 _add_time(white_name, time)
575 if /@NORATE\+/ =~ id # the player having @NORATE in the name should not be rated
578 id.gsub(/@.*?\+/,"+")
582 str = File.open(file).read
584 if /^N\+(.*)$/ =~ str then black_name = $1.strip end
585 if /^N\-(.*)$/ =~ str then white_name = $1.strip end
587 if /^'summary:(.*)$/ =~ str
588 state, p1, p2 = $1.split(":").map {|a| a.strip}
589 return if state == "abnormal"
590 p1_name, p1_mark = p1.split(" ")
591 p2_name, p2_mark = p2.split(" ")
592 if p1_name == black_name
593 black_name, black_mark = p1_name, p1_mark
594 white_name, white_mark = p2_name, p2_mark
595 elsif p2_name == black_name
596 black_name, black_mark = p2_name, p2_mark
597 white_name, white_mark = p1_name, p1_mark
599 raise "Never reach!: #{black} #{white} #{p3} #{p2}"
602 if /^'\$END_TIME:(.*)$/ =~ str
603 time = Time.parse($1.strip)
605 if /^'rating:(.*)$/ =~ str
606 black_id, white_id = $1.split(":").map {|a| a.strip}
607 black_id = identify_id(black_id)
608 white_id = identify_id(white_id)
609 if black_id && white_id && (black_id != white_id)
610 add(black_mark, black_id, white_id, white_mark, time)
617 USAGE: #{$0} dir [...]
624 while dir = ARGV.shift do
625 Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(f)}
632 obj = WinLossMatrix::mk_win_loss_matrix($players)
633 obj.connected_subsets.each do |win_loss_matrix|
634 yaml["players"][rating_group] = {}
636 rating = Rating.new(win_loss_matrix.matrix)
638 rating.average!(Rating::AVERAGE_RATE)
641 win_loss_matrix.keys.each_with_index do |p, i| # player_id, index#
642 win = win_loss_matrix.matrix.row(i).sum
643 loss = win_loss_matrix.matrix.col(i).sum
645 yaml["players"][rating_group][p] =
646 { 'name' => p.split("+")[0],
647 'rating_group' => rating_group,
648 'rate' => rating.rate[i],
649 'last_modified' => $players_time[p].dup,
657 non_rated_group = 999 # large enough
658 yaml["players"][non_rated_group] = {}
659 $players.each_key do |id|
660 # skip players who have already been rated
662 (0..rating_group).each do |i|
663 found = true if yaml["players"][i][id]
668 v = GSL::Vector[0, 0]
669 $players[id].each_value {|value| v += value}
670 next if v[0] < 1 && v[1] < 1
672 yaml["players"][non_rated_group][id] =
673 { 'name' => id.split("+")[0],
674 'rating_group' => non_rated_group,
676 'last_modified' => $players_time[id].dup,
687 # vim: ts=2 sw=2 sts=0