]> git.sesse.net Git - stockfish/blobdiff - src/evaluate.cpp
Shrink OutpostBonus[] definition
[stockfish] / src / evaluate.cpp
index d791ed36ae73c36832c7bd534dc674eceb917759..842d477740533353ce251a8679dd015d5d5c4b46 100644 (file)
@@ -133,18 +133,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
@@ -199,9 +195,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;
 
@@ -229,7 +226,7 @@ namespace {
 
   // Function prototypes
   template<bool HasPopCnt>
-  Value do_evaluate(const Position& pos, Value margins[]);
+  Value do_evaluate(const Position& pos, Value& margin);
 
   template<Color Us, bool HasPopCnt>
   void init_eval_info(const Position& pos, EvalInfo& ei);
@@ -238,7 +235,7 @@ namespace {
   Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility);
 
   template<Color Us, bool HasPopCnt>
-  Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]);
+  Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin);
 
   template<Color Us>
   Score evaluate_threats(const Position& pos, EvalInfo& ei);
@@ -250,7 +247,7 @@ namespace {
   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[]);
+  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();
 }
@@ -272,19 +269,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<true>(pos, margins)
-                        : do_evaluate<false>(pos, margins);
+    return CpuHasPOPCNT ? do_evaluate<true>(pos, margin)
+                        : do_evaluate<false>(pos, margin);
 }
 
 namespace {
 
 template<bool HasPopCnt>
-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());
@@ -295,9 +291,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);
@@ -308,10 +304,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]);
@@ -328,8 +320,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<WHITE, HasPopCnt>(pos, ei, margins)
-          - evaluate_king<BLACK, HasPopCnt>(pos, ei, margins);
+  bonus +=  evaluate_king<WHITE, HasPopCnt>(pos, ei, margin)
+          - evaluate_king<BLACK, HasPopCnt>(pos, ei, margin);
 
   // Evaluate tactical threats, we need full attack information including king
   bonus +=  evaluate_threats<WHITE>(pos, ei)
@@ -348,15 +340,16 @@ Value do_evaluate(const Position& pos, Value margins[]) {
       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_ZERO ? mi->scale_factor(pos, WHITE)
+                                                : mi->scale_factor(pos, BLACK);
+
   // 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)
@@ -370,15 +363,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;
 }
 
@@ -495,7 +483,7 @@ namespace {
   // evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given color
 
   template<PieceType Piece, Color Us, bool HasPopCnt>
-  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;
@@ -534,8 +522,8 @@ namespace {
         }
 
         // Mobility
-        mob = (Piece != QUEEN ? count_1s_max_15<HasPopCnt>(b & no_mob_area)
-                              : count_1s<HasPopCnt>(b & no_mob_area));
+        mob = (Piece != QUEEN ? count_1s_max_15<HasPopCnt>(b & mobilityArea)
+                              : count_1s<HasPopCnt>(b & mobilityArea));
 
         mobility += MobilityBonus[Piece][mob];
 
@@ -644,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<KNIGHT, Us, HasPopCnt>(pos, ei, mobility, no_mob_area);
-    bonus += evaluate_pieces<BISHOP, Us, HasPopCnt>(pos, ei, mobility, no_mob_area);
-    bonus += evaluate_pieces<ROOK,   Us, HasPopCnt>(pos, ei, mobility, no_mob_area);
-    bonus += evaluate_pieces<QUEEN,  Us, HasPopCnt>(pos, ei, mobility, no_mob_area);
+    bonus += evaluate_pieces<KNIGHT, Us, HasPopCnt>(pos, ei, mobility, mobilityArea);
+    bonus += evaluate_pieces<BISHOP, Us, HasPopCnt>(pos, ei, mobility, mobilityArea);
+    bonus += evaluate_pieces<ROOK,   Us, HasPopCnt>(pos, ei, mobility, mobilityArea);
+    bonus += evaluate_pieces<QUEEN,  Us, HasPopCnt>(pos, ei, mobility, mobilityArea);
 
     // Sum up all attacked squares
     ei.attackedBy[Us][0] =   ei.attackedBy[Us][PAWN]   | ei.attackedBy[Us][KNIGHT]
@@ -662,7 +650,7 @@ namespace {
   // evaluate_king<>() assigns bonuses and penalties to a king of a given color
 
   template<Color Us, bool HasPopCnt>
-  Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]) {
+  Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin) {
 
     const Color Them = (Us == WHITE ? BLACK : WHITE);
 
@@ -710,6 +698,24 @@ namespace {
                               * (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_max_15<HasPopCnt>(b)
+                              * (Them == pos.side_to_move() ? 2 : 1);
+        }
+
         // Analyse enemy's safe distance checks for sliders and knights
         safe = ~(pos.pieces_of_color(Them) | ei.attackedBy[Us][0]);
 
@@ -745,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;
   }
@@ -888,15 +895,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));