X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fevaluate.cpp;h=89b074eb2f16a63b1594e97da5b107f27f616e9a;hp=d5cabaeb8fd31b11b53550cd98a9b92878e29575;hb=453e815d4b0977ef3c6396363c3b43035639d697;hpb=3263ee8557437c9a172305681834d357634edb6a diff --git a/src/evaluate.cpp b/src/evaluate.cpp index d5cabaeb..89b074eb 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -63,6 +63,7 @@ namespace { const int WeightPassedPawnsMidgameInternal = 0x100; const int WeightPassedPawnsEndgameInternal = 0x100; const int WeightKingSafetyInternal = 0x100; + const int WeightKingOppSafetyInternal = 0x100; // Visually better to define tables constants typedef Value V; @@ -269,11 +270,12 @@ namespace { EvalInfo &ei); inline Value apply_weight(Value v, int w); - Value scale_by_game_phase(Value mv, Value ev, Phase ph, ScaleFactor sf[]); + Value scale_by_game_phase(Value mv, Value ev, Phase ph, const ScaleFactor sf[]); int count_1s_8bit(Bitboard b); int compute_weight(int uciWeight, int internalWeight); + int weight_option(const std::string& opt, int weight); void init_safety(); } @@ -288,141 +290,122 @@ namespace { /// between them based on the remaining material. Value evaluate(const Position &pos, EvalInfo &ei, int threadID) { - Color stm; - Square s; - ScaleFactor factor[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL}; - Phase phase; - - memset(&ei, 0, sizeof(EvalInfo)); assert(pos.is_ok()); assert(threadID >= 0 && threadID < THREAD_MAX); - stm = pos.side_to_move(); + memset(&ei, 0, sizeof(EvalInfo)); // Initialize by reading the incrementally updated scores included in the - // position object (material + piece square tables): + // position object (material + piece square tables) ei.mgValue = pos.mg_value(); ei.egValue = pos.eg_value(); - // Probe the material hash table: + // Probe the material hash table ei.mi = MaterialTable[threadID]->get_material_info(pos); ei.mgValue += ei.mi->mg_value(); ei.egValue += ei.mi->eg_value(); - factor[WHITE] = ei.mi->scale_factor(pos, WHITE); - factor[BLACK] = ei.mi->scale_factor(pos, BLACK); - // If we have a specialized evaluation function for the current material - // configuration, call it and return: - if(ei.mi->specialized_eval_exists()) - return ei.mi->evaluate(pos); + // configuration, call it and return + if (ei.mi->specialized_eval_exists()) + return ei.mi->evaluate(pos); - phase = pos.game_phase(); + // After get_material_info() call that modifies them + ScaleFactor factor[2]; + factor[WHITE] = ei.mi->scale_factor(pos, WHITE); + factor[BLACK] = ei.mi->scale_factor(pos, BLACK); - // Probe the pawn hash table: + // Probe the pawn hash table ei.pi = PawnTable[threadID]->get_pawn_info(pos); ei.mgValue += apply_weight(ei.pi->mg_value(), WeightPawnStructureMidgame); ei.egValue += apply_weight(ei.pi->eg_value(), WeightPawnStructureEndgame); - // Initialize king attack bitboards and king attack zones for both sides: + // Initialize king attack bitboards and king attack zones for both sides ei.attackedBy[WHITE][KING] = pos.king_attacks(pos.king_square(WHITE)); ei.attackedBy[BLACK][KING] = pos.king_attacks(pos.king_square(BLACK)); - ei.attackZone[WHITE] = - ei.attackedBy[BLACK][KING] | (ei.attackedBy[BLACK][KING] >> 8); - ei.attackZone[BLACK] = - ei.attackedBy[WHITE][KING] | (ei.attackedBy[WHITE][KING] << 8); - - // Initialize pawn attack bitboards for both sides: - ei.attackedBy[WHITE][PAWN] = - ((pos.pawns(WHITE) << 9) & ~FileABB) | ((pos.pawns(WHITE) << 7) & ~FileHBB); - ei.attackCount[WHITE] += - count_1s_max_15(ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING])/2; - ei.attackedBy[BLACK][PAWN] = - ((pos.pawns(BLACK) >> 7) & ~FileABB) | ((pos.pawns(BLACK) >> 9) & ~FileHBB); - ei.attackCount[BLACK] += - count_1s_max_15(ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING])/2; - - // Evaluate pieces: - for(Color c = WHITE; c <= BLACK; c++) { - Bitboard b; + ei.kingZone[WHITE] = ei.attackedBy[BLACK][KING] | (ei.attackedBy[BLACK][KING] >> 8); + ei.kingZone[BLACK] = ei.attackedBy[WHITE][KING] | (ei.attackedBy[WHITE][KING] << 8); + + // Initialize pawn attack bitboards for both sides + ei.attackedBy[WHITE][PAWN] = ((pos.pawns(WHITE) << 9) & ~FileABB) | ((pos.pawns(WHITE) << 7) & ~FileHBB); + ei.attackedBy[BLACK][PAWN] = ((pos.pawns(BLACK) >> 7) & ~FileABB) | ((pos.pawns(BLACK) >> 9) & ~FileHBB); + ei.kingAttackersCount[WHITE] = count_1s_max_15(ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING])/2; + ei.kingAttackersCount[BLACK] = count_1s_max_15(ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING])/2; + // Evaluate pieces + for (Color c = WHITE; c <= BLACK; c++) + { // Knights - for(int i = 0; i < pos.knight_count(c); i++) { - s = pos.knight_list(c, i); - evaluate_knight(pos, s, c, ei); - } + for (int i = 0; i < pos.knight_count(c); i++) + evaluate_knight(pos, pos.knight_list(c, i), c, ei); // Bishops - for(int i = 0; i < pos.bishop_count(c); i++) { - s = pos.bishop_list(c, i); - evaluate_bishop(pos, s, c, ei); - } + for (int i = 0; i < pos.bishop_count(c); i++) + evaluate_bishop(pos, pos.bishop_list(c, i), c, ei); // Rooks - for(int i = 0; i < pos.rook_count(c); i++) { - s = pos.rook_list(c, i); - evaluate_rook(pos, s, c, ei); - } + for (int i = 0; i < pos.rook_count(c); i++) + evaluate_rook(pos, pos.rook_list(c, i), c, ei); // Queens - for(int i = 0; i < pos.queen_count(c); i++) { - s = pos.queen_list(c, i); - evaluate_queen(pos, s, c, ei); - } - - // Some special patterns: + for(int i = 0; i < pos.queen_count(c); i++) + evaluate_queen(pos, pos.queen_list(c, i), c, ei); - // Trapped bishops on a7/h7/a2/h2 - b = pos.bishops(c) & MaskA7H7[c]; - while(b) { - s = pop_1st_bit(&b); - evaluate_trapped_bishop_a7h7(pos, s, c, ei); + // Special pattern: trapped bishops on a7/h7/a2/h2 + Bitboard b = pos.bishops(c) & MaskA7H7[c]; + while (b) + { + Square s = pop_1st_bit(&b); + evaluate_trapped_bishop_a7h7(pos, s, c, ei); } - // Trapped bishops on a1/h1/a8/h8 in Chess960: - if(Chess960) { - b = pos.bishops(c) & MaskA1H1[c]; - while(b) { - s = pop_1st_bit(&b); - evaluate_trapped_bishop_a1h1(pos, s, c, ei); - } + // Special pattern: trapped bishops on a1/h1/a8/h8 in Chess960: + if (Chess960) + { + b = pos.bishops(c) & MaskA1H1[c]; + while (b) + { + Square s = pop_1st_bit(&b); + evaluate_trapped_bishop_a1h1(pos, s, c, ei); + } } - ei.attackedBy[c][0] = - ei.attackedBy[c][PAWN] | ei.attackedBy[c][KNIGHT] - | ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK] - | ei.attackedBy[c][QUEEN] | ei.attackedBy[c][KING]; + // Sum up all attacked squares + ei.attackedBy[c][0] = ei.attackedBy[c][PAWN] | ei.attackedBy[c][KNIGHT] + | ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK] + | ei.attackedBy[c][QUEEN] | ei.attackedBy[c][KING]; } // Kings. Kings are evaluated after all other pieces for both sides, // because we need complete attack information for all pieces when computing // the king safety evaluation. - for(Color c = WHITE; c <= BLACK; c++) { - s = pos.king_square(c); - evaluate_king(pos, s, c, ei); - } + for (Color c = WHITE; c <= BLACK; c++) + evaluate_king(pos, pos.king_square(c), c, ei); // Evaluate passed pawns. We evaluate passed pawns for both sides at once, // because we need to know which side promotes first in positions where // both sides have an unstoppable passed pawn. - if(ei.pi->passed_pawns()) - evaluate_passed_pawns(pos, ei); + if (ei.pi->passed_pawns()) + evaluate_passed_pawns(pos, ei); - // Middle-game specific evaluation terms - if(phase > PHASE_ENDGAME) { + Phase phase = pos.game_phase(); + // Middle-game specific evaluation terms + if (phase > PHASE_ENDGAME) + { // Pawn storms in positions with opposite castling. - if(square_file(pos.king_square(WHITE)) >= FILE_E && - square_file(pos.king_square(BLACK)) <= FILE_D) - ei.mgValue += - ei.pi->queenside_storm_value(WHITE) - - ei.pi->kingside_storm_value(BLACK); - else if(square_file(pos.king_square(WHITE)) <= FILE_D && - square_file(pos.king_square(BLACK)) >= FILE_E) - ei.mgValue += - ei.pi->kingside_storm_value(WHITE) - - ei.pi->queenside_storm_value(BLACK); + if ( square_file(pos.king_square(WHITE)) >= FILE_E + && square_file(pos.king_square(BLACK)) <= FILE_D) + + ei.mgValue += ei.pi->queenside_storm_value(WHITE) + - ei.pi->kingside_storm_value(BLACK); + + else if ( square_file(pos.king_square(WHITE)) <= FILE_D + && square_file(pos.king_square(BLACK)) >= FILE_E) + + ei.mgValue += ei.pi->kingside_storm_value(WHITE) + - ei.pi->queenside_storm_value(BLACK); } // Mobility @@ -431,46 +414,40 @@ Value evaluate(const Position &pos, EvalInfo &ei, int threadID) { // If we don't already have an unusual scale factor, check for opposite // colored bishop endgames, and use a lower scale for those: - if(phase < PHASE_MIDGAME && pos.opposite_colored_bishops() - && ((factor[WHITE] == SCALE_FACTOR_NORMAL && ei.egValue > Value(0)) || - (factor[BLACK] == SCALE_FACTOR_NORMAL && ei.egValue < Value(0)))) { - if(pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == - 2*BishopValueMidgame) { - // Only the two bishops - if(pos.pawn_count(WHITE) + pos.pawn_count(BLACK) == 1) { - // KBP vs KB with only a single pawn; almost certainly a draw. - if(factor[WHITE] == SCALE_FACTOR_NORMAL) - factor[WHITE] = ScaleFactor(8); - if(factor[BLACK] == SCALE_FACTOR_NORMAL) - factor[BLACK] = ScaleFactor(8); - } - else { - // At least two pawns - if(factor[WHITE] == SCALE_FACTOR_NORMAL) - factor[WHITE] = ScaleFactor(32); - if(factor[BLACK] == SCALE_FACTOR_NORMAL) - factor[BLACK] = ScaleFactor(32); + if ( phase < PHASE_MIDGAME + && pos.opposite_colored_bishops() + && ( (factor[WHITE] == SCALE_FACTOR_NORMAL && ei.egValue > Value(0)) + || (factor[BLACK] == SCALE_FACTOR_NORMAL && ei.egValue < Value(0)))) + { + ScaleFactor sf; + + // Only the two bishops ? + if ( pos.non_pawn_material(WHITE) == BishopValueMidgame + && pos.non_pawn_material(BLACK) == BishopValueMidgame) + { + // Check for KBP vs KB with only a single pawn that is almost + // certainly a draw or at least two pawns. + bool one_pawn = (pos.pawn_count(WHITE) + pos.pawn_count(BLACK) == 1); + sf = one_pawn ? ScaleFactor(8) : ScaleFactor(32); } - } - else { - // Endgame with opposite-colored bishops, but also other pieces. - // Still a bit drawish, but not as drawish as with only the two - // bishops. - if(factor[WHITE] == SCALE_FACTOR_NORMAL) - factor[WHITE] = ScaleFactor(50); - if(factor[BLACK] == SCALE_FACTOR_NORMAL) - factor[BLACK] = ScaleFactor(50); - } + else + // 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, and - // return: - Value value = scale_by_game_phase(ei.mgValue, ei.egValue, phase, factor); + // return + Color stm = pos.side_to_move(); - if(ei.mateThreat[stm] != MOVE_NONE) - return 8 * QueenValueMidgame - Sign[stm] * value; - else - return Sign[stm] * value; + Value v = Sign[stm] * scale_by_game_phase(ei.mgValue, ei.egValue, phase, factor); + + return (ei.mateThreat[stm] == MOVE_NONE ? v : 8 * QueenValueMidgame - v); } @@ -479,22 +456,18 @@ Value evaluate(const Position &pos, EvalInfo &ei, int threadID) { /// we should add scores from the pawn and material hash tables? Value quick_evaluate(const Position &pos) { - Color stm; - Value mgValue, egValue; - ScaleFactor factor[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL}; - Phase phase; assert(pos.is_ok()); - stm = pos.side_to_move(); - - mgValue = pos.mg_value(); - egValue = pos.eg_value(); - phase = pos.game_phase(); + static const + ScaleFactor sf[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL}; - Value value = scale_by_game_phase(mgValue, egValue, phase, factor); + Value mgv = pos.mg_value(); + Value egv = pos.eg_value(); + Phase ph = pos.game_phase(); + Color stm = pos.side_to_move(); - return Sign[stm] * value; + return Sign[stm] * scale_by_game_phase(mgv, egv, ph, sf); } @@ -538,32 +511,19 @@ void quit_eval() { /// read_weights() reads evaluation weights from the corresponding UCI /// parameters. -void read_weights(Color sideToMove) { - WeightMobilityMidgame = - compute_weight(get_option_value_int("Mobility (Middle Game)"), - WeightMobilityMidgameInternal); - WeightMobilityEndgame = - compute_weight(get_option_value_int("Mobility (Endgame)"), - WeightMobilityEndgameInternal); - WeightPawnStructureMidgame = - compute_weight(get_option_value_int("Pawn Structure (Middle Game)"), - WeightPawnStructureMidgameInternal); - WeightPawnStructureEndgame = - compute_weight(get_option_value_int("Pawn Structure (Endgame)"), - WeightPawnStructureEndgameInternal); - WeightPassedPawnsMidgame = - compute_weight(get_option_value_int("Passed Pawns (Middle Game)"), - WeightPassedPawnsMidgameInternal); - WeightPassedPawnsEndgame = - compute_weight(get_option_value_int("Passed Pawns (Endgame)"), - WeightPassedPawnsEndgameInternal); - WeightKingSafety[sideToMove] = - compute_weight(get_option_value_int("Cowardice"), WeightKingSafetyInternal); - WeightKingSafety[opposite_color(sideToMove)] = - compute_weight(get_option_value_int("Aggressiveness"), - WeightKingSafetyInternal); - WeightKingSafety[opposite_color(sideToMove)] = - (get_option_value_int("Aggressiveness") * 0x100) / 100; +void read_weights(Color us) { + + WeightMobilityMidgame = weight_option("Mobility (Middle Game)", WeightMobilityMidgameInternal); + WeightMobilityEndgame = weight_option("Mobility (Endgame)", WeightMobilityEndgameInternal); + WeightPawnStructureMidgame = weight_option("Pawn Structure (Middle Game)", WeightPawnStructureMidgameInternal); + WeightPawnStructureEndgame = weight_option("Pawn Structure (Endgame)", WeightPawnStructureEndgameInternal); + WeightPassedPawnsMidgame = weight_option("Passed Pawns (Middle Game)", WeightPassedPawnsMidgameInternal); + WeightPassedPawnsEndgame = weight_option("Passed Pawns (Endgame)", WeightPassedPawnsEndgameInternal); + + Color them = opposite_color(us); + + WeightKingSafety[us] = weight_option("Cowardice", WeightKingSafetyInternal); + WeightKingSafety[them] = weight_option("Aggressiveness", WeightKingOppSafetyInternal); init_safety(); } @@ -580,13 +540,13 @@ namespace { Color them = opposite_color(us); // King attack - if (b & ei.attackZone[us]) + if (b & ei.kingZone[us]) { - ei.attackCount[us]++; - ei.attackWeight[us] += AttackWeight; + ei.kingAttackersCount[us]++; + ei.kingAttackersWeight[us] += AttackWeight; Bitboard bb = (b & ei.attackedBy[them][KING]); if (bb) - ei.attacked[us] += count_1s_max_15(bb); + ei.kingZoneAttacksCount[us] += count_1s_max_15(bb); } // Mobility @@ -650,28 +610,6 @@ namespace { void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei) { - Color them = opposite_color(us); - - // Open and half-open files: - File f = square_file(s); - if(ei.pi->file_is_half_open(us, f)) { - if(ei.pi->file_is_half_open(them, f)) { - ei.mgValue += Sign[us] * RookOpenFileBonus; - ei.egValue += Sign[us] * RookOpenFileBonus; - } - else { - ei.mgValue += Sign[us] * RookHalfOpenFileBonus; - ei.egValue += Sign[us] * RookHalfOpenFileBonus; - } - } - - // Rook on 7th rank: - if(pawn_rank(us, s) == RANK_7 && - pawn_rank(us, p.king_square(them)) == RANK_8) { - ei.mgValue += Sign[us] * MidgameRookOn7thBonus; - ei.egValue += Sign[us] * EndgameRookOn7thBonus; - } - //Bitboard b = p.rook_attacks(s); Bitboard b = rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us)); ei.attackedBy[us][ROOK] |= b; @@ -680,31 +618,56 @@ namespace { int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus, EndgameRookMobilityBonus); - // Penalize rooks which are trapped inside a king which has lost the - // right to castle: - if(mob <= 6 && !ei.pi->file_is_half_open(us, f)) { - Square ksq = p.king_square(us); - if(square_file(ksq) >= FILE_E && square_file(s) > square_file(ksq) && - (pawn_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s))) { - // Is there a half-open file between the king and the edge of the - // board? - if(!(ei.pi->has_open_file_to_right(us, square_file(ksq)))) { - ei.mgValue -= p.can_castle(us)? - Sign[us] * ((TrappedRookPenalty - mob * 16) / 2) : - Sign[us] * (TrappedRookPenalty - mob * 16); + // Rook on 7th rank + Color them = opposite_color(us); + + if ( relative_rank(us, s) == RANK_7 + && relative_rank(us, p.king_square(them)) == RANK_8) + { + ei.mgValue += Sign[us] * MidgameRookOn7thBonus; + ei.egValue += Sign[us] * EndgameRookOn7thBonus; + } + + // Open and half-open files + File f = square_file(s); + if (ei.pi->file_is_half_open(us, f)) + { + if (ei.pi->file_is_half_open(them, f)) + { + ei.mgValue += Sign[us] * RookOpenFileBonus; + ei.egValue += Sign[us] * RookOpenFileBonus; } - } - else if(square_file(ksq) <= FILE_D && square_file(s) < square_file(ksq) - && (pawn_rank(us, ksq) == RANK_1 || - square_rank(ksq) == square_rank(s))) { - // Is there a half-open file between the king and the edge of the - // board? - if(!(ei.pi->has_open_file_to_left(us, square_file(ksq)))) { - ei.mgValue -= p.can_castle(us)? - Sign[us] * ((TrappedRookPenalty - mob * 16) / 2) : - Sign[us] * (TrappedRookPenalty - mob * 16); + else + { + ei.mgValue += Sign[us] * RookHalfOpenFileBonus; + ei.egValue += Sign[us] * RookHalfOpenFileBonus; } - } + } + + // Penalize rooks which are trapped inside a king. Penalize more if + // king has lost right to castle + if (mob > 6 || ei.pi->file_is_half_open(us, f)) + return; + + Square ksq = p.king_square(us); + + if ( square_file(ksq) >= FILE_E + && square_file(s) > square_file(ksq) + && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s))) + { + // Is there a half-open file between the king and the edge of the board? + if (!ei.pi->has_open_file_to_right(us, square_file(ksq))) + ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2) + : Sign[us] * (TrappedRookPenalty - mob * 16); + } + else if ( square_file(ksq) <= FILE_D + && square_file(s) < square_file(ksq) + && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s))) + { + // Is there a half-open file between the king and the edge of the board? + if (!ei.pi->has_open_file_to_left(us, square_file(ksq))) + ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2) + : Sign[us] * (TrappedRookPenalty - mob * 16); } } @@ -714,21 +677,22 @@ namespace { void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei) { - Color them = opposite_color(us); - - // Queen on 7th rank: - if(pawn_rank(us, s) == RANK_7 && - pawn_rank(us, p.king_square(them)) == RANK_8) { - ei.mgValue += Sign[us] * MidgameQueenOn7thBonus; - ei.egValue += Sign[us] * EndgameQueenOn7thBonus; - } - Bitboard b = p.queen_attacks(s); ei.attackedBy[us][QUEEN] |= b; // King attack and mobility evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus, EndgameQueenMobilityBonus); + + // Queen on 7th rank + Color them = opposite_color(us); + + if ( relative_rank(us, s) == RANK_7 + && relative_rank(us, p.king_square(them)) == RANK_8) + { + ei.mgValue += Sign[us] * MidgameQueenOn7thBonus; + ei.egValue += Sign[us] * EndgameQueenOn7thBonus; + } } @@ -740,7 +704,7 @@ namespace { int shelter = 0, sign = Sign[us]; // King shelter. - if(pawn_rank(us, s) <= RANK_4) { + if(relative_rank(us, s) <= RANK_4) { Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s); Rank r = square_rank(s); for(int i = 0; i < 3; i++) @@ -751,9 +715,9 @@ namespace { // King safety. This is quite complicated, and is almost certainly far // from optimally tuned. Color them = opposite_color(us); - if(p.queen_count(them) >= 1 && ei.attackCount[them] >= 2 + if(p.queen_count(them) >= 1 && ei.kingAttackersCount[them] >= 2 && p.non_pawn_material(them) >= QueenValueMidgame + RookValueMidgame - && ei.attacked[them]) { + && ei.kingZoneAttacksCount[them]) { // Is it the attackers turn to move? bool sente = (them == p.side_to_move()); @@ -773,8 +737,8 @@ namespace { // undefended squares around the king, the square of the king, and the // quality of the pawn shelter. int attackUnits = - Min((ei.attackCount[them] * ei.attackWeight[them]) / 2, 25) - + (ei.attacked[them] + count_1s_max_15(undefended)) * 3 + Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25) + + (ei.kingZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3 + InitKingDanger[relative_square(us, s)] - shelter / 32; // Analyse safe queen contact checks: @@ -912,7 +876,7 @@ namespace { assert(pos.piece_on(s) == pawn_of_color(us)); assert(pos.pawn_is_passed(us, s)); - int r = int(pawn_rank(us, s) - RANK_2); + int r = int(relative_rank(us, s) - RANK_2); int tr = Max(0, r * (r-1)); Square blockSq = s + pawn_push(us); @@ -977,7 +941,7 @@ namespace { + ((us == pos.side_to_move())? 0 : 1); if(d < 0) { - int mtg = RANK_8 - pawn_rank(us, s); + int mtg = RANK_8 - relative_rank(us, s); int blockerCount = count_1s_max_15(squares_in_front_of(us,s)&pos.occupied_squares()); mtg += blockerCount; @@ -1043,26 +1007,20 @@ namespace { void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us, EvalInfo &ei) { - Piece pawn = pawn_of_color(opposite_color(us)); - Square b6, b8; assert(square_is_ok(s)); assert(pos.piece_on(s) == bishop_of_color(us)); - if(square_file(s) == FILE_A) { - b6 = relative_square(us, SQ_B6); - b8 = relative_square(us, SQ_B8); - } - else { - b6 = relative_square(us, SQ_G6); - b8 = relative_square(us, SQ_G8); - } + Square b6 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B6 : SQ_G6); + Square b8 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B8 : SQ_G8); - if(pos.piece_on(b6) == pawn && pos.see(s, b6) < 0 && pos.see(s, b8) < 0) { - ei.mgValue -= Sign[us] * TrappedBishopA7H7Penalty; - ei.egValue -= Sign[us] * TrappedBishopA7H7Penalty; + if ( pos.piece_on(b6) == pawn_of_color(opposite_color(us)) + && pos.see(s, b6) < 0 + && pos.see(s, b8) < 0) + { + ei.mgValue -= Sign[us] * TrappedBishopA7H7Penalty; + ei.egValue -= Sign[us] * TrappedBishopA7H7Penalty; } - } @@ -1119,22 +1077,21 @@ namespace { // score, based on game phase. It also scales the return value by a // ScaleFactor array. - Value scale_by_game_phase(Value mv, Value ev, Phase ph, ScaleFactor sf[]) { + Value scale_by_game_phase(Value mv, Value ev, Phase ph, const ScaleFactor sf[]) { + assert(mv > -VALUE_INFINITE && mv < VALUE_INFINITE); assert(ev > -VALUE_INFINITE && ev < VALUE_INFINITE); assert(ph >= PHASE_ENDGAME && ph <= PHASE_MIDGAME); - if(ev > Value(0)) - ev = apply_scale_factor(ev, sf[WHITE]); - else - ev = apply_scale_factor(ev, sf[BLACK]); + ev = apply_scale_factor(ev, sf[(ev > Value(0) ? WHITE : BLACK)]); + + // Linearized sigmoid interpolator + int sph = int(ph); + sph -= (64 - sph) / 4; + sph = Min(PHASE_MIDGAME, Max(PHASE_ENDGAME, sph)); - // Superlinear interpolator - int sli_ph = int(ph); - sli_ph -= (64 - sli_ph) / 4; - sli_ph = Min(PHASE_MIDGAME, Max(PHASE_ENDGAME, sli_ph)); // ceiling + Value result = Value(int((mv * sph + ev * (128 - sph)) / 128)); - Value result = Value(int((mv * sli_ph + ev * (128 - sli_ph)) / 128)); return Value(int(result) & ~(GrainSize - 1)); } @@ -1157,43 +1114,53 @@ namespace { } + // helper used in read_weights() + int weight_option(const std::string& opt, int weight) { + + return compute_weight(get_option_value_int(opt), weight); + } + + // init_safety() initizes the king safety evaluation, based on UCI // parameters. It is called from read_weights(). void init_safety() { - double a, b; - int maxSlope, peak, i, j; QueenContactCheckBonus = get_option_value_int("Queen Contact Check Bonus"); - RookContactCheckBonus = get_option_value_int("Rook Contact Check Bonus"); - QueenCheckBonus = get_option_value_int("Queen Check Bonus"); - RookCheckBonus = get_option_value_int("Rook Check Bonus"); - BishopCheckBonus = get_option_value_int("Bishop Check Bonus"); - KnightCheckBonus = get_option_value_int("Knight Check Bonus"); - DiscoveredCheckBonus = get_option_value_int("Discovered Check Bonus"); - MateThreatBonus = get_option_value_int("Mate Threat Bonus"); - - a = get_option_value_int("King Safety Coefficient") / 100.0; - b = get_option_value_int("King Safety X Intercept") * 1.0; - maxSlope = get_option_value_int("King Safety Max Slope"); - peak = (get_option_value_int("King Safety Max Value") * 256) / 100; - - for(i = 0; i < 100; i++) { - if(i < b) SafetyTable[i] = Value(0); - else if(get_option_value_string("King Safety Curve") == "Quadratic") - SafetyTable[i] = Value((int)(a * (i - b) * (i - b))); - else if(get_option_value_string("King Safety Curve") == "Linear") - SafetyTable[i] = Value((int)(100 * a * (i - b))); + RookContactCheckBonus = get_option_value_int("Rook Contact Check Bonus"); + QueenCheckBonus = get_option_value_int("Queen Check Bonus"); + RookCheckBonus = get_option_value_int("Rook Check Bonus"); + BishopCheckBonus = get_option_value_int("Bishop Check Bonus"); + KnightCheckBonus = get_option_value_int("Knight Check Bonus"); + DiscoveredCheckBonus = get_option_value_int("Discovered Check Bonus"); + MateThreatBonus = get_option_value_int("Mate Threat Bonus"); + + int maxSlope = get_option_value_int("King Safety Max Slope"); + int peak = get_option_value_int("King Safety Max Value") * 256 / 100; + double a = get_option_value_int("King Safety Coefficient") / 100.0; + double b = get_option_value_int("King Safety X Intercept"); + bool quad = (get_option_value_string("King Safety Curve") == "Quadratic"); + bool linear = (get_option_value_string("King Safety Curve") == "Linear"); + + for (int i = 0; i < 100; i++) + { + if (i < b) + SafetyTable[i] = Value(0); + else if(quad) + SafetyTable[i] = Value((int)(a * (i - b) * (i - b))); + else if(linear) + SafetyTable[i] = Value((int)(100 * a * (i - b))); } - for(i = 0; i < 100; i++) - if(SafetyTable[i+1] - SafetyTable[i] > maxSlope) { - for(j = i + 1; j < 100; j++) - SafetyTable[j] = SafetyTable[j-1] + Value(maxSlope); - } - for(i = 0; i < 100; i++) - if(SafetyTable[i] > Value(peak)) - SafetyTable[i] = Value(peak); + for (int i = 0; i < 100; i++) + { + if (SafetyTable[i+1] - SafetyTable[i] > maxSlope) + for (int j = i + 1; j < 100; j++) + SafetyTable[j] = SafetyTable[j-1] + Value(maxSlope); + + if (SafetyTable[i] > Value(peak)) + SafetyTable[i] = Value(peak); + } } }