X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fevaluate.cpp;h=e167bb6994f788ec4a81ae12b236d34b78d371c8;hp=664f3e859f32ea3e77fa352fa576fba98137000f;hb=85a7456bd7e8a1a01cdbfa8f4b6fb563d15a37c6;hpb=2a2353aac65d6f7263081dc373c768c8717602db diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 664f3e85..e167bb69 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -128,18 +128,14 @@ namespace { V(0), V(0), V(4), V(8), V(8), V(4), V(0), V(0), V(0), V(4),V(17),V(26),V(26),V(17), V(4), V(0), V(0), V(8),V(26),V(35),V(35),V(26), V(8), V(0), - V(0), V(4),V(17),V(17),V(17),V(17), V(4), V(0), - V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), - V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, + V(0), V(4),V(17),V(17),V(17),V(17), V(4), V(0) }, { V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // Bishops V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(5), V(5), V(5), V(5), V(0), V(0), V(0), V(5),V(10),V(10),V(10),V(10), V(5), V(0), V(0),V(10),V(21),V(21),V(21),V(21),V(10), V(0), - V(0), V(5), V(8), V(8), V(8), V(8), V(5), V(0), - V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), - V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) } + V(0), V(5), V(8), V(8), V(8), V(8), V(5), V(0) } }; // ThreatBonus[attacking][attacked] contains threat bonuses according to @@ -194,9 +190,10 @@ namespace { const int KingAttackWeights[] = { 0, 0, 2, 2, 3, 5 }; // Bonuses for enemy's safe checks - const int QueenContactCheckBonus = 3; - const int QueenCheckBonus = 2; - const int RookCheckBonus = 1; + const int QueenContactCheckBonus = 6; + const int RookContactCheckBonus = 4; + const int QueenCheckBonus = 3; + const int RookCheckBonus = 2; const int BishopCheckBonus = 1; const int KnightCheckBonus = 1; @@ -224,7 +221,7 @@ namespace { // Function prototypes template - Value do_evaluate(const Position& pos, Value margins[]); + Value do_evaluate(const Position& pos, Value& margin); template void init_eval_info(const Position& pos, EvalInfo& ei); @@ -233,7 +230,7 @@ namespace { Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility); template - Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]); + Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin); template Score evaluate_threats(const Position& pos, EvalInfo& ei); @@ -244,8 +241,8 @@ namespace { template Score evaluate_passed_pawns(const Position& pos, EvalInfo& ei); - Score apply_weight(Score v, Score weight); - Value scale_by_game_phase(const Score& v, Phase ph, const ScaleFactor sf[]); + inline Score apply_weight(Score v, Score weight); + Value scale_by_game_phase(const Score& v, Phase ph, ScaleFactor sf); Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight); void init_safety(); } @@ -267,19 +264,18 @@ void prefetchPawn(Key key, int threadID) { /// evaluate() is the main evaluation function. It always computes two /// values, an endgame score and a middle game score, and interpolates /// between them based on the remaining material. -Value evaluate(const Position& pos, Value margins[]) { +Value evaluate(const Position& pos, Value& margin) { - return CpuHasPOPCNT ? do_evaluate(pos, margins) - : do_evaluate(pos, margins); + return CpuHasPOPCNT ? do_evaluate(pos, margin) + : do_evaluate(pos, margin); } namespace { template -Value do_evaluate(const Position& pos, Value margins[]) { +Value do_evaluate(const Position& pos, Value& margin) { EvalInfo ei; - ScaleFactor factor[2]; Score mobilityWhite, mobilityBlack; assert(pos.is_ok()); @@ -290,9 +286,9 @@ Value do_evaluate(const Position& pos, Value margins[]) { // in the position object (material + piece square tables). Score bonus = pos.value(); - // margins[color] is the uncertainty estimation of position's evaluation + // margin is the uncertainty estimation of position's evaluation // and typically is used by the search for pruning decisions. - margins[WHITE] = margins[BLACK] = VALUE_ZERO; + margin = VALUE_ZERO; // Probe the material hash table MaterialInfo* mi = MaterialTable[pos.thread()]->get_material_info(pos); @@ -303,10 +299,6 @@ Value do_evaluate(const Position& pos, Value margins[]) { if (mi->specialized_eval_exists()) return mi->evaluate(pos); - // After get_material_info() call that modifies them - factor[WHITE] = mi->scale_factor(pos, WHITE); - factor[BLACK] = mi->scale_factor(pos, BLACK); - // Probe the pawn hash table ei.pi = PawnTable[pos.thread()]->get_pawn_info(pos); bonus += apply_weight(ei.pi->pawns_value(), Weights[PawnStructure]); @@ -323,8 +315,8 @@ Value do_evaluate(const Position& pos, Value margins[]) { // Evaluate kings after all other pieces because we need complete attack // information when computing the king safety evaluation. - bonus += evaluate_king(pos, ei, margins) - - evaluate_king(pos, ei, margins); + bonus += evaluate_king(pos, ei, margin) + - evaluate_king(pos, ei, margin); // Evaluate tactical threats, we need full attack information including king bonus += evaluate_threats(pos, ei) @@ -334,39 +326,24 @@ Value do_evaluate(const Position& pos, Value margins[]) { bonus += evaluate_passed_pawns(pos, ei) - evaluate_passed_pawns(pos, ei); - Phase phase = mi->game_phase(); - - // Middle-game specific evaluation terms - if (phase > PHASE_ENDGAME) + // Evaluate space for both sides, only in middle-game. + if (mi->space_weight()) { - // Evaluate pawn storms in positions with opposite castling - if ( square_file(pos.king_square(WHITE)) >= FILE_E - && square_file(pos.king_square(BLACK)) <= FILE_D) - - bonus += make_score(ei.pi->queenside_storm_value(WHITE) - ei.pi->kingside_storm_value(BLACK), 0); - - else if ( square_file(pos.king_square(WHITE)) <= FILE_D - && square_file(pos.king_square(BLACK)) >= FILE_E) - - bonus += make_score(ei.pi->kingside_storm_value(WHITE) - ei.pi->queenside_storm_value(BLACK), 0); - - // Evaluate space for both sides - if (mi->space_weight() > 0) - { - int s = evaluate_space(pos, ei) - evaluate_space(pos, ei); - bonus += apply_weight(make_score(s * mi->space_weight(), 0), Weights[Space]); - } + int s = evaluate_space(pos, ei) - evaluate_space(pos, ei); + bonus += apply_weight(make_score(s * mi->space_weight(), 0), Weights[Space]); } + // Scale winning side if position is more drawish that what it appears + ScaleFactor sf = eg_value(bonus) > VALUE_DRAW ? mi->scale_factor(pos, WHITE) + : mi->scale_factor(pos, BLACK); + Phase phase = mi->game_phase(); + // If we don't already have an unusual scale factor, check for opposite - // colored bishop endgames, and use a lower scale for those + // colored bishop endgames, and use a lower scale for those. if ( phase < PHASE_MIDGAME && pos.opposite_colored_bishops() - && ( (factor[WHITE] == SCALE_FACTOR_NORMAL && eg_value(bonus) > VALUE_ZERO) - || (factor[BLACK] == SCALE_FACTOR_NORMAL && eg_value(bonus) < VALUE_ZERO))) + && sf == SCALE_FACTOR_NORMAL) { - ScaleFactor sf; - // Only the two bishops ? if ( pos.non_pawn_material(WHITE) == BishopValueMidgame && pos.non_pawn_material(BLACK) == BishopValueMidgame) @@ -380,15 +357,10 @@ Value do_evaluate(const Position& pos, Value margins[]) { // Endgame with opposite-colored bishops, but also other pieces. Still // a bit drawish, but not as drawish as with only the two bishops. sf = ScaleFactor(50); - - if (factor[WHITE] == SCALE_FACTOR_NORMAL) - factor[WHITE] = sf; - if (factor[BLACK] == SCALE_FACTOR_NORMAL) - factor[BLACK] = sf; } // Interpolate between the middle game and the endgame score - Value v = scale_by_game_phase(bonus, phase, factor); + Value v = scale_by_game_phase(bonus, phase, sf); return pos.side_to_move() == WHITE ? v : -v; } @@ -461,14 +433,22 @@ namespace { template void init_eval_info(const Position& pos, EvalInfo& ei) { + const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15; const Color Them = (Us == WHITE ? BLACK : WHITE); Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from(pos.king_square(Them)); - ei.kingZone[Us] = (b | (Us == WHITE ? b >> 8 : b << 8)); ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us); - b &= ei.attackedBy[Us][PAWN]; - ei.kingAttackersCount[Us] = b ? count_1s_max_15(b) / 2 : EmptyBoardBB; - ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = EmptyBoardBB; + + // Init king safety tables only if we are going to use them + if ( pos.piece_count(Us, QUEEN) + && pos.non_pawn_material(Us) >= QueenValueMidgame + RookValueMidgame) + { + ei.kingZone[Us] = (b | (Us == WHITE ? b >> 8 : b << 8)); + b &= ei.attackedBy[Us][PAWN]; + ei.kingAttackersCount[Us] = b ? count_1s(b) / 2 : 0; + ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0; + } else + ei.kingZone[Us] = ei.kingAttackersCount[Us] = 0; } @@ -501,7 +481,7 @@ namespace { // evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given color template - Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score& mobility, Bitboard no_mob_area) { + Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score& mobility, Bitboard mobilityArea) { Bitboard b; Square s, ksq; @@ -509,6 +489,8 @@ namespace { File f; Score bonus = SCORE_ZERO; + const BitCountType Full = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64 : CNT32; + const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15; const Color Them = (Us == WHITE ? BLACK : WHITE); const Square* ptr = pos.piece_list_begin(Us, Piece); @@ -536,12 +518,12 @@ namespace { ei.kingAttackersWeight[Us] += KingAttackWeights[Piece]; Bitboard bb = (b & ei.attackedBy[Them][KING]); if (bb) - ei.kingAdjacentZoneAttacksCount[Us] += count_1s_max_15(bb); + ei.kingAdjacentZoneAttacksCount[Us] += count_1s(bb); } // Mobility - mob = (Piece != QUEEN ? count_1s_max_15(b & no_mob_area) - : count_1s(b & no_mob_area)); + mob = (Piece != QUEEN ? count_1s(b & mobilityArea) + : count_1s(b & mobilityArea)); mobility += MobilityBonus[Piece][mob]; @@ -650,12 +632,12 @@ namespace { Score bonus = mobility = SCORE_ZERO; // Do not include in mobility squares protected by enemy pawns or occupied by our pieces - const Bitboard no_mob_area = ~(ei.attackedBy[Them][PAWN] | pos.pieces_of_color(Us)); + const Bitboard mobilityArea = ~(ei.attackedBy[Them][PAWN] | pos.pieces_of_color(Us)); - bonus += evaluate_pieces(pos, ei, mobility, no_mob_area); - bonus += evaluate_pieces(pos, ei, mobility, no_mob_area); - bonus += evaluate_pieces(pos, ei, mobility, no_mob_area); - bonus += evaluate_pieces(pos, ei, mobility, no_mob_area); + bonus += evaluate_pieces(pos, ei, mobility, mobilityArea); + bonus += evaluate_pieces(pos, ei, mobility, mobilityArea); + bonus += evaluate_pieces(pos, ei, mobility, mobilityArea); + bonus += evaluate_pieces(pos, ei, mobility, mobilityArea); // Sum up all attacked squares ei.attackedBy[Us][0] = ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT] @@ -668,8 +650,9 @@ namespace { // evaluate_king<>() assigns bonuses and penalties to a king of a given color template - Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]) { + Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin) { + const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15; const Color Them = (Us == WHITE ? BLACK : WHITE); Bitboard undefended, b, b1, b2, safe; @@ -681,9 +664,7 @@ namespace { // King safety. This is quite complicated, and is almost certainly far // from optimally tuned. - if ( ei.kingAttackersCount[Them] >= 2 - && pos.non_pawn_material(Them) >= QueenValueMidgame + RookValueMidgame - && pos.piece_count(Them, QUEEN) >= 1 + if ( ei.kingAttackersCount[Them] >= 2 && ei.kingAdjacentZoneAttacksCount[Them]) { // Find the attacked squares around the king which has no defenders @@ -699,7 +680,7 @@ namespace { // attacked and undefended squares around our king, the square of the // king, and the quality of the pawn shelter. attackUnits = Min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2) - + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s_max_15(undefended)) + + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s(undefended)) + InitKingDanger[relative_square(Us, ksq)] - mg_value(ei.pi->king_shelter(pos, ksq)) / 32; @@ -713,7 +694,25 @@ namespace { | ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK]); if (b) attackUnits += QueenContactCheckBonus - * count_1s_max_15(b) + * count_1s(b) + * (Them == pos.side_to_move() ? 2 : 1); + } + + // Analyse enemy's safe rook contact checks. First find undefended + // squares around the king attacked by enemy rooks... + b = undefended & ei.attackedBy[Them][ROOK] & ~pos.pieces_of_color(Them); + + // Consider only squares where the enemy rook gives check + b &= RookPseudoAttacks[ksq]; + + if (b) + { + // ...then remove squares not supported by another enemy piece + b &= ( ei.attackedBy[Them][PAWN] | ei.attackedBy[Them][KNIGHT] + | ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][QUEEN]); + if (b) + attackUnits += RookContactCheckBonus + * count_1s(b) * (Them == pos.side_to_move() ? 2 : 1); } @@ -726,22 +725,22 @@ namespace { // Enemy queen safe checks b = (b1 | b2) & ei.attackedBy[Them][QUEEN]; if (b) - attackUnits += QueenCheckBonus * count_1s_max_15(b); + attackUnits += QueenCheckBonus * count_1s(b); // Enemy rooks safe checks b = b1 & ei.attackedBy[Them][ROOK]; if (b) - attackUnits += RookCheckBonus * count_1s_max_15(b); + attackUnits += RookCheckBonus * count_1s(b); // Enemy bishops safe checks b = b2 & ei.attackedBy[Them][BISHOP]; if (b) - attackUnits += BishopCheckBonus * count_1s_max_15(b); + attackUnits += BishopCheckBonus * count_1s(b); // Enemy knights safe checks b = pos.attacks_from(ksq) & ei.attackedBy[Them][KNIGHT] & safe; if (b) - attackUnits += KnightCheckBonus * count_1s_max_15(b); + attackUnits += KnightCheckBonus * count_1s(b); // To index KingDangerTable[] attackUnits must be in [0, 99] range attackUnits = Min(99, Max(0, attackUnits)); @@ -752,7 +751,8 @@ namespace { // be very big, and so capturing a single attacking piece can therefore // result in a score change far bigger than the value of the captured piece. bonus -= KingDangerTable[Us][attackUnits]; - margins[Us] += mg_value(KingDangerTable[Us][attackUnits]); + if (pos.side_to_move() == Us) + margin += mg_value(KingDangerTable[Us][attackUnits]); } return bonus; } @@ -865,6 +865,7 @@ namespace { template int evaluate_space(const Position& pos, EvalInfo& ei) { + const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15; const Color Them = (Us == WHITE ? BLACK : WHITE); // Find the safe squares for our pieces inside the area defined by @@ -880,7 +881,7 @@ namespace { behind |= (Us == WHITE ? behind >> 8 : behind << 8); behind |= (Us == WHITE ? behind >> 16 : behind << 16); - return count_1s_max_15(safe) + count_1s_max_15(behind & safe); + return count_1s(safe) + count_1s(behind & safe); } @@ -895,15 +896,14 @@ namespace { // scale_by_game_phase() interpolates between a middle game and an endgame score, // based on game phase. It also scales the return value by a ScaleFactor array. - Value scale_by_game_phase(const Score& v, Phase ph, const ScaleFactor sf[]) { + Value scale_by_game_phase(const Score& v, Phase ph, ScaleFactor sf) { assert(mg_value(v) > -VALUE_INFINITE && mg_value(v) < VALUE_INFINITE); assert(eg_value(v) > -VALUE_INFINITE && eg_value(v) < VALUE_INFINITE); assert(ph >= PHASE_ENDGAME && ph <= PHASE_MIDGAME); Value eg = eg_value(v); - ScaleFactor f = sf[eg > VALUE_ZERO ? WHITE : BLACK]; - Value ev = Value((eg * int(f)) / SCALE_FACTOR_NORMAL); + Value ev = Value((eg * int(sf)) / SCALE_FACTOR_NORMAL); int result = (mg_value(v) * int(ph) + ev * int(128 - ph)) / 128; return Value(result & ~(GrainSize - 1));