OSDN Git Service

enabled ExcludeSacrifice by default
[shogi-server/shogi-server.git] / mk_rate
diff --git a/mk_rate b/mk_rate
index 4200997..f8046f0 100755 (executable)
--- a/mk_rate
+++ b/mk_rate
@@ -1,7 +1,7 @@
 #!/usr/bin/ruby
 ## $Id$
 
-## Copyright (C) 2006 Daigo Moriwaki <daigo at debian dot org>
+## Copyright (C) 2006-2008 Daigo Moriwaki <daigo at debian dot org>
 ##
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
 #
 # Sample:
 #   $ ./mk_rate . > players.yaml
+#   $ ./mk_rate . && ./mk_rate . > players.yaml
 #
 # The conditions that games and players are rated as following:
 #   * Rated games, which were played by both rated players.
 #   * Rated players, who logged in the server with a name followed by a trip:
 #     "name,trip".
-#   * (Rated) players, who played more than $GAMES_LIMIT [ten] (rated) games. 
+#   * (Rated) players, who played more than $GAMES_LIMIT [15] (rated) games. 
 #
 #
 # PREREQUIRE
@@ -60,7 +61,7 @@ require 'rgl/connected_components'
 #
 
 # Count out players who play less games than $GAMES_LIMIT
-$GAMES_LIMIT = $DEBUG ? 0 : 10
+$GAMES_LIMIT = $DEBUG ? 0 : 15
 WIN_MARK  = "win"
 LOSS_MARK = "lose"
 DRAW_MARK = "draw"
@@ -210,9 +211,9 @@ class Rating
   end
 
   ##
-  # The initial value of the rate, which is of very importance for Newton method.
-  # This is based on my huristics; the higher the win probablity of a player is, 
-  # the greater points he takes.
+  # The initial value of the rate, which is of very importance for Newton
+  # method.  This is based on my huristics; the higher the win probablity of
+  # a player is, the greater points he takes.
   #
   def initial_rate
     possibility = 
@@ -368,7 +369,7 @@ class WinLossMatrix
     keys = players.keys.sort
     size = keys.size
     matrix =
