]> git.sesse.net Git - stockfish/blob - src/material.h
Restore development versioning and LSN filtering
[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 Marco Costalba
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 "scale.h"
31
32
33 ////
34 //// Types
35 ////
36
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.
42 ///
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.
46
47 class MaterialInfo {
48
49   friend class MaterialInfoTable;
50
51 public:
52   Value mg_value() const;
53   Value eg_value() const;
54   ScaleFactor scale_factor(const Position& pos, Color c) const;
55   bool specialized_eval_exists() const;
56   Value evaluate(const Position& pos) const;
57
58 private:
59   void clear();
60   
61   Key key;
62   int16_t mgValue;
63   int16_t egValue;
64   uint8_t factor[2];
65   EndgameEvaluationFunction* evaluationFunction;
66   ScalingFunction* scalingFunction[2];
67 };
68
69
70 /// EndgameFunctions class stores the endgame evaluation functions std::map.
71 /// Because STL library is not thread safe even for read access, the maps,
72 /// although with identical content, are replicated for each thread. This
73 /// is faster then using locks with an unique set of global maps.
74
75 class EndgameFunctions;
76
77
78 /// The MaterialInfoTable class represents a pawn hash table. It is basically
79 /// just an array of MaterialInfo objects and a few methods for accessing these
80 /// objects. The most important method is get_material_info, which looks up a
81 /// position in the table and returns a pointer to a MaterialInfo object.
82
83 class MaterialInfoTable {
84
85 public:
86   MaterialInfoTable(unsigned numOfEntries);
87   ~MaterialInfoTable();
88   void clear();
89   MaterialInfo* get_material_info(const Position& pos);
90
91 private:
92   unsigned size;
93   MaterialInfo* entries;
94   EndgameFunctions* funcs;
95 };
96
97
98 ////
99 //// Inline functions
100 ////
101
102 /// MaterialInfo::mg_value and MaterialInfo::eg_value simply returns the
103 /// material balance evaluation for the middle game and the endgame.
104
105 inline Value MaterialInfo::mg_value() const {
106
107   return Value(mgValue);
108 }
109
110 inline Value MaterialInfo::eg_value() const {
111
112   return Value(egValue);
113 }
114
115
116 /// MaterialInfo::clear() resets a MaterialInfo object to an empty state,
117 /// with all slots at their default values.
118
119 inline void MaterialInfo::clear() {
120
121   mgValue = egValue = 0;
122   factor[WHITE] = factor[BLACK] = uint8_t(SCALE_FACTOR_NORMAL);
123   evaluationFunction = NULL;
124   scalingFunction[WHITE] = scalingFunction[BLACK] = NULL;
125 }
126
127
128 /// MaterialInfo::scale_factor takes a position and a color as input, and
129 /// returns a scale factor for the given color.  We have to provide the
130 /// position in addition to the color, because the scale factor need not
131 /// to be a constant: It can also be a function which should be applied to
132 /// the position. For instance, in KBP vs K endgames, a scaling function
133 /// which checks for draws with rook pawns and wrong-colored bishops.
134
135 inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
136
137   if (scalingFunction[c] != NULL)
138   {
139       ScaleFactor sf = scalingFunction[c]->apply(pos);
140       if (sf != SCALE_FACTOR_NONE)
141           return sf;
142   }
143   return ScaleFactor(factor[c]);
144 }
145
146
147 /// MaterialInfo::specialized_eval_exists decides whether there is a
148 /// specialized evaluation function for the current material configuration,
149 /// or if the normal evaluation function should be used.
150
151 inline bool MaterialInfo::specialized_eval_exists() const {
152
153   return evaluationFunction != NULL;
154 }
155
156
157 /// MaterialInfo::evaluate applies a specialized evaluation function
158 /// to a given position object.  It should only be called when
159 /// specialized_eval_exists() returns 'true'.
160
161 inline Value MaterialInfo::evaluate(const Position& pos) const {
162
163   return evaluationFunction->apply(pos);
164 }
165
166 #endif // !defined(MATERIAL_H_INCLUDED)