[go: up one dir, main page]

Menu

[r115]: / evaluation.h  Maximize  Restore  History

Download this file

364 lines (305 with data), 10.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
Protector -- a UCI chess engine
Copyright (C) 2009-2010 Raimund Heid (Raimund_Heid@yahoo.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _evaluation_h_
#define _evaluation_h_
#include "position.h"
#include "bitboard.h"
#include "keytable.h"
#define WHITE_BISHOP_LIGHT 0x01
#define WHITE_BISHOP_DARK 0x02
#define BLACK_BISHOP_LIGHT 0x04
#define BLACK_BISHOP_DARK 0x08
#define WHITE_BISHOP_PAIR 0x03
#define BLACK_BISHOP_PAIR 0x0C
typedef struct
{
Bitboard upwardRealm[2];
Bitboard downwardRealm[2];
Bitboard pawnAttackableSquares[2];
Bitboard pawnProtectedSquares[2];
Bitboard doubledPawns[2];
Bitboard passedPawns[2];
Bitboard candidatePawns[2];
#ifdef BONUS_TARGETS
Bitboard openingTargets[2];
Bitboard endgameTargets[2];
#endif
#ifdef BONUS_HIDDEN_PASSER
Bitboard hiddenCandidatePawns[2];
bool hasPassersOrCandidates[2];
#endif
#ifdef MALUS_SLIGHTLY_BACKWARD
Bitboard slightlyBackward[2];
#endif
Bitboard weakPawns[2];
Bitboard fixedPawns[2];
Bitboard countedSquares[2];
Bitboard unprotectedPieces[2];
Bitboard weakOutpostSquares[2];
KingAttacks *kingAttacks[2];
Bitboard kingAttackSquares[2];
#ifdef CALCULATE_TARGETS
Bitboard pieceAttacks[2];
#endif
int numAttackers[2];
int attackPoints[2];
int spaceAttackPoints[2];
int openingPoints[2], endgamePoints[2];
bool evaluateKingSafety[2];
KingSafetyHashInfo *kingsafetyHashtable;
}
EvaluationBase;
extern const int PHASE_MAX;
/**
* Calculate the weight of the non-pawn-pieces of the specified color.
*/
INLINE int getMaterialValue(const Position * position, const Color color)
{
const int VALUE_SMALL_PIECE = 300;
const int ROOK_DIFF = 500 - VALUE_SMALL_PIECE;
const int QUEEN_DIFF = 950 - VALUE_SMALL_PIECE;
const int numNonPawnPieces = numberOfNonPawnPieces(position, color) - 1;
const int numRooks = getPieceCount(position, (Piece) (ROOK | color));
const int numQueens = getPieceCount(position, (Piece) (QUEEN | color));
return numQueens * QUEEN_DIFF + numRooks * ROOK_DIFF +
numNonPawnPieces * VALUE_SMALL_PIECE +
position->numberOfPawns[color] * 100;
}
/**
* Calculate the weight of the non-pawn-pieces of the specified color.
*/
INLINE int getPieceValue(const Position * position, const Color color)
{
const int VALUE_SMALL_PIECE = 300;
const int ROOK_DIFF = 500 - VALUE_SMALL_PIECE;
const int QUEEN_DIFF = 950 - VALUE_SMALL_PIECE;
const int numNonPawnPieces = numberOfNonPawnPieces(position, color) - 1;
const int numRooks = getPieceCount(position, (Piece) (ROOK | color));
const int numQueens = getPieceCount(position, (Piece) (QUEEN | color));
return numQueens * QUEEN_DIFF + numRooks * ROOK_DIFF +
numNonPawnPieces * VALUE_SMALL_PIECE;
}
/**
* Calculate the weight of the non-pawn-pieces of the specified color.
*
* @return a value in the range [0-44]
*/
INLINE int getPieceWeight(const Position * position, const Color color)
{
const int numNonPawnPieces = numberOfNonPawnPieces(position, color) - 1;
const int numRooks = getPieceCount(position, (Piece) (ROOK | color));
const int numQueens = getPieceCount(position, (Piece) (QUEEN | color));
return 3 * numQueens + numRooks + numNonPawnPieces;
}
/**
* Calculate the value of the non-pawn-pieces of the specified color.
*
* @return a centipawn value
*/
INLINE int getOpeningPieceWeight(const Position * position, const Color color)
{
return position->openingValue[color] -
PAWN_VALUE_OPENING * position->numberOfPawns[color];
}
/**
* Calculate the phase index of the specified position.
*
* @return a value in the range [0(initial position)-256(endgame)]
*/
INLINE int phaseIndex(const Position * position)
{
const int basicPhase = max(0, PHASE_MAX -
getPieceWeight(position, WHITE) -
getPieceWeight(position, BLACK));
assert(getPieceWeight(position, WHITE) >= 0);
assert(getPieceWeight(position, WHITE) <= 44);
assert(getPieceWeight(position, BLACK) >= 0);
assert(getPieceWeight(position, BLACK) <= 44);
assert(basicPhase >= 0);
assert(basicPhase <= PHASE_MAX);
return (basicPhase * 256 + (PHASE_MAX / 2)) / PHASE_MAX;
}
INLINE bool hasBishopPair(const Position * position, const Color color)
{
const Bitboard *bishops =
&position->piecesOfType[(Piece) (BISHOP | color)];
return (bool) ((lightSquares & *bishops) != EMPTY_BITBOARD &&
(darkSquares & *bishops) != EMPTY_BITBOARD);
}
#define VALUE_TEMPO_OPENING 20
#define VALUE_TEMPO_ENDGAME 10
/**
* Calculate a rough value of the specified position,
* based on the current pst-values and the specified evaluation base.
*
* @return the value of the specified position
*/
INLINE int positionalBalance(const Position * position, EvaluationBase * base)
{
int openingValue =
(base->openingPoints[WHITE] - base->openingPoints[BLACK] +
position->openingValue[WHITE] - position->openingValue[BLACK]);
int endgameValue =
(base->endgamePoints[WHITE] - base->endgamePoints[BLACK] +
position->endgameValue[WHITE] - position->endgameValue[BLACK]);
const int pi = phaseIndex(position);
int value;
assert(pi >= 0 && pi <= 256);
if (hasBishopPair(position, WHITE))
{
openingValue += VALUE_BISHOP_PAIR_OPENING;
endgameValue += VALUE_BISHOP_PAIR_ENDGAME;
}
if (hasBishopPair(position, BLACK))
{
openingValue -= VALUE_BISHOP_PAIR_OPENING;
endgameValue -= VALUE_BISHOP_PAIR_ENDGAME;
}
if (position->activeColor == WHITE)
{
openingValue += VALUE_TEMPO_OPENING;
endgameValue += VALUE_TEMPO_ENDGAME;
}
else
{
openingValue -= VALUE_TEMPO_OPENING;
endgameValue -= VALUE_TEMPO_ENDGAME;
}
value = (openingValue * (256 - pi) + endgameValue * pi) / 256;
return (position->activeColor == WHITE ? value : -value);
}
/**
* Calculate a rough value of the specified position,
* based on the current pst-values.
*
* @return the value of the specified position
*/
INLINE int basicPositionalBalance(Position * position)
{
int openingValue =
(position->openingValue[WHITE] - position->openingValue[BLACK]);
int endgameValue =
(position->endgameValue[WHITE] - position->endgameValue[BLACK]);
const int pi = phaseIndex(position);
int value;
assert(pi >= 0 && pi <= 256);
if (hasBishopPair(position, WHITE))
{
openingValue += VALUE_BISHOP_PAIR_OPENING;
endgameValue += VALUE_BISHOP_PAIR_ENDGAME;
}
if (hasBishopPair(position, BLACK))
{
openingValue -= VALUE_BISHOP_PAIR_OPENING;
endgameValue -= VALUE_BISHOP_PAIR_ENDGAME;
}
if (position->activeColor == WHITE)
{
openingValue += VALUE_TEMPO_OPENING;
endgameValue += VALUE_TEMPO_ENDGAME;
}
else
{
openingValue -= VALUE_TEMPO_OPENING;
endgameValue -= VALUE_TEMPO_ENDGAME;
}
value = (openingValue * (256 - pi) + endgameValue * pi) / 256;
return (position->activeColor == WHITE ? value : -value);
}
/**
* Calculate the value of the specified position.
*
* @return the value of the specified position
*/
int getValue(Position * position, const int alpha, const int beta,
PawnHashInfo * pawnHashtable,
KingSafetyHashInfo * kingsafetyHashtable);
/**
* Check if the specified color can win the specified position.
*
* @return FALSE if the specified color doesn't have sufficient material
* left to win the position
*/
INLINE bool hasWinningPotential(Position * position, Color color)
{
return (bool) (position->numberOfPieces[color] > 1);
}
/**
* Check if the pawn at the specified square is a passed pawn.
*/
bool pawnIsPassed(const Position * position, const Square pawnSquare,
const Color pawnColor);
/**
* Reset the pawn hashtable.
*/
void resetPawnHashtable(void);
/**
* Flip the given position and check if it yields the same result.
*
* @return FALSE if the flipped position yields a diffent result
*/
bool flipTest(Position * position,
PawnHashInfo * pawnHashtable,
KingSafetyHashInfo * kingsafetyHashtable);
/**
* Initialize this module.
*
* @return 0 if no errors occurred.
*/
int initializeModuleEvaluation(void);
/**
* Test this module.
*
* @return 0 if all tests succeed.
*/
int testModuleEvaluation(void);
/**
* Get the king safety hash value for the given king square.
*/
INLINE Bitboard getKingPawnSafetyHashValue(const Position * position,
const Color color)
{
const int mask[2] =
{ WHITE_00 | WHITE_000 | 16, BLACK_00 | BLACK_000 | 32 };
const int index = (position->castlingRights | 48) & mask[color];
return position->pawnHashValue ^
GENERATED_KEYTABLE[color][position->king[color]] ^
GENERATED_KEYTABLE[2][index];
}
INLINE bool piecesAreBalanced(const Position * position)
{
return (bool) (getPieceCount(position, WHITE_QUEEN) ==
getPieceCount(position, BLACK_QUEEN) &&
getPieceCount(position, WHITE_ROOK) ==
getPieceCount(position, BLACK_ROOK) &&
getPieceCount(position, WHITE_BISHOP) ==
getPieceCount(position, BLACK_BISHOP) &&
getPieceCount(position, WHITE_KNIGHT) ==
getPieceCount(position, BLACK_KNIGHT));
}
INLINE int getPawnWidth(const Position * position, const Color color)
{
const Bitboard tmp = position->piecesOfType[(Piece) (PAWN | color)] |
minValue[position->king[opponent(color)]];
return getWidth(tmp);
}
INLINE int getPassedPawnWidth(const Position * position,
const EvaluationBase * base, const Color color)
{
const Bitboard tmp = base->passedPawns[color] |
minValue[position->king[opponent(color)]];
return getWidth(tmp);
}
#endif