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