X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fevaluate.cpp;h=17c52e180019230d810be047d1ec971418c50844;hp=c7b9f894bf0b9a6f3e034eb90a815972b26becd7;hb=3acacf8471efd018e33e4743cbbb2aed8d59e855;hpb=cf5d683408a2ef8a1c80be9bf7d6790a38b16277 diff --git a/src/evaluate.cpp b/src/evaluate.cpp index c7b9f894..17c52e18 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -152,25 +152,25 @@ namespace { }; // Assorted bonuses and penalties - constexpr Score BishopPawns = S( 3, 8); - constexpr Score CloseEnemies = S( 7, 0); + constexpr Score BishopPawns = S( 3, 7); + constexpr Score CloseEnemies = S( 8, 0); constexpr Score CorneredBishop = S( 50, 50); - constexpr Score Hanging = S( 62, 34); - constexpr Score KingProtector = S( 6, 7); - constexpr Score KnightOnQueen = S( 20, 12); - constexpr Score LongDiagonalBishop = S( 44, 0); - constexpr Score MinorBehindPawn = S( 16, 0); - constexpr Score Overload = S( 12, 6); - constexpr Score PawnlessFlank = S( 18, 94); - constexpr Score RookOnPawn = S( 10, 28); - constexpr Score SliderOnQueen = S( 49, 21); - constexpr Score ThreatByKing = S( 21, 84); - constexpr Score ThreatByPawnPush = S( 48, 42); - constexpr Score ThreatByRank = S( 14, 3); - constexpr Score ThreatBySafePawn = S(169, 99); - constexpr Score TrappedRook = S( 98, 5); - constexpr Score WeakQueen = S( 51, 10); - constexpr Score WeakUnopposedPawn = S( 14, 20); + constexpr Score Hanging = S( 69, 36); + constexpr Score KingProtector = S( 7, 8); + constexpr Score KnightOnQueen = S( 16, 12); + constexpr Score LongDiagonalBishop = S( 45, 0); + constexpr Score MinorBehindPawn = S( 18, 3); + constexpr Score PawnlessFlank = S( 17, 95); + constexpr Score RestrictedPiece = S( 7, 7); + constexpr Score RookOnPawn = S( 10, 32); + constexpr Score SliderOnQueen = S( 59, 18); + constexpr Score ThreatByKing = S( 24, 89); + constexpr Score ThreatByPawnPush = S( 48, 39); + constexpr Score ThreatByRank = S( 13, 0); + constexpr Score ThreatBySafePawn = S(173, 94); + constexpr Score TrappedRook = S( 96, 4); + constexpr Score WeakQueen = S( 49, 15); + constexpr Score WeakUnopposedPawn = S( 12, 23); #undef S @@ -213,7 +213,7 @@ namespace { // kingRing[color] are the squares adjacent to the king, plus (only for a // king on its first rank) the squares two ranks in front. For instance, // if black's king is on g8, kingRing[BLACK] is f8, h8, f7, g7, h7, f6, g6 - // and h6. It is set to 0 when king safety evaluation is skipped. + // and h6. Bitboard kingRing[COLOR_NB]; // kingAttackersCount[color] is the number of pieces of the given color @@ -258,24 +258,20 @@ namespace { attackedBy[Us][ALL_PIECES] = attackedBy[Us][KING] | attackedBy[Us][PAWN]; attackedBy2[Us] = attackedBy[Us][KING] & attackedBy[Us][PAWN]; - kingRing[Us] = kingAttackersCount[Them] = 0; + // Init our king safety tables + kingRing[Us] = attackedBy[Us][KING]; + if (relative_rank(Us, pos.square(Us)) == RANK_1) + kingRing[Us] |= shift(kingRing[Us]); - // Init our king safety tables only if we are going to use them - if (pos.non_pawn_material(Them) >= RookValueMg + KnightValueMg) - { - kingRing[Us] = attackedBy[Us][KING]; - if (relative_rank(Us, pos.square(Us)) == RANK_1) - kingRing[Us] |= shift(kingRing[Us]); - - if (file_of(pos.square(Us)) == FILE_H) - kingRing[Us] |= shift(kingRing[Us]); + if (file_of(pos.square(Us)) == FILE_H) + kingRing[Us] |= shift(kingRing[Us]); - else if (file_of(pos.square(Us)) == FILE_A) - kingRing[Us] |= shift(kingRing[Us]); + else if (file_of(pos.square(Us)) == FILE_A) + kingRing[Us] |= shift(kingRing[Us]); - kingAttackersCount[Them] = popcount(kingRing[Us] & pe->pawn_attacks(Them)); - kingAttacksCount[Them] = kingAttackersWeight[Them] = 0; - } + kingAttackersCount[Them] = popcount(kingRing[Us] & pe->pawn_attacks(Them)); + kingRing[Us] &= ~double_pawn_attacks_bb(pos.pieces(Us, PAWN)); + kingAttacksCount[Them] = kingAttackersWeight[Them] = 0; } @@ -381,7 +377,7 @@ namespace { { File kf = file_of(pos.square(Us)); if ((kf < FILE_E) == (file_of(s) < kf)) - score -= (TrappedRook - make_score(mob * 22, 0)) * (1 + !pos.can_castle(Us)); + score -= (TrappedRook - make_score(mob * 22, 0)) * (1 + !pos.castling_rights(Us)); } } @@ -423,67 +419,64 @@ namespace { int tropism = popcount(b1) + popcount(b2); // Main king safety evaluation - if (kingAttackersCount[Them] > 1 - pos.count(Them)) - { - int kingDanger = 0; - unsafeChecks = 0; + int kingDanger = 0; + unsafeChecks = 0; - // Attacked squares defended at most once by our queen or king - weak = attackedBy[Them][ALL_PIECES] - & ~attackedBy2[Us] - & (~attackedBy[Us][ALL_PIECES] | attackedBy[Us][KING] | attackedBy[Us][QUEEN]); + // Attacked squares defended at most once by our queen or king + weak = attackedBy[Them][ALL_PIECES] + & ~attackedBy2[Us] + & (~attackedBy[Us][ALL_PIECES] | attackedBy[Us][KING] | attackedBy[Us][QUEEN]); - // Analyse the safe enemy's checks which are possible on next move - safe = ~pos.pieces(Them); - safe &= ~attackedBy[Us][ALL_PIECES] | (weak & attackedBy2[Them]); + // Analyse the safe enemy's checks which are possible on next move + safe = ~pos.pieces(Them); + safe &= ~attackedBy[Us][ALL_PIECES] | (weak & attackedBy2[Them]); - b1 = attacks_bb(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN)); - b2 = attacks_bb(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN)); + b1 = attacks_bb(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN)); + b2 = attacks_bb(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN)); - // Enemy queen safe checks - if ((b1 | b2) & attackedBy[Them][QUEEN] & safe & ~attackedBy[Us][QUEEN]) - kingDanger += QueenSafeCheck; + // Enemy queen safe checks + if ((b1 | b2) & attackedBy[Them][QUEEN] & safe & ~attackedBy[Us][QUEEN]) + kingDanger += QueenSafeCheck; - b1 &= attackedBy[Them][ROOK]; - b2 &= attackedBy[Them][BISHOP]; + b1 &= attackedBy[Them][ROOK]; + b2 &= attackedBy[Them][BISHOP]; - // Enemy rooks checks - if (b1 & safe) - kingDanger += RookSafeCheck; - else - unsafeChecks |= b1; + // Enemy rooks checks + if (b1 & safe) + kingDanger += RookSafeCheck; + else + unsafeChecks |= b1; - // Enemy bishops checks - if (b2 & safe) - kingDanger += BishopSafeCheck; - else - unsafeChecks |= b2; + // Enemy bishops checks + if (b2 & safe) + kingDanger += BishopSafeCheck; + else + unsafeChecks |= b2; - // Enemy knights checks - b = pos.attacks_from(ksq) & attackedBy[Them][KNIGHT]; - if (b & safe) - kingDanger += KnightSafeCheck; - else - unsafeChecks |= b; - - // Unsafe or occupied checking squares will also be considered, as long as - // the square is in the attacker's mobility area. - unsafeChecks &= mobilityArea[Them]; - - kingDanger += kingAttackersCount[Them] * kingAttackersWeight[Them] - + 69 * kingAttacksCount[Them] - + 185 * popcount(kingRing[Us] & weak) - + 150 * popcount(pos.blockers_for_king(Us) | unsafeChecks) - + 4 * tropism - - 873 * !pos.count(Them) - - 6 * mg_value(score) / 8 - + mg_value(mobility[Them] - mobility[Us]) - - 30; - - // Transform the kingDanger units into a Score, and subtract it from the evaluation - if (kingDanger > 0) - score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16); - } + // Enemy knights checks + b = pos.attacks_from(ksq) & attackedBy[Them][KNIGHT]; + if (b & safe) + kingDanger += KnightSafeCheck; + else + unsafeChecks |= b; + + // Unsafe or occupied checking squares will also be considered, as long as + // the square is in the attacker's mobility area. + unsafeChecks &= mobilityArea[Them]; + + kingDanger += kingAttackersCount[Them] * kingAttackersWeight[Them] + + 69 * kingAttacksCount[Them] + + 185 * popcount(kingRing[Us] & weak) + + 150 * popcount(pos.blockers_for_king(Us) | unsafeChecks) + + tropism * tropism / 4 + - 873 * !pos.count(Them) + - 6 * mg_value(score) / 8 + + mg_value(mobility[Them] - mobility[Us]) + - 30; + + // Transform the kingDanger units into a Score, and subtract it from the evaluation + if (kingDanger > 0) + score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16); // Penalty when our king is on a pawnless flank if (!(pos.pieces(PAWN) & kingFlank)) @@ -508,11 +501,11 @@ namespace { constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH); constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB); - Bitboard b, weak, defended, nonPawnEnemies, stronglyProtected, safe; + Bitboard b, weak, defended, nonPawnEnemies, stronglyProtected, safe, restricted; Score score = SCORE_ZERO; // Non-pawn enemies - nonPawnEnemies = pos.pieces(Them) ^ pos.pieces(Them, PAWN); + nonPawnEnemies = pos.pieces(Them) & ~pos.pieces(PAWN); // Squares strongly protected by the enemy, either because they defend the // square with a pawn, or because they defend the square twice and we don't. @@ -552,12 +545,17 @@ namespace { if (weak & attackedBy[Us][KING]) score += ThreatByKing; - score += Hanging * popcount(weak & ~attackedBy[Them][ALL_PIECES]); - - b = weak & nonPawnEnemies & attackedBy[Them][ALL_PIECES]; - score += Overload * popcount(b); + b = ~attackedBy[Them][ALL_PIECES] + | (nonPawnEnemies & attackedBy2[Us]); + score += Hanging * popcount(weak & b); } + // Bonus for restricting their piece moves + restricted = attackedBy[Them][ALL_PIECES] + & ~stronglyProtected + & attackedBy[Us][ALL_PIECES]; + score += RestrictedPiece * popcount(restricted); + // Bonus for enemy unopposed weak pawns if (pos.pieces(Us, ROOK, QUEEN)) score += WeakUnopposedPawn * pe->weak_unopposed(Them); @@ -719,7 +717,8 @@ namespace { behind |= (Us == WHITE ? behind >> 16 : behind << 16); int bonus = popcount(safe) + popcount(behind & safe); - int weight = pos.count(Us) - 2 * pe->open_files(); + int weight = pos.count(Us) + - 2 * popcount(pe->semiopenFiles[WHITE] & pe->semiopenFiles[BLACK]); Score score = make_score(bonus * weight * weight / 16, 0); @@ -744,12 +743,12 @@ namespace { && (pos.pieces(PAWN) & KingSide); // Compute the initiative bonus for the attacking side - int complexity = 8 * pe->pawn_asymmetry() - + 12 * pos.count() - + 12 * outflanking - + 16 * pawnsOnBothFlanks - + 48 * !pos.non_pawn_material() - -118 ; + int complexity = 9 * pe->pawn_asymmetry() + + 11 * pos.count() + + 9 * outflanking + + 18 * pawnsOnBothFlanks + + 49 * !pos.non_pawn_material() + -121 ; // Now apply the bonus: note that we find the attacking side by extracting // the sign of the endgame value, and that we carefully cap the bonus so