Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/evaluate.cpp
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)
4
5
Stockfish is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Stockfish is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include "evaluate.h"
20
21
#include <algorithm>
22
#include <cassert>
23
#include <cmath>
24
#include <cstdlib>
25
#include <iomanip>
26
#include <iostream>
27
#include <memory>
28
#include <sstream>
29
#include <tuple>
30
31
#include "nnue/network.h"
32
#include "nnue/nnue_misc.h"
33
#include "position.h"
34
#include "types.h"
35
#include "uci.h"
36
#include "nnue/nnue_accumulator.h"
37
38
namespace Stockfish {
39
40
// Returns a static, purely materialistic evaluation of the position from
41
// the point of view of the side to move. It can be divided by PawnValue to get
42
// an approximation of the material advantage on the board in terms of pawns.
43
int Eval::simple_eval(const Position& pos) {
44
Color c = pos.side_to_move();
45
return PawnValue * (pos.count<PAWN>(c) - pos.count<PAWN>(~c))
46
+ (pos.non_pawn_material(c) - pos.non_pawn_material(~c));
47
}
48
49
bool Eval::use_smallnet(const Position& pos) { return std::abs(simple_eval(pos)) > 962; }
50
51
// Evaluate is the evaluator for the outer world. It returns a static evaluation
52
// of the position from the point of view of the side to move.
53
Value Eval::evaluate(const Eval::NNUE::Networks& networks,
54
const Position& pos,
55
Eval::NNUE::AccumulatorStack& accumulators,
56
Eval::NNUE::AccumulatorCaches& caches,
57
int optimism) {
58
59
assert(!pos.checkers());
60
61
bool smallNet = use_smallnet(pos);
62
auto [psqt, positional] = smallNet ? networks.small.evaluate(pos, accumulators, &caches.small)
63
: networks.big.evaluate(pos, accumulators, &caches.big);
64
65
Value nnue = (125 * psqt + 131 * positional) / 128;
66
67
// Re-evaluate the position when higher eval accuracy is worth the time spent
68
if (smallNet && (std::abs(nnue) < 236))
69
{
70
std::tie(psqt, positional) = networks.big.evaluate(pos, accumulators, &caches.big);
71
nnue = (125 * psqt + 131 * positional) / 128;
72
smallNet = false;
73
}
74
75
// Blend optimism and eval with nnue complexity
76
int nnueComplexity = std::abs(psqt - positional);
77
optimism += optimism * nnueComplexity / 468;
78
nnue -= nnue * nnueComplexity / 18000;
79
80
int material = 535 * pos.count<PAWN>() + pos.non_pawn_material();
81
int v = (nnue * (77777 + material) + optimism * (7777 + material)) / 77777;
82
83
// Damp down the evaluation linearly when shuffling
84
v -= v * pos.rule50_count() / 212;
85
86
// Guarantee evaluation does not hit the tablebase range
87
v = std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
88
89
return v;
90
}
91
92
// Like evaluate(), but instead of returning a value, it returns
93
// a string (suitable for outputting to stdout) that contains the detailed
94
// descriptions and values of each evaluation term. Useful for debugging.
95
// Trace scores are from white's point of view
96
std::string Eval::trace(Position& pos, const Eval::NNUE::Networks& networks) {
97
98
if (pos.checkers())
99
return "Final evaluation: none (in check)";
100
101
Eval::NNUE::AccumulatorStack accumulators;
102
auto caches = std::make_unique<Eval::NNUE::AccumulatorCaches>(networks);
103
104
std::stringstream ss;
105
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2);
106
ss << '\n' << NNUE::trace(pos, networks, *caches) << '\n';
107
108
ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15);
109
110
auto [psqt, positional] = networks.big.evaluate(pos, accumulators, &caches->big);
111
Value v = psqt + positional;
112
v = pos.side_to_move() == WHITE ? v : -v;
113
ss << "NNUE evaluation " << 0.01 * UCIEngine::to_cp(v, pos) << " (white side)\n";
114
115
v = evaluate(networks, pos, accumulators, *caches, VALUE_ZERO);
116
v = pos.side_to_move() == WHITE ? v : -v;
117
ss << "Final evaluation " << 0.01 * UCIEngine::to_cp(v, pos) << " (white side)";
118
ss << " [with scaled NNUE, ...]";
119
ss << "\n";
120
121
return ss.str();
122
}
123
124
} // namespace Stockfish
125
126