]> git.sesse.net Git - stockfish/blobdiff - src/evaluate.cpp
Better naming and document some endgame functions
[stockfish] / src / evaluate.cpp
index 0bc38badd5d90d7dbb8bc40de0e1e110608f62e2..7f7df3c5465e491bc78775338f12de908fd3d6ab 100644 (file)
@@ -58,15 +58,15 @@ namespace {
   // parameters at 100, which looks prettier.
   //
   // Values modified by Joona Kiiski
-  const int WeightMobilityMidgameInternal      = 0x0FA;
-  const int WeightMobilityEndgameInternal      = 0x10A;
-  const int WeightPawnStructureMidgameInternal = 0x0EC;
-  const int WeightPawnStructureEndgameInternal = 0x0CD;
-  const int WeightPassedPawnsMidgameInternal   = 0x108;
-  const int WeightPassedPawnsEndgameInternal   = 0x109;
-  const int WeightKingSafetyInternal           = 0x0F7;
-  const int WeightKingOppSafetyInternal        = 0x101;
-  const int WeightSpaceInternal                = 0x02F;
+  const int WeightMobilityMidgameInternal      = 248;
+  const int WeightMobilityEndgameInternal      = 271;
+  const int WeightPawnStructureMidgameInternal = 233;
+  const int WeightPawnStructureEndgameInternal = 201;
+  const int WeightPassedPawnsMidgameInternal   = 252;
+  const int WeightPassedPawnsEndgameInternal   = 259;
+  const int WeightKingSafetyInternal           = 247;
+  const int WeightKingOppSafetyInternal        = 259;
+  const int WeightSpaceInternal                = 46;
 
   // Mobility and outposts bonus modified by Joona Kiiski
   //
@@ -173,14 +173,14 @@ namespace {
   const Value UnstoppablePawnValue = Value(0x500);
 
   // Rooks and queens on the 7th rank (modified by Joona Kiiski)
-  const Value MidgameRookOn7thBonus  = Value(43);
-  const Value EndgameRookOn7thBonus  = Value(91);
-  const Value MidgameQueenOn7thBonus = Value(28);
-  const Value EndgameQueenOn7thBonus = Value(53);
+  const Value MidgameRookOn7thBonus  = Value(47);
+  const Value EndgameRookOn7thBonus  = Value(98);
+  const Value MidgameQueenOn7thBonus = Value(27);
+  const Value EndgameQueenOn7thBonus = Value(54);
 
   // Rooks on open files (modified by Joona Kiiski)
-  const Value RookOpenFileBonus = Value(42);
-  const Value RookHalfOpenFileBonus = Value(24);
+  const Value RookOpenFileBonus = Value(43);
+  const Value RookHalfOpenFileBonus = Value(19);
 
   // Penalty for rooks trapped inside a friendly king which has lost the
   // right to castle.
@@ -266,9 +266,6 @@ namespace {
   const int PawnTableSize = 16384;
   const int MaterialTableSize = 1024;
 
-  // Array which gives the number of nonzero bits in an 8-bit integer
-  uint8_t BitCount8Bit[256];
-
   // Function prototypes
   template<bool HasPopCnt>
   Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID);
@@ -326,8 +323,8 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) {
 
   // 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();
+  ei.mgValue += ei.mi->material_value();
+  ei.egValue += ei.mi->material_value();
 
   // If we have a specialized evaluation function for the current material
   // configuration, call it and return
@@ -353,8 +350,13 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) {
   // 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<HasPopCnt>(ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING])/2;
-  ei.kingAttackersCount[BLACK] = count_1s_max_15<HasPopCnt>(ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING])/2;
+  Bitboard b1 = ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING];
+  Bitboard b2 = ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING];
+  if (b1)
+      ei.kingAttackersCount[WHITE] = count_1s_max_15<HasPopCnt>(b1)/2;
+
+  if (b2)
+      ei.kingAttackersCount[BLACK] = count_1s_max_15<HasPopCnt>(b2)/2;
 
   // Evaluate pieces
   for (Color c = WHITE; c <= BLACK; c++)
@@ -493,12 +495,6 @@ void init_eval(int threads) {
     if (!MaterialTable[i])
         MaterialTable[i] = new MaterialInfoTable(MaterialTableSize);
   }
-
-  for (Bitboard b = 0ULL; b < 256ULL; b++)
-  {
-      assert(count_1s(b) == int(uint8_t(count_1s(b))));
-      BitCount8Bit[b] = (uint8_t)count_1s(b);
-  }
 }
 
 
@@ -532,6 +528,14 @@ void read_weights(Color us) {
 
   WeightKingSafety[us]   = weight_option("Cowardice", WeightKingSafetyInternal);
   WeightKingSafety[them] = weight_option("Aggressiveness", WeightKingOppSafetyInternal);
+  // If running in analysis mode, make sure we use symmetrical king safety.
+  // We do this by replacing both WeightKingSafety[us] and 
+  // WeightKingSafety[them] by their average.
+  if (get_option_value_bool("UCI_AnalyseMode")) {
+      WeightKingSafety[us] = (WeightKingSafety[us] + WeightKingSafety[them]) / 2;
+      WeightKingSafety[them] = WeightKingSafety[us];
+  }
+
   WeightSpace = weight_option("Space", WeightSpaceInternal);
 
   init_safety();
@@ -701,10 +705,6 @@ namespace {
     }
   }
 
-  inline Bitboard shiftRowsDown(const Bitboard& b, int num) {
-
-    return b >> (num << 3);
-  }
 
   // evaluate_king<>() assigns bonuses and penalties to a king of a given color.
 
@@ -717,19 +717,7 @@ namespace {
     // King shelter
     if (relative_rank(us, s) <= RANK_4)
     {
-        // Shelter cache lookup
-        shelter = ei.pi->kingShelter(us, s);
-        if (shelter == -1)
-        {
-            shelter = 0;
-            Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s);
-            Rank r = square_rank(s);
-            for (int i = 1; i < 4; i++)
-                shelter += BitCount8Bit[shiftRowsDown(pawns, r+i*sign) & 0xFF] * (128 >> i);
-
-            // Cache shelter value in pawn info
-            ei.pi->setKingShelter(us, s, shelter);
-        }
+        shelter = ei.pi->get_king_shelter(p, us, s);
         ei.mgValue += sign * Value(shelter);
     }