-      Matrix::Int[*
+      GSL::Matrix[*
       ((0...size).collect do |k|
         p1 = keys[k]
         p1_hash = players[p1]
@@ -424,11 +425,15 @@ class WinLossMatrix
     copied_cols = []
     (0...size).each do |i|
       next if i == delete_index
-      row = @matrix.get_row(i)  # get_row returns a copy of the row
+      row = @matrix.row(i).clone
       row.delete_at(delete_index)
       copied_cols << row
     end
-    new_matrix = Matrix::Int[*copied_cols]
+    if copied_cols.size == 0
+      new_matrix = GSL::Matrix.new
+    else
+      new_matrix = GSL::Matrix[*copied_cols]
+    end
     new_keys = @keys.clone
     new_keys.delete_at(delete_index)
     return WinLossMatrix.new(new_keys, new_matrix)
@@ -446,7 +451,8 @@ class WinLossMatrix
   end
 
   ##
-  # Removes players who do not pass a criteria to be rated, and returns a new object.
+  # Removes players who do not pass a criteria to be rated, and returns a
+  # new object.
   # 
   def filter
     $stderr.puts @keys.inspect if $DEBUG
@@ -497,7 +503,7 @@ class WinLossMatrix
 
     result = subsets.collect do |keys|
       matrix =
-        Matrix::Int[*
+        GSL::Matrix[*
         ((0...keys.size).collect do |k|
           p1 = @keys.index(keys[k])
           ((0...keys.size).collect do |j|
@@ -505,7 +511,7 @@ class WinLossMatrix
               0
             else
               p2 = @keys.index(keys[j])
-              @matrix[p1][p2]
+              @matrix[p1,p2]
             end
           end)
         end)]
@@ -528,11 +534,24 @@ end
 # Main methods
 #
 
-def _add_win_loss(winner, loser)
+# Half-life effect
+# After NHAFE_LIFE days value will get half.
+# 0.693 is constant, where exp(0.693) ~ 0.5
+NHALF_LIFE=60
+def half_life(days)
+  if days < 7
+    return 1.0
+  else
+    Math::exp(-0.693/NHALF_LIFE*(days-7))
+  end
+end
+
+def _add_win_loss(winner, loser, time)
+  how_long_days = (Time.now - time)/(3600*24)
   $players[winner] ||= Hash.new { GSL::Vector[0,0] }
   $players[loser]  ||= Hash.new { GSL::Vector[0,0] }
-  $players[winner][loser] += GSL::Vector[1,0]
-  $players[loser][winner] += GSL::Vector[0,1]
+  $players[winner][loser] += GSL::Vector[1.0*half_life(how_long_days),0]
+  $players[loser][winner] += GSL::Vector[0,1.0*half_life(how_long_days)]
 end
 
 def _add_time(player, time)
@@ -541,9 +560,9 @@ end
 
 def add(black_mark, black_name, white_name, white_mark, time)
   if black_mark == WIN_MARK && white_mark == LOSS_MARK
-    _add_win_loss(black_name, white_name)
+    _add_win_loss(black_name, white_name, time)
   elsif black_mark == LOSS_MARK && white_mark == WIN_MARK
-    _add_win_loss(white_name, black_name)
+    _add_win_loss(white_name, black_name, time)
   elsif black_mark == DRAW_MARK && white_mark == DRAW_MARK
     return
   else
@@ -567,7 +586,8 @@ def grep(file)
   if /^N\-(.*)$/ =~ str then white_name = $1.strip end
 
   if /^'summary:(.*)$/ =~ str
-    dummy, p1, p2 = $1.split(":").map {|a| a.strip}    
+    state, p1, p2 = $1.split(":").map {|a| a.strip}    
+    return if state == "abnormal"
     p1_name, p1_mark = p1.split(" ")
     p2_name, p2_mark = p2.split(" ")
     if p1_name == black_name
@@ -600,37 +620,80 @@ USAGE: #{$0} dir [...]
   exit 1
 end
 
+def validate(yaml)
+  yaml["players"].each do |group_key, group|
+    group.each do |player_key, player|
+      rate = player['rate']
+      next unless rate
+      if rate > 10000 || rate < -10000
+        return false
+      end
+    end
+  end
+  return true
+end
+
 def main
   usage if ARGV.empty?
   while dir = ARGV.shift do
     Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(f)}
   end
 
-  obj = WinLossMatrix::mk_win_loss_matrix($players)
   yaml = {} 
   yaml["players"] = {}
   rating_group = 0
-  obj.connected_subsets.each do |win_loss_matrix|
-    yaml["players"][rating_group] = {}
-
-    rating = Rating.new(win_loss_matrix.matrix)
-    rating.rating
-    rating.average!(Rating::AVERAGE_RATE)
-    rating.integer!
-
-    win_loss_matrix.keys.each_with_index do |p, i| # player_id, index#
-      win  = win_loss_matrix.matrix.row(i).sum
-      loss = win_loss_matrix.matrix.col(i).sum
-
-      yaml["players"][rating_group][p] = 
-        { 'name' => p.split("+")[0],
-          'rating_group' => rating_group,
-          'rate' => rating.rate[i],
-          'last_modified' => $players_time[p].dup,
-          'win'  => win,
-          'loss' => loss}
+  if $players.size > 0
+    obj = WinLossMatrix::mk_win_loss_matrix($players)
+    obj.connected_subsets.each do |win_loss_matrix|
+      yaml["players"][rating_group] = {}
+
+      rating = Rating.new(win_loss_matrix.matrix)
+      rating.rating
+      rating.average!(Rating::AVERAGE_RATE)
+      rating.integer!
+
+      win_loss_matrix.keys.each_with_index do |p, i| # player_id, index#
+        win  = win_loss_matrix.matrix.row(i).sum
+        loss = win_loss_matrix.matrix.col(i).sum
+
+        yaml["players"][rating_group][p] = 
+          { 'name' => p.split("+")[0],
+            'rating_group' => rating_group,
+            'rate' => rating.rate[i],
+            'last_modified' => $players_time[p].dup,
+            'win'  => win,
+            'loss' => loss}
+      end
+      rating_group += 1
+    end
+  end
+  rating_group -= 1
+  non_rated_group = 999 # large enough
+  yaml["players"][non_rated_group] = {}
+  $players.each_key do |id|
+    # skip players who have already been rated
+    found = false
+    (0..rating_group).each do |i|
+       found = true if yaml["players"][i][id]
+       break if found
     end
-    rating_group += 1
+    next if found
+
+    v = GSL::Vector[0, 0]
+    $players[id].each_value {|value| v += value}
+    next if v[0] < 1 && v[1] < 1
+
+    yaml["players"][non_rated_group][id] =
+      { 'name' => id.split("+")[0],
+        'rating_group' => non_rated_group,
+        'rate' => 0,
+        'last_modified' => $players_time[id].dup,
+        'win'  => v[0],
+        'loss' => v[1]}
+  end
+  unless validate(yaml)
+    $stderr.puts "Aborted. It did not result in valid ratings."
+    exit 10
   end
   puts yaml.to_yaml
 end