first-search-eval.cc
Go to the documentation of this file.
00001 #include "osl/container/moveVector.h"
00002 #include "osl/misc/math.h"
00003 #include "osl/record/csaRecord.h"
00004 #include "osl/record/checkDuplicate.h"
00005 #include "osl/record/record.h"
00006 #include "osl/record/searchInfo.h"
00007 #include <boost/algorithm/string/trim.hpp>
00008 #include <boost/foreach.hpp>
00009 #include <boost/program_options.hpp>
00010 #include <fstream>
00011 #include <iostream>
00012 #include <string>
00013 #include <vector>
00014 
00015 typedef std::vector<int> value_t;
00016 typedef std::pair<value_t, value_t> pair_t;
00017 typedef std::map<std::string, pair_t> map_t;
00018 
00019 void logValue(const std::string& player,
00020               const std::string& turn,
00021               const int value,
00022               const std::string& csa_file)
00023 {
00024   const std::string log_file = player + "_" + turn + ".log";
00025   std::ofstream out(log_file.c_str(), std::ios::app);
00026   out << value << "\t" << csa_file << "\n";
00027 }
00028 
00033 void readFile(const std::string& csa_file, map_t& map)
00034 {
00035   const osl::record::csa::CsaFile csa(csa_file);
00036   const osl::record::Record& record = csa.getRecord();
00037 
00038   // black
00039   for (size_t i=0; i < record.moveRecordSize(); i += 2) {
00040     const osl::record::MoveRecord *mr = record.moveOf(i);
00041     if (mr->info.isValid()) {
00042       const std::string player = record.getPlayer(osl::BLACK);
00043       const int eval = mr->info.value;
00044       pair_t& pair    = map[player];
00045       value_t& values = pair.first;
00046       values.push_back(eval);
00047 
00048       logValue(player, "black", eval, csa_file);
00049       break;
00050     }
00051   }
00052   // white
00053   for (size_t i=1; i < record.moveRecordSize(); i += 2) {
00054     const osl::record::MoveRecord *mr = record.moveOf(i);
00055     if (mr->info.isValid()) {
00056       const std::string player = record.getPlayer(osl::WHITE);
00057       const int eval = mr->info.value;
00058       pair_t& pair    = map[player];
00059       value_t& values = pair.second;
00060       values.push_back(eval);
00061 
00062       logValue(player, "white", eval, csa_file);
00063       break;
00064     }
00065   }
00066 }
00067 
00068 void showResult(const map_t& map)
00069 {
00070   BOOST_FOREACH(const map_t::value_type& vt, map) {
00071     std::cout << "===== " << vt.first << " =====\n";
00072     {
00073       int sum, mean, var, std_dev, skew, kurt; 
00074       osl::misc::computeStats(vt.second.first.begin(), vt.second.first.end(),
00075                               sum, mean, var, std_dev, skew,  kurt);
00076       std::cout << "[BLACK] mean: " << mean << ", std_dev: " << std_dev << std::endl;
00077     }
00078     {
00079       int sum, mean, var, std_dev, skew, kurt; 
00080       osl::misc::computeStats(vt.second.second.begin(), vt.second.second.end(),
00081                               sum, mean, var, std_dev, skew,  kurt);
00082       std::cout << "[WHITE] mean: " << mean << ", std_dev: " << std_dev << std::endl;
00083     }
00084   }
00085 }
00086 
00087 
00088 int main(int argc, char **argv)
00089 {
00090   namespace bp = boost::program_options;
00091 
00092   bp::options_description command_line_options;
00093   command_line_options.add_options()
00094     ("input-file", bp::value<std::vector<std::string> >(),
00095      "input files in the CSA format")
00096     ("help", "Show help message");
00097   bp::variables_map vm;
00098   bp::positional_options_description p;
00099   p.add("input-file", -1);
00100 
00101   try {
00102     bp::store(
00103       bp::command_line_parser(argc, argv).options(command_line_options).positional(p).run(), vm);
00104     bp::notify(vm);
00105     if (vm.count("help")) {
00106       std::cerr << "Calculate evaluation values for the initial search moves after finishing opening moves.\n";
00107       std::cerr << "Usage: " << argv[0] << " [options] csa-file [...]\n";
00108       std::cerr << "       " << argv[0] << " [options]\n";
00109       std::cout << command_line_options << std::endl;
00110       return 0;
00111     }
00112   } catch (std::exception &e) {
00113     std::cerr << "error in parsing options" << std::endl
00114               << e.what() << std::endl;
00115     std::cerr << "Calculate evaluation values for the initial search moves after finishing opening moves.\n";
00116     std::cerr << "Usage: " << argv[0] << " [options] csa-file [...]\n";
00117     std::cerr << "       " << argv[0] << " [options]\n";
00118     std::cerr << command_line_options << std::endl;
00119     return 1;
00120   }
00121 
00122   std::vector<std::string> files;
00123   if (vm.count("input-file")) {
00124     const std::vector<std::string> temp = vm["input-file"].as<std::vector<std::string> >();
00125     files.insert(files.end(), temp.begin(), temp.end());
00126   } else {
00127     std::string line;
00128     while(std::getline(std::cin , line)) {
00129       boost::algorithm::trim(line);
00130       files.push_back(line);
00131     }
00132   }
00133 
00134   map_t map;
00135   BOOST_FOREACH(const std::string& file, files) {
00136     readFile(file, map);
00137   }
00138 
00139   showResult(map);
00140 
00141   return 0;
00142 }
00143 
00144 /* vim: set ts=2 sw=2 ft=cpp : */
00145 // ;;; Local Variables:
00146 // ;;; mode:c++
00147 // ;;; c-basic-offset:2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines