]> git.sesse.net Git - stockfish/blob - src/material.h
Update default net to nn-c3ca321c51c9.nnue
[stockfish] / src / material.h
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 #ifndef MATERIAL_H_INCLUDED
20 #define MATERIAL_H_INCLUDED
21
22 #include "endgame.h"
23 #include "misc.h"
24 #include "position.h"
25 #include "types.h"
26
27 namespace Material {
28
29 /// Material::Entry contains various information about a material configuration.
30 /// It contains a material imbalance evaluation, a function pointer to a special
31 /// endgame evaluation function (which in most cases is NULL, meaning that the
32 /// standard evaluation function will be used), and scale factors.
33 ///
34 /// The scale factors are used to scale the evaluation score up or down. For
35 /// instance, in KRB vs KR endgames, the score is scaled down by a factor of 4,
36 /// which will result in scores of absolute value less than one pawn.
37
38 struct Entry {
39
40   Score imbalance() const { return make_score(value, value); }
41   Phase game_phase() const { return gamePhase; }
42   bool specialized_eval_exists() const { return evaluationFunction != nullptr; }
43   Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); }
44
45   // scale_factor() takes a position and a color as input and returns a scale factor
46   // for the given color. We have to provide the position in addition to the color
47   // because the scale factor may also be a function which should be applied to
48   // the position. For instance, in KBP vs K endgames, the scaling function looks
49   // for rook pawns and wrong-colored bishops.
50   ScaleFactor scale_factor(const Position& pos, Color c) const {
51     ScaleFactor sf = scalingFunction[c] ? (*scalingFunction[c])(pos)
52                                         :  SCALE_FACTOR_NONE;
53     return sf != SCALE_FACTOR_NONE ? sf : ScaleFactor(factor[c]);
54   }
55
56   Key key;
57   const EndgameBase<Value>* evaluationFunction;
58   const EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB]; // Could be one for each
59                                                              // side (e.g. KPKP, KBPsK)
60   int16_t value;
61   uint8_t factor[COLOR_NB];
62   Phase gamePhase;
63 };
64
65 typedef HashTable<Entry, 8192> Table;
66
67 Entry* probe(const Position& pos);
68
69 } // namespace Material
70
71 #endif // #ifndef MATERIAL_H_INCLUDED