14 #include <boost/format.hpp>
20 using namespace osl::rating;
25 cerr <<
"Usage: " << prog <<
" [-v] [-f skip] csafiles or -k kisen-filename -n num\n"
62 return *histograms[i];
66 for (
int i=0; i<8; ++i)
69 void add(Progress16 progress,
int data,
double weight = 1.0)
71 const int min = histograms[0]->start();
72 const int max = histograms[0]->start() + histograms[0]->width()*histograms[0]->length();
73 if (data < min || data >= max) {
76 histograms[progress.value()/2]->add(data,
weight);
104 assert(numerator.
width() == denominator.
width());
106 assert(numerator.
start() == denominator.
start());
108 for (
size_t i=0; i<numerator.
length(); ++i) {
110 const double d = denominator.
frequency(i);
111 const double prob = n / d;
112 logprob.frequency(i) = d >= 15 ?
static_cast<int>(-100.0*log(prob)/log(2.0)) : 0;
114 logprob.show(std::cout);
124 for (
size_t i=0; i<numerator.
length(); ++i) {
128 const double p1 = n / d1;
129 const double p2 = n / d2;
130 l1.frequency(i) = d1 > 20 ?
static_cast<int>(-100.0*log(p1)/log(2.0)) : 0;
131 l2.frequency(i) = d2 > 20 ?
static_cast<int>(-100.0*log(p2)/log(2.0)) : 0;
133 int value=l1.start();
134 for (
size_t i=0; i<l1.length(); ++i, value+=l1.width()) {
135 std::cout << std::setw(5) << value <<
" - "
136 << std::setw(5) << value+(int)l1.width();
137 std::cout <<
" " << std::setw(8) << l1.frequency(i)
138 <<
" " << std::setw(8) << l2.frequency(i) <<
"\n";
143 assert(numerator[0].
width() == denom1[0].
width());
145 assert(numerator[0].start() == denom1[0].start());
146 assert(denom1[0].
width() == denom2[0].
width() && denom1[0].
length() == denom2[0].
length() && denom1[0].start() == denom2[0].start());
148 int value=numerator[0].start();
149 for (
size_t i=0; i<numerator[0].length(); ++i, value+=numerator[0].width()) {
150 std::cout << std::setw(4) << value <<
" - "
151 << std::setw(4) << value+(int)numerator[0].
width();
153 for (
int p=0; p<8; ++p) {
154 const double n = numerator[p].frequency(i);
155 const double d1 = denom1[p].frequency(i);
156 const double d2 = denom2[p].frequency(i);
157 const double p1 = n / d1;
158 const double p2 = n / d2;
159 const double f1 = n > 20 ?
static_cast<int>(-100.0*log(p1)/log(2.0)) : 0;
160 const double f2 = n > 20 ?
static_cast<int>(-100.0*log(p2)/log(2.0)) : 0;
161 std::cout <<
" " << std::setw(5) << f1
162 <<
" " << std::setw(4) << f2;
167 std::cout <<
"static const osl::CArray2d<int, 8, "
168 << numerator[0].length() <<
"> xxx_to_depth = {{\n";
169 for (
int p=0; p<8; ++p) {
171 for (
size_t i=0; i<numerator[0].length(); ++i, value+=numerator[0].width()) {
172 const double n = numerator[p].frequency(i);
173 const double d = denom1[p].frequency(i);
174 const double p = n / d;
175 const double f = n > 20 ?
static_cast<int>(-100.0*log(p)/log(2.0)) : 0;
176 std::cout << std::setw(4) << f <<
",";
182 std::cout <<
"}};\n";
184 std::cout <<
"static const osl::CArray2d<int, 8, "
185 << numerator[0].length() <<
"> xxx_to_width = {{\n";
186 for (
int p=0; p<8; ++p) {
188 for (
size_t i=0; i<numerator[0].length(); ++i, value+=numerator[0].width()) {
189 const double n = numerator[p].frequency(i);
190 const double d = denom2[p].frequency(i);
191 const double p = n / d;
192 const double f = n > 20 ?
static_cast<int>(-100.0*log(p)/log(2.0)) : 0;
193 std::cout << std::setw(4) << f <<
",";
199 std::cout <<
"}};\n";
218 size_t find(
Property property,
const NumEffectState& state,
const RatingEnv& e,
219 const RatedMoveVector& moves,
Move selected)
225 if (! e.history.lastMove().isNormal())
227 for (; i<moves.size(); ++i)
228 if (moves[i].move().to() == e.history.lastMove().to())
233 for (; i<moves.size(); ++i)
234 if (moves[i].move().to() == e.history.lastMove().to())
238 if (e.history.lastMove().isNormal()) {
239 for (; i<moves.size(); ++i)
240 if (moves[i].move().to() != e.history.lastMove().to())
245 for (; i<moves.size(); ++i) {
246 if (e.history.lastMove().isNormal() && moves[i].move().to() == e.history.lastMove().to())
248 if (PieceEval::computeDiffAfterMoveForRP(state, moves[i].move()) > 0)
254 for (; i<moves.size(); ++i) {
255 if (PieceEval::computeDiffAfterMoveForRP(state, moves[i].move()) > 0)
261 for (; i<moves.size(); ++i) {
262 if (e.history.lastMove().isNormal() && moves[i].move().to() == e.history.lastMove().to())
264 if (PieceEval::computeDiffAfterMoveForRP(state, moves[i].move()) > 0) {
265 if (++num_seeplus <= 1)
272 for (; i<moves.size(); ++i) {
273 if (e.history.lastMove().isNormal() && moves[i].move().to() == e.history.lastMove().to())
275 if (PieceEval::computeDiffAfterMoveForRP(state, moves[i].move()) > 0)
294 void add(
const NumEffectState& state,
const RatingEnv& e,
295 const RatedMoveVector& moves,
Move selected)
297 const size_t i =
find(property, state, e, moves, selected);
298 if (i >= moves.size())
300 generated_all.add(moves[i].rating());
301 const RatedMove *found = moves.find(selected);
302 if (found && (found - &*moves.begin()) <= (
int)i) {
303 if (moves[i].move() == selected)
304 this->selected.add(moves[i].rating());
305 generated.add(moves[i].rating());
319 CArray<stat::Variance, sc_length_2d*sc_length_2d>
variance;
328 static int index(
int score,
int diff)
332 assert(diff_index >= 0);
335 void add(
const NumEffectState& state,
const RatedMoveVector& moves,
Move selected)
337 if (moves.empty() || state.inCheck())
339 const int highest = moves[0].rating();
340 const RatedMove *found = moves.find(selected);
343 const size_t selected_order = found - &*moves.begin();
344 if (first <= selected_order && selected_order < last) {
345 const int selected_index = index(found->rating(), highest - found->rating());
346 this->selected.add(selected_index);
348 for (
size_t i=first; i<
std::min(last,moves.size()); ++i) {
349 const int index = this->index(moves[i].rating(), highest - moves[i].rating());
350 all_generated.add(index);
351 if (i <= selected_order)
352 generated.add(index);
353 variance[index].add(i);
362 double s = selected.frequency(i*sc_length_2d+j);
363 double g = generated.frequency(i*sc_length_2d+j);
365 os << std::setw(5) << (std::min(s,g) > 20 ?
static_cast<int>(-100.0*log(s/g)/log(2.0)) : 0);
372 double s = selected.frequency(i*sc_length_2d+j);
373 double a = all_generated.frequency(i*sc_length_2d+j);
375 os << std::setw(5) << (std::min(s,a) > 20 ?
static_cast<int>(-100.0*log(s/a)/log(2.0)) : 0);
383 os << std::setw(5) << static_cast<int>(variance[i*sc_length_2d+j].getAverage());
393 r0(1,25), r1(25,800), r_all(1,800)
396 void add(
const NumEffectState& state,
const RatedMoveVector& moves,
Move selected)
398 #ifdef SHOW_SPLIT_RATING
399 r0.add(state, moves, selected);
400 r1.add(state, moves, selected);
402 r_all.add(state, moves, selected);
406 #if SHOW_SPLIT_RATING
422 const SimpleState& initial,
425 int main(
int argc,
char **argv)
427 const char *program_name = argv[0];
428 bool error_flag =
false;
433 while ((c = getopt(argc, argv,
"f:k:n:vh")) != EOF)
445 default: error_flag =
true;
459 KisenIpxFile ipx(kisen_file.ipxFileName());
473 for (
int i=0; i<argc; ++i)
481 <<
"average moves/position " << moves.
getAverage() <<
"\n"
482 <<
"average order " << order.
getAverage() <<
"\n"
483 <<
"average selected score " << selected_score.
getAverage() <<
"\n"
484 <<
"min selected score " << min_selected.
value() <<
"\n"
485 <<
"average top score " << top_score.
getAverage() <<
"\n"
486 <<
"min top score " << min_top.
value() <<
"\n"
487 <<
"max top score (notakeback) " << max_notakeback.
value() <<
"\n"
488 <<
"max top score (nocapture) " << max_nocapture.
value() <<
"\n";
489 std::cout <<
"order to logprob (depth, width)\n";
491 std::cout <<
"score to logprob (all)\n";
493 std::cout <<
"relative score to logprob (all)\n";
495 std::cout <<
"score to logprob (takeback)\n";
497 std::cout <<
"score to logprob (see+)\n";
499 std::cout <<
"score to logprob (king_escape)\n";
502 std::cout <<
"order to logprob (takeback)\n";
504 std::cout <<
"order to logprob (seeplus)\n";
506 std::cout <<
"order to logprob (kingescape)\n";
508 rating_diff.
show(std::cout);
509 std::cout <<
"top move\n";
511 std::cout <<
"top move (takeback)\n";
513 std::cout <<
"top move (2nd takeback)\n";
516 std::cout <<
"top move (no takeback)\n";
519 std::cout <<
"top move (see+)\n";
521 std::cout <<
"top move (2nd see+)\n";
523 std::cout <<
"top move (2nd see+ or no see+)\n";
526 std::cout <<
"top rating for each progress8\n";
528 std::cout <<
"progress8 " << i <<
"\tave. " << std::setprecision(3) <<
top_rating_progress[i].getAverage()
538 const bool in_check = state.inCheck();
548 rating_diff.
add(state, moves, next_move);
557 bool notakeback_added = in_check, nocapture_added = in_check;
558 const int highest = moves[0].rating();
559 min_top.
add(highest);
560 top_score.
add(highest);
562 size_t index =
find(
NoSeePlus, state, env, moves, next_move);
563 if (index < moves.size())
566 if (! notakeback_added
567 && moves[0].move().to() != last_move.
to()) {
568 nocapture_added =
true;
569 max_notakeback.
add(highest);
571 if (! nocapture_added
573 && ! moves[0].move().isPromotion()) {
574 nocapture_added =
true;
575 max_nocapture.
add(highest);
578 const int count = moves.size();
579 const int order = p ? p - &*moves.begin() +1 :
count;
581 const double selected_weight = 1.0-1.0/(moves.size()-order+1);
582 const double other_weight = 1.0;
585 for (
int i=0; i<
count; ++i) {
589 if (moves[i].move() == next_move) {
601 if (p->
rating() < -2000) {
602 std::cerr << state <<
"selected " << *p <<
"\n" <<
moves;
604 for (
int i=0; i<
order; ++i)
606 for (
size_t i=0; i<moves.size(); ++i)
612 for (
int i=0; i<
count; ++i) {
613 if (moves[i].move().to() != last_move.
to())
618 if (moves[i].move() == next_move) {
627 for (
int i=0; i<
count; ++i) {
628 if (moves[i].move().to() == last_move.
to())
630 if (! (moves[i].move().capturePtype() !=
PTYPE_EMPTY
631 || moves[i].move().isPromotion())
632 || PieceEval::computeDiffAfterMoveForRP(state, moves[i].move()) <= 0)
637 if (moves[i].move() == next_move) {
646 for (
int i=0; i<
order; ++i) {
648 if (moves[i].move() == next_move)
651 for (
size_t i=0; i<moves.size(); ++i) {
653 if (! notakeback_added && moves[i].move().to() != last_move.
to()) {
654 notakeback_added =
true;
655 max_notakeback.
add(moves[i].rating());
657 if (! nocapture_added && moves[i].move().capturePtype() ==
PTYPE_EMPTY
658 && ! moves[i].move().isPromotion()) {
659 nocapture_added =
true;
660 max_nocapture.
add(moves[i].rating());
663 if (moves[0].move() != next_move) {
664 const int top_score = moves[0].rating();
665 for (
int i=1; i<
order; ++i) {
667 if (moves[i].move() == next_move)
670 for (
size_t i=1; i<moves.size(); ++i) {
677 const SimpleState& initial,
680 NumEffectState state(initial);
685 for (
size_t i=0; i<moves.size(); ++i) {
686 if (state.inCheck(
alt(state.turn())))
689 const Move move = moves[i];
690 assert(state.isValidMove(move));
695 state.makeMove(move);
705 rec = CsaFile(filename).getRecord();
707 catch (CsaIOError& e) {
708 std::cerr <<
"skip " << filename <<
"\n";
709 std::cerr << e.what() <<
"\n";
715 test_record(f, rec.getInitialState(), rec.getMoves());