From 71e852ea815be8dd718685cb9e15ccdb8b756211 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Fri, 13 Nov 2009 13:29:04 +0100 Subject: [PATCH] Move game phase computation to MaterialInfo 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 --- src/evaluate.cpp | 11 ++++------- src/material.cpp | 23 +++++++++++++++++++++++ src/material.h | 13 +++++++++++++ src/position.h | 17 ----------------- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 21bd97b0..1e8dae95 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -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); } diff --git a/src/material.cpp b/src/material.cpp index a3d9f7c8..597d7229 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -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. diff --git a/src/material.h b/src/material.h index 72d44913..bba46d3f 100644 --- a/src/material.h +++ b/src/material.h @@ -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, diff --git a/src/position.h b/src/position.h index f6e49687..5539b7a6 100644 --- a/src/position.h +++ b/src/position.h @@ -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(); -- 2.39.2