Fix NNUE evaluation trace for variants
authorFabian Fichter <ianfab@users.noreply.github.com>
Sun, 25 Jul 2021 19:06:52 +0000 (21:06 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sun, 25 Jul 2021 19:06:52 +0000 (21:06 +0200)
src/material.cpp
src/nnue/evaluate_nnue.cpp
src/position.h

index 65ad7a7..e59af5c 100644 (file)
@@ -169,7 +169,7 @@ Entry* probe(const Position& pos) {
           npm2 += pos.count_in_hand(pt) * PieceValue[MG][make_piece(WHITE, pt)];
       e->gamePhase = Phase(PHASE_MIDGAME * npm / std::max(int(npm + npm2), 1));
       int countAll = pos.count_with_hand(WHITE, ALL_PIECES) + pos.count_with_hand(BLACK, ALL_PIECES);
-      e->materialDensity = (npm + npm2 + pos.count<PAWN>() * PawnValueMg) * countAll / ((pos.max_file() + 1) * (pos.max_rank() + 1));
+      e->materialDensity = (npm + npm2 + pos.count<PAWN>() * PawnValueMg) * countAll / (pos.files() * pos.ranks());
   }
   else
       e->gamePhase = Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit));
index 38e390e..94e7e5d 100644 (file)
@@ -225,8 +225,6 @@ namespace Stockfish::Eval::NNUE {
     return t;
   }
 
-  static const std::string PieceToChar(" PNBRQK  pnbrqk");
-
   // Requires the buffer to have capacity for at least 5 values
   static void format_cp_compact(Value v, char* buffer) {
 
@@ -300,13 +298,13 @@ namespace Stockfish::Eval::NNUE {
 
     std::stringstream ss;
 
-    char board[3*8+1][8*8+2];
+    char board[3*RANK_NB+1][8*FILE_NB+2];
     std::memset(board, ' ', sizeof(board));
-    for (int row = 0; row < 3*8+1; ++row)
-      board[row][8*8+1] = '\0';
+    for (int row = 0; row < 3*pos.ranks()+1; ++row)
+      board[row][pos.ranks()*pos.files()+1] = '\0';
 
     // A lambda to output one box of the board
-    auto writeSquare = [&board](File file, Rank rank, Piece pc, Value value) {
+    auto writeSquare = [&board, &pos](File file, Rank rank, Piece pc, Value value) {
 
       const int x = ((int)file) * 8;
       const int y = (7 - (int)rank) * 3;
@@ -316,7 +314,7 @@ namespace Stockfish::Eval::NNUE {
          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];
+        board[y+1][x+4] = pos.piece_to_char()[pc];
       if (value != VALUE_NONE)
         format_cp_compact(value, &board[y+2][x+2]);
     };
@@ -326,11 +324,13 @@ namespace Stockfish::Eval::NNUE {
     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)
+    for (File f = FILE_A; f <= pos.max_file(); ++f)
+      for (Rank r = RANK_1; r <= pos.max_rank(); ++r)
       {
         Square sq = make_square(f, r);
         Piece pc = pos.piece_on(sq);
+        Piece unpromotedPc = pos.unpromoted_piece_on(sq);
+        bool isPromoted = pos.is_promoted(sq);
         Value v = VALUE_NONE;
 
         if (pc != NO_PIECE && type_of(pc) != pos.nnue_king())
@@ -345,7 +345,7 @@ namespace Stockfish::Eval::NNUE {
           eval = pos.side_to_move() == WHITE ? eval : -eval;
           v = base - eval;
 
-          pos.put_piece(pc, sq);
+          pos.put_piece(pc, sq, isPromoted, unpromotedPc);
           st->accumulator.computed[WHITE] = false;
           st->accumulator.computed[BLACK] = false;
         }
@@ -354,7 +354,7 @@ namespace Stockfish::Eval::NNUE {
       }
 
     ss << " NNUE derived piece values:\n";
-    for (int row = 0; row < 3*8+1; ++row)
+    for (int row = 0; row < 3*pos.ranks()+1; ++row)
         ss << board[row] << '\n';
     ss << '\n';
 
index d2db3eb..5b6a723 100644 (file)
@@ -115,6 +115,8 @@ public:
   const Variant* variant() const;
   Rank max_rank() const;
   File max_file() const;
+  int ranks() const;
+  int files() const;
   bool two_boards() const;
   Bitboard board_bb() const;
   Bitboard board_bb(Color c, PieceType pt) const;
@@ -363,6 +365,16 @@ inline File Position::max_file() const {
   return var->maxFile;
 }
 
+inline int Position::ranks() const {
+  assert(var != nullptr);
+  return var->maxRank + 1;
+}
+
+inline int Position::files() const {
+  assert(var != nullptr);
+  return var->maxFile + 1;
+}
+
 inline bool Position::two_boards() const {
   assert(var != nullptr);
   return var->twoBoards;