]> git.sesse.net Git - stockfish/blob - src/material.h
Increase MaterialTableSize 8 times
[stockfish] / src / material.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #if !defined(MATERIAL_H_INCLUDED)
21 #define MATERIAL_H_INCLUDED
22
23 #include "endgame.h"
24 #include "position.h"
25 #include "tt.h"
26 #include "types.h"
27
28 const int MaterialTableSize = 8192;
29
30 /// MaterialInfo is a class which contains various information about a
31 /// material configuration. It contains a material balance evaluation,
32 /// a function pointer to a special endgame evaluation function (which in
33 /// most cases is NULL, meaning that the standard evaluation function will
34 /// be used), and "scale factors" for black and white.
35 ///
36 /// The scale factors are used to scale the evaluation score up or down.
37 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
38 /// of 4, which will result in scores of absolute value less than one pawn.
39
40 class MaterialInfo {
41
42   friend class MaterialInfoTable;
43
44 public:
45   Score material_value() const;
46   ScaleFactor scale_factor(const Position& pos, Color c) const;
47   int space_weight() const;
48   Phase game_phase() const;
49   bool specialized_eval_exists() const;
50   Value evaluate(const Position& pos) const;
51
52 private:
53   Key key;
54   int16_t value;
55   uint8_t factor[2];
56   EndgameEvaluationFunctionBase* evaluationFunction;
57   EndgameScalingFunctionBase* scalingFunction[2];
58   int spaceWeight;
59   Phase gamePhase;
60 };
61
62
63 /// The MaterialInfoTable class represents a pawn hash table. The most important
64 /// method is get_material_info, which returns a pointer to a MaterialInfo object.
65 class EndgameFunctions;
66
67 class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> {
68 public:
69   MaterialInfoTable();
70   ~MaterialInfoTable();
71   MaterialInfo* get_material_info(const Position& pos) const;
72   static Phase game_phase(const Position& pos);
73
74 private:
75   template<Color Us>
76   static int imbalance(const int pieceCount[][8]);
77
78   EndgameFunctions* funcs;
79 };
80
81
82 /// MaterialInfo::scale_factor takes a position and a color as input, and
83 /// returns a scale factor for the given color. We have to provide the
84 /// position in addition to the color, because the scale factor need not
85 /// to be a constant: It can also be a function which should be applied to
86 /// the position. For instance, in KBP vs K endgames, a scaling function
87 /// which checks for draws with rook pawns and wrong-colored bishops.
88
89 inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
90
91   if (!scalingFunction[c])
92       return ScaleFactor(factor[c]);
93
94   ScaleFactor sf = scalingFunction[c]->apply(pos);
95   return sf == SCALE_FACTOR_NONE ? ScaleFactor(factor[c]) : sf;
96 }
97
98 inline Score MaterialInfo::material_value() const {
99   return make_score(value, value);
100 }
101
102 inline int MaterialInfo::space_weight() const {
103   return spaceWeight;
104 }
105
106 inline Phase MaterialInfo::game_phase() const {
107   return gamePhase;
108 }
109
110 inline bool MaterialInfo::specialized_eval_exists() const {
111   return evaluationFunction != NULL;
112 }
113
114 inline Value MaterialInfo::evaluate(const Position& pos) const {
115   return evaluationFunction->apply(pos);
116 }
117
118 #endif // !defined(MATERIAL_H_INCLUDED)