rating-perf.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rating-perf.cc
Go to the documentation of this file.
1 #include "osl/rating/featureSet.h"
2 #include "osl/rating/ratingEnv.h"
3 #include "osl/eval/progressEval.h"
4 #include "osl/effect_util/sendOffSquare.h"
5 #include "osl/effect_util/effectUtil.h"
6 #include "osl/record/csaRecord.h"
7 #include "osl/record/csaIOError.h"
8 #include "osl/stat/average.h"
9 #include "osl/misc/perfmon.h"
10
11 #include <boost/format.hpp>
12 #include <string>
13 #include <iostream>
14 #include <iomanip>
15 #include <cmath>
16 #include <cstdio>
17 using namespace osl;
18 using namespace osl::rating;
19
25 void usage(const char *prog)
26 {
27 using namespace std;
28 cerr << "Usage: " << prog << " [-v] [-f skip] csafiles\n"
29 << endl;
30 exit(1);
31 }
32
33 size_t first_skip = 0;
34 bool verbose = false;
35
36 stat::Average moves, cycles, cycles_per_move, probs, order, top_score, selected_score;
37 int min_selected = 1000;
38
39 void test_file(const FeatureSet&, const char *filename);
40
41 int main(int argc, char **argv)
42 {
43 const char *program_name = argv[0];
44 bool error_flag = false;
45 extern char *optarg;
46 extern int optind;
47
48 char c;
49 while ((c = getopt(argc, argv, "f:vh")) != EOF)
50 {
51 switch(c)
52 {
53 case 'f': first_skip = atoi(optarg);
54 break;
55 case 'v': verbose = true;
56 break;
57 default: error_flag = true;
58 }
59 }
60 argc -= optind;
61 argv += optind;
62
63 if (error_flag || (argc < 1))
64 usage(program_name);
65
66 eval::ProgressEval::setUp();
67 StandardFeatureSet f;
68
69 for (int i=0; i<argc; ++i)
70 {
71 if (i % 128 == 0)
72 std::cerr << '.';
73 test_file(f, argv[i]);
74 }
75
76 std::cout << "\n"
77 << "average moves/position " << moves.getAverage() << "\n"
78 << "average order " << order.getAverage() << "\n"
79 << "average selected score " << selected_score.getAverage() << "\n"
80 << "min selected score " << min_selected << "\n"
81 << "average top score " << top_score.getAverage() << "\n";
82 std::cout << "average cycles/position " << cycles.getAverage() << "\n"
83 << "average cycles/position/move " << cycles_per_move.getAverage()
84 << "\n";
85 }
86
87 /* ------------------------------------------------------------------------- */
88
89 size_t num_positions = 0;
90 void test_position(const FeatureSet& f, Move next_move, Move last_move, const RatingEnv& env,
91 const NumEffectState& state)
92 {
93 RatedMoveVector my_moves;
94
95 misc::PerfMon clock;
96 f.generateRating(state, env, 1400, my_moves);
97
98 const size_t consumed = clock.stop();
99 if (my_moves.size())
100 top_score.add(my_moves[0].rating());
101 const RatedMove *p = my_moves.find(next_move);
102 int count = my_moves.size();
103 int order = p ? p - &*my_moves.begin() +1 : count;
104 if (p) {
105 ::order.add(order);
106 if (p->rating() < min_selected)
107 min_selected = p->rating();
108 if (p->rating() < -2000) {
109 std::cerr << state << "selected " << *p << "\n" << my_moves;
110 }
111 }
112 else {
113 ::order.add(count);
114 }
115 moves.add(count);
116 cycles.add(consumed);
117 cycles_per_move.add(consumed/count);
118 ++num_positions;
119
120 }
121
122 void test_file(const FeatureSet& f, const char *filename)
123 {
124 Record rec;
125 try {
126 rec = CsaFile(filename).getRecord();
127 }
128 catch (CsaIOError& e) {
129 std::cerr << "skip " << filename <<"\n";
130 std::cerr << e.what() << "\n";
131 return;
132 }
133 catch (...) {
134 throw;
135 }
136
137 NumEffectState state(rec.getInitialState());
138 const osl::stl::vector<osl::Move> moves=rec.getMoves();
139
140 RatingEnv env;
141 env.make(state);
142 for (size_t i=0; i<moves.size(); ++i) {
143 const Move move = moves[i];
144 assert(state.isValidMove(move));
145 if (i >= first_skip) {
146 test_position(f, moves[i], (i>0 ? moves[i-1] : Move::PASS(alt(moves[i].player()))),
147 env, state);
148 }
149 state.makeMove(move);
150 env.update(state, move);
151 }
152 }
153
154 /* ------------------------------------------------------------------------- */
155 // ;;; Local Variables:
156 // ;;; mode:c++
157 // ;;; c-basic-offset:2
158 // ;;; End: