]> git.sesse.net Git - stockfish/blob - src/material.h
Fix very theoretical History corner case
[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
21 #if !defined(MATERIAL_H_INCLUDED)
22 #define MATERIAL_H_INCLUDED
23
24 ////
25 //// Includes
26 ////
27
28 #include "endgame.h"
29 #include "position.h"
30 #include "tt.h"
31
32
33 ////
34 //// Types
35 ////
36
37 const int MaterialTableSize = 1024;
38
39 /// MaterialInfo is a class which contains various information about a
40 /// material configuration. It contains a material balance evaluation,
41 /// a function pointer to a special endgame evaluation function (which in
42 /// most cases is NULL, meaning that the standard evaluation function will
43 /// be used), and "scale factors" for black and white.
44 ///
45 /// The scale factors are used to scale the evaluation score up or down.
46 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
47 /// of 4, which will result in scores of absolute value less than one pawn.
48
49 class MaterialInfo {
50
51   friend class MaterialInfoTable;
52
53 public:
54   Score material_value() const;
55   ScaleFactor scale_factor(const Position& pos, Color c) const;
56   int space_weight() const;
57   Phase game_phase() const;
58   bool specialized_eval_exists() const;
59   Value evaluate(const Position& pos) const;
60
61 private:
62   Key key;
63   int16_t value;
64   uint8_t factor[2];
65   EndgameEvaluationFunctionBase* evaluationFunction;
66   EndgameScalingFunctionBase* scalingFunction[2];
67   int spaceWeight;
68   Phase gamePhase;
69 };
70
71 /// The MaterialInfoTable class represents a pawn hash table. The most important
72 /// method is get_material_info, which returns a pointer to a MaterialInfo object.
73 class EndgameFunctions;
74
75 class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> {
76 public:
77   MaterialInfoTable();
78   ~MaterialInfoTable();
79   MaterialInfo* get_material_info(const Position& pos);
80   static Phase game_phase(const Position& pos);
81 private:
82   EndgameFunctions* funcs;
83 };
84
85
86 ////
87 //// Inline functions
88 ////
89
90
91 /// MaterialInfo::material_value simply returns the material balance
92 /// evaluation that is independent from game phase.
93
94 inline Score MaterialInfo::material_value() const {
95
96   return make_score(value, value);
97 }
98
99 /// MaterialInfo::scale_factor takes a position and a color as input, and
100 /// returns a scale factor for the given color. We have to provide the
101 /// position in addition to the color, because the scale factor need not
102 /// to be a constant: It can also be a function which should be applied to
103 /// the position. For instance, in KBP vs K endgames, a scaling function
104 /// which checks for draws with rook pawns and wrong-colored bishops.
105
106 inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
107
108   if (scalingFunction[c] != NULL)
109   {
110       ScaleFactor sf = scalingFunction[c]->apply(pos);
111       if (sf != SCALE_FACTOR_NONE)
112           return sf;
113   }
114   return ScaleFactor(factor[c]);
115 }
116
117
118 /// MaterialInfo::space_weight() simply returns the weight for the space
119 /// evaluation for this material configuration.
120
121 inline int MaterialInfo::space_weight() const {
122
123   return spaceWeight;
124 }
125
126 /// MaterialInfo::game_phase() returns the game phase according
127 /// to this material configuration.
128
129 inline Phase MaterialInfo::game_phase() const {
130
131   return gamePhase;
132 }
133
134
135 /// MaterialInfo::specialized_eval_exists decides whether there is a
136 /// specialized evaluation function for the current material configuration,
137 /// or if the normal evaluation function should be used.
138
139 inline bool MaterialInfo::specialized_eval_exists() const {
140
141   return evaluationFunction != NULL;
142 }
143
144
145 /// MaterialInfo::evaluate applies a specialized evaluation function
146 /// to a given position object. It should only be called when
147 /// specialized_eval_exists() returns 'true'.
148
149 inline Value MaterialInfo::evaluate(const Position& pos) const {
150
151   return evaluationFunction->apply(pos);
152 }
153
154 #endif // !defined(MATERIAL_H_INCLUDED)