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-2013 Marco Costalba, Joona Kiiski, Tord Romstad
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/>.
28 #include "ucioption.h"
34 // Evaluation grain size, must be a power of 2
35 const int GrainSize = 8;
37 // Evaluation weights, initialized from UCI options
38 enum { Mobility, PassedPawns, Space, KingDangerUs, KingDangerThem };
42 #define S(mg, eg) make_score(mg, eg)
44 // Internal evaluation weights. These are applied on top of the evaluation
45 // weights read from UCI parameters. The purpose is to be able to change
46 // the evaluation weights while keeping the default values of the UCI
47 // parameters at 100, which looks prettier.
49 // Values modified by Joona Kiiski
50 const Score WeightsInternal[] = {
51 S(289, 344), S(221, 273), S(46, 0), S(271, 0), S(307, 0)
54 // MobilityBonus[PieceType][attacked] contains mobility bonuses for middle and
55 // end game, indexed by piece type and number of attacked squares not occupied
56 // by friendly pieces.
57 const Score MobilityBonus[][32] = {
59 { S(-38,-33), S(-25,-23), S(-12,-13), S( 0, -3), S(12, 7), S(25, 17), // Knights
60 S( 31, 22), S( 38, 27), S( 38, 27) },
61 { S(-25,-30), S(-11,-16), S( 3, -2), S(17, 12), S(31, 26), S(45, 40), // Bishops
62 S( 57, 52), S( 65, 60), S( 71, 65), S(74, 69), S(76, 71), S(78, 73),
63 S( 79, 74), S( 80, 75), S( 81, 76), S(81, 76) },
64 { S(-20,-36), S(-14,-19), S( -8, -3), S(-2, 13), S( 4, 29), S(10, 46), // Rooks
65 S( 14, 62), S( 19, 79), S( 23, 95), S(26,106), S(27,111), S(28,114),
66 S( 29,116), S( 30,117), S( 31,118), S(32,118) },
67 { S(-10,-18), S( -8,-13), S( -6, -7), S(-3, -2), S(-1, 3), S( 1, 8), // Queens
68 S( 3, 13), S( 5, 19), S( 8, 23), S(10, 27), S(12, 32), S(15, 34),
69 S( 16, 35), S( 17, 35), S( 18, 35), S(20, 35), S(20, 35), S(20, 35),
70 S( 20, 35), S( 20, 35), S( 20, 35), S(20, 35), S(20, 35), S(20, 35),
71 S( 20, 35), S( 20, 35), S( 20, 35), S(20, 35), S(20, 35), S(20, 35),
72 S( 20, 35), S( 20, 35) }
75 // OutpostBonus[PieceType][Square] contains outpost bonuses of knights and
76 // bishops, indexed by piece type and square (from white's point of view).
77 const Value OutpostBonus[][SQUARE_NB] = {
80 V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // Knights
81 V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0),
82 V(0), V(0), V(4), V(8), V(8), V(4), V(0), V(0),
83 V(0), V(4),V(17),V(26),V(26),V(17), V(4), V(0),
84 V(0), V(8),V(26),V(35),V(35),V(26), V(8), V(0),
85 V(0), V(4),V(17),V(17),V(17),V(17), V(4), V(0) },
87 V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // Bishops
88 V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0),
89 V(0), V(0), V(5), V(5), V(5), V(5), V(0), V(0),
90 V(0), V(5),V(10),V(10),V(10),V(10), V(5), V(0),
91 V(0),V(10),V(21),V(21),V(21),V(21),V(10), V(0),
92 V(0), V(5), V(8), V(8), V(8), V(8), V(5), V(0) }
95 // ThreatBonus[attacking][attacked] contains threat bonuses according to
96 // which piece type attacks which one.
97 const Score ThreatBonus[][PIECE_TYPE_NB] = {
99 { S(0, 0), S( 7, 39), S( 0, 0), S(24, 49), S(41,100), S(41,100) }, // KNIGHT
100 { S(0, 0), S( 7, 39), S(24, 49), S( 0, 0), S(41,100), S(41,100) }, // BISHOP
101 { S(0, 0), S( 0, 22), S(15, 49), S(15, 49), S( 0, 0), S(24, 49) }, // ROOK
102 { S(0, 0), S(15, 39), S(15, 39), S(15, 39), S(15, 39), S( 0, 0) } // QUEEN
105 // ThreatenedByPawnPenalty[PieceType] contains a penalty according to which
106 // piece type is attacked by an enemy pawn.
107 const Score ThreatenedByPawnPenalty[] = {
108 S(0, 0), S(0, 0), S(56, 70), S(56, 70), S(76, 99), S(86, 118)
113 const Score BishopPinBonus = make_score(66, 11);
115 // Bonus for having the side to move (modified by Joona Kiiski)
116 const Score Tempo = make_score(24, 11);
118 // Rooks and queens on the 7th rank
119 const Score RookOn7thBonus = make_score(11, 20);
120 const Score QueenOn7thBonus = make_score( 3, 8);
122 // Rooks and queens attacking pawns on the same rank
123 const Score RookOnPawnBonus = make_score(10, 28);
124 const Score QueenOnPawnBonus = make_score( 4, 20);
126 // Rooks on open files (modified by Joona Kiiski)
127 const Score RookOpenFileBonus = make_score(43, 21);
128 const Score RookHalfOpenFileBonus = make_score(19, 10);
130 // Penalty for rooks trapped inside a friendly king which has lost the
132 const Value TrappedRookPenalty = Value(180);
134 // Penalty for bishop with pawns on the same coloured squares
135 const Score BishopPawnsPenalty = make_score(8, 12);
137 // Penalty for a bishop on a1/h1 (a8/h8 for black) which is trapped by
138 // a friendly pawn on b2/g2 (b7/g7 for black). This can obviously only
139 // happen in Chess960 games.
140 const Score TrappedBishopA1H1Penalty = make_score(100, 100);
142 // Penalty for an undefended bishop or knight
143 const Score UndefendedMinorPenalty = make_score(25, 10);
145 // The SpaceMask[Color] contains the area of the board which is considered
146 // by the space evaluation. In the middle game, each side is given a bonus
147 // based on how many squares inside this area are safe and available for
148 // friendly minor pieces.
149 const Bitboard SpaceMask[] = {
150 (1ULL << SQ_C2) | (1ULL << SQ_D2) | (1ULL << SQ_E2) | (1ULL << SQ_F2) |
151 (1ULL << SQ_C3) | (1ULL << SQ_D3) | (1ULL << SQ_E3) | (1ULL << SQ_F3) |
152 (1ULL << SQ_C4) | (1ULL << SQ_D4) | (1ULL << SQ_E4) | (1ULL << SQ_F4),
153 (1ULL << SQ_C7) | (1ULL << SQ_D7) | (1ULL << SQ_E7) | (1ULL << SQ_F7) |
154 (1ULL << SQ_C6) | (1ULL << SQ_D6) | (1ULL << SQ_E6) | (1ULL << SQ_F6) |
155 (1ULL << SQ_C5) | (1ULL << SQ_D5) | (1ULL << SQ_E5) | (1ULL << SQ_F5)
158 // King danger constants and variables. The king danger scores are taken
159 // from the KingDangerTable[]. Various little "meta-bonuses" measuring
160 // the strength of the enemy attack are added up into an integer, which
161 // is used as an index to KingDangerTable[].
163 // KingAttackWeights[PieceType] contains king attack weights by piece type
164 const int KingAttackWeights[] = { 0, 0, 2, 2, 3, 5 };
166 // Bonuses for enemy's safe checks
167 const int QueenContactCheckBonus = 6;
168 const int RookContactCheckBonus = 4;
169 const int QueenCheckBonus = 3;
170 const int RookCheckBonus = 2;
171 const int BishopCheckBonus = 1;
172 const int KnightCheckBonus = 1;
174 // InitKingDanger[Square] contains penalties based on the position of the
175 // defending king, indexed by king's square (from white's point of view).
176 const int InitKingDanger[] = {
177 2, 0, 2, 5, 5, 2, 0, 2,
178 2, 2, 4, 8, 8, 4, 2, 2,
179 7, 10, 12, 12, 12, 12, 10, 7,
180 15, 15, 15, 15, 15, 15, 15, 15,
181 15, 15, 15, 15, 15, 15, 15, 15,
182 15, 15, 15, 15, 15, 15, 15, 15,
183 15, 15, 15, 15, 15, 15, 15, 15,
184 15, 15, 15, 15, 15, 15, 15, 15
187 // KingDangerTable[Color][attackUnits] contains the actual king danger
188 // weighted scores, indexed by color and by a calculated integer number.
189 Score KingDangerTable[COLOR_NB][128];
191 // TracedTerms[Color][PieceType || TracedType] contains a breakdown of the
192 // evaluation terms, used when tracing.
193 Score TracedScores[COLOR_NB][16];
194 std::stringstream TraceStream;
197 PST = 8, IMBALANCE = 9, MOBILITY = 10, THREAT = 11,
198 PASSED = 12, UNSTOPPABLE = 13, SPACE = 14, TOTAL = 15
201 // Function prototypes
203 Value do_evaluate(const Position& pos, Value& margin, Info& ei);
206 void init_eval_info(const Position& pos, Info& ei);
208 template<Color Us, bool Trace>
209 Score evaluate_pieces_of_color(const Position& pos, Info& ei, Score& mobility);
211 template<Color Us, bool Trace>
212 Score evaluate_king(const Position& pos, Info& ei, Value margins[]);
215 Score evaluate_threats(const Position& pos, Info& ei);
218 int evaluate_space(const Position& pos, Info& ei);
221 Score evaluate_passed_pawns(const Position& pos, Info& ei);
223 Score evaluate_unstoppable_pawns(const Position& pos, Info& ei);
225 Value interpolate(const Score& v, Phase ph, ScaleFactor sf);
226 Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
227 double to_cp(Value v);
228 void trace_add(int idx, Score term_w, Score term_b = SCORE_ZERO);
229 void trace_row(const char* name, int idx);
235 /// evaluate() is the main evaluation function. It always computes two
236 /// values, an endgame score and a middle game score, and interpolates
237 /// between them based on the remaining material.
239 Value evaluate(const Position& pos, Value& margin, Info* ei) {
240 return do_evaluate<false>(pos, margin, *ei);
244 /// init() computes evaluation weights from the corresponding UCI parameters
245 /// and setup king tables.
249 Weights[Mobility] = weight_option("Mobility (Middle Game)", "Mobility (Endgame)", WeightsInternal[Mobility]);
250 Weights[PassedPawns] = weight_option("Passed Pawns (Middle Game)", "Passed Pawns (Endgame)", WeightsInternal[PassedPawns]);
251 Weights[Space] = weight_option("Space", "Space", WeightsInternal[Space]);
252 Weights[KingDangerUs] = weight_option("Cowardice", "Cowardice", WeightsInternal[KingDangerUs]);
253 Weights[KingDangerThem] = weight_option("Aggressiveness", "Aggressiveness", WeightsInternal[KingDangerThem]);
255 const int MaxSlope = 30;
256 const int Peak = 1280;
258 for (int t = 0, i = 1; i < 100; i++)
260 t = std::min(Peak, std::min(int(0.4 * i * i), t + MaxSlope));
262 KingDangerTable[1][i] = apply_weight(make_score(t, 0), Weights[KingDangerUs]);
263 KingDangerTable[0][i] = apply_weight(make_score(t, 0), Weights[KingDangerThem]);
268 /// trace() is like evaluate() but instead of a value returns a string suitable
269 /// to be print on stdout with the detailed descriptions and values of each
270 /// evaluation term. Used mainly for debugging.
272 std::string trace(const Position& pos) {
278 Search::RootColor = pos.side_to_move();
281 TraceStream << std::showpoint << std::showpos << std::fixed << std::setprecision(2);
282 memset(TracedScores, 0, 2 * 16 * sizeof(Score));
284 do_evaluate<true>(pos, margin, ei);
286 totals = TraceStream.str();
289 TraceStream << std::setw(21) << "Eval term " << "| White | Black | Total \n"
290 << " | MG EG | MG EG | MG EG \n"
291 << "---------------------+-------------+-------------+---------------\n";
293 trace_row("Material, PST, Tempo", PST);
294 trace_row("Material imbalance", IMBALANCE);
295 trace_row("Pawns", PAWN);
296 trace_row("Knights", KNIGHT);
297 trace_row("Bishops", BISHOP);
298 trace_row("Rooks", ROOK);
299 trace_row("Queens", QUEEN);
300 trace_row("Mobility", MOBILITY);
301 trace_row("King safety", KING);
302 trace_row("Threats", THREAT);
303 trace_row("Passed pawns", PASSED);
304 trace_row("Unstoppable pawns", UNSTOPPABLE);
305 trace_row("Space", SPACE);
307 TraceStream << "---------------------+-------------+-------------+---------------\n";
308 trace_row("Total", TOTAL);
309 TraceStream << totals;
311 return TraceStream.str();
320 Value do_evaluate(const Position& pos, Value& margin, Info& ei) {
322 assert(!pos.checkers());
324 Value margins[COLOR_NB];
325 Score score, mobilityWhite, mobilityBlack;
326 Thread* th = pos.this_thread();
328 // margins[] store the uncertainty estimation of position's evaluation
329 // that typically is used by the search for pruning decisions.
330 margins[WHITE] = margins[BLACK] = VALUE_ZERO;
332 // Initialize score by reading the incrementally updated scores included
333 // in the position object (material + piece square tables) and adding
334 // Tempo bonus. Score is computed from the point of view of white.
335 score = pos.psq_score() + (pos.side_to_move() == WHITE ? Tempo : -Tempo);
337 // Probe the material hash table
338 ei.mi = Material::probe(pos, th->materialTable, th->endgames);
339 score += ei.mi->material_value();
341 // If we have a specialized evaluation function for the current material
342 // configuration, call it and return.
343 if (ei.mi->specialized_eval_exists())
346 return ei.mi->evaluate(pos);
349 // Probe the pawn hash table
350 ei.pi = Pawns::probe(pos, th->pawnsTable);
351 score += ei.pi->pawns_value();
353 // Initialize attack and king safety bitboards
354 init_eval_info<WHITE>(pos, ei);
355 init_eval_info<BLACK>(pos, ei);
357 // Evaluate pieces and mobility
358 score += evaluate_pieces_of_color<WHITE, Trace>(pos, ei, mobilityWhite)
359 - evaluate_pieces_of_color<BLACK, Trace>(pos, ei, mobilityBlack);
361 score += apply_weight(mobilityWhite - mobilityBlack, Weights[Mobility]);
363 // Evaluate kings after all other pieces because we need complete attack
364 // information when computing the king safety evaluation.
365 score += evaluate_king<WHITE, Trace>(pos, ei, margins)
366 - evaluate_king<BLACK, Trace>(pos, ei, margins);
368 // Evaluate tactical threats, we need full attack information including king
369 score += evaluate_threats<WHITE>(pos, ei)
370 - evaluate_threats<BLACK>(pos, ei);
372 // Evaluate passed pawns, we need full attack information including king
373 score += evaluate_passed_pawns<WHITE>(pos, ei)
374 - evaluate_passed_pawns<BLACK>(pos, ei);
376 // If one side has only a king, check whether exists any unstoppable passed pawn
377 if (!pos.non_pawn_material(WHITE) || !pos.non_pawn_material(BLACK))
378 score += evaluate_unstoppable_pawns(pos, ei);
380 // Evaluate space for both sides, only in middle-game.
381 if (ei.mi->space_weight())
383 int s = evaluate_space<WHITE>(pos, ei) - evaluate_space<BLACK>(pos, ei);
384 score += apply_weight(make_score(s * ei.mi->space_weight(), 0), Weights[Space]);
387 // Scale winning side if position is more drawish that what it appears
388 ScaleFactor sf = eg_value(score) > VALUE_DRAW ? ei.mi->scale_factor(pos, WHITE)
389 : ei.mi->scale_factor(pos, BLACK);
391 // If we don't already have an unusual scale factor, check for opposite
392 // colored bishop endgames, and use a lower scale for those.
393 if ( ei.mi->game_phase() < PHASE_MIDGAME
394 && pos.opposite_bishops()
395 && sf == SCALE_FACTOR_NORMAL)
397 // Only the two bishops ?
398 if ( pos.non_pawn_material(WHITE) == BishopValueMg
399 && pos.non_pawn_material(BLACK) == BishopValueMg)
401 // Check for KBP vs KB with only a single pawn that is almost
402 // certainly a draw or at least two pawns.
403 bool one_pawn = (pos.piece_count(WHITE, PAWN) + pos.piece_count(BLACK, PAWN) == 1);
404 sf = one_pawn ? ScaleFactor(8) : ScaleFactor(32);
407 // Endgame with opposite-colored bishops, but also other pieces. Still
408 // a bit drawish, but not as drawish as with only the two bishops.
409 sf = ScaleFactor(50);
412 margin = margins[pos.side_to_move()];
413 Value v = interpolate(score, ei.mi->game_phase(), sf);
415 // In case of tracing add all single evaluation contributions for both white and black
418 trace_add(PST, pos.psq_score());
419 trace_add(IMBALANCE, ei.mi->material_value());
420 trace_add(PAWN, ei.pi->pawns_value());
421 trace_add(MOBILITY, apply_weight(mobilityWhite, Weights[Mobility]), apply_weight(mobilityBlack, Weights[Mobility]));
422 trace_add(THREAT, evaluate_threats<WHITE>(pos, ei), evaluate_threats<BLACK>(pos, ei));
423 trace_add(PASSED, evaluate_passed_pawns<WHITE>(pos, ei), evaluate_passed_pawns<BLACK>(pos, ei));
424 trace_add(UNSTOPPABLE, evaluate_unstoppable_pawns(pos, ei));
425 Score w = make_score(ei.mi->space_weight() * evaluate_space<WHITE>(pos, ei), 0);
426 Score b = make_score(ei.mi->space_weight() * evaluate_space<BLACK>(pos, ei), 0);
427 trace_add(SPACE, apply_weight(w, Weights[Space]), apply_weight(b, Weights[Space]));
428 trace_add(TOTAL, score);
429 TraceStream << "\nUncertainty margin: White: " << to_cp(margins[WHITE])
430 << ", Black: " << to_cp(margins[BLACK])
431 << "\nScaling: " << std::noshowpos
432 << std::setw(6) << 100.0 * ei.mi->game_phase() / 128.0 << "% MG, "
433 << std::setw(6) << 100.0 * (1.0 - ei.mi->game_phase() / 128.0) << "% * "
434 << std::setw(6) << (100.0 * sf) / SCALE_FACTOR_NORMAL << "% EG.\n"
435 << "Total evaluation: " << to_cp(v);
438 return pos.side_to_move() == WHITE ? v : -v;
442 // init_eval_info() initializes king bitboards for given color adding
443 // pawn attacks. To be done at the beginning of the evaluation.
446 void init_eval_info(const Position& pos, Info& ei) {
448 const Color Them = (Us == WHITE ? BLACK : WHITE);
450 Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from<KING>(pos.king_square(Them));
451 ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us);
453 // Init king safety tables only if we are going to use them
454 if ( pos.piece_count(Us, QUEEN)
455 && pos.non_pawn_material(Us) > QueenValueMg + PawnValueMg)
457 ei.kingRing[Them] = (b | (Us == WHITE ? b >> 8 : b << 8));
458 b &= ei.attackedBy[Us][PAWN];
459 ei.kingAttackersCount[Us] = b ? popcount<Max15>(b) / 2 : 0;
460 ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0;
462 ei.kingRing[Them] = ei.kingAttackersCount[Us] = 0;
466 // evaluate_outposts() evaluates bishop and knight outposts squares
468 template<PieceType Piece, Color Us>
469 Score evaluate_outposts(const Position& pos, Info& ei, Square s) {
471 const Color Them = (Us == WHITE ? BLACK : WHITE);
473 assert (Piece == BISHOP || Piece == KNIGHT);
475 // Initial bonus based on square
476 Value bonus = OutpostBonus[Piece == BISHOP][relative_square(Us, s)];
478 // Increase bonus if supported by pawn, especially if the opponent has
479 // no minor piece which can exchange the outpost piece.
480 if (bonus && (ei.attackedBy[Us][PAWN] & s))
482 if ( !pos.pieces(Them, KNIGHT)
483 && !(same_color_squares(s) & pos.pieces(Them, BISHOP)))
484 bonus += bonus + bonus / 2;
488 return make_score(bonus, bonus);
492 // evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given color
494 template<PieceType Piece, Color Us, bool Trace>
495 Score evaluate_pieces(const Position& pos, Info& ei, Score& mobility, Bitboard mobilityArea) {
501 Score score = SCORE_ZERO;
503 const Color Them = (Us == WHITE ? BLACK : WHITE);
504 const Square* pl = pos.piece_list(Us, Piece);
506 ei.attackedBy[Us][Piece] = 0;
508 while ((s = *pl++) != SQ_NONE)
510 // Find attacked squares, including x-ray attacks for bishops and rooks
511 if (Piece == KNIGHT || Piece == QUEEN)
512 b = pos.attacks_from<Piece>(s);
513 else if (Piece == BISHOP)
514 b = attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(Us, QUEEN));
515 else if (Piece == ROOK)
516 b = attacks_bb<ROOK>(s, pos.pieces() ^ pos.pieces(Us, ROOK, QUEEN));
520 ei.attackedBy[Us][Piece] |= b;
522 if (b & ei.kingRing[Them])
524 ei.kingAttackersCount[Us]++;
525 ei.kingAttackersWeight[Us] += KingAttackWeights[Piece];
526 Bitboard bb = (b & ei.attackedBy[Them][KING]);
528 ei.kingAdjacentZoneAttacksCount[Us] += popcount<Max15>(bb);
531 mob = (Piece != QUEEN ? popcount<Max15>(b & mobilityArea)
532 : popcount<Full >(b & mobilityArea));
534 mobility += MobilityBonus[Piece][mob];
536 // Decrease score if we are attacked by an enemy pawn. Remaining part
537 // of threat evaluation must be done later when we have full attack info.
538 if (ei.attackedBy[Them][PAWN] & s)
539 score -= ThreatenedByPawnPenalty[Piece];
541 // Otherwise give a bonus if we are a bishop and can pin a piece or
542 // can give a discovered check through an x-ray attack.
543 else if ( Piece == BISHOP
544 && (PseudoAttacks[Piece][pos.king_square(Them)] & s)
545 && !more_than_one(BetweenBB[s][pos.king_square(Them)] & pos.pieces()))
546 score += BishopPinBonus;
548 // Penalty for bishop with same coloured pawns
550 score -= BishopPawnsPenalty * ei.pi->pawns_on_same_color_squares(Us, s);
552 // Bishop and knight outposts squares
553 if ( (Piece == BISHOP || Piece == KNIGHT)
554 && !(pos.pieces(Them, PAWN) & attack_span_mask(Us, s)))
555 score += evaluate_outposts<Piece, Us>(pos, ei, s);
557 if ((Piece == ROOK || Piece == QUEEN) && relative_rank(Us, s) >= RANK_5)
559 // Major piece on 7th rank
560 if ( relative_rank(Us, s) == RANK_7
561 && relative_rank(Us, pos.king_square(Them)) == RANK_8)
562 score += (Piece == ROOK ? RookOn7thBonus : QueenOn7thBonus);
564 // Major piece attacking pawns on the same rank
565 Bitboard pawns = pos.pieces(Them, PAWN) & rank_bb(s);
567 score += (Piece == ROOK ? RookOnPawnBonus
568 : QueenOnPawnBonus) * popcount<Max15>(pawns);
571 // Special extra evaluation for bishops
572 if (Piece == BISHOP && pos.is_chess960())
574 // An important Chess960 pattern: A cornered bishop blocked by
575 // a friendly pawn diagonally in front of it is a very serious
576 // problem, especially when that pawn is also blocked.
577 if (s == relative_square(Us, SQ_A1) || s == relative_square(Us, SQ_H1))
579 Square d = pawn_push(Us) + (file_of(s) == FILE_A ? DELTA_E : DELTA_W);
580 if (pos.piece_on(s + d) == make_piece(Us, PAWN))
582 if (!pos.is_empty(s + d + pawn_push(Us)))
583 score -= 2*TrappedBishopA1H1Penalty;
584 else if (pos.piece_on(s + 2*d) == make_piece(Us, PAWN))
585 score -= TrappedBishopA1H1Penalty;
587 score -= TrappedBishopA1H1Penalty / 2;
592 // Special extra evaluation for rooks
595 // Open and half-open files
597 if (ei.pi->file_is_half_open(Us, f))
599 if (ei.pi->file_is_half_open(Them, f))
600 score += RookOpenFileBonus;
602 score += RookHalfOpenFileBonus;
605 // Penalize rooks which are trapped inside a king. Penalize more if
606 // king has lost right to castle.
607 if (mob > 6 || ei.pi->file_is_half_open(Us, f))
610 ksq = pos.king_square(Us);
612 if ( file_of(ksq) >= FILE_E
613 && file_of(s) > file_of(ksq)
614 && (relative_rank(Us, ksq) == RANK_1 || rank_of(ksq) == rank_of(s)))
616 // Is there a half-open file between the king and the edge of the board?
617 if (!ei.pi->has_open_file_to_right(Us, file_of(ksq)))
618 score -= make_score(pos.can_castle(Us) ? (TrappedRookPenalty - mob * 16) / 2
619 : (TrappedRookPenalty - mob * 16), 0);
621 else if ( file_of(ksq) <= FILE_D
622 && file_of(s) < file_of(ksq)
623 && (relative_rank(Us, ksq) == RANK_1 || rank_of(ksq) == rank_of(s)))
625 // Is there a half-open file between the king and the edge of the board?
626 if (!ei.pi->has_open_file_to_left(Us, file_of(ksq)))
627 score -= make_score(pos.can_castle(Us) ? (TrappedRookPenalty - mob * 16) / 2
628 : (TrappedRookPenalty - mob * 16), 0);
634 TracedScores[Us][Piece] = score;
640 // evaluate_threats<>() assigns bonuses according to the type of attacking piece
641 // and the type of attacked one.
644 Score evaluate_threats(const Position& pos, Info& ei) {
646 const Color Them = (Us == WHITE ? BLACK : WHITE);
648 Bitboard b, undefendedMinors, weakEnemies;
649 Score score = SCORE_ZERO;
651 // Undefended minors get penalized even if not under attack
652 undefendedMinors = pos.pieces(Them)
653 & (pos.pieces(BISHOP) | pos.pieces(KNIGHT))
654 & ~ei.attackedBy[Them][ALL_PIECES];
656 if (undefendedMinors)
657 score += UndefendedMinorPenalty;
659 // Enemy pieces not defended by a pawn and under our attack
660 weakEnemies = pos.pieces(Them)
661 & ~ei.attackedBy[Them][PAWN]
662 & ei.attackedBy[Us][ALL_PIECES];
667 // Add bonus according to type of attacked enemy piece and to the
668 // type of attacking piece, from knights to queens. Kings are not
669 // considered because are already handled in king evaluation.
670 for (PieceType pt1 = KNIGHT; pt1 < KING; pt1++)
672 b = ei.attackedBy[Us][pt1] & weakEnemies;
674 for (PieceType pt2 = PAWN; pt2 < KING; pt2++)
675 if (b & pos.pieces(pt2))
676 score += ThreatBonus[pt1][pt2];
682 // evaluate_pieces_of_color<>() assigns bonuses and penalties to all the
683 // pieces of a given color.
685 template<Color Us, bool Trace>
686 Score evaluate_pieces_of_color(const Position& pos, Info& ei, Score& mobility) {
688 const Color Them = (Us == WHITE ? BLACK : WHITE);
690 Score score = mobility = SCORE_ZERO;
692 // Do not include in mobility squares protected by enemy pawns or occupied by our pieces
693 const Bitboard mobilityArea = ~(ei.attackedBy[Them][PAWN] | pos.pieces(Us));
695 score += evaluate_pieces<KNIGHT, Us, Trace>(pos, ei, mobility, mobilityArea);
696 score += evaluate_pieces<BISHOP, Us, Trace>(pos, ei, mobility, mobilityArea);
697 score += evaluate_pieces<ROOK, Us, Trace>(pos, ei, mobility, mobilityArea);
698 score += evaluate_pieces<QUEEN, Us, Trace>(pos, ei, mobility, mobilityArea);
700 // Sum up all attacked squares
701 ei.attackedBy[Us][ALL_PIECES] = ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
702 | ei.attackedBy[Us][BISHOP] | ei.attackedBy[Us][ROOK]
703 | ei.attackedBy[Us][QUEEN] | ei.attackedBy[Us][KING];
708 // evaluate_king<>() assigns bonuses and penalties to a king of a given color
710 template<Color Us, bool Trace>
711 Score evaluate_king(const Position& pos, Info& ei, Value margins[]) {
713 const Color Them = (Us == WHITE ? BLACK : WHITE);
715 Bitboard undefended, b, b1, b2, safe;
717 const Square ksq = pos.king_square(Us);
719 // King shelter and enemy pawns storm
720 Score score = ei.pi->king_safety<Us>(pos, ksq);
722 // King safety. This is quite complicated, and is almost certainly far
723 // from optimally tuned.
724 if ( ei.kingAttackersCount[Them] >= 2
725 && ei.kingAdjacentZoneAttacksCount[Them])
727 // Find the attacked squares around the king which has no defenders
728 // apart from the king itself
729 undefended = ei.attackedBy[Them][ALL_PIECES] & ei.attackedBy[Us][KING];
730 undefended &= ~( ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
731 | ei.attackedBy[Us][BISHOP] | ei.attackedBy[Us][ROOK]
732 | ei.attackedBy[Us][QUEEN]);
734 // Initialize the 'attackUnits' variable, which is used later on as an
735 // index to the KingDangerTable[] array. The initial value is based on
736 // the number and types of the enemy's attacking pieces, the number of
737 // attacked and undefended squares around our king, the square of the
738 // king, and the quality of the pawn shelter.
739 attackUnits = std::min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2)
740 + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + popcount<Max15>(undefended))
741 + InitKingDanger[relative_square(Us, ksq)]
742 - mg_value(score) / 32;
744 // Analyse enemy's safe queen contact checks. First find undefended
745 // squares around the king attacked by enemy queen...
746 b = undefended & ei.attackedBy[Them][QUEEN] & ~pos.pieces(Them);
749 // ...then remove squares not supported by another enemy piece
750 b &= ( ei.attackedBy[Them][PAWN] | ei.attackedBy[Them][KNIGHT]
751 | ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK]);
753 attackUnits += QueenContactCheckBonus
755 * (Them == pos.side_to_move() ? 2 : 1);
758 // Analyse enemy's safe rook contact checks. First find undefended
759 // squares around the king attacked by enemy rooks...
760 b = undefended & ei.attackedBy[Them][ROOK] & ~pos.pieces(Them);
762 // Consider only squares where the enemy rook gives check
763 b &= PseudoAttacks[ROOK][ksq];
767 // ...then remove squares not supported by another enemy piece
768 b &= ( ei.attackedBy[Them][PAWN] | ei.attackedBy[Them][KNIGHT]
769 | ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][QUEEN]);
771 attackUnits += RookContactCheckBonus
773 * (Them == pos.side_to_move() ? 2 : 1);
776 // Analyse enemy's safe distance checks for sliders and knights
777 safe = ~(pos.pieces(Them) | ei.attackedBy[Us][ALL_PIECES]);
779 b1 = pos.attacks_from<ROOK>(ksq) & safe;
780 b2 = pos.attacks_from<BISHOP>(ksq) & safe;
782 // Enemy queen safe checks
783 b = (b1 | b2) & ei.attackedBy[Them][QUEEN];
785 attackUnits += QueenCheckBonus * popcount<Max15>(b);
787 // Enemy rooks safe checks
788 b = b1 & ei.attackedBy[Them][ROOK];
790 attackUnits += RookCheckBonus * popcount<Max15>(b);
792 // Enemy bishops safe checks
793 b = b2 & ei.attackedBy[Them][BISHOP];
795 attackUnits += BishopCheckBonus * popcount<Max15>(b);
797 // Enemy knights safe checks
798 b = pos.attacks_from<KNIGHT>(ksq) & ei.attackedBy[Them][KNIGHT] & safe;
800 attackUnits += KnightCheckBonus * popcount<Max15>(b);
802 // To index KingDangerTable[] attackUnits must be in [0, 99] range
803 attackUnits = std::min(99, std::max(0, attackUnits));
805 // Finally, extract the king danger score from the KingDangerTable[]
806 // array and subtract the score from evaluation. Set also margins[]
807 // value that will be used for pruning because this value can sometimes
808 // be very big, and so capturing a single attacking piece can therefore
809 // result in a score change far bigger than the value of the captured piece.
810 score -= KingDangerTable[Us == Search::RootColor][attackUnits];
811 margins[Us] += mg_value(KingDangerTable[Us == Search::RootColor][attackUnits]);
815 TracedScores[Us][KING] = score;
821 // evaluate_passed_pawns<>() evaluates the passed pawns of the given color
824 Score evaluate_passed_pawns(const Position& pos, Info& ei) {
826 const Color Them = (Us == WHITE ? BLACK : WHITE);
828 Bitboard b, squaresToQueen, defendedSquares, unsafeSquares, supportingPawns;
829 Score score = SCORE_ZERO;
831 b = ei.pi->passed_pawns(Us);
837 Square s = pop_lsb(&b);
839 assert(pos.pawn_is_passed(Us, s));
841 int r = int(relative_rank(Us, s) - RANK_2);
842 int rr = r * (r - 1);
844 // Base bonus based on rank
845 Value mbonus = Value(20 * rr);
846 Value ebonus = Value(10 * (rr + r + 1));
850 Square blockSq = s + pawn_push(Us);
852 // Adjust bonus based on kings proximity
853 ebonus += Value(square_distance(pos.king_square(Them), blockSq) * 5 * rr);
854 ebonus -= Value(square_distance(pos.king_square(Us), blockSq) * 2 * rr);
856 // If blockSq is not the queening square then consider also a second push
857 if (rank_of(blockSq) != (Us == WHITE ? RANK_8 : RANK_1))
858 ebonus -= Value(square_distance(pos.king_square(Us), blockSq + pawn_push(Us)) * rr);
860 // If the pawn is free to advance, increase bonus
861 if (pos.is_empty(blockSq))
863 squaresToQueen = forward_bb(Us, s);
864 defendedSquares = squaresToQueen & ei.attackedBy[Us][ALL_PIECES];
866 // If there is an enemy rook or queen attacking the pawn from behind,
867 // add all X-ray attacks by the rook or queen. Otherwise consider only
868 // the squares in the pawn's path attacked or occupied by the enemy.
869 if ( (forward_bb(Them, s) & pos.pieces(Them, ROOK, QUEEN))
870 && (forward_bb(Them, s) & pos.pieces(Them, ROOK, QUEEN) & pos.attacks_from<ROOK>(s)))
871 unsafeSquares = squaresToQueen;
873 unsafeSquares = squaresToQueen & (ei.attackedBy[Them][ALL_PIECES] | pos.pieces(Them));
875 // If there aren't enemy attacks or pieces along the path to queen give
876 // huge bonus. Even bigger if we protect the pawn's path.
878 ebonus += Value(rr * (squaresToQueen == defendedSquares ? 17 : 15));
880 // OK, there are enemy attacks or pieces (but not pawns). Are those
881 // squares which are attacked by the enemy also attacked by us ?
882 // If yes, big bonus (but smaller than when there are no enemy attacks),
883 // if no, somewhat smaller bonus.
884 ebonus += Value(rr * ((unsafeSquares & defendedSquares) == unsafeSquares ? 13 : 8));
888 // Increase the bonus if the passed pawn is supported by a friendly pawn
889 // on the same rank and a bit smaller if it's on the previous rank.
890 supportingPawns = pos.pieces(Us, PAWN) & adjacent_files_bb(file_of(s));
891 if (supportingPawns & rank_bb(s))
892 ebonus += Value(r * 20);
894 else if (supportingPawns & rank_bb(s - pawn_push(Us)))
895 ebonus += Value(r * 12);
897 // Rook pawns are a special case: They are sometimes worse, and
898 // sometimes better than other passed pawns. It is difficult to find
899 // good rules for determining whether they are good or bad. For now,
900 // we try the following: Increase the value for rook pawns if the
901 // other side has no pieces apart from a knight, and decrease the
902 // value if the other side has a rook or queen.
903 if (file_of(s) == FILE_A || file_of(s) == FILE_H)
905 if (pos.non_pawn_material(Them) <= KnightValueMg)
906 ebonus += ebonus / 4;
907 else if (pos.pieces(Them, ROOK, QUEEN))
908 ebonus -= ebonus / 4;
910 score += make_score(mbonus, ebonus);
914 // Add the scores to the middle game and endgame eval
915 return apply_weight(score, Weights[PassedPawns]);
919 // evaluate_unstoppable_pawns() evaluates the unstoppable passed pawns for both sides, this is quite
920 // conservative and returns a winning score only when we are very sure that the pawn is winning.
922 Score evaluate_unstoppable_pawns(const Position& pos, Info& ei) {
924 Bitboard b, b2, blockers, supporters, queeningPath, candidates;
925 Square s, blockSq, queeningSquare;
926 Color c, winnerSide, loserSide;
927 bool pathDefended, opposed;
928 int pliesToGo, movesToGo, oppMovesToGo, sacptg, blockersCount, minKingDist, kingptg, d;
929 int pliesToQueen[] = { 256, 256 };
931 // Step 1. Hunt for unstoppable passed pawns. If we find at least one,
932 // record how many plies are required for promotion.
933 for (c = WHITE; c <= BLACK; c++)
935 // Skip if other side has non-pawn pieces
936 if (pos.non_pawn_material(~c))
939 b = ei.pi->passed_pawns(c);
944 queeningSquare = relative_square(c, file_of(s) | RANK_8);
945 queeningPath = forward_bb(c, s);
947 // Compute plies to queening and check direct advancement
948 movesToGo = rank_distance(s, queeningSquare) - int(relative_rank(c, s) == RANK_2);
949 oppMovesToGo = square_distance(pos.king_square(~c), queeningSquare) - int(c != pos.side_to_move());
950 pathDefended = ((ei.attackedBy[c][ALL_PIECES] & queeningPath) == queeningPath);
952 if (movesToGo >= oppMovesToGo && !pathDefended)
955 // Opponent king cannot block because path is defended and position
956 // is not in check. So only friendly pieces can be blockers.
957 assert(!pos.checkers());
958 assert((queeningPath & pos.pieces()) == (queeningPath & pos.pieces(c)));
960 // Add moves needed to free the path from friendly pieces and retest condition
961 movesToGo += popcount<Max15>(queeningPath & pos.pieces(c));
963 if (movesToGo >= oppMovesToGo && !pathDefended)
966 pliesToGo = 2 * movesToGo - int(c == pos.side_to_move());
967 pliesToQueen[c] = std::min(pliesToQueen[c], pliesToGo);
971 // Step 2. If either side cannot promote at least three plies before the other side then situation
972 // becomes too complex and we give up. Otherwise we determine the possibly "winning side"
973 if (abs(pliesToQueen[WHITE] - pliesToQueen[BLACK]) < 3)
976 winnerSide = (pliesToQueen[WHITE] < pliesToQueen[BLACK] ? WHITE : BLACK);
977 loserSide = ~winnerSide;
979 // Step 3. Can the losing side possibly create a new passed pawn and thus prevent the loss?
980 b = candidates = pos.pieces(loserSide, PAWN);
986 // Compute plies from queening
987 queeningSquare = relative_square(loserSide, file_of(s) | RANK_8);
988 movesToGo = rank_distance(s, queeningSquare) - int(relative_rank(loserSide, s) == RANK_2);
989 pliesToGo = 2 * movesToGo - int(loserSide == pos.side_to_move());
991 // Check if (without even considering any obstacles) we're too far away or doubled
992 if ( pliesToQueen[winnerSide] + 3 <= pliesToGo
993 || (forward_bb(loserSide, s) & pos.pieces(loserSide, PAWN)))
997 // If any candidate is already a passed pawn it _may_ promote in time. We give up.
998 if (candidates & ei.pi->passed_pawns(loserSide))
1001 // Step 4. Check new passed pawn creation through king capturing and pawn sacrifices
1007 sacptg = blockersCount = 0;
1008 minKingDist = kingptg = 256;
1010 // Compute plies from queening
1011 queeningSquare = relative_square(loserSide, file_of(s) | RANK_8);
1012 movesToGo = rank_distance(s, queeningSquare) - int(relative_rank(loserSide, s) == RANK_2);
1013 pliesToGo = 2 * movesToGo - int(loserSide == pos.side_to_move());
1015 // Generate list of blocking pawns and supporters
1016 supporters = adjacent_files_bb(file_of(s)) & candidates;
1017 opposed = forward_bb(loserSide, s) & pos.pieces(winnerSide, PAWN);
1018 blockers = passed_pawn_mask(loserSide, s) & pos.pieces(winnerSide, PAWN);
1022 // How many plies does it take to remove all the blocking pawns?
1025 blockSq = pop_lsb(&blockers);
1028 // Check pawns that can give support to overcome obstacle, for instance
1029 // black pawns: a4, b4 white: b2 then pawn in b4 is giving support.
1032 b2 = supporters & in_front_bb(winnerSide, blockSq + pawn_push(winnerSide));
1034 while (b2) // This while-loop could be replaced with LSB/MSB (depending on color)
1036 d = square_distance(blockSq, pop_lsb(&b2)) - 2;
1037 movesToGo = std::min(movesToGo, d);
1041 // Check pawns that can be sacrificed against the blocking pawn
1042 b2 = attack_span_mask(winnerSide, blockSq) & candidates & ~(1ULL << s);
1044 while (b2) // This while-loop could be replaced with LSB/MSB (depending on color)
1046 d = square_distance(blockSq, pop_lsb(&b2)) - 2;
1047 movesToGo = std::min(movesToGo, d);
1050 // If obstacle can be destroyed with an immediate pawn exchange / sacrifice,
1051 // it's not a real obstacle and we have nothing to add to pliesToGo.
1055 // Plies needed to sacrifice against all the blocking pawns
1056 sacptg += movesToGo * 2;
1059 // Plies needed for the king to capture all the blocking pawns
1060 d = square_distance(pos.king_square(loserSide), blockSq);
1061 minKingDist = std::min(minKingDist, d);
1062 kingptg = (minKingDist + blockersCount) * 2;
1065 // Check if pawn sacrifice plan _may_ save the day
1066 if (pliesToQueen[winnerSide] + 3 > pliesToGo + sacptg)
1069 // Check if king capture plan _may_ save the day (contains some false positives)
1070 if (pliesToQueen[winnerSide] + 3 > pliesToGo + kingptg)
1074 // Winning pawn is unstoppable and will promote as first, return big score
1075 Score score = make_score(0, (Value) 1280 - 32 * pliesToQueen[winnerSide]);
1076 return winnerSide == WHITE ? score : -score;
1080 // evaluate_space() computes the space evaluation for a given side. The
1081 // space evaluation is a simple bonus based on the number of safe squares
1082 // available for minor pieces on the central four files on ranks 2--4. Safe
1083 // squares one, two or three squares behind a friendly pawn are counted
1084 // twice. Finally, the space bonus is scaled by a weight taken from the
1085 // material hash table. The aim is to improve play on game opening.
1087 int evaluate_space(const Position& pos, Info& ei) {
1089 const Color Them = (Us == WHITE ? BLACK : WHITE);
1091 // Find the safe squares for our pieces inside the area defined by
1092 // SpaceMask[]. A square is unsafe if it is attacked by an enemy
1093 // pawn, or if it is undefended and attacked by an enemy piece.
1094 Bitboard safe = SpaceMask[Us]
1095 & ~pos.pieces(Us, PAWN)
1096 & ~ei.attackedBy[Them][PAWN]
1097 & (ei.attackedBy[Us][ALL_PIECES] | ~ei.attackedBy[Them][ALL_PIECES]);
1099 // Find all squares which are at most three squares behind some friendly pawn
1100 Bitboard behind = pos.pieces(Us, PAWN);
1101 behind |= (Us == WHITE ? behind >> 8 : behind << 8);
1102 behind |= (Us == WHITE ? behind >> 16 : behind << 16);
1104 // Since SpaceMask[Us] is fully on our half of the board
1105 assert(unsigned(safe >> (Us == WHITE ? 32 : 0)) == 0);
1107 // Count safe + (behind & safe) with a single popcount
1108 return popcount<Full>((Us == WHITE ? safe << 32 : safe >> 32) | (behind & safe));
1112 // interpolate() interpolates between a middle game and an endgame score,
1113 // based on game phase. It also scales the return value by a ScaleFactor array.
1115 Value interpolate(const Score& v, Phase ph, ScaleFactor sf) {
1117 assert(mg_value(v) > -VALUE_INFINITE && mg_value(v) < VALUE_INFINITE);
1118 assert(eg_value(v) > -VALUE_INFINITE && eg_value(v) < VALUE_INFINITE);
1119 assert(ph >= PHASE_ENDGAME && ph <= PHASE_MIDGAME);
1121 int ev = (eg_value(v) * int(sf)) / SCALE_FACTOR_NORMAL;
1122 int result = (mg_value(v) * int(ph) + ev * int(128 - ph)) / 128;
1123 return Value((result + GrainSize / 2) & ~(GrainSize - 1));
1127 // weight_option() computes the value of an evaluation weight, by combining
1128 // two UCI-configurable weights (midgame and endgame) with an internal weight.
1130 Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight) {
1132 // Scale option value from 100 to 256
1133 int mg = Options[mgOpt] * 256 / 100;
1134 int eg = Options[egOpt] * 256 / 100;
1136 return apply_weight(make_score(mg, eg), internalWeight);
1140 // A couple of little helpers used by tracing code, to_cp() converts a value to
1141 // a double in centipawns scale, trace_add() stores white and black scores.
1143 double to_cp(Value v) { return double(v) / double(PawnValueMg); }
1145 void trace_add(int idx, Score wScore, Score bScore) {
1147 TracedScores[WHITE][idx] = wScore;
1148 TracedScores[BLACK][idx] = bScore;
1152 // trace_row() is an helper function used by tracing code to register the
1153 // values of a single evaluation term.
1155 void trace_row(const char* name, int idx) {
1157 Score wScore = TracedScores[WHITE][idx];
1158 Score bScore = TracedScores[BLACK][idx];
1161 case PST: case IMBALANCE: case PAWN: case UNSTOPPABLE: case TOTAL:
1162 TraceStream << std::setw(20) << name << " | --- --- | --- --- | "
1163 << std::setw(6) << to_cp(mg_value(wScore)) << " "
1164 << std::setw(6) << to_cp(eg_value(wScore)) << " \n";
1167 TraceStream << std::setw(20) << name << " | " << std::noshowpos
1168 << std::setw(5) << to_cp(mg_value(wScore)) << " "
1169 << std::setw(5) << to_cp(eg_value(wScore)) << " | "
1170 << std::setw(5) << to_cp(mg_value(bScore)) << " "
1171 << std::setw(5) << to_cp(eg_value(bScore)) << " | "
1173 << std::setw(6) << to_cp(mg_value(wScore - bScore)) << " "
1174 << std::setw(6) << to_cp(eg_value(wScore - bScore)) << " \n";