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-2009 Marco Costalba
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.
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.
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/>.
21 #if !defined(MATERIAL_H_INCLUDED)
22 #define MATERIAL_H_INCLUDED
37 /// MaterialInfo is a class which contains various information about a
38 /// material configuration. It contains a material balance evaluation,
39 /// a function pointer to a special endgame evaluation function (which in
40 /// most cases is NULL, meaning that the standard evaluation function will
41 /// be used), and "scale factors" for black and white.
43 /// The scale factors are used to scale the evaluation score up or down.
44 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
45 /// of 4, which will result in scores of absolute value less than one pawn.
49 friend class MaterialInfoTable;
52 MaterialInfo() : key(0) { clear(); }
54 Value mg_value() const;
55 Value eg_value() const;
56 ScaleFactor scale_factor(const Position& pos, Color c) const;
57 int space_weight() const;
58 bool specialized_eval_exists() const;
59 Value evaluate(const Position& pos) const;
68 EndgameEvaluationFunctionBase* evaluationFunction;
69 EndgameScalingFunctionBase* scalingFunction[2];
74 /// EndgameFunctions class stores the endgame evaluation functions std::map.
75 /// Because STL library is not thread safe even for read access, the maps,
76 /// although with identical content, are replicated for each thread. This
77 /// is faster then using locks with an unique set of global maps.
79 class EndgameFunctions;
82 /// The MaterialInfoTable class represents a pawn hash table. It is basically
83 /// just an array of MaterialInfo objects and a few methods for accessing these
84 /// objects. The most important method is get_material_info, which looks up a
85 /// position in the table and returns a pointer to a MaterialInfo object.
87 class MaterialInfoTable {
90 MaterialInfoTable(unsigned numOfEntries);
92 MaterialInfo* get_material_info(const Position& pos);
96 MaterialInfo* entries;
97 EndgameFunctions* funcs;
102 //// Inline functions
105 /// MaterialInfo::mg_value and MaterialInfo::eg_value simply returns the
106 /// material balance evaluation for the middle game and the endgame.
108 inline Value MaterialInfo::mg_value() const {
110 return Value(mgValue);
113 inline Value MaterialInfo::eg_value() const {
115 return Value(egValue);
119 /// MaterialInfo::clear() resets a MaterialInfo object to an empty state,
120 /// with all slots at their default values but the key.
122 inline void MaterialInfo::clear() {
124 mgValue = egValue = 0;
125 factor[WHITE] = factor[BLACK] = uint8_t(SCALE_FACTOR_NORMAL);
126 evaluationFunction = NULL;
127 scalingFunction[WHITE] = scalingFunction[BLACK] = NULL;
132 /// MaterialInfo::scale_factor takes a position and a color as input, and
133 /// returns a scale factor for the given color. We have to provide the
134 /// position in addition to the color, because the scale factor need not
135 /// to be a constant: It can also be a function which should be applied to
136 /// the position. For instance, in KBP vs K endgames, a scaling function
137 /// which checks for draws with rook pawns and wrong-colored bishops.
139 inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
141 if (scalingFunction[c] != NULL)
143 ScaleFactor sf = scalingFunction[c]->apply(pos);
144 if (sf != SCALE_FACTOR_NONE)
147 return ScaleFactor(factor[c]);
151 /// MaterialInfo::space_weight() simply returns the weight for the space
152 /// evaluation for this material configuration.
154 inline int MaterialInfo::space_weight() const {
160 /// MaterialInfo::specialized_eval_exists decides whether there is a
161 /// specialized evaluation function for the current material configuration,
162 /// or if the normal evaluation function should be used.
164 inline bool MaterialInfo::specialized_eval_exists() const {
166 return evaluationFunction != NULL;
170 /// MaterialInfo::evaluate applies a specialized evaluation function
171 /// to a given position object. It should only be called when
172 /// specialized_eval_exists() returns 'true'.
174 inline Value MaterialInfo::evaluate(const Position& pos) const {
176 return evaluationFunction->apply(pos);
179 #endif // !defined(MATERIAL_H_INCLUDED)