mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 00:33:09 +00:00

This effectively reverts the removal of delta in: https://github.com/official-stockfish/Stockfish/pull/5373 Passed STC: https://tests.stockfishchess.org/tests/view/6664d41922234461cef58e6b LLR: 2.95 (-2.94,2.94) <0.00,2.00> Total: 56448 W: 14849 L: 14500 D: 27099 Ptnml(0-2): 227, 6481, 14457, 6834, 225 Passed LTC: https://tests.stockfishchess.org/tests/view/666587a1996b40829f4ee007 LLR: 2.94 (-2.94,2.94) <0.50,2.50> Total: 91686 W: 23402 L: 22963 D: 45321 Ptnml(0-2): 78, 10205, 24840, 10640, 80 closes https://github.com/official-stockfish/Stockfish/pull/5382 bench 1160467
128 lines
4.7 KiB
C++
128 lines
4.7 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2024 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 "evaluate.h"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <sstream>
|
|
#include <tuple>
|
|
|
|
#include "nnue/network.h"
|
|
#include "nnue/nnue_misc.h"
|
|
#include "position.h"
|
|
#include "types.h"
|
|
#include "uci.h"
|
|
#include "nnue/nnue_accumulator.h"
|
|
|
|
namespace Stockfish {
|
|
|
|
// Returns a static, purely materialistic evaluation of the position from
|
|
// the point of view of the given color. It can be divided by PawnValue to get
|
|
// an approximation of the material advantage on the board in terms of pawns.
|
|
int Eval::simple_eval(const Position& pos, Color c) {
|
|
return PawnValue * (pos.count<PAWN>(c) - pos.count<PAWN>(~c))
|
|
+ (pos.non_pawn_material(c) - pos.non_pawn_material(~c));
|
|
}
|
|
|
|
bool Eval::use_smallnet(const Position& pos) {
|
|
int simpleEval = simple_eval(pos, pos.side_to_move());
|
|
return std::abs(simpleEval) > 962;
|
|
}
|
|
|
|
// 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 Eval::NNUE::Networks& networks,
|
|
const Position& pos,
|
|
Eval::NNUE::AccumulatorCaches& caches,
|
|
int optimism) {
|
|
|
|
assert(!pos.checkers());
|
|
|
|
int simpleEval = simple_eval(pos, pos.side_to_move());
|
|
bool smallNet = use_smallnet(pos);
|
|
int v;
|
|
|
|
auto [psqt, positional] = smallNet ? networks.small.evaluate(pos, &caches.small)
|
|
: networks.big.evaluate(pos, &caches.big);
|
|
|
|
Value nnue = (125 * psqt + 131 * positional) / 128;
|
|
int nnueComplexity = std::abs(psqt - positional);
|
|
|
|
// Re-evaluate the position when higher eval accuracy is worth the time spent
|
|
if (smallNet && (nnue * simpleEval < 0 || std::abs(nnue) < 227))
|
|
{
|
|
std::tie(psqt, positional) = networks.big.evaluate(pos, &caches.big);
|
|
nnue = (125 * psqt + 131 * positional) / 128;
|
|
nnueComplexity = std::abs(psqt - positional);
|
|
smallNet = false;
|
|
}
|
|
|
|
// Blend optimism and eval with nnue complexity
|
|
optimism += optimism * nnueComplexity / 457;
|
|
nnue -= nnue * nnueComplexity / 19157;
|
|
|
|
int material = 554 * pos.count<PAWN>() + pos.non_pawn_material();
|
|
v = (nnue * (73921 + material) + optimism * (8112 + material)) / 73260;
|
|
|
|
// Damp down the evaluation linearly when shuffling
|
|
v -= v * pos.rule50_count() / 212;
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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, const Eval::NNUE::Networks& networks) {
|
|
|
|
if (pos.checkers())
|
|
return "Final evaluation: none (in check)";
|
|
|
|
auto caches = std::make_unique<Eval::NNUE::AccumulatorCaches>(networks);
|
|
|
|
std::stringstream ss;
|
|
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2);
|
|
ss << '\n' << NNUE::trace(pos, networks, *caches) << '\n';
|
|
|
|
ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15);
|
|
|
|
auto [psqt, positional] = networks.big.evaluate(pos, &caches->big);
|
|
Value v = psqt + positional;
|
|
v = pos.side_to_move() == WHITE ? v : -v;
|
|
ss << "NNUE evaluation " << 0.01 * UCIEngine::to_cp(v, pos) << " (white side)\n";
|
|
|
|
v = evaluate(networks, pos, *caches, VALUE_ZERO);
|
|
v = pos.side_to_move() == WHITE ? v : -v;
|
|
ss << "Final evaluation " << 0.01 * UCIEngine::to_cp(v, pos) << " (white side)";
|
|
ss << " [with scaled NNUE, ...]";
|
|
ss << "\n";
|
|
|
|
return ss.str();
|
|
}
|
|
|
|
} // namespace Stockfish
|