2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
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.
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.
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/>.
20 #include <cstring> // For std::memset
28 #define S(mg, eg) make_score(mg, eg)
30 // Polynomial material imbalance parameters
32 constexpr Score QuadraticOurs[][PIECE_TYPE_NB] = {
34 // pair pawn knight bishop rook queen
35 {S(1419, 1455) }, // Bishop pair
36 {S( 101, 28), S( 37, 39) }, // Pawn
37 {S( 57, 64), S(249, 187), S(-49, -62) }, // Knight OUR PIECES
38 {S( 0, 0), S(118, 137), S( 10, 27), S( 0, 0) }, // Bishop
39 {S( -63, -68), S( -5, 3), S(100, 81), S(132, 118), S(-246, -244) }, // Rook
40 {S(-210, -211), S( 37, 14), S(147, 141), S(161, 105), S(-158, -174), S(-9,-31) } // Queen
43 constexpr Score QuadraticTheirs[][PIECE_TYPE_NB] = {
45 // pair pawn knight bishop rook queen
47 {S( 33, 30) }, // Pawn
48 {S( 46, 18), S(106, 84) }, // Knight OUR PIECES
49 {S( 75, 35), S( 59, 44), S( 60, 15) }, // Bishop
50 {S( 26, 35), S( 6, 22), S( 38, 39), S(-12, -2) }, // Rook
51 {S( 97, 93), S(100, 163), S(-58, -91), S(112, 192), S(276, 225) } // Queen
56 // Endgame evaluation and scaling functions are accessed directly and not through
57 // the function maps because they correspond to more than one material hash key.
58 Endgame<KXK> EvaluateKXK[] = { Endgame<KXK>(WHITE), Endgame<KXK>(BLACK) };
60 Endgame<KBPsK> ScaleKBPsK[] = { Endgame<KBPsK>(WHITE), Endgame<KBPsK>(BLACK) };
61 Endgame<KQKRPs> ScaleKQKRPs[] = { Endgame<KQKRPs>(WHITE), Endgame<KQKRPs>(BLACK) };
62 Endgame<KPsK> ScaleKPsK[] = { Endgame<KPsK>(WHITE), Endgame<KPsK>(BLACK) };
63 Endgame<KPKP> ScaleKPKP[] = { Endgame<KPKP>(WHITE), Endgame<KPKP>(BLACK) };
65 // Helper used to detect a given material distribution
66 bool is_KXK(const Position& pos, Color us) {
67 return !more_than_one(pos.pieces(~us))
68 && pos.non_pawn_material(us) >= RookValueMg;
71 bool is_KBPsK(const Position& pos, Color us) {
72 return pos.non_pawn_material(us) == BishopValueMg
73 && pos.count<PAWN >(us) >= 1;
76 bool is_KQKRPs(const Position& pos, Color us) {
77 return !pos.count<PAWN>(us)
78 && pos.non_pawn_material(us) == QueenValueMg
79 && pos.count<ROOK>(~us) == 1
80 && pos.count<PAWN>(~us) >= 1;
84 /// imbalance() calculates the imbalance by comparing the piece count of each
85 /// piece type for both colors.
88 Score imbalance(const int pieceCount[][PIECE_TYPE_NB]) {
90 constexpr Color Them = ~Us;
92 Score bonus = SCORE_ZERO;
94 // Second-degree polynomial material imbalance, by Tord Romstad
95 for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
97 if (!pieceCount[Us][pt1])
100 int v = QuadraticOurs[pt1][pt1] * pieceCount[Us][pt1];
102 for (int pt2 = NO_PIECE_TYPE; pt2 < pt1; ++pt2)
103 v += QuadraticOurs[pt1][pt2] * pieceCount[Us][pt2]
104 + QuadraticTheirs[pt1][pt2] * pieceCount[Them][pt2];
106 bonus += pieceCount[Us][pt1] * v;
117 /// Material::probe() looks up the current position's material configuration in
118 /// the material hash table. It returns a pointer to the Entry if the position
119 /// is found. Otherwise a new Entry is computed and stored there, so we don't
120 /// have to recompute all when the same material configuration occurs again.
122 Entry* probe(const Position& pos) {
124 Key key = pos.material_key();
125 Entry* e = pos.this_thread()->materialTable[key];
130 std::memset(e, 0, sizeof(Entry));
132 e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
134 Value npm_w = pos.non_pawn_material(WHITE);
135 Value npm_b = pos.non_pawn_material(BLACK);
136 Value npm = std::clamp(npm_w + npm_b, EndgameLimit, MidgameLimit);
138 // Map total non-pawn material into [PHASE_ENDGAME, PHASE_MIDGAME]
139 e->gamePhase = Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit));
141 // Let's look if we have a specialized evaluation function for this particular
142 // material configuration. Firstly we look for a fixed configuration one, then
143 // for a generic one if the previous search failed.
144 if ((e->evaluationFunction = Endgames::probe<Value>(key)) != nullptr)
147 for (Color c : { WHITE, BLACK })
150 e->evaluationFunction = &EvaluateKXK[c];
154 // OK, we didn't find any special evaluation function for the current material
155 // configuration. Is there a suitable specialized scaling function?
156 const auto* sf = Endgames::probe<ScaleFactor>(key);
160 e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned
164 // We didn't find any specialized scaling function, so fall back on generic
165 // ones that refer to more than one material distribution. Note that in this
166 // case we don't return after setting the function.
167 for (Color c : { WHITE, BLACK })
169 if (is_KBPsK(pos, c))
170 e->scalingFunction[c] = &ScaleKBPsK[c];
172 else if (is_KQKRPs(pos, c))
173 e->scalingFunction[c] = &ScaleKQKRPs[c];
176 if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN)) // Only pawns on the board
178 if (!pos.count<PAWN>(BLACK))
180 assert(pos.count<PAWN>(WHITE) >= 2);
182 e->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
184 else if (!pos.count<PAWN>(WHITE))
186 assert(pos.count<PAWN>(BLACK) >= 2);
188 e->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
190 else if (pos.count<PAWN>(WHITE) == 1 && pos.count<PAWN>(BLACK) == 1)
192 // This is a special case because we set scaling functions
193 // for both colors instead of only one.
194 e->scalingFunction[WHITE] = &ScaleKPKP[WHITE];
195 e->scalingFunction[BLACK] = &ScaleKPKP[BLACK];
199 // Zero or just one pawn makes it difficult to win, even with a small material
200 // advantage. This catches some trivial draws like KK, KBK and KNK and gives a
201 // drawish scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
202 if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
203 e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW :
204 npm_b <= BishopValueMg ? 4 : 14);
206 if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
207 e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
208 npm_w <= BishopValueMg ? 4 : 14);
210 // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
211 // for the bishop pair "extended piece", which allows us to be more flexible
212 // in defining bishop pair bonuses.
213 const int pieceCount[COLOR_NB][PIECE_TYPE_NB] = {
214 { pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
215 pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
216 { pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
217 pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
219 e->score = (imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16;
223 } // namespace Material