+
+ // format_cp_aligned_dot() converts a Value into (centi)pawns and writes it in a buffer,
+ // always keeping two decimals. The buffer must have capacity for at least 7 chars.
+ static void format_cp_aligned_dot(Value v, char* buffer) {
+
+ buffer[0] = (v < 0 ? '-' : v > 0 ? '+' : ' ');
+
+ double cp = 1.0 * std::abs(int(v)) / PawnValueEg;
+ sprintf(&buffer[1], "%6.2f", cp);
+ }
+
+
+ // trace() returns a string with the value of each piece on a board,
+ // and a table for (PSQT, Layers) values bucket by bucket.
+
+ std::string trace(Position& pos) {
+
+ std::stringstream ss;
+
+ char board[3*8+1][8*8+2];
+ std::memset(board, ' ', sizeof(board));
+ for (int row = 0; row < 3*8+1; ++row)
+ board[row][8*8+1] = '\0';
+
+ // A lambda to output one box of the board
+ auto writeSquare = [&board](File file, Rank rank, Piece pc, Value value) {
+
+ const int x = ((int)file) * 8;
+ const int y = (7 - (int)rank) * 3;
+ for (int i = 1; i < 8; ++i)
+ board[y][x+i] = board[y+3][x+i] = '-';
+ for (int i = 1; i < 3; ++i)
+ board[y+i][x] = board[y+i][x+8] = '|';
+ board[y][x] = board[y][x+8] = board[y+3][x+8] = board[y+3][x] = '+';
+ if (pc != NO_PIECE)
+ board[y+1][x+4] = PieceToChar[pc];
+ if (value != VALUE_NONE)
+ format_cp_compact(value, &board[y+2][x+2]);
+ };
+
+ // We estimate the value of each piece by doing a differential evaluation from
+ // the current base eval, simulating the removal of the piece from its square.
+ Value base = evaluate(pos);
+ base = pos.side_to_move() == WHITE ? base : -base;
+
+ for (File f = FILE_A; f <= FILE_H; ++f)
+ for (Rank r = RANK_1; r <= RANK_8; ++r)
+ {
+ Square sq = make_square(f, r);
+ Piece pc = pos.piece_on(sq);
+ Value v = VALUE_NONE;
+
+ if (pc != NO_PIECE && type_of(pc) != KING)
+ {
+ auto st = pos.state();
+
+ pos.remove_piece(sq);
+ st->accumulator.computed[WHITE] = false;
+ st->accumulator.computed[BLACK] = false;
+
+ Value eval = evaluate(pos);
+ eval = pos.side_to_move() == WHITE ? eval : -eval;
+ v = base - eval;
+
+ pos.put_piece(pc, sq);
+ st->accumulator.computed[WHITE] = false;
+ st->accumulator.computed[BLACK] = false;
+ }
+
+ writeSquare(f, r, pc, v);
+ }
+
+ ss << " NNUE derived piece values:\n";
+ for (int row = 0; row < 3*8+1; ++row)
+ ss << board[row] << '\n';
+ ss << '\n';
+
+ auto t = trace_evaluate(pos);
+
+ ss << " NNUE network contributions "
+ << (pos.side_to_move() == WHITE ? "(White to move)" : "(Black to move)") << std::endl
+ << "+------------+------------+------------+------------+\n"
+ << "| Bucket | Material | Positional | Total |\n"
+ << "| | (PSQT) | (Layers) | |\n"
+ << "+------------+------------+------------+------------+\n";
+
+ for (std::size_t bucket = 0; bucket < LayerStacks; ++bucket)
+ {
+ char buffer[3][8];
+ std::memset(buffer, '\0', sizeof(buffer));
+
+ format_cp_aligned_dot(t.psqt[bucket], buffer[0]);
+ format_cp_aligned_dot(t.positional[bucket], buffer[1]);
+ format_cp_aligned_dot(t.psqt[bucket] + t.positional[bucket], buffer[2]);
+
+ ss << "| " << bucket << " "
+ << " | " << buffer[0] << " "
+ << " | " << buffer[1] << " "
+ << " | " << buffer[2] << " "
+ << " |";
+ if (bucket == t.correctBucket)
+ ss << " <-- this bucket is used";
+ ss << '\n';
+ }
+
+ ss << "+------------+------------+------------+------------+\n";
+
+ return ss.str();
+ }
+
+