mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 08:43:09 +00:00
Convert Position to use Score struct
No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
06f06a9be8
commit
1ae8c59c0b
4 changed files with 25 additions and 47 deletions
|
@ -352,7 +352,7 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) {
|
||||||
|
|
||||||
// Initialize by reading the incrementally updated scores included in the
|
// Initialize by reading the incrementally updated scores included in the
|
||||||
// position object (material + piece square tables)
|
// position object (material + piece square tables)
|
||||||
ei.value = Score(pos.mg_value(), pos.eg_value());
|
ei.value = pos.value();
|
||||||
|
|
||||||
// Probe the material hash table
|
// Probe the material hash table
|
||||||
ei.mi = MaterialTable[threadID]->get_material_info(pos);
|
ei.mi = MaterialTable[threadID]->get_material_info(pos);
|
||||||
|
@ -489,11 +489,10 @@ Value quick_evaluate(const Position &pos) {
|
||||||
static const
|
static const
|
||||||
ScaleFactor sf[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL};
|
ScaleFactor sf[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL};
|
||||||
|
|
||||||
Score v = Score(pos.mg_value(), pos.eg_value());
|
|
||||||
Phase ph = pos.game_phase();
|
Phase ph = pos.game_phase();
|
||||||
Color stm = pos.side_to_move();
|
Color stm = pos.side_to_move();
|
||||||
|
|
||||||
return Sign[stm] * scale_by_game_phase(v, ph, sf);
|
return Sign[stm] * scale_by_game_phase(pos.value(), ph, sf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -213,8 +213,7 @@ void Position::from_fen(const string& fen) {
|
||||||
st->key = compute_key();
|
st->key = compute_key();
|
||||||
st->pawnKey = compute_pawn_key();
|
st->pawnKey = compute_pawn_key();
|
||||||
st->materialKey = compute_material_key();
|
st->materialKey = compute_material_key();
|
||||||
st->mgValue = compute_value<MidGame>();
|
st->value = Score(compute_value<MidGame>(), compute_value<EndGame>());
|
||||||
st->egValue = compute_value<EndGame>();
|
|
||||||
st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
|
st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
|
||||||
st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
|
st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
|
||||||
}
|
}
|
||||||
|
@ -830,8 +829,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
st->mgValue += pst_delta<MidGame>(piece, from, to);
|
st->value += Score(pst_delta<MidGame>(piece, from, to), pst_delta<EndGame>(piece, from, to));
|
||||||
st->egValue += pst_delta<EndGame>(piece, from, to);
|
|
||||||
|
|
||||||
if (pm) // promotion ?
|
if (pm) // promotion ?
|
||||||
{
|
{
|
||||||
|
@ -866,10 +864,8 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
st->pawnKey ^= zobrist[us][PAWN][to];
|
st->pawnKey ^= zobrist[us][PAWN][to];
|
||||||
|
|
||||||
// Partially revert and update incremental scores
|
// Partially revert and update incremental scores
|
||||||
st->mgValue -= pst<MidGame>(us, PAWN, to);
|
st->value -= Score(pst<MidGame>(us, PAWN, to), pst<EndGame>(us, PAWN, to));
|
||||||
st->mgValue += pst<MidGame>(us, promotion, to);
|
st->value += Score(pst<MidGame>(us, promotion, to), pst<EndGame>(us, promotion, to));
|
||||||
st->egValue -= pst<EndGame>(us, PAWN, to);
|
|
||||||
st->egValue += pst<EndGame>(us, promotion, to);
|
|
||||||
|
|
||||||
// Update material
|
// Update material
|
||||||
st->npMaterial[us] += piece_value_midgame(promotion);
|
st->npMaterial[us] += piece_value_midgame(promotion);
|
||||||
|
@ -899,9 +895,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
|
||||||
|
|
||||||
// Finish
|
// Finish
|
||||||
sideToMove = opposite_color(sideToMove);
|
sideToMove = opposite_color(sideToMove);
|
||||||
|
st->value += (sideToMove == WHITE) ? TempoValue : -TempoValue;
|
||||||
st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame;
|
|
||||||
st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame;
|
|
||||||
|
|
||||||
assert(is_ok());
|
assert(is_ok());
|
||||||
}
|
}
|
||||||
|
@ -937,8 +931,7 @@ void Position::do_capture_move(Bitboard& key, PieceType capture, Color them, Squ
|
||||||
key ^= zobrist[them][capture][capsq];
|
key ^= zobrist[them][capture][capsq];
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
st->mgValue -= pst<MidGame>(them, capture, capsq);
|
st->value -= Score(pst<MidGame>(them, capture, capsq), pst<EndGame>(them, capture, capsq));
|
||||||
st->egValue -= pst<EndGame>(them, capture, capsq);
|
|
||||||
|
|
||||||
// If the captured piece was a pawn, update pawn hash key,
|
// If the captured piece was a pawn, update pawn hash key,
|
||||||
// otherwise update non-pawn material.
|
// otherwise update non-pawn material.
|
||||||
|
@ -1035,10 +1028,8 @@ void Position::do_castle_move(Move m) {
|
||||||
index[rto] = tmp;
|
index[rto] = tmp;
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
st->mgValue += pst_delta<MidGame>(king, kfrom, kto);
|
st->value += Score(pst_delta<MidGame>(king, kfrom, kto), pst_delta<EndGame>(king, kfrom, kto));
|
||||||
st->egValue += pst_delta<EndGame>(king, kfrom, kto);
|
st->value += Score(pst_delta<MidGame>(rook, rfrom, rto), pst_delta<EndGame>(rook, rfrom, rto));
|
||||||
st->mgValue += pst_delta<MidGame>(rook, rfrom, rto);
|
|
||||||
st->egValue += pst_delta<EndGame>(rook, rfrom, rto);
|
|
||||||
|
|
||||||
// Update hash key
|
// Update hash key
|
||||||
st->key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
|
st->key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
|
||||||
|
@ -1064,9 +1055,7 @@ void Position::do_castle_move(Move m) {
|
||||||
|
|
||||||
// Finish
|
// Finish
|
||||||
sideToMove = opposite_color(sideToMove);
|
sideToMove = opposite_color(sideToMove);
|
||||||
|
st->value += (sideToMove == WHITE) ? TempoValue : -TempoValue;
|
||||||
st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame;
|
|
||||||
st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame;
|
|
||||||
|
|
||||||
assert(is_ok());
|
assert(is_ok());
|
||||||
}
|
}
|
||||||
|
@ -1258,8 +1247,7 @@ void Position::do_null_move(StateInfo& backupSt) {
|
||||||
// a backup storage not as a new state to be used.
|
// a backup storage not as a new state to be used.
|
||||||
backupSt.key = st->key;
|
backupSt.key = st->key;
|
||||||
backupSt.epSquare = st->epSquare;
|
backupSt.epSquare = st->epSquare;
|
||||||
backupSt.mgValue = st->mgValue;
|
backupSt.value = st->value;
|
||||||
backupSt.egValue = st->egValue;
|
|
||||||
backupSt.previous = st->previous;
|
backupSt.previous = st->previous;
|
||||||
backupSt.pliesFromNull = st->pliesFromNull;
|
backupSt.pliesFromNull = st->pliesFromNull;
|
||||||
st->previous = &backupSt;
|
st->previous = &backupSt;
|
||||||
|
@ -1279,10 +1267,8 @@ void Position::do_null_move(StateInfo& backupSt) {
|
||||||
st->epSquare = SQ_NONE;
|
st->epSquare = SQ_NONE;
|
||||||
st->rule50++;
|
st->rule50++;
|
||||||
st->pliesFromNull = 0;
|
st->pliesFromNull = 0;
|
||||||
|
st->value += (sideToMove == WHITE) ? TempoValue : -TempoValue;
|
||||||
gamePly++;
|
gamePly++;
|
||||||
|
|
||||||
st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame;
|
|
||||||
st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1297,8 +1283,7 @@ void Position::undo_null_move() {
|
||||||
StateInfo* backupSt = st->previous;
|
StateInfo* backupSt = st->previous;
|
||||||
st->key = backupSt->key;
|
st->key = backupSt->key;
|
||||||
st->epSquare = backupSt->epSquare;
|
st->epSquare = backupSt->epSquare;
|
||||||
st->mgValue = backupSt->mgValue;
|
st->value = backupSt->value;
|
||||||
st->egValue = backupSt->egValue;
|
|
||||||
st->previous = backupSt->previous;
|
st->previous = backupSt->previous;
|
||||||
st->pliesFromNull = backupSt->pliesFromNull;
|
st->pliesFromNull = backupSt->pliesFromNull;
|
||||||
|
|
||||||
|
@ -1664,8 +1649,8 @@ Value Position::compute_value() const {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Value TempoValue = (Phase == MidGame ? TempoValueMidgame : TempoValueEndgame);
|
const Value tv = (Phase == MidGame ? TempoValue.mg() : TempoValue.eg());
|
||||||
result += (side_to_move() == WHITE)? TempoValue / 2 : -TempoValue / 2;
|
result += (side_to_move() == WHITE)? tv / 2 : -tv / 2;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1878,8 +1863,7 @@ void Position::flipped_copy(const Position& pos) {
|
||||||
st->materialKey = compute_material_key();
|
st->materialKey = compute_material_key();
|
||||||
|
|
||||||
// Incremental scores
|
// Incremental scores
|
||||||
st->mgValue = compute_value<MidGame>();
|
st->value = Score(compute_value<MidGame>(), compute_value<EndGame>());
|
||||||
st->egValue = compute_value<EndGame>();
|
|
||||||
|
|
||||||
// Material
|
// Material
|
||||||
st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
|
st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
|
||||||
|
@ -2008,10 +1992,10 @@ bool Position::is_ok(int* failedStep) const {
|
||||||
if (failedStep) (*failedStep)++;
|
if (failedStep) (*failedStep)++;
|
||||||
if (debugIncrementalEval)
|
if (debugIncrementalEval)
|
||||||
{
|
{
|
||||||
if (st->mgValue != compute_value<MidGame>())
|
if (st->value.mg() != compute_value<MidGame>())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (st->egValue != compute_value<EndGame>())
|
if (st->value.eg() != compute_value<EndGame>())
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -90,7 +90,7 @@ struct StateInfo {
|
||||||
Key key, pawnKey, materialKey;
|
Key key, pawnKey, materialKey;
|
||||||
int castleRights, rule50, pliesFromNull;
|
int castleRights, rule50, pliesFromNull;
|
||||||
Square epSquare;
|
Square epSquare;
|
||||||
Value mgValue, egValue;
|
Score value;
|
||||||
Value npMaterial[2];
|
Value npMaterial[2];
|
||||||
|
|
||||||
PieceType capture;
|
PieceType capture;
|
||||||
|
@ -240,8 +240,7 @@ public:
|
||||||
Key get_material_key() const;
|
Key get_material_key() const;
|
||||||
|
|
||||||
// Incremental evaluation
|
// Incremental evaluation
|
||||||
Value mg_value() const;
|
Score value() const;
|
||||||
Value eg_value() const;
|
|
||||||
Value non_pawn_material(Color c) const;
|
Value non_pawn_material(Color c) const;
|
||||||
Phase game_phase() const;
|
Phase game_phase() const;
|
||||||
template<GamePhase> Value pst_delta(Piece piece, Square from, Square to) const;
|
template<GamePhase> Value pst_delta(Piece piece, Square from, Square to) const;
|
||||||
|
@ -514,12 +513,8 @@ inline Value Position::pst_delta(Piece piece, Square from, Square to) const {
|
||||||
: EgPieceSquareTable[piece][to] - EgPieceSquareTable[piece][from]);
|
: EgPieceSquareTable[piece][to] - EgPieceSquareTable[piece][from]);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value Position::mg_value() const {
|
inline Score Position::value() const {
|
||||||
return st->mgValue;
|
return st->value;
|
||||||
}
|
|
||||||
|
|
||||||
inline Value Position::eg_value() const {
|
|
||||||
return st->egValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value Position::non_pawn_material(Color c) const {
|
inline Value Position::non_pawn_material(Color c) const {
|
||||||
|
|
|
@ -148,8 +148,8 @@ namespace {
|
||||||
else if (token == "eval")
|
else if (token == "eval")
|
||||||
{
|
{
|
||||||
EvalInfo ei;
|
EvalInfo ei;
|
||||||
cout << "Incremental mg: " << RootPosition.mg_value()
|
cout << "Incremental mg: " << RootPosition.value().mg()
|
||||||
<< "\nIncremental eg: " << RootPosition.eg_value()
|
<< "\nIncremental eg: " << RootPosition.value().eg()
|
||||||
<< "\nFull eval: " << evaluate(RootPosition, ei, 0) << endl;
|
<< "\nFull eval: " << evaluate(RootPosition, ei, 0) << endl;
|
||||||
}
|
}
|
||||||
else if (token == "key")
|
else if (token == "key")
|
||||||
|
|
Loading…
Add table
Reference in a new issue