All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
topn.cc
Go to the documentation of this file.
1 /* probability.cc
2  */
4 #include "osl/rating/ratingEnv.h"
7 #include "osl/stl/vector.h"
8 #include "osl/record/csa.h"
9 #include "osl/record/csaRecord.h"
10 #include "osl/record/kisen.h"
11 #include "osl/progress/effect5x3.h"
12 #include "osl/stat/average.h"
13 #include "osl/stat/histogram.h"
14 #include <boost/program_options.hpp>
15 #include <iostream>
16 #include <cmath>
17 using namespace osl;
18 using namespace osl::rating;
19 namespace po = boost::program_options;
20 
22 
23 void run(NumEffectState& state, const vector<Move>& moves);
24 void show_statistics();
25 
26 int main(int argc, char **argv)
27 {
28  std::string kisen_filename;
29  std::vector<std::string> filenames;
30  po::options_description options("Options");
31  options.add_options()
32  ("csa-file",
33  po::value<std::vector<std::string> >(),
34  "csa filename")
35  ("kisen,k",
36  po::value<std::string>(&kisen_filename),
37  "kisen filename")
38  ("num-kisen",
39  po::value<size_t>(&num_kisen)->default_value(0),
40  "number of records in kisen to be processed")
41  ("target-limit",
42  po::value<size_t>(&target_limit)->default_value(1000),
43  "ignore moves whose log-probability is greater than this threshold")
44  ("opening-skip",
45  po::value<size_t>(&opening_skip)->default_value(20),
46  "number of opening moves ignored in analysis");
47  po::variables_map vm;
48  po::positional_options_description p;
49  p.add("csa-file", -1);
50  try
51  {
52  po::store(po::command_line_parser(argc, argv).
53  options(options).positional(p).run(), vm);
54  notify(vm);
55  if (vm.count("help")) {
56  std::cout << options << std::endl;
57  return 0;
58  }
59  if (vm.count("csa-file"))
60  filenames = vm["csa-file"].as<std::vector<std::string> >();
61  }
62  catch (std::exception& e)
63  {
64  std::cerr << "error in parsing options" << std::endl
65  << e.what() << std::endl;
66  std::cerr << options << std::endl;
67  throw;
68  }
69  if (kisen_filename != "") {
70  std::cerr << "kisen " << kisen_filename << "\n";
71  KisenFile kisen_file(kisen_filename.c_str());
72  if (num_kisen == 0)
73  num_kisen = kisen_file.size();
74  for (size_t i=0; i<num_kisen; i++) {
75  if (i % 16 == 0)
76  std::cerr << '.';
77  NumEffectState state(kisen_file.getInitialState());
78  const osl::vector<Move> moves = kisen_file.getMoves(i);
79  run(state, moves);
80  }
81  }
82  for (size_t i=0; i<filenames.size(); ++i) {
83  if (i % 16 == 0)
84  std::cerr << '.';
85  CsaFile file(filenames[i]);
86  NumEffectState state(file.getInitialState());
87  const osl::vector<Move> moves = file.getRecord().getMoves();
88  run(state, moves);
89  }
90  std::cerr << "\n";
92 }
93 
94 CArray<stat::Average,8> top_rated, active;
95 
96 void show(const NumEffectState& state, Move next)
97 {
98  static const StandardFeatureSet& feature_set = StandardFeatureSet::instance();
99  MoveLogProbVector moves;
100  RatingEnv env;
101  env.make(state);
102 
103  feature_set.generateLogProb(state, env, 2000, moves);
104  const MoveLogProb *rm = moves.find(next);
105  if (! rm)
106  return;
107  int index = rm - &*moves.begin();
108  for (size_t i=0; i<top_rated.size(); ++i) {
109  if (i >= moves.size())
110  break;
111  bool a = moves[i].logProb() <= (int)target_limit;
112  active[i].add(a);
113  if (! a)
114  continue;
115  bool found = index == (int)i;
116  top_rated[i].add(found);
117  }
118 }
119 
120 void run(NumEffectState& state, const vector<Move>& moves)
121 {
122  for (size_t i=0; i<moves.size(); ++i) {
123  if (state.inCheck(alt(state.turn())))
124  break; // illegal
125 
126  const Move move = moves[i];
127  if (i >= opening_skip)
128  show(state, move);
129  state.makeMove(move);
130  }
131 }
132 
133 void show_statistics()
134 {
135  for (size_t i=0; i<top_rated.size(); ++i)
136  std::cout << "top " << i
137  << " " << top_rated[i].getAverage()*100.0
138  << " " << active[i].getAverage()*100.0
139  << "\n";
140 }
141 
142 
143 // ;;; Local Variables:
144 // ;;; mode:c++
145 // ;;; c-basic-offset:2
146 // ;;; End: