1
0
Fork 0

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 <mcostalba@gmail.com>
sf_2.3.1_base
Marco Costalba 2009-11-13 13:29:04 +01:00
parent 314faa905a
commit 71e852ea81
4 changed files with 40 additions and 24 deletions

View 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);
}

View 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.

View 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,

View 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();