Support NNUE for placement variants
authorFabian Fichter <ianfab@users.noreply.github.com>
Sat, 4 Sep 2021 07:53:35 +0000 (09:53 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sat, 4 Sep 2021 07:53:35 +0000 (09:53 +0200)
Use classical evaluation during setup phase to enable NNUE usage.

src/evaluate.cpp
src/nnue/features/half_ka_v2_variants.cpp
src/position.h
src/variant.cpp
src/variant.h

index 705d2fb..a9af47f 100644 (file)
@@ -1564,7 +1564,7 @@ Value Eval::evaluate(const Position& pos) {
 
   Value v;
 
-  if (!Eval::useNNUE)
+  if (!Eval::useNNUE || !pos.nnue_applicable())
       v = Evaluation<NO_TRACE>(pos).value();
   else
   {
@@ -1664,14 +1664,14 @@ std::string Eval::trace(Position& pos) {
      << "|      Total | " << Term(TOTAL)
      << "+------------+-------------+-------------+-------------+\n";
 
-  if (Eval::useNNUE)
+  if (Eval::useNNUE && pos.nnue_applicable())
       ss << '\n' << NNUE::trace(pos) << '\n';
 
   ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15);
 
   v = pos.side_to_move() == WHITE ? v : -v;
   ss << "\nClassical evaluation   " << to_cp(v) << " (white side)\n";
-  if (Eval::useNNUE)
+  if (Eval::useNNUE && pos.nnue_applicable())
   {
       v = NNUE::evaluate(pos, false);
       v = pos.side_to_move() == WHITE ? v : -v;
@@ -1681,7 +1681,7 @@ std::string Eval::trace(Position& pos) {
   v = evaluate(pos);
   v = pos.side_to_move() == WHITE ? v : -v;
   ss << "Final evaluation       " << to_cp(v) << " (white side)";
-  if (Eval::useNNUE)
+  if (Eval::useNNUE && pos.nnue_applicable())
      ss << " [with scaled NNUE, hybrid, ...]";
   ss << "\n";
 
index a025de2..8e766f8 100644 (file)
@@ -60,7 +60,7 @@ namespace Stockfish::Eval::NNUE::Features {
     }
 
     // Indices for pieces in hand
-    if (pos.piece_drops() || pos.seirawan_gating())
+    if (pos.nnue_use_pockets())
       for (Color c : {WHITE, BLACK})
           for (PieceType pt : pos.piece_types())
               for (int i = 0; i < pos.count_in_hand(c, pt); i++)
index 5b6a723..b80de7a 100644 (file)
@@ -147,6 +147,8 @@ public:
   PieceType castling_rook_piece() const;
   PieceType king_type() const;
   PieceType nnue_king() const;
+  bool nnue_use_pockets() const;
+  bool nnue_applicable() const;
   bool checking_permitted() const;
   bool drop_checks() const;
   bool must_capture() const;
@@ -525,6 +527,16 @@ inline PieceType Position::nnue_king() const {
   return var->nnueKing;
 }
 
+inline bool Position::nnue_use_pockets() const {
+  assert(var != nullptr);
+  return var->nnueUsePockets;
+}
+
+inline bool Position::nnue_applicable() const {
+  // Do not use NNUE during setup phases (placement, sittuyin)
+  return !count_in_hand(ALL_PIECES) || nnue_use_pockets();
+}
+
 inline bool Position::checking_permitted() const {
   assert(var != nullptr);
   return var->checking;
index 205168b..ccec15d 100644 (file)
@@ -493,6 +493,7 @@ namespace {
         v->blackDropRegion = Rank8BB;
         v->dropOppositeColoredBishop = true;
         v->castlingDroppedPiece = true;
+        v->nnueAlias = "nn-";
         return v;
     }
     // Sittuyin (Burmese chess)
index 636197c..db55426 100644 (file)
@@ -139,6 +139,7 @@ struct Variant {
   std::string nnueAlias = "";
   PieceType nnueKing = KING;
   int nnueSquares;
+  bool nnueUsePockets;
   int nnuePieceIndices;
   int pieceSquareIndex[COLOR_NB][PIECE_NB];
   int pieceHandIndex[COLOR_NB][PIECE_NB];
@@ -207,7 +208,8 @@ struct Variant {
                 : extinctionPieceTypes.find(COMMONER) != extinctionPieceTypes.end() ? COMMONER
                 : NO_PIECE_TYPE;
       nnueSquares = (maxRank + 1) * (maxFile + 1);
-      int nnuePockets = pieceDrops || seirawanGating ? 2 * int(maxFile + 1) : 0;
+      nnueUsePockets = (pieceDrops && (!mustDrop || capturesToHand)) || seirawanGating;
+      int nnuePockets = nnueUsePockets ? 2 * int(maxFile + 1) : 0;
       int nnueNonDropPieceIndices = (2 * pieceTypes.size() - 1) * nnueSquares;
       nnuePieceIndices = nnueNonDropPieceIndices + 2 * (pieceTypes.size() - 1) * nnuePockets;
       int i = 0;