]> git.sesse.net Git - stockfish/commitdiff
Move game phase computation to MaterialInfo
authorMarco Costalba <mcostalba@gmail.com>
Fri, 13 Nov 2009 12:29:04 +0000 (13:29 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 14 Nov 2009 16:57:49 +0000 (17:57 +0100)
Game phase is a strictly function of the material
combination so its natural place is MaterialInfo,
not position.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/evaluate.cpp
src/material.cpp
src/material.h
src/position.h

index 21bd97b09e264243745f060f10fe9c67b58b0122..1e8dae9585026ab8cda57e5f6e7834af57b965d4 100644 (file)
@@ -370,7 +370,7 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) {
   if (ei.pi->passed_pawns())
       evaluate_passed_pawns(pos, ei);
 
-  Phase phase = pos.game_phase();
+  Phase phase = ei.mi->game_phase();
 
   // Middle-game specific evaluation terms
   if (phase > PHASE_ENDGAME)
@@ -444,13 +444,10 @@ Value quick_evaluate(const Position &pos) {
 
   assert(pos.is_ok());
 
-  static const
-  ScaleFactor sf[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL};
+  static const ScaleFactor sf[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL};
 
-  Phase ph = pos.game_phase();
-  Color stm = pos.side_to_move();
-
-  return Sign[stm] * scale_by_game_phase(pos.value(), ph, sf);
+  Value v = scale_by_game_phase(pos.value(), MaterialInfoTable::game_phase(pos), sf);
+  return (pos.side_to_move() == WHITE ? v : -v);
 }
 
 
index a3d9f7c8a73958ca7c8235e6f62c81ec85479d58..597d72298902179bef40bb04e4fa11d92ab6cee2 100644 (file)
@@ -37,6 +37,10 @@ using namespace std;
 
 namespace {
 
+  // Values modified by Joona Kiiski
+  const Value MidgameLimit = Value(15581);
+  const Value EndgameLimit = Value(3998);
+
   // Polynomial material balance parameters
   const Value RedundantQueenPenalty = Value(320);
   const Value RedundantRookPenalty  = Value(554);
@@ -129,6 +133,22 @@ MaterialInfoTable::~MaterialInfoTable() {
 }
 
 
+/// MaterialInfoTable::game_phase() calculate the phase given the current
+/// position. Because the phase is strictly a function of the material, it
+/// is stored in MaterialInfo.
+
+Phase MaterialInfoTable::game_phase(const Position& pos) {
+
+  Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);
+
+  if (npm >= MidgameLimit)
+      return PHASE_MIDGAME;
+  else if (npm <= EndgameLimit)
+      return PHASE_ENDGAME;
+
+  return Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
+}
+
 /// MaterialInfoTable::get_material_info() takes a position object as input,
 /// computes or looks up a MaterialInfo object, and returns a pointer to it.
 /// If the material configuration is not already present in the table, it
@@ -151,6 +171,9 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
   mi->clear();
   mi->key = key;
 
+  // Calculate game phase
+  mi->gamePhase = MaterialInfoTable::game_phase(pos);
+
   // Let's look if we have a specialized evaluation function for this
   // particular material configuration. First we look for a fixed
   // configuration one, then a generic one if previous search failed.
index 72d449131439e1ed05cec5bea24056549b1374a0..bba46d3f7ff71250968d553e6e7c47dc87740116 100644 (file)
@@ -54,6 +54,7 @@ public:
   Score material_value() const;
   ScaleFactor scale_factor(const Position& pos, Color c) const;
   int space_weight() const;
+  Phase game_phase() const;
   bool specialized_eval_exists() const;
   Value evaluate(const Position& pos) const;
 
@@ -66,6 +67,7 @@ private:
   EndgameEvaluationFunctionBase* evaluationFunction;
   EndgameScalingFunctionBase* scalingFunction[2];
   int spaceWeight;
+  Phase gamePhase;
 };
 
 /// The MaterialInfoTable class represents a pawn hash table. It is basically
@@ -81,6 +83,8 @@ public:
   ~MaterialInfoTable();
   MaterialInfo* get_material_info(const Position& pos);
 
+  static Phase game_phase(const Position& pos);
+
 private:
   unsigned size;
   MaterialInfo* entries;
@@ -92,6 +96,7 @@ private:
 //// Inline functions
 ////
 
+
 /// MaterialInfo::material_value simply returns the material balance
 /// evaluation that is independent from game phase.
 
@@ -141,6 +146,14 @@ inline int MaterialInfo::space_weight() const {
   return spaceWeight;
 }
 
+/// MaterialInfo::game_phase() returns the game phase according
+/// to this material configuration.
+
+inline Phase MaterialInfo::game_phase() const {
+
+  return gamePhase;
+}
+
 
 /// MaterialInfo::specialized_eval_exists decides whether there is a
 /// specialized evaluation function for the current material configuration,
index f6e49687de45c7b7bf2e5cb04ef61c5ff5ee9b5f..5539b7a6faa660da7897c7caf43d0b5d662102bf 100644 (file)
@@ -255,7 +255,6 @@ public:
   // Incremental evaluation
   Score value() const;
   Value non_pawn_material(Color c) const;
-  Phase game_phase() const;
   Score pst_delta(Piece piece, Square from, Square to) const;
 
   // Game termination checks
@@ -526,22 +525,6 @@ inline Value Position::non_pawn_material(Color c) const {
   return st->npMaterial[c];
 }
 
-inline Phase Position::game_phase() const {
-
-  // Values modified by Joona Kiiski
-  static const Value MidgameLimit = Value(15581);
-  static const Value EndgameLimit = Value(3998);
-
-  Value npm = non_pawn_material(WHITE) + non_pawn_material(BLACK);
-
-  if (npm >= MidgameLimit)
-      return PHASE_MIDGAME;
-  else if(npm <= EndgameLimit)
-      return PHASE_ENDGAME;
-  else
-      return Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
-}
-
 inline bool Position::move_is_passed_pawn_push(Move m) const {
 
   Color c = side_to_move();