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