All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
probability.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/kisen.h"
10 #include "osl/progress/effect5x3.h"
11 #include "osl/stat/average.h"
12 #include "osl/stat/histogram.h"
13 #include <iostream>
14 #include <cmath>
15 using namespace osl;
16 using namespace osl::rating;
17 
18 int verbose = 0;
19 const char *kisen_filename="0.kif";
20 size_t num_kisen = 4096;
21 double verbose_limit = 0;
22 
23 void show(const NumEffectState&, Move, const progress::Effect5x3& progress);
24 void show_statistics();
25 
26 int main()
27 {
28  KisenFile kisen_file(kisen_filename);
29  for (size_t i=0; i<num_kisen; i++) {
30  if (i % 16 == 0)
31  std::cerr << '.';
32  NumEffectState state(kisen_file.getInitialState());
33  const osl::vector<Move> moves = kisen_file.getMoves(i);
34  RatingEnv env;
35  env.make(state);
36  progress::Effect5x3 progress(state);
37  for (size_t i=0; i<moves.size(); ++i) {
38  if (state.inCheck(alt(state.turn())))
39  break; // illegal
40 
41  const Move move = moves[i];
42  show(state, move, progress);
43  state.makeMove(move);
44  env.update(state, move);
45  progress.update(state, move);
46  }
47  }
48  std::cerr << "\n";
50 }
51 
53 {
54  double sum = 0;
55  for (size_t i=0; i<moves.size(); ++i)
56  sum += pow(10, moves[i].rating()/400.0);
57  vector<double> result(moves.size());
58  for (size_t i=0; i<moves.size(); ++i)
59  result[i] = pow(10, moves[i].rating()/400.0) / sum;
60  return result;
61 }
62 
64 stat::Histogram histogram(1,10,0,true);
65 
66 void show(const NumEffectState& state, Move next, const progress::Effect5x3& progress)
67 {
68  static const StandardFeatureSet& feature_set = StandardFeatureSet::instance();
70  RatingEnv env;
71  env.make(state);
72 
73  feature_set.generateRating(state, env, 2000, moves);
74  vector<double> probability = rating_to_probability(moves);
75  const RatedMove *rm = moves.find(next);
76  top_rated[progress.progress16().value()].add(rm && rm == &moves[0]);
77  if (rm)
78  histogram.add(-log10(probability[rm - &moves[0]]));
79  if (verbose || (rm && probability[rm - &moves[0]] < verbose_limit)) {
80  std::cout << state;
81  if (verbose > 1) {
82  for (size_t i=0; i<probability.size(); ++i) {
83  std::cout << record::csa::show(moves[i].move()) << " " << probability[i] << "\n";
84  }
85  }
86  std::cout << "max " << probability.front() << " min " << probability.back()
87  << " selected " << record::csa::show(next) << " ";
88  if (rm)
89  std::cout << probability[rm - &moves[0]] << "\n";
90  else
91  std::cout << "move-not-found?!\n";
92  std::cout << "\n";
93  }
94 }
95 
97 {
98  for (int i=0; i<16; ++i)
99  std::cout << "top " << i << " " << top_rated[i].getAverage() << "\n";
100 }
101 
102 
103 // ;;; Local Variables:
104 // ;;; mode:c++
105 // ;;; c-basic-offset:2
106 // ;;; End: