mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-23 02:27:00 +08:00
STC: https://tests.stockfishchess.org/tests/view/64d166235b17f7c21c0ddc15 LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 100032 W: 25698 L: 25547 D: 48787 Ptnml(0-2): 308, 11748, 25771, 11863, 326 LTC: https://tests.stockfishchess.org/tests/view/64d28c085b17f7c21c0df775 LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 123870 W: 31463 L: 31348 D: 61059 Ptnml(0-2): 63, 13487, 34719, 13604, 62 Besides rebasing I replaced PawnValueMg w/ 126 explicitly to decouple from https://tests.stockfishchess.org/tests/view/64d212de5b17f7c21c0debbb by @peregrineshahin which also passed. #4734 closes https://github.com/official-stockfish/Stockfish/pull/4731 Bench: 1447866
209 lines
7.6 KiB
C++
209 lines
7.6 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <fstream>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <iostream>
|
|
#include <streambuf>
|
|
#include <vector>
|
|
|
|
#include "bitboard.h"
|
|
#include "evaluate.h"
|
|
#include "misc.h"
|
|
#include "thread.h"
|
|
#include "timeman.h"
|
|
#include "uci.h"
|
|
#include "incbin/incbin.h"
|
|
#include "nnue/evaluate_nnue.h"
|
|
|
|
// Macro to embed the default efficiently updatable neural network (NNUE) file
|
|
// data in the engine binary (using incbin.h, by Dale Weiler).
|
|
// This macro invocation will declare the following three variables
|
|
// const unsigned char gEmbeddedNNUEData[]; // a pointer to the embedded data
|
|
// const unsigned char *const gEmbeddedNNUEEnd; // a marker to the end
|
|
// const unsigned int gEmbeddedNNUESize; // the size of the embedded file
|
|
// Note that this does not work in Microsoft Visual Studio.
|
|
#if !defined(_MSC_VER) && !defined(NNUE_EMBEDDING_OFF)
|
|
INCBIN(EmbeddedNNUE, EvalFileDefaultName);
|
|
#else
|
|
const unsigned char gEmbeddedNNUEData[1] = {0x0};
|
|
const unsigned char *const gEmbeddedNNUEEnd = &gEmbeddedNNUEData[1];
|
|
const unsigned int gEmbeddedNNUESize = 1;
|
|
#endif
|
|
|
|
|
|
using namespace std;
|
|
|
|
namespace Stockfish {
|
|
|
|
namespace Eval {
|
|
|
|
string currentEvalFileName = "None";
|
|
|
|
/// NNUE::init() tries to load a NNUE network at startup time, or when the engine
|
|
/// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
|
|
/// The name of the NNUE network is always retrieved from the EvalFile option.
|
|
/// We search the given network in three locations: internally (the default
|
|
/// network may be embedded in the binary), in the active working directory and
|
|
/// in the engine directory. Distro packagers may define the DEFAULT_NNUE_DIRECTORY
|
|
/// variable to have the engine search in a special directory in their distro.
|
|
|
|
void NNUE::init() {
|
|
|
|
string eval_file = string(Options["EvalFile"]);
|
|
if (eval_file.empty())
|
|
eval_file = EvalFileDefaultName;
|
|
|
|
#if defined(DEFAULT_NNUE_DIRECTORY)
|
|
vector<string> dirs = { "<internal>" , "" , CommandLine::binaryDirectory , stringify(DEFAULT_NNUE_DIRECTORY) };
|
|
#else
|
|
vector<string> dirs = { "<internal>" , "" , CommandLine::binaryDirectory };
|
|
#endif
|
|
|
|
for (const string& directory : dirs)
|
|
if (currentEvalFileName != eval_file)
|
|
{
|
|
if (directory != "<internal>")
|
|
{
|
|
ifstream stream(directory + eval_file, ios::binary);
|
|
if (NNUE::load_eval(eval_file, stream))
|
|
currentEvalFileName = eval_file;
|
|
}
|
|
|
|
if (directory == "<internal>" && eval_file == EvalFileDefaultName)
|
|
{
|
|
// C++ way to prepare a buffer for a memory stream
|
|
class MemoryBuffer : public basic_streambuf<char> {
|
|
public: MemoryBuffer(char* p, size_t n) { setg(p, p, p + n); setp(p, p + n); }
|
|
};
|
|
|
|
MemoryBuffer buffer(const_cast<char*>(reinterpret_cast<const char*>(gEmbeddedNNUEData)),
|
|
size_t(gEmbeddedNNUESize));
|
|
(void) gEmbeddedNNUEEnd; // Silence warning on unused variable
|
|
|
|
istream stream(&buffer);
|
|
if (NNUE::load_eval(eval_file, stream))
|
|
currentEvalFileName = eval_file;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// NNUE::verify() verifies that the last net used was loaded successfully
|
|
void NNUE::verify() {
|
|
|
|
string eval_file = string(Options["EvalFile"]);
|
|
if (eval_file.empty())
|
|
eval_file = EvalFileDefaultName;
|
|
|
|
if (currentEvalFileName != eval_file)
|
|
{
|
|
|
|
string msg1 = "Network evaluation parameters compatible with the engine must be available.";
|
|
string msg2 = "The network file " + eval_file + " was not loaded successfully.";
|
|
string msg3 = "The UCI option EvalFile might need to specify the full path, including the directory name, to the network file.";
|
|
string msg4 = "The default net can be downloaded from: https://tests.stockfishchess.org/api/nn/" + std::string(EvalFileDefaultName);
|
|
string msg5 = "The engine will be terminated now.";
|
|
|
|
sync_cout << "info string ERROR: " << msg1 << sync_endl;
|
|
sync_cout << "info string ERROR: " << msg2 << sync_endl;
|
|
sync_cout << "info string ERROR: " << msg3 << sync_endl;
|
|
sync_cout << "info string ERROR: " << msg4 << sync_endl;
|
|
sync_cout << "info string ERROR: " << msg5 << sync_endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
sync_cout << "info string NNUE evaluation using " << eval_file << sync_endl;
|
|
}
|
|
}
|
|
|
|
/// evaluate() is the evaluator for the outer world. It returns a static
|
|
/// evaluation of the position from the point of view of the side to move.
|
|
|
|
Value Eval::evaluate(const Position& pos) {
|
|
|
|
assert(!pos.checkers());
|
|
|
|
Value v;
|
|
|
|
int nnueComplexity;
|
|
int npm = pos.non_pawn_material() / 64;
|
|
|
|
Color stm = pos.side_to_move();
|
|
Value optimism = pos.this_thread()->optimism[stm];
|
|
|
|
Value nnue = NNUE::evaluate(pos, true, &nnueComplexity);
|
|
|
|
int material = pos.non_pawn_material(stm) - pos.non_pawn_material(~stm)
|
|
+ 126 * (pos.count<PAWN>(stm) - pos.count<PAWN>(~stm));
|
|
|
|
// Blend optimism with nnue complexity and (semi)classical complexity
|
|
optimism += optimism * (nnueComplexity + abs(material - nnue)) / 512;
|
|
|
|
v = ( nnue * (915 + npm + 9 * pos.count<PAWN>())
|
|
+ optimism * (154 + npm + pos.count<PAWN>())) / 1024;
|
|
|
|
// Damp down the evaluation linearly when shuffling
|
|
v = v * (200 - pos.rule50_count()) / 214;
|
|
|
|
// Guarantee evaluation does not hit the tablebase range
|
|
v = std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
|
|
|
|
return v;
|
|
}
|
|
|
|
/// trace() is like evaluate(), but instead of returning a value, it returns
|
|
/// a string (suitable for outputting to stdout) that contains the detailed
|
|
/// descriptions and values of each evaluation term. Useful for debugging.
|
|
/// Trace scores are from white's point of view
|
|
|
|
std::string Eval::trace(Position& pos) {
|
|
|
|
if (pos.checkers())
|
|
return "Final evaluation: none (in check)";
|
|
|
|
// Reset any global variable used in eval
|
|
pos.this_thread()->bestValue = VALUE_ZERO;
|
|
pos.this_thread()->optimism[WHITE] = VALUE_ZERO;
|
|
pos.this_thread()->optimism[BLACK] = VALUE_ZERO;
|
|
|
|
std::stringstream ss;
|
|
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2);
|
|
ss << '\n' << NNUE::trace(pos) << '\n';
|
|
|
|
ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15);
|
|
|
|
Value v;
|
|
v = NNUE::evaluate(pos, false);
|
|
v = pos.side_to_move() == WHITE ? v : -v;
|
|
ss << "NNUE evaluation " << 0.01 * UCI::to_cp(v) << " (white side)\n";
|
|
|
|
v = evaluate(pos);
|
|
v = pos.side_to_move() == WHITE ? v : -v;
|
|
ss << "Final evaluation " << 0.01 * UCI::to_cp(v) << " (white side)";
|
|
ss << " [with scaled NNUE, ...]";
|
|
ss << "\n";
|
|
|
|
return ss.str();
|
|
}
|
|
|
|
} // namespace Stockfish
|