All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
first-search-eval.cc
Go to the documentation of this file.
2 #include "osl/misc/math.h"
3 #include "osl/record/csaRecord.h"
5 #include "osl/record/record.h"
7 #include <boost/algorithm/string/trim.hpp>
8 #include <boost/foreach.hpp>
9 #include <boost/program_options.hpp>
10 #include <fstream>
11 #include <iostream>
12 #include <string>
13 #include <vector>
14 
15 typedef std::vector<int> value_t;
16 typedef std::pair<value_t, value_t> pair_t;
17 typedef std::map<std::string, pair_t> map_t;
18 
19 void logValue(const std::string& player,
20  const std::string& turn,
21  const int value,
22  const std::string& csa_file)
23 {
24  const std::string log_file = player + "_" + turn + ".log";
25  std::ofstream out(log_file.c_str(), std::ios::app);
26  out << value << "\t" << csa_file << "\n";
27 }
28 
33 void readFile(const std::string& csa_file, map_t& map)
34 {
35  const osl::record::csa::CsaFile csa(csa_file);
36  const osl::record::Record& record = csa.getRecord();
37 
38  // black
39  for (size_t i=0; i < record.moveRecordSize(); i += 2) {
40  const osl::record::MoveRecord *mr = record.moveOf(i);
41  if (mr->info.isValid()) {
42  const std::string player = record.getPlayer(osl::BLACK);
43  const int eval = mr->info.value;
44  pair_t& pair = map[player];
45  value_t& values = pair.first;
46  values.push_back(eval);
47 
48  logValue(player, "black", eval, csa_file);
49  break;
50  }
51  }
52  // white
53  for (size_t i=1; i < record.moveRecordSize(); i += 2) {
54  const osl::record::MoveRecord *mr = record.moveOf(i);
55  if (mr->info.isValid()) {
56  const std::string player = record.getPlayer(osl::WHITE);
57  const int eval = mr->info.value;
58  pair_t& pair = map[player];
59  value_t& values = pair.second;
60  values.push_back(eval);
61 
62  logValue(player, "white", eval, csa_file);
63  break;
64  }
65  }
66 }
67 
68 void showResult(const map_t& map)
69 {
70  BOOST_FOREACH(const map_t::value_type& vt, map) {
71  std::cout << "===== " << vt.first << " =====\n";
72  {
73  int sum, mean, var, std_dev, skew, kurt;
74  osl::misc::computeStats(vt.second.first.begin(), vt.second.first.end(),
75  sum, mean, var, std_dev, skew, kurt);
76  std::cout << "[BLACK] mean: " << mean << ", std_dev: " << std_dev << std::endl;
77  }
78  {
79  int sum, mean, var, std_dev, skew, kurt;
80  osl::misc::computeStats(vt.second.second.begin(), vt.second.second.end(),
81  sum, mean, var, std_dev, skew, kurt);
82  std::cout << "[WHITE] mean: " << mean << ", std_dev: " << std_dev << std::endl;
83  }
84  }
85 }
86 
87 
88 int main(int argc, char **argv)
89 {
90  namespace bp = boost::program_options;
91 
92  bp::options_description command_line_options;
93  command_line_options.add_options()
94  ("input-file", bp::value<std::vector<std::string> >(),
95  "input files in the CSA format")
96  ("help", "Show help message");
97  bp::variables_map vm;
98  bp::positional_options_description p;
99  p.add("input-file", -1);
100 
101  try {
102  bp::store(
103  bp::command_line_parser(argc, argv).options(command_line_options).positional(p).run(), vm);
104  bp::notify(vm);
105  if (vm.count("help")) {
106  std::cerr << "Calculate evaluation values for the initial search moves after finishing opening moves.\n";
107  std::cerr << "Usage: " << argv[0] << " [options] csa-file [...]\n";
108  std::cerr << " " << argv[0] << " [options]\n";
109  std::cout << command_line_options << std::endl;
110  return 0;
111  }
112  } catch (std::exception &e) {
113  std::cerr << "error in parsing options" << std::endl
114  << e.what() << std::endl;
115  std::cerr << "Calculate evaluation values for the initial search moves after finishing opening moves.\n";
116  std::cerr << "Usage: " << argv[0] << " [options] csa-file [...]\n";
117  std::cerr << " " << argv[0] << " [options]\n";
118  std::cerr << command_line_options << std::endl;
119  return 1;
120  }
121 
122  std::vector<std::string> files;
123  if (vm.count("input-file")) {
124  const std::vector<std::string> temp = vm["input-file"].as<std::vector<std::string> >();
125  files.insert(files.end(), temp.begin(), temp.end());
126  } else {
127  std::string line;
128  while(std::getline(std::cin , line)) {
129  boost::algorithm::trim(line);
130  files.push_back(line);
131  }
132  }
133 
134  map_t map;
135  BOOST_FOREACH(const std::string& file, files) {
136  readFile(file, map);
137  }
138 
139  showResult(map);
140 
141  return 0;
142 }
143 
144 /* vim: set ts=2 sw=2 ft=cpp : */
145 // ;;; Local Variables:
146 // ;;; mode:c++
147 // ;;; c-basic-offset:2