2 # $Id: mk_rate 316 2008-12-28 15:10:10Z beatles $
4 # Author:: Daigo Moriwaki
5 # Homepage:: http://sourceforge.jp/projects/shogi-server/
8 # Copyright (C) 2006-2008 Daigo Moriwaki <daigo at debian dot org>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 # mk_rate reads CSA files, calculates rating scores of each player, and then
28 # outputs a yaml file (players.yaml) that Shogi-server can recognize.
32 # ./mk_rate [options] DIR..
35 # CSA files are recursively looked up the directories.
38 # n [days] (default 60)
40 # --half-life-ignore::
41 # m [days] (default 7)
42 # after m days, the half-life effect works
44 # --fixed-rate-player::
45 # player whose rate is fixed at the rate
55 # Sample Command lines that isntall prerequires will work on Debian.
59 # $ sudo aptitude install ruby1.8
63 # $ sudo aptitude install rubygems
65 # * Ruby bindings for the GNU Scientific Library (GSL[http://rb-gsl.rubyforge.org/])
67 # $ sudo aptitude install libgsl-ruby1.8
69 # * RGL: {Ruby Graph Library}[http://rubyforge.org/projects/rgl/]
71 # $ sudo gem install rgl
75 # $ ./mk_rate . > players.yaml
77 # or, if you do not want the file to be update in case of errors,
79 # $ ./mk_rate . && ./mk_rate . > players.yaml
81 # == How players are rated
83 # The conditions that games and players are rated as following:
85 # * Rated games, which were played by both rated players.
86 # * Rated players, who logged in the server with a name followed by a trip: "name,trip".
87 # * (Rated) players, who played more than $GAMES_LIMIT [15] (rated) games.
95 require 'rgl/adjacency'
96 require 'rgl/connected_components'
98 #################################################
102 # Count out players who play less games than $GAMES_LIMIT
103 $GAMES_LIMIT = $DEBUG ? 0 : 15
110 # Holds the last time when a player gamed
111 $players_time = Hash.new { Time.at(0) }
114 #################################################
115 # Keeps the value of the lowest key
123 if @lowest.empty? || key < @lowest[0]
124 @lowest = [key, value]
137 #################################################
138 # Calculates rates of every player from a Win Loss GSL::Matrix
143 # The model of the win possibility is 1/(1 + 10^(-d/400)).
144 # The equation in this class is 1/(1 + e^(-Kd)).
145 # So, K should be calculated like this.
146 K = Math.log(10.0) / 400.0
148 # Convergence limit to stop Newton method.
150 # Stop Newton method after this iterations.
153 # Average rate among the players
162 # Calcurates the average of the vector.
164 def Rating.average(vector, mean=0.0)
165 sum = Array(vector).inject(0.0) {|sum, n| sum + n}
166 vector -= GSL::Vector[*Array.new(vector.size, sum/vector.size - mean)]
173 def initialize(win_loss_matrix)
177 when GSL::Matrix, GSL::Matrix::Int
186 attr_reader :rate, :n
190 (0...@size).collect {|k| yield k}
195 (0...@size).each {|k| yield k}
199 # The possibility that the player k will beet the player i.
202 1.0/(1.0 + exp(@rate[i]-@rate[k]))
206 # Most possible equation
213 sum += @n[k,i] * win_rate(i,k) - @n[i,k] * win_rate(k,i)
220 # / f0/R0 f0/R1 f0/R2 ... \
221 # dfk/dRj = | f1/R0 f1/R1 f1/R2 ... |
222 # \ f2/R0 f2/R1 f2/R2 ... /
228 sum += win_rate(i,k) * win_rate(k,i) * (@n[k,i] + @n[i,k])
232 sum = 2.0 * win_rate(j,k) * win_rate(k,j) * (@n[k,j] + @n[j,k])
238 # Jacobi matrix of the func().
244 (0...@size).collect do |k|
245 (0...@size).collect do |j|
253 # The initial value of the rate, which is of very importance for Newton
254 # method. This is based on my huristics; the higher the win probablity of
255 # a player is, the greater points he takes.
260 v = GSL::Vector[0, 0]
263 v += GSL::Vector[@n[k,i], @n[i,k]]
265 v.nrm2 < 1 ? 0 : v[0] / (v[0] + v[1])
267 rank = possibility.sort_index
268 @rate = player_vector do |k|
269 K*500 * (rank[k]+1) / @size
275 # Resets @rate as the higher the current win probablity of a player is,
276 # the greater points he takes.
279 @rate = @record.get || @rate
280 rank = @rate.sort_index
281 @rate = player_vector do |k|
282 K*@count*1.5 * (rank[k]+1) / @size
287 # mu is the deaccelrating parameter in Deaccelerated Newton method
288 def deaccelrate(mu, old_rate, a, old_f_nrm2)
289 @rate = old_rate - a * mu
290 if func_vector.nrm2 < (1 - mu / 4.0 ) * old_f_nrm2 then
294 @record.set(func_vector.nrm2, @rate)
298 $stderr.puts "mu: %f " % [mu] if $DEBUG
299 deaccelrate(mu*0.5, old_rate, a, old_f_nrm2)
303 # Main process to calculate ratings.
306 # Counter to stop the process.
307 # Calulation in Newton method may fall in an infinite loop
312 # Solve the equation:
314 # @rate_(n+1) = @rate_(n) - a
316 # f.nrm2 should approach to zero.
320 # $stderr.puts "j: %s" % [j.inspect] if $DEBUG
321 $stderr.puts "f: %s -> %f" % [f.to_a.inspect, f.nrm2] if $DEBUG
323 # GSL::Linalg::LU.solve or GSL::Linalg::HH.solve would be available instead.
324 #a = GSL::Linalg::HH.solve(j, f)
325 a, = GSL::MultiFit::linear(j, f)
326 a = self.class.average(a)
327 # $stderr.puts "a: %s -> %f" % [a.to_a.inspect, a.nrm2] if $DEBUG
329 # Deaccelerated Newton method
330 # GSL::Vector object should be immutable.
333 old_f_nrm2 = old_f.nrm2
334 deaccelrate(1.0, old_rate, a, old_f_nrm2)
335 @record.set(func_vector.nrm2, @rate)
337 $stderr.printf "|error| : %5.2e\n", a.nrm2 if $DEBUG
340 if @count > COUNT_MAX
341 $stderr.puts "Values seem to oscillate. Stopped the process."
342 $stderr.puts "f: %s -> %f" % [func_vector.to_a.inspect, func_vector.nrm2]
346 end while (a.nrm2 > ERROR_LIMIT * @rate.nrm2)
349 $stderr.puts "resolved f: %s -> %f" %
350 [func_vector.to_a.inspect, func_vector.nrm2] if $DEBUG
358 # Make the values of @rate finite.
361 @rate = @rate.collect do |a|
363 a.infinite? * AVERAGE_RATE * 100
371 # Flatten the values of @rate.
373 def average!(mean=0.0)
374 @rate = self.class.average(@rate, mean)
380 def translate!(value)
385 # Make the values of @rate integer.
388 @rate = @rate.collect do |a|
394 a.infinite? * AVERAGE_RATE * 100
400 #################################################
401 # Encapsulate a pair of keys and win loss matrix.
402 # - keys is an array of player IDs; [gps+123, foo+234, ...]
403 # - matrix holds games # where player i (row index) beats player j (column index).
404 # The row and column indexes match with the keys.
406 # This object should be immutable. If an internal state is being modified, a
407 # new object is always returned.
415 def self.mk_matrix(players)
416 keys = players.keys.sort
420 ((0...size).collect do |k|
422 p1_hash = players[p1]
423 ((0...size).collect do |j|
428 v = p1_hash[p2] || Vector[0,0]
433 return WinLossMatrix.new(keys, matrix)
436 def self.mk_win_loss_matrix(players)
437 obj = mk_matrix(players)
445 # an array of player IDs; [gps+123, foo+234, ...]
448 # matrix holds games # where player i (row index) beats player j (column index).
449 # The row and column indexes match with the keys.
452 def initialize(keys, matrix)
458 # Returns the size of the keys/matrix
469 # Removes players in a rows such as [1,3,5], and then returns a new
472 def delete_rows(rows)
473 rows = rows.sort.reverse
476 (0...size).each do |i|
477 next if rows.include?(i)
478 row = @matrix.row(i).clone
484 if copied_cols.size == 0
485 new_matrix = GSL::Matrix.new
487 new_matrix = GSL::Matrix[*copied_cols]
490 new_keys = @keys.clone
492 new_keys.delete_at(j)
495 return WinLossMatrix.new(new_keys, new_matrix)
499 # Removes players who do not pass a criteria to be rated, and returns a
503 $stderr.puts @keys.inspect if $DEBUG
504 $stderr.puts @matrix.inspect if $DEBUG
506 (0...size).each do |i|
511 if win < 1 || loss < 1 || win + loss < $GAMES_LIMIT
516 # The recursion ends if there is nothing to delete
517 return self if delete.empty?
519 new_obj = delete_rows(delete)
524 # Cuts self into connecting groups such as each player in a group has at least
525 # one game with other players in the group. Returns them as an array.
527 def connected_subsets
528 g = RGL::AdjacencyGraph.new
529 (0...size).each do |k|
530 (0...size).each do |i|
539 g.each_connected_component do |c|
542 new_keys << keys[v.to_s.to_i]
547 subsets = subsets.sort {|a,b| b.size <=> a.size}
549 result = subsets.collect do |keys|
552 ((0...keys.size).collect do |k|
553 p1 = @keys.index(keys[k])
554 ((0...keys.size).collect do |j|
558 p2 = @keys.index(keys[j])
563 WinLossMatrix.new(keys, matrix)
570 "size : #{@keys.size}" + "\n" +
571 @keys.inspect + "\n" +
578 #################################################
583 # After NHAFE_LIFE days value will get half.
584 # 0.693 is constant, where exp(0.693) ~ 0.5
586 if days < $options["half-life-ignore"]
589 Math::exp(-0.693/$options["half-life"]*(days-$options["half-life-ignore"]))
593 def _add_win_loss(winner, loser, time)
594 how_long_days = (Time.now - time)/(3600*24)
595 $players[winner] ||= Hash.new { GSL::Vector[0,0] }
596 $players[loser] ||= Hash.new { GSL::Vector[0,0] }
597 $players[winner][loser] += GSL::Vector[1.0*half_life(how_long_days),0]
598 $players[loser][winner] += GSL::Vector[0,1.0*half_life(how_long_days)]
601 def _add_time(player, time)
602 $players_time[player] = time if $players_time[player] < time
605 def add(black_mark, black_name, white_name, white_mark, time)
606 if black_mark == WIN_MARK && white_mark == LOSS_MARK
607 _add_win_loss(black_name, white_name, time)
608 elsif black_mark == LOSS_MARK && white_mark == WIN_MARK
609 _add_win_loss(white_name, black_name, time)
610 elsif black_mark == DRAW_MARK && white_mark == DRAW_MARK
613 raise "Never reached!"
615 _add_time(black_name, time)
616 _add_time(white_name, time)
620 if /@NORATE\+/ =~ id # the player having @NORATE in the name should not be rated
623 id.gsub(/@.*?\+/,"+")
627 str = File.open(file).read
629 if /^N\+(.*)$/ =~ str then black_name = $1.strip end
630 if /^N\-(.*)$/ =~ str then white_name = $1.strip end
632 if /^'summary:(.*)$/ =~ str
633 state, p1, p2 = $1.split(":").map {|a| a.strip}
634 return if state == "abnormal"
635 p1_name, p1_mark = p1.split(" ")
636 p2_name, p2_mark = p2.split(" ")
637 if p1_name == black_name
638 black_name, black_mark = p1_name, p1_mark
639 white_name, white_mark = p2_name, p2_mark
640 elsif p2_name == black_name
641 black_name, black_mark = p2_name, p2_mark
642 white_name, white_mark = p1_name, p1_mark
644 raise "Never reach!: #{black} #{white} #{p3} #{p2}"
647 if /^'\$END_TIME:(.*)$/ =~ str
648 time = Time.parse($1.strip)
650 if /^'rating:(.*)$/ =~ str
651 black_id, white_id = $1.split(":").map {|a| a.strip}
652 black_id = identify_id(black_id)
653 white_id = identify_id(white_id)
654 if black_id && white_id && (black_id != white_id) &&
655 black_mark && white_mark
656 $stdout.printf("%s %s %s %s %d\n", black_mark, black_id, white_id, white_mark, time)
664 USAGE: #{$0} dir [...]
670 yaml["players"].each do |group_key, group|
671 group.each do |player_key, player|
672 rate = player['rate']
674 if rate > 10000 || rate < -10000
684 USAGE: #{$0} [options] DIR..
685 DIR where CSA files are looked up recursively
687 --half-life n [days] (default 60)
688 --half-life-ignore m [days] (default 7)
689 after m days, half-life effect works
690 --fixed-rate-player player whose rate is fixed at the rate
692 --help show this message
698 parser = GetoptLong.new(
699 ["--half-life", GetoptLong::REQUIRED_ARGUMENT],
700 ["--half-life-ignore", GetoptLong::REQUIRED_ARGUMENT],
701 ["--help", "-h", GetoptLong::NO_ARGUMENT],
702 ["--fixed-rate-player", GetoptLong::REQUIRED_ARGUMENT],
703 ["--fixed-rate", GetoptLong::REQUIRED_ARGUMENT])
706 parser.each_option do |name, arg|
708 $options[name] = arg.dup
710 if ( $options["fixed-rate-player"] && !$options["fixed-rate"]) ||
711 (!$options["fixed-rate-player"] && $options["fixed-rate"]) ||
712 ( $options["fixed-rate-player"] && $options["fixed-rate"].to_i <= 0)
718 raise parser.error_message
724 $options["half-life"] ||= 60
725 $options["half-life"] = $options["half-life"].to_i
726 $options["half-life-ignore"] ||= 7
727 $options["half-life-ignore"] = $options["half-life-ignore"].to_i
728 $options["fixed-rate"] = $options["fixed-rate"].to_i if $options["fixed-rate"]
731 while line = $stdin.gets do
732 next unless %r!.*\.csa$! =~ line
736 while dir = ARGV.shift do
737 Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(f)}
740 $stderr.puts "read done."
747 # vim: ts=2 sw=2 sts=0