OSDN Git Service

Forgot to add a bug number
[shogi-server/shogi-server.git] / mk_rate
diff --git a/mk_rate b/mk_rate
index 9982ce8..d072ae1 100755 (executable)
--- a/mk_rate
+++ b/mk_rate
@@ -1,7 +1,7 @@
 #!/usr/bin/ruby
 ## $Id$
 
 #!/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
 ##
 ## 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
@@ -23,6 +23,7 @@
 #
 # Sample:
 #   $ ./mk_rate . > players.yaml
 #
 # 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.
 #
 # The conditions that games and players are rated as following:
 #   * Rated games, which were played by both rated players.
@@ -50,6 +51,7 @@
 
 require 'yaml'
 require 'time'
 
 require 'yaml'
 require 'time'
+require 'getoptlong'
 require 'gsl'
 require 'rubygems'
 require 'rgl/adjacency'
 require 'gsl'
 require 'rubygems'
 require 'rgl/adjacency'
@@ -281,7 +283,8 @@ class Rating
       $stderr.puts "f: %s -> %f" % [f.to_a.inspect, f.nrm2] if $DEBUG
 
       # GSL::Linalg::LU.solve or GSL::Linalg::HH.solve would be available instead.
       $stderr.puts "f: %s -> %f" % [f.to_a.inspect, f.nrm2] if $DEBUG
 
       # GSL::Linalg::LU.solve or GSL::Linalg::HH.solve would be available instead.
-      a = GSL::Linalg::SV.solve(j, f)
+      #a = GSL::Linalg::HH.solve(j, f)
+      a, = GSL::MultiFit::linear(j, f)
       a = self.class.average(a)
       # $stderr.puts "a: %s -> %f" % [a.to_a.inspect, a.nrm2] if $DEBUG
       
       a = self.class.average(a)
       # $stderr.puts "a: %s -> %f" % [a.to_a.inspect, a.nrm2] if $DEBUG
       
@@ -334,6 +337,13 @@ class Rating
   end
 
   ##
   end
 
   ##
+  # Translate by value
+  #
+  def translate!(value)
+    @rate += value
+  end
+
+  ##
   # Make the values of @rate integer.
   #
   def integer!
   # Make the values of @rate integer.
   #
   def integer!
@@ -536,12 +546,11 @@ end
 # Half-life effect
 # After NHAFE_LIFE days value will get half.
 # 0.693 is constant, where exp(0.693) ~ 0.5
 # 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)
 def half_life(days)
-  if days < 7
+  if days < $options["half-life-ignore"]
     return 1.0
   else
     return 1.0
   else
-    Math::exp(-0.693/NHALF_LIFE*(days-7))
+    Math::exp(-0.693/$options["half-life"]*(days-$options["half-life-ignore"]))
   end
 end
 
   end
 end
 
@@ -619,8 +628,67 @@ USAGE: #{$0} dir [...]
   exit 1
 end
 
   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 usage(io)
+    io.puts <<EOF
+USAGE: #{$0} [options] DIR..
+  DIR                where CSA files are looked up recursively
+OPTOINS:
+  --half-life         n [days] (default 60)
+  --half-life-ignore  m [days] (default  7)
+                      after m days, half-life effect works
+  --fixed-rate-player player whose rate is fixed at the rate
+  --fixed-rate        rate 
+  --help              show this message
+EOF
+end
+
 def main
 def main
-  usage if ARGV.empty?
+  $options = Hash::new
+  parser = GetoptLong.new(
+    ["--half-life",         GetoptLong::REQUIRED_ARGUMENT],
+    ["--half-life-ignore",  GetoptLong::REQUIRED_ARGUMENT],
+    ["--help", "-h",        GetoptLong::NO_ARGUMENT],
+    ["--fixed-rate-player", GetoptLong::REQUIRED_ARGUMENT],
+    ["--fixed-rate",        GetoptLong::REQUIRED_ARGUMENT])
+  parser.quiet = true
+  begin
+    parser.each_option do |name, arg|
+      name.sub!(/^--/, '')
+      $options[name] = arg.dup
+    end
+    if ( $options["fixed-rate-player"] && !$options["fixed-rate"]) ||
+       (!$options["fixed-rate-player"] &&  $options["fixed-rate"]) ||
+       ( $options["fixed-rate-player"] &&  $options["fixed-rate"].to_i <= 0) 
+      usage($stderr)
+      exit 1
+    end
+  rescue
+    usage($stderr)
+    raise parser.error_message
+  end
+  if $options["help"]
+    usage($stdout) 
+    exit 0
+  end
+  $options["half-life"] ||= 60
+  $options["half-life"] = $options["half-life"].to_i
+  $options["half-life-ignore"] ||= 7
+  $options["half-life-ignore"] = $options["half-life-ignore"].to_i
+  $options["fixed-rate"] = $options["fixed-rate"].to_i if $options["fixed-rate"]
+
   while dir = ARGV.shift do
     Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(f)}
   end
   while dir = ARGV.shift do
     Dir.glob( File.join(dir, "**", "*.csa") ) {|f| grep(f)}
   end
@@ -638,6 +706,23 @@ def main
       rating.average!(Rating::AVERAGE_RATE)
       rating.integer!
 
       rating.average!(Rating::AVERAGE_RATE)
       rating.integer!
 
+      if $options["fixed-rate-player"]
+        # first, try exact match
+        index = win_loss_matrix.keys.index($options["fixed-rate-player"])
+        # second, try regular match
+        unless index
+          win_loss_matrix.keys.each_with_index do |p, i|
+            if %r!#{$options["fixed-rate-player"]}! =~ p
+              index = i
+            end
+          end
+        end
+        if index
+          the_rate = rating.rate[index]
+          rating.translate!($options["fixed-rate"] - the_rate)
+        end
+      end
+
       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
       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
@@ -677,6 +762,11 @@ def main
         'win'  => v[0],
         'loss' => v[1]}
   end
         'win'  => v[0],
         'loss' => v[1]}
   end
+  unless validate(yaml)
+    $stderr.puts "Aborted. It did not result in valid ratings."
+    $stderr.puts yaml.to_yaml if $DEBUG
+    exit 10
+  end
   puts yaml.to_yaml
 end
 
   puts yaml.to_yaml
 end