mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00
Remove classical evaluation
since the introduction of NNUE (first released with Stockfish 12), we have maintained the classical evaluation as part of SF in frozen form. The idea that this code could lead to further inputs to the NN or search did not materialize. Now, after five releases, this PR removes the classical evaluation from SF. Even though this evaluation is probably the best of its class, it has become unimportant for the engine's strength, and there is little need to maintain this code (roughly 25% of SF) going forward, or to expend resources on trying to improve its integration in the NNUE eval. Indeed, it had still a very limited use in the current SF, namely for the evaluation of positions that are nearly decided based on material difference, where the speed of the classical evaluation outweights its inaccuracies. This impact on strength is small, roughly 2Elo, and probably decreasing in importance as the TC grows. Potentially, removal of this code could lead to the development of techniques to have faster, but less accurate NN evaluation, for certain positions. STC https://tests.stockfishchess.org/tests/view/64a320173ee09aa549c52157 Elo: -2.35 ± 1.1 (95%) LOS: 0.0% Total: 100000 W: 24916 L: 25592 D: 49492 Ptnml(0-2): 287, 12123, 25841, 11477, 272 nElo: -4.62 ± 2.2 (95%) PairsRatio: 0.95 LTC https://tests.stockfishchess.org/tests/view/64a320293ee09aa549c5215b Elo: -1.74 ± 1.0 (95%) LOS: 0.0% Total: 100000 W: 25010 L: 25512 D: 49478 Ptnml(0-2): 44, 11069, 28270, 10579, 38 nElo: -3.72 ± 2.2 (95%) PairsRatio: 0.96 VLTC SMP https://tests.stockfishchess.org/tests/view/64a3207c3ee09aa549c52168 Elo: -1.70 ± 0.9 (95%) LOS: 0.0% Total: 100000 W: 25673 L: 26162 D: 48165 Ptnml(0-2): 8, 9455, 31569, 8954, 14 nElo: -3.95 ± 2.2 (95%) PairsRatio: 0.95 closes https://github.com/official-stockfish/Stockfish/pull/4674 Bench: 1444646
This commit is contained in:
parent
6a8767a0d5
commit
af110e02ec
16 changed files with 37 additions and 2745 deletions
|
@ -56,8 +56,8 @@ else
|
|||
endif
|
||||
|
||||
### Source and object files
|
||||
SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
|
||||
material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
|
||||
SRCS = benchmark.cpp bitboard.cpp evaluate.cpp main.cpp \
|
||||
misc.cpp movegen.cpp movepick.cpp position.cpp psqt.cpp \
|
||||
search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
|
||||
nnue/evaluate_nnue.cpp nnue/features/half_ka_v2_hm.cpp
|
||||
|
||||
|
|
|
@ -153,24 +153,15 @@ vector<string> setup_bench(const Position& current, istream& is) {
|
|||
list.emplace_back("setoption name Hash value " + ttSize);
|
||||
list.emplace_back("ucinewgame");
|
||||
|
||||
size_t posCounter = 0;
|
||||
|
||||
for (const string& fen : fens)
|
||||
if (fen.find("setoption") != string::npos)
|
||||
list.emplace_back(fen);
|
||||
else
|
||||
{
|
||||
if (evalType == "classical" || (evalType == "mixed" && posCounter % 2 == 0))
|
||||
list.emplace_back("setoption name Use NNUE value false");
|
||||
else if (evalType == "NNUE" || (evalType == "mixed" && posCounter % 2 != 0))
|
||||
list.emplace_back("setoption name Use NNUE value true");
|
||||
list.emplace_back("position fen " + fen);
|
||||
list.emplace_back(go);
|
||||
++posCounter;
|
||||
}
|
||||
|
||||
list.emplace_back("setoption name Use NNUE value true");
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
|
172
src/bitbase.cpp
172
src/bitbase.cpp
|
@ -1,172 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <bitset>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace Stockfish {
|
||||
|
||||
namespace {
|
||||
|
||||
// There are 24 possible pawn squares: files A to D and ranks from 2 to 7.
|
||||
// Positions with the pawn on files E to H will be mirrored before probing.
|
||||
constexpr unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608
|
||||
|
||||
std::bitset<MAX_INDEX> KPKBitbase;
|
||||
|
||||
// A KPK bitbase index is an integer in [0, IndexMax] range
|
||||
//
|
||||
// Information is mapped in a way that minimizes the number of iterations:
|
||||
//
|
||||
// bit 0- 5: white king square (from SQ_A1 to SQ_H8)
|
||||
// bit 6-11: black king square (from SQ_A1 to SQ_H8)
|
||||
// bit 12: side to move (WHITE or BLACK)
|
||||
// bit 13-14: white pawn file (from FILE_A to FILE_D)
|
||||
// bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
|
||||
unsigned index(Color stm, Square bksq, Square wksq, Square psq) {
|
||||
return int(wksq) | (bksq << 6) | (stm << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15);
|
||||
}
|
||||
|
||||
enum Result {
|
||||
INVALID = 0,
|
||||
UNKNOWN = 1,
|
||||
DRAW = 2,
|
||||
WIN = 4
|
||||
};
|
||||
|
||||
Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
|
||||
|
||||
struct KPKPosition {
|
||||
KPKPosition() = default;
|
||||
explicit KPKPosition(unsigned idx);
|
||||
operator Result() const { return result; }
|
||||
Result classify(const std::vector<KPKPosition>& db);
|
||||
|
||||
Color stm;
|
||||
Square ksq[COLOR_NB], psq;
|
||||
Result result;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color stm) {
|
||||
|
||||
assert(file_of(wpsq) <= FILE_D);
|
||||
|
||||
return KPKBitbase[index(stm, bksq, wksq, wpsq)];
|
||||
}
|
||||
|
||||
|
||||
void Bitbases::init() {
|
||||
|
||||
std::vector<KPKPosition> db(MAX_INDEX);
|
||||
unsigned idx, repeat = 1;
|
||||
|
||||
// Initialize db with known win / draw positions
|
||||
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||
db[idx] = KPKPosition(idx);
|
||||
|
||||
// Iterate through the positions until none of the unknown positions can be
|
||||
// changed to either wins or draws (15 cycles needed).
|
||||
while (repeat)
|
||||
for (repeat = idx = 0; idx < MAX_INDEX; ++idx)
|
||||
repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
|
||||
|
||||
// Fill the bitbase with the decisive results
|
||||
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||
if (db[idx] == WIN)
|
||||
KPKBitbase.set(idx);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
KPKPosition::KPKPosition(unsigned idx) {
|
||||
|
||||
ksq[WHITE] = Square((idx >> 0) & 0x3F);
|
||||
ksq[BLACK] = Square((idx >> 6) & 0x3F);
|
||||
stm = Color ((idx >> 12) & 0x01);
|
||||
psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
|
||||
|
||||
// Invalid if two pieces are on the same square or if a king can be captured
|
||||
if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
|
||||
|| ksq[WHITE] == psq
|
||||
|| ksq[BLACK] == psq
|
||||
|| (stm == WHITE && (pawn_attacks_bb(WHITE, psq) & ksq[BLACK])))
|
||||
result = INVALID;
|
||||
|
||||
// Win if the pawn can be promoted without getting captured
|
||||
else if ( stm == WHITE
|
||||
&& rank_of(psq) == RANK_7
|
||||
&& ksq[WHITE] != psq + NORTH
|
||||
&& ( distance(ksq[BLACK], psq + NORTH) > 1
|
||||
|| (distance(ksq[WHITE], psq + NORTH) == 1)))
|
||||
result = WIN;
|
||||
|
||||
// Draw if it is stalemate or the black king can capture the pawn
|
||||
else if ( stm == BLACK
|
||||
&& ( !(attacks_bb<KING>(ksq[BLACK]) & ~(attacks_bb<KING>(ksq[WHITE]) | pawn_attacks_bb(WHITE, psq)))
|
||||
|| (attacks_bb<KING>(ksq[BLACK]) & ~attacks_bb<KING>(ksq[WHITE]) & psq)))
|
||||
result = DRAW;
|
||||
|
||||
// Position will be classified later
|
||||
else
|
||||
result = UNKNOWN;
|
||||
}
|
||||
|
||||
Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
|
||||
|
||||
// White to move: If one move leads to a position classified as WIN, the result
|
||||
// of the current position is WIN. If all moves lead to positions classified
|
||||
// as DRAW, the current position is classified as DRAW, otherwise the current
|
||||
// position is classified as UNKNOWN.
|
||||
//
|
||||
// Black to move: If one move leads to a position classified as DRAW, the result
|
||||
// of the current position is DRAW. If all moves lead to positions classified
|
||||
// as WIN, the position is classified as WIN, otherwise the current position is
|
||||
// classified as UNKNOWN.
|
||||
const Result Good = (stm == WHITE ? WIN : DRAW);
|
||||
const Result Bad = (stm == WHITE ? DRAW : WIN);
|
||||
|
||||
Result r = INVALID;
|
||||
Bitboard b = attacks_bb<KING>(ksq[stm]);
|
||||
|
||||
while (b)
|
||||
r |= stm == WHITE ? db[index(BLACK, ksq[BLACK], pop_lsb(b), psq)]
|
||||
: db[index(WHITE, pop_lsb(b), ksq[WHITE], psq)];
|
||||
|
||||
if (stm == WHITE)
|
||||
{
|
||||
if (rank_of(psq) < RANK_7) // Single push
|
||||
r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH)];
|
||||
|
||||
if ( rank_of(psq) == RANK_2 // Double push
|
||||
&& psq + NORTH != ksq[WHITE]
|
||||
&& psq + NORTH != ksq[BLACK])
|
||||
r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH + NORTH)];
|
||||
}
|
||||
|
||||
return result = r & Good ? Good : r & UNKNOWN ? UNKNOWN : Bad;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace Stockfish
|
|
@ -25,13 +25,6 @@
|
|||
|
||||
namespace Stockfish {
|
||||
|
||||
namespace Bitbases {
|
||||
|
||||
void init();
|
||||
bool probe(Square wksq, Square wpsq, Square bksq, Color us);
|
||||
|
||||
} // namespace Stockfish::Bitbases
|
||||
|
||||
namespace Bitboards {
|
||||
|
||||
void init();
|
||||
|
|
747
src/endgame.cpp
747
src/endgame.cpp
|
@ -1,747 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "endgame.h"
|
||||
#include "movegen.h"
|
||||
|
||||
namespace Stockfish {
|
||||
|
||||
namespace {
|
||||
|
||||
// Used to drive the king towards the edge of the board
|
||||
// in KX vs K and KQ vs KR endgames.
|
||||
// Values range from 27 (center squares) to 90 (in the corners)
|
||||
inline int push_to_edge(Square s) {
|
||||
int rd = edge_distance(rank_of(s)), fd = edge_distance(file_of(s));
|
||||
return 90 - (7 * fd * fd / 2 + 7 * rd * rd / 2);
|
||||
}
|
||||
|
||||
// Used to drive the king towards A1H8 corners in KBN vs K endgames.
|
||||
// Values range from 0 on A8H1 diagonal to 7 in A1H8 corners
|
||||
inline int push_to_corner(Square s) {
|
||||
return abs(7 - rank_of(s) - file_of(s));
|
||||
}
|
||||
|
||||
// Drive a piece close to or away from another piece
|
||||
inline int push_close(Square s1, Square s2) { return 140 - 20 * distance(s1, s2); }
|
||||
inline int push_away(Square s1, Square s2) { return 120 - push_close(s1, s2); }
|
||||
|
||||
#ifndef NDEBUG
|
||||
bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) {
|
||||
return pos.non_pawn_material(c) == npm && pos.count<PAWN>(c) == pawnsCnt;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Map the square as if strongSide is white and strongSide's only pawn
|
||||
// is on the left half of the board.
|
||||
Square normalize(const Position& pos, Color strongSide, Square sq) {
|
||||
|
||||
assert(pos.count<PAWN>(strongSide) == 1);
|
||||
|
||||
if (file_of(pos.square<PAWN>(strongSide)) >= FILE_E)
|
||||
sq = flip_file(sq);
|
||||
|
||||
return strongSide == WHITE ? sq : flip_rank(sq);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
namespace Endgames {
|
||||
|
||||
std::pair<Map<Value>, Map<ScaleFactor>> maps;
|
||||
|
||||
void init() {
|
||||
|
||||
add<KPK>("KPK");
|
||||
add<KNNK>("KNNK");
|
||||
add<KBNK>("KBNK");
|
||||
add<KRKP>("KRKP");
|
||||
add<KRKB>("KRKB");
|
||||
add<KRKN>("KRKN");
|
||||
add<KQKP>("KQKP");
|
||||
add<KQKR>("KQKR");
|
||||
add<KNNKP>("KNNKP");
|
||||
|
||||
add<KRPKR>("KRPKR");
|
||||
add<KRPKB>("KRPKB");
|
||||
add<KBPKB>("KBPKB");
|
||||
add<KBPKN>("KBPKN");
|
||||
add<KBPPKB>("KBPPKB");
|
||||
add<KRPPKRP>("KRPPKRP");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Mate with KX vs K. This function is used to evaluate positions with
|
||||
/// king and plenty of material vs a lone king. It simply gives the
|
||||
/// attacking side a bonus for driving the defending king towards the edge
|
||||
/// of the board, and for keeping the distance between the two kings small.
|
||||
template<>
|
||||
Value Endgame<KXK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
assert(!pos.checkers()); // Eval is never called when in check
|
||||
|
||||
// Stalemate detection with lone king
|
||||
if (pos.side_to_move() == weakSide && !MoveList<LEGAL>(pos).size())
|
||||
return VALUE_DRAW;
|
||||
|
||||
Square strongKing = pos.square<KING>(strongSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
|
||||
Value result = pos.non_pawn_material(strongSide)
|
||||
+ pos.count<PAWN>(strongSide) * PawnValueEg
|
||||
+ push_to_edge(weakKing)
|
||||
+ push_close(strongKing, weakKing);
|
||||
|
||||
if ( pos.count<QUEEN>(strongSide)
|
||||
|| pos.count<ROOK>(strongSide)
|
||||
||(pos.count<BISHOP>(strongSide) && pos.count<KNIGHT>(strongSide))
|
||||
|| ( (pos.pieces(strongSide, BISHOP) & ~DarkSquares)
|
||||
&& (pos.pieces(strongSide, BISHOP) & DarkSquares)))
|
||||
result = std::min(result + VALUE_KNOWN_WIN, VALUE_TB_WIN_IN_MAX_PLY - 1);
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// Mate with KBN vs K. This is similar to KX vs K, but we have to drive the
|
||||
/// defending king towards a corner square that our bishop attacks.
|
||||
template<>
|
||||
Value Endgame<KBNK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, KnightValueMg + BishopValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
Square strongKing = pos.square<KING>(strongSide);
|
||||
Square strongBishop = pos.square<BISHOP>(strongSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
|
||||
// If our bishop does not attack A1/H8, we flip the enemy king square
|
||||
// to drive to opposite corners (A8/H1).
|
||||
|
||||
Value result = (VALUE_KNOWN_WIN + 3520)
|
||||
+ push_close(strongKing, weakKing)
|
||||
+ 420 * push_to_corner(opposite_colors(strongBishop, SQ_A1) ? flip_file(weakKing) : weakKing);
|
||||
|
||||
assert(abs(result) < VALUE_TB_WIN_IN_MAX_PLY);
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KP vs K. This endgame is evaluated with the help of a bitbase
|
||||
template<>
|
||||
Value Endgame<KPK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, VALUE_ZERO, 1));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square strongKing = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||
Square strongPawn = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||
Square weakKing = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||
|
||||
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
||||
|
||||
if (!Bitbases::probe(strongKing, strongPawn, weakKing, us))
|
||||
return VALUE_DRAW;
|
||||
|
||||
Value result = VALUE_KNOWN_WIN + PawnValueEg + Value(rank_of(strongPawn));
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KR vs KP. This is a somewhat tricky endgame to evaluate precisely without
|
||||
/// a bitbase. The function below returns drawish scores when the pawn is
|
||||
/// far advanced with support of the king, while the attacking king is far
|
||||
/// away.
|
||||
template<>
|
||||
Value Endgame<KRKP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
Square strongKing = pos.square<KING>(strongSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Square strongRook = pos.square<ROOK>(strongSide);
|
||||
Square weakPawn = pos.square<PAWN>(weakSide);
|
||||
Square queeningSquare = make_square(file_of(weakPawn), relative_rank(weakSide, RANK_8));
|
||||
Value result;
|
||||
|
||||
// If the stronger side's king is in front of the pawn, it's a win
|
||||
if (forward_file_bb(strongSide, strongKing) & weakPawn)
|
||||
result = RookValueEg - distance(strongKing, weakPawn);
|
||||
|
||||
// If the weaker side's king is too far from the pawn and the rook,
|
||||
// it's a win.
|
||||
else if ( distance(weakKing, weakPawn) >= 3 + (pos.side_to_move() == weakSide)
|
||||
&& distance(weakKing, strongRook) >= 3)
|
||||
result = RookValueEg - distance(strongKing, weakPawn);
|
||||
|
||||
// If the pawn is far advanced and supported by the defending king,
|
||||
// the position is drawish
|
||||
else if ( relative_rank(strongSide, weakKing) <= RANK_3
|
||||
&& distance(weakKing, weakPawn) == 1
|
||||
&& relative_rank(strongSide, strongKing) >= RANK_4
|
||||
&& distance(strongKing, weakPawn) > 2 + (pos.side_to_move() == strongSide))
|
||||
result = Value(80) - 8 * distance(strongKing, weakPawn);
|
||||
|
||||
else
|
||||
result = Value(200) - 8 * ( distance(strongKing, weakPawn + pawn_push(weakSide))
|
||||
- distance(weakKing, weakPawn + pawn_push(weakSide))
|
||||
- distance(weakPawn, queeningSquare));
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KR vs KB. This is very simple, and always returns drawish scores. The
|
||||
/// score is slightly bigger when the defending king is close to the edge.
|
||||
template<>
|
||||
Value Endgame<KRKB>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Value result = Value(push_to_edge(pos.square<KING>(weakSide)));
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KR vs KN. The attacking side has slightly better winning chances than
|
||||
/// in KR vs KB, particularly if the king and the knight are far apart.
|
||||
template<>
|
||||
Value Endgame<KRKN>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
||||
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Square weakKnight = pos.square<KNIGHT>(weakSide);
|
||||
Value result = Value(push_to_edge(weakKing) + push_away(weakKing, weakKnight));
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KQ vs KP. In general, this is a win for the stronger side, but there are a
|
||||
/// few important exceptions. A pawn on 7th rank and on the A,C,F or H files
|
||||
/// with a king positioned next to it can be a draw, so in that case, we only
|
||||
/// use the distance between the kings.
|
||||
template<>
|
||||
Value Endgame<KQKP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
Square strongKing = pos.square<KING>(strongSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Square weakPawn = pos.square<PAWN>(weakSide);
|
||||
|
||||
Value result = Value(push_close(strongKing, weakKing));
|
||||
|
||||
if ( relative_rank(weakSide, weakPawn) != RANK_7
|
||||
|| distance(weakKing, weakPawn) != 1
|
||||
|| ((FileBBB | FileDBB | FileEBB | FileGBB) & weakPawn))
|
||||
result += QueenValueEg - PawnValueEg;
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KQ vs KR. This is almost identical to KX vs K: we give the attacking
|
||||
/// king a bonus for having the kings close together, and for forcing the
|
||||
/// defending king towards the edge. If we also take care to avoid null move for
|
||||
/// the defending side in the search, this is usually sufficient to win KQ vs KR.
|
||||
template<>
|
||||
Value Endgame<KQKR>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
||||
|
||||
Square strongKing = pos.square<KING>(strongSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
|
||||
Value result = QueenValueEg
|
||||
- RookValueEg
|
||||
+ push_to_edge(weakKing)
|
||||
+ push_close(strongKing, weakKing);
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// KNN vs KP. Very drawish, but there are some mate opportunities if we can
|
||||
/// press the weakSide King to a corner before the pawn advances too much.
|
||||
template<>
|
||||
Value Endgame<KNNKP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, 2 * KnightValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Square weakPawn = pos.square<PAWN>(weakSide);
|
||||
|
||||
Value result = PawnValueEg
|
||||
+ 2 * push_to_edge(weakKing)
|
||||
- 10 * relative_rank(weakSide, weakPawn);
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
|
||||
/// Some cases of trivial draws
|
||||
template<> Value Endgame<KNNK>::operator()(const Position&) const { return VALUE_DRAW; }
|
||||
|
||||
|
||||
/// KB and one or more pawns vs K. It checks for draws with rook pawns and
|
||||
/// a bishop of the wrong color. If such a draw is detected, SCALE_FACTOR_DRAW
|
||||
/// is returned. If not, the return value is SCALE_FACTOR_NONE, i.e. no scaling
|
||||
/// will be used.
|
||||
template<>
|
||||
ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(pos.non_pawn_material(strongSide) == BishopValueMg);
|
||||
assert(pos.count<PAWN>(strongSide) >= 1);
|
||||
|
||||
// No assertions about the material of weakSide, because we want draws to
|
||||
// be detected even when the weaker side has some pawns.
|
||||
|
||||
Bitboard strongPawns = pos.pieces(strongSide, PAWN);
|
||||
Bitboard allPawns = pos.pieces(PAWN);
|
||||
|
||||
Square strongBishop = pos.square<BISHOP>(strongSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Square strongKing = pos.square<KING>(strongSide);
|
||||
|
||||
// All strongSide pawns are on a single rook file?
|
||||
if (!(strongPawns & ~FileABB) || !(strongPawns & ~FileHBB))
|
||||
{
|
||||
Square queeningSquare = relative_square(strongSide, make_square(file_of(lsb(strongPawns)), RANK_8));
|
||||
|
||||
if ( opposite_colors(queeningSquare, strongBishop)
|
||||
&& distance(queeningSquare, weakKing) <= 1)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
}
|
||||
|
||||
// If all the pawns are on the same B or G file, then it's potentially a draw
|
||||
if ((!(allPawns & ~FileBBB) || !(allPawns & ~FileGBB))
|
||||
&& pos.non_pawn_material(weakSide) == 0
|
||||
&& pos.count<PAWN>(weakSide) >= 1)
|
||||
{
|
||||
// Get the least advanced weakSide pawn
|
||||
Square weakPawn = frontmost_sq(strongSide, pos.pieces(weakSide, PAWN));
|
||||
|
||||
// There's potential for a draw if our pawn is blocked on the 7th rank,
|
||||
// the bishop cannot attack it or they only have one pawn left.
|
||||
if ( relative_rank(strongSide, weakPawn) == RANK_7
|
||||
&& (strongPawns & (weakPawn + pawn_push(weakSide)))
|
||||
&& (opposite_colors(strongBishop, weakPawn) || !more_than_one(strongPawns)))
|
||||
{
|
||||
int strongKingDist = distance(weakPawn, strongKing);
|
||||
int weakKingDist = distance(weakPawn, weakKing);
|
||||
|
||||
// It's a draw if the weak king is on its back two ranks, within 2
|
||||
// squares of the blocking pawn and the strong king is not
|
||||
// closer. (I think this rule only fails in practically
|
||||
// unreachable positions such as 5k1K/6p1/6P1/8/8/3B4/8/8 w
|
||||
// and positions where qsearch will immediately correct the
|
||||
// problem such as 8/4k1p1/6P1/1K6/3B4/8/8/8 w).
|
||||
if ( relative_rank(strongSide, weakKing) >= RANK_7
|
||||
&& weakKingDist <= 2
|
||||
&& weakKingDist <= strongKingDist)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
}
|
||||
}
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KQ vs KR and one or more pawns. It tests for fortress draws with a rook on
|
||||
/// the third rank defended by a pawn.
|
||||
template<>
|
||||
ScaleFactor Endgame<KQKRPs>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||
assert(pos.count<ROOK>(weakSide) == 1);
|
||||
assert(pos.count<PAWN>(weakSide) >= 1);
|
||||
|
||||
Square strongKing = pos.square<KING>(strongSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Square weakRook = pos.square<ROOK>(weakSide);
|
||||
|
||||
if ( relative_rank(weakSide, weakKing) <= RANK_2
|
||||
&& relative_rank(weakSide, strongKing) >= RANK_4
|
||||
&& relative_rank(weakSide, weakRook) == RANK_3
|
||||
&& ( pos.pieces(weakSide, PAWN)
|
||||
& attacks_bb<KING>(weakKing)
|
||||
& pawn_attacks_bb(strongSide, weakRook)))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KRP vs KR. This function knows a handful of the most important classes of
|
||||
/// drawn positions, but is far from perfect. It would probably be a good idea
|
||||
/// to add more knowledge in the future.
|
||||
///
|
||||
/// It would also be nice to rewrite the actual code for this function,
|
||||
/// which is mostly copied from Glaurung 1.x, and isn't very pretty.
|
||||
template<>
|
||||
ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square strongKing = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||
Square strongRook = normalize(pos, strongSide, pos.square<ROOK>(strongSide));
|
||||
Square strongPawn = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||
Square weakKing = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||
Square weakRook = normalize(pos, strongSide, pos.square<ROOK>(weakSide));
|
||||
|
||||
File pawnFile = file_of(strongPawn);
|
||||
Rank pawnRank = rank_of(strongPawn);
|
||||
Square queeningSquare = make_square(pawnFile, RANK_8);
|
||||
int tempo = (pos.side_to_move() == strongSide);
|
||||
|
||||
// If the pawn is not too far advanced and the defending king defends the
|
||||
// queening square, use the third-rank defence.
|
||||
if ( pawnRank <= RANK_5
|
||||
&& distance(weakKing, queeningSquare) <= 1
|
||||
&& strongKing <= SQ_H5
|
||||
&& (rank_of(weakRook) == RANK_6 || (pawnRank <= RANK_3 && rank_of(strongRook) != RANK_6)))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// The defending side saves a draw by checking from behind in case the pawn
|
||||
// has advanced to the 6th rank with the king behind.
|
||||
if ( pawnRank == RANK_6
|
||||
&& distance(weakKing, queeningSquare) <= 1
|
||||
&& rank_of(strongKing) + tempo <= RANK_6
|
||||
&& (rank_of(weakRook) == RANK_1 || (!tempo && distance<File>(weakRook, strongPawn) >= 3)))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
if ( pawnRank >= RANK_6
|
||||
&& weakKing == queeningSquare
|
||||
&& rank_of(weakRook) == RANK_1
|
||||
&& (!tempo || distance(strongKing, strongPawn) >= 2))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7
|
||||
// and the black rook is behind the pawn.
|
||||
if ( strongPawn == SQ_A7
|
||||
&& strongRook == SQ_A8
|
||||
&& (weakKing == SQ_H7 || weakKing == SQ_G7)
|
||||
&& file_of(weakRook) == FILE_A
|
||||
&& (rank_of(weakRook) <= RANK_3 || file_of(strongKing) >= FILE_D || rank_of(strongKing) <= RANK_5))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// If the defending king blocks the pawn and the attacking king is too far
|
||||
// away, it's a draw.
|
||||
if ( pawnRank <= RANK_5
|
||||
&& weakKing == strongPawn + NORTH
|
||||
&& distance(strongKing, strongPawn) - tempo >= 2
|
||||
&& distance(strongKing, weakRook) - tempo >= 2)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// Pawn on the 7th rank supported by the rook from behind usually wins if the
|
||||
// attacking king is closer to the queening square than the defending king,
|
||||
// and the defending king cannot gain tempi by threatening the attacking rook.
|
||||
if ( pawnRank == RANK_7
|
||||
&& pawnFile != FILE_A
|
||||
&& file_of(strongRook) == pawnFile
|
||||
&& strongRook != queeningSquare
|
||||
&& (distance(strongKing, queeningSquare) < distance(weakKing, queeningSquare) - 2 + tempo)
|
||||
&& (distance(strongKing, queeningSquare) < distance(weakKing, strongRook) + tempo))
|
||||
return ScaleFactor(SCALE_FACTOR_MAX - 2 * distance(strongKing, queeningSquare));
|
||||
|
||||
// Similar to the above, but with the pawn further back
|
||||
if ( pawnFile != FILE_A
|
||||
&& file_of(strongRook) == pawnFile
|
||||
&& strongRook < strongPawn
|
||||
&& (distance(strongKing, queeningSquare) < distance(weakKing, queeningSquare) - 2 + tempo)
|
||||
&& (distance(strongKing, strongPawn + NORTH) < distance(weakKing, strongPawn + NORTH) - 2 + tempo)
|
||||
&& ( distance(weakKing, strongRook) + tempo >= 3
|
||||
|| ( distance(strongKing, queeningSquare) < distance(weakKing, strongRook) + tempo
|
||||
&& (distance(strongKing, strongPawn + NORTH) < distance(weakKing, strongPawn) + tempo))))
|
||||
return ScaleFactor( SCALE_FACTOR_MAX
|
||||
- 8 * distance(strongPawn, queeningSquare)
|
||||
- 2 * distance(strongKing, queeningSquare));
|
||||
|
||||
// If the pawn is not far advanced and the defending king is somewhere in
|
||||
// the pawn's path, it's probably a draw.
|
||||
if (pawnRank <= RANK_4 && weakKing > strongPawn)
|
||||
{
|
||||
if (file_of(weakKing) == file_of(strongPawn))
|
||||
return ScaleFactor(10);
|
||||
if ( distance<File>(weakKing, strongPawn) == 1
|
||||
&& distance(strongKing, weakKing) > 2)
|
||||
return ScaleFactor(24 - 2 * distance(strongKing, weakKing));
|
||||
}
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
template<>
|
||||
ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
// Test for a rook pawn
|
||||
if (pos.pieces(PAWN) & (FileABB | FileHBB))
|
||||
{
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Square weakBishop = pos.square<BISHOP>(weakSide);
|
||||
Square strongKing = pos.square<KING>(strongSide);
|
||||
Square strongPawn = pos.square<PAWN>(strongSide);
|
||||
Rank pawnRank = relative_rank(strongSide, strongPawn);
|
||||
Direction push = pawn_push(strongSide);
|
||||
|
||||
// If the pawn is on the 5th rank and the pawn (currently) is on
|
||||
// the same color square as the bishop then there is a chance of
|
||||
// a fortress. Depending on the king position give a moderate
|
||||
// reduction or a stronger one if the defending king is near the
|
||||
// corner but not trapped there.
|
||||
if (pawnRank == RANK_5 && !opposite_colors(weakBishop, strongPawn))
|
||||
{
|
||||
int d = distance(strongPawn + 3 * push, weakKing);
|
||||
|
||||
if (d <= 2 && !(d == 0 && weakKing == strongKing + 2 * push))
|
||||
return ScaleFactor(24);
|
||||
else
|
||||
return ScaleFactor(48);
|
||||
}
|
||||
|
||||
// When the pawn has moved to the 6th rank we can be fairly sure
|
||||
// it's drawn if the bishop attacks the square in front of the
|
||||
// pawn from a reasonable distance and the defending king is near
|
||||
// the corner
|
||||
if ( pawnRank == RANK_6
|
||||
&& distance(strongPawn + 2 * push, weakKing) <= 1
|
||||
&& (attacks_bb<BISHOP>(weakBishop) & (strongPawn + push))
|
||||
&& distance<File>(weakBishop, strongPawn) >= 2)
|
||||
return ScaleFactor(8);
|
||||
}
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
/// KRPP vs KRP. There is just a single rule: if the stronger side has no passed
|
||||
/// pawns and the defending king is actively placed, the position is drawish.
|
||||
template<>
|
||||
ScaleFactor Endgame<KRPPKRP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 2));
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 1));
|
||||
|
||||
Square strongPawn1 = lsb(pos.pieces(strongSide, PAWN));
|
||||
Square strongPawn2 = msb(pos.pieces(strongSide, PAWN));
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
|
||||
// Does the stronger side have a passed pawn?
|
||||
if (pos.pawn_passed(strongSide, strongPawn1) || pos.pawn_passed(strongSide, strongPawn2))
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
Rank pawnRank = std::max(relative_rank(strongSide, strongPawn1), relative_rank(strongSide, strongPawn2));
|
||||
|
||||
if ( distance<File>(weakKing, strongPawn1) <= 1
|
||||
&& distance<File>(weakKing, strongPawn2) <= 1
|
||||
&& relative_rank(strongSide, weakKing) > pawnRank)
|
||||
{
|
||||
assert(pawnRank > RANK_1 && pawnRank < RANK_7);
|
||||
return ScaleFactor(7 * pawnRank);
|
||||
}
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// K and two or more pawns vs K. There is just a single rule here: if all pawns
|
||||
/// are on the same rook file and are blocked by the defending king, it's a draw.
|
||||
template<>
|
||||
ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
|
||||
|
||||
assert(pos.non_pawn_material(strongSide) == VALUE_ZERO);
|
||||
assert(pos.count<PAWN>(strongSide) >= 2);
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Bitboard strongPawns = pos.pieces(strongSide, PAWN);
|
||||
|
||||
// If all pawns are ahead of the king on a single rook file, it's a draw.
|
||||
if ( !(strongPawns & ~(FileABB | FileHBB))
|
||||
&& !(strongPawns & ~passed_pawn_span(weakSide, weakKing)))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KBP vs KB. There are two rules: if the defending king is somewhere along the
|
||||
/// path of the pawn, and the square of the king is not of the same color as the
|
||||
/// stronger side's bishop, it's a draw. If the two bishops have opposite color,
|
||||
/// it's almost always a draw.
|
||||
template<>
|
||||
ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Square strongPawn = pos.square<PAWN>(strongSide);
|
||||
Square strongBishop = pos.square<BISHOP>(strongSide);
|
||||
Square weakBishop = pos.square<BISHOP>(weakSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
|
||||
// Case 1: Defending king blocks the pawn, and cannot be driven away
|
||||
if ( (forward_file_bb(strongSide, strongPawn) & weakKing)
|
||||
&& ( opposite_colors(weakKing, strongBishop)
|
||||
|| relative_rank(strongSide, weakKing) <= RANK_6))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
// Case 2: Opposite colored bishops
|
||||
if (opposite_colors(strongBishop, weakBishop))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KBPP vs KB. It detects a few basic draws with opposite-colored bishops
|
||||
template<>
|
||||
ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 2));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Square strongBishop = pos.square<BISHOP>(strongSide);
|
||||
Square weakBishop = pos.square<BISHOP>(weakSide);
|
||||
|
||||
if (!opposite_colors(strongBishop, weakBishop))
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
Square strongPawn1 = lsb(pos.pieces(strongSide, PAWN));
|
||||
Square strongPawn2 = msb(pos.pieces(strongSide, PAWN));
|
||||
Square blockSq1, blockSq2;
|
||||
|
||||
if (relative_rank(strongSide, strongPawn1) > relative_rank(strongSide, strongPawn2))
|
||||
{
|
||||
blockSq1 = strongPawn1 + pawn_push(strongSide);
|
||||
blockSq2 = make_square(file_of(strongPawn2), rank_of(strongPawn1));
|
||||
}
|
||||
else
|
||||
{
|
||||
blockSq1 = strongPawn2 + pawn_push(strongSide);
|
||||
blockSq2 = make_square(file_of(strongPawn1), rank_of(strongPawn2));
|
||||
}
|
||||
|
||||
switch (distance<File>(strongPawn1, strongPawn2))
|
||||
{
|
||||
case 0:
|
||||
// Both pawns are on the same file. It's an easy draw if the defender firmly
|
||||
// controls some square in the frontmost pawn's path.
|
||||
if ( file_of(weakKing) == file_of(blockSq1)
|
||||
&& relative_rank(strongSide, weakKing) >= relative_rank(strongSide, blockSq1)
|
||||
&& opposite_colors(weakKing, strongBishop))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
else
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
case 1:
|
||||
// Pawns on adjacent files. It's a draw if the defender firmly controls the
|
||||
// square in front of the frontmost pawn's path, and the square diagonally
|
||||
// behind this square on the file of the other pawn.
|
||||
if ( weakKing == blockSq1
|
||||
&& opposite_colors(weakKing, strongBishop)
|
||||
&& ( weakBishop == blockSq2
|
||||
|| (attacks_bb<BISHOP>(blockSq2, pos.pieces()) & pos.pieces(weakSide, BISHOP))
|
||||
|| distance<Rank>(strongPawn1, strongPawn2) >= 2))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
else if ( weakKing == blockSq2
|
||||
&& opposite_colors(weakKing, strongBishop)
|
||||
&& ( weakBishop == blockSq1
|
||||
|| (attacks_bb<BISHOP>(blockSq1, pos.pieces()) & pos.pieces(weakSide, BISHOP))))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
else
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
default:
|
||||
// The pawns are not on the same file or adjacent files. No scaling.
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// KBP vs KN. There is a single rule: if the defending king is somewhere along
|
||||
/// the path of the pawn, and the square of the king is not of the same color as
|
||||
/// the stronger side's bishop, it's a draw.
|
||||
template<>
|
||||
ScaleFactor Endgame<KBPKN>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
||||
|
||||
Square strongPawn = pos.square<PAWN>(strongSide);
|
||||
Square strongBishop = pos.square<BISHOP>(strongSide);
|
||||
Square weakKing = pos.square<KING>(weakSide);
|
||||
|
||||
if ( file_of(weakKing) == file_of(strongPawn)
|
||||
&& relative_rank(strongSide, strongPawn) < relative_rank(strongSide, weakKing)
|
||||
&& ( opposite_colors(weakKing, strongBishop)
|
||||
|| relative_rank(strongSide, weakKing) <= RANK_6))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// KP vs KP. This is done by removing the weakest side's pawn and probing the
|
||||
/// KP vs K bitbase: if the weakest side has a draw without the pawn, it probably
|
||||
/// has at least a draw with the pawn as well. The exception is when the stronger
|
||||
/// side's pawn is far advanced and not on a rook file; in this case it is often
|
||||
/// possible to win (e.g. 8/4k3/3p4/3P4/6K1/8/8/8 w - - 0 1).
|
||||
template<>
|
||||
ScaleFactor Endgame<KPKP>::operator()(const Position& pos) const {
|
||||
|
||||
assert(verify_material(pos, strongSide, VALUE_ZERO, 1));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square strongKing = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||
Square weakKing = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||
Square strongPawn = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||
|
||||
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
||||
|
||||
// If the pawn has advanced to the fifth rank or further, and is not a
|
||||
// rook pawn, it's too dangerous to assume that it's at least a draw.
|
||||
if (rank_of(strongPawn) >= RANK_5 && file_of(strongPawn) != FILE_A)
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
// Probe the KPK bitbase with the weakest side's pawn removed. If it's a draw,
|
||||
// it's probably at least a draw even with the pawn.
|
||||
return Bitbases::probe(strongKing, strongPawn, weakKing, us) ? SCALE_FACTOR_NONE : SCALE_FACTOR_DRAW;
|
||||
}
|
||||
|
||||
} // namespace Stockfish
|
126
src/endgame.h
126
src/endgame.h
|
@ -1,126 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef ENDGAME_H_INCLUDED
|
||||
#define ENDGAME_H_INCLUDED
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace Stockfish {
|
||||
|
||||
/// EndgameCode lists all supported endgame functions by corresponding codes
|
||||
|
||||
enum EndgameCode {
|
||||
|
||||
EVALUATION_FUNCTIONS,
|
||||
KNNK, // KNN vs K
|
||||
KNNKP, // KNN vs KP
|
||||
KXK, // Generic "mate lone king" eval
|
||||
KBNK, // KBN vs K
|
||||
KPK, // KP vs K
|
||||
KRKP, // KR vs KP
|
||||
KRKB, // KR vs KB
|
||||
KRKN, // KR vs KN
|
||||
KQKP, // KQ vs KP
|
||||
KQKR, // KQ vs KR
|
||||
|
||||
SCALING_FUNCTIONS,
|
||||
KBPsK, // KB and pawns vs K
|
||||
KQKRPs, // KQ vs KR and pawns
|
||||
KRPKR, // KRP vs KR
|
||||
KRPKB, // KRP vs KB
|
||||
KRPPKRP, // KRPP vs KRP
|
||||
KPsK, // K and pawns vs K
|
||||
KBPKB, // KBP vs KB
|
||||
KBPPKB, // KBPP vs KB
|
||||
KBPKN, // KBP vs KN
|
||||
KPKP // KP vs KP
|
||||
};
|
||||
|
||||
|
||||
/// Endgame functions can be of two types depending on whether they return a
|
||||
/// Value or a ScaleFactor.
|
||||
|
||||
template<EndgameCode E> using
|
||||
eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
|
||||
|
||||
|
||||
/// Base and derived functors for endgame evaluation and scaling functions
|
||||
|
||||
template<typename T>
|
||||
struct EndgameBase {
|
||||
|
||||
explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {}
|
||||
virtual ~EndgameBase() = default;
|
||||
virtual T operator()(const Position&) const = 0;
|
||||
|
||||
const Color strongSide, weakSide;
|
||||
};
|
||||
|
||||
|
||||
template<EndgameCode E, typename T = eg_type<E>>
|
||||
struct Endgame : public EndgameBase<T> {
|
||||
|
||||
explicit Endgame(Color c) : EndgameBase<T>(c) {}
|
||||
T operator()(const Position&) const override;
|
||||
};
|
||||
|
||||
|
||||
/// The Endgames namespace handles the pointers to endgame evaluation and scaling
|
||||
/// base objects in two std::map. We use polymorphism to invoke the actual
|
||||
/// endgame function by calling its virtual operator().
|
||||
|
||||
namespace Endgames {
|
||||
|
||||
template<typename T> using Ptr = std::unique_ptr<EndgameBase<T>>;
|
||||
template<typename T> using Map = std::unordered_map<Key, Ptr<T>>;
|
||||
|
||||
extern std::pair<Map<Value>, Map<ScaleFactor>> maps;
|
||||
|
||||
void init();
|
||||
|
||||
template<typename T>
|
||||
Map<T>& map() {
|
||||
return std::get<std::is_same<T, ScaleFactor>::value>(maps);
|
||||
}
|
||||
|
||||
template<EndgameCode E, typename T = eg_type<E>>
|
||||
void add(const std::string& code) {
|
||||
|
||||
StateInfo st;
|
||||
map<T>()[Position().set(code, WHITE, &st).material_key()] = Ptr<T>(new Endgame<E>(WHITE));
|
||||
map<T>()[Position().set(code, BLACK, &st).material_key()] = Ptr<T>(new Endgame<E>(BLACK));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const EndgameBase<T>* probe(Key key) {
|
||||
auto it = map<T>().find(key);
|
||||
return it != map<T>().end() ? it->second.get() : nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Stockfish
|
||||
|
||||
#endif // #ifndef ENDGAME_H_INCLUDED
|
992
src/evaluate.cpp
992
src/evaluate.cpp
File diff suppressed because it is too large
Load diff
|
@ -19,7 +19,6 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "endgame.h"
|
||||
#include "position.h"
|
||||
#include "psqt.h"
|
||||
#include "search.h"
|
||||
|
@ -40,8 +39,6 @@ int main(int argc, char* argv[]) {
|
|||
PSQT::init();
|
||||
Bitboards::init();
|
||||
Position::init();
|
||||
Bitbases::init();
|
||||
Endgames::init();
|
||||
Threads.set(size_t(Options["Threads"]));
|
||||
Search::clear(); // After threads are up
|
||||
Eval::NNUE::init();
|
||||
|
|
229
src/material.cpp
229
src/material.cpp
|
@ -1,229 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring> // For std::memset
|
||||
|
||||
#include "material.h"
|
||||
#include "thread.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Stockfish {
|
||||
|
||||
namespace {
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
// Polynomial material imbalance parameters
|
||||
|
||||
// One Score parameter for each pair (our piece, another of our pieces)
|
||||
constexpr Score QuadraticOurs[][PIECE_TYPE_NB] = {
|
||||
// OUR PIECE 2
|
||||
// bishop pair pawn knight bishop rook queen
|
||||
{S(1419, 1455) }, // Bishop pair
|
||||
{S( 101, 28), S( 37, 39) }, // Pawn
|
||||
{S( 57, 64), S(249, 187), S(-49, -62) }, // Knight OUR PIECE 1
|
||||
{S( 0, 0), S(118, 137), S( 10, 27), S( 0, 0) }, // Bishop
|
||||
{S( -63, -68), S( -5, 3), S(100, 81), S(132, 118), S(-246, -244) }, // Rook
|
||||
{S(-210, -211), S( 37, 14), S(147, 141), S(161, 105), S(-158, -174), S(-9,-31) } // Queen
|
||||
};
|
||||
|
||||
// One Score parameter for each pair (our piece, their piece)
|
||||
constexpr Score QuadraticTheirs[][PIECE_TYPE_NB] = {
|
||||
// THEIR PIECE
|
||||
// bishop pair pawn knight bishop rook queen
|
||||
{ }, // Bishop pair
|
||||
{S( 33, 30) }, // Pawn
|
||||
{S( 46, 18), S(106, 84) }, // Knight OUR PIECE
|
||||
{S( 75, 35), S( 59, 44), S( 60, 15) }, // Bishop
|
||||
{S( 26, 35), S( 6, 22), S( 38, 39), S(-12, -2) }, // Rook
|
||||
{S( 97, 93), S(100, 163), S(-58, -91), S(112, 192), S(276, 225) } // Queen
|
||||
};
|
||||
|
||||
#undef S
|
||||
|
||||
// Endgame evaluation and scaling functions are accessed directly and not through
|
||||
// the function maps because they correspond to more than one material hash key.
|
||||
Endgame<KXK> EvaluateKXK[] = { Endgame<KXK>(WHITE), Endgame<KXK>(BLACK) };
|
||||
|
||||
Endgame<KBPsK> ScaleKBPsK[] = { Endgame<KBPsK>(WHITE), Endgame<KBPsK>(BLACK) };
|
||||
Endgame<KQKRPs> ScaleKQKRPs[] = { Endgame<KQKRPs>(WHITE), Endgame<KQKRPs>(BLACK) };
|
||||
Endgame<KPsK> ScaleKPsK[] = { Endgame<KPsK>(WHITE), Endgame<KPsK>(BLACK) };
|
||||
Endgame<KPKP> ScaleKPKP[] = { Endgame<KPKP>(WHITE), Endgame<KPKP>(BLACK) };
|
||||
|
||||
// Helper used to detect a given material distribution
|
||||
bool is_KXK(const Position& pos, Color us) {
|
||||
return !more_than_one(pos.pieces(~us))
|
||||
&& pos.non_pawn_material(us) >= RookValueMg;
|
||||
}
|
||||
|
||||
bool is_KBPsK(const Position& pos, Color us) {
|
||||
return pos.non_pawn_material(us) == BishopValueMg
|
||||
&& pos.count<PAWN>(us) >= 1;
|
||||
}
|
||||
|
||||
bool is_KQKRPs(const Position& pos, Color us) {
|
||||
return !pos.count<PAWN>(us)
|
||||
&& pos.non_pawn_material(us) == QueenValueMg
|
||||
&& pos.count<ROOK>(~us) == 1
|
||||
&& pos.count<PAWN>(~us) >= 1;
|
||||
}
|
||||
|
||||
|
||||
/// imbalance() calculates the imbalance by comparing the piece count of each
|
||||
/// piece type for both colors.
|
||||
|
||||
template<Color Us>
|
||||
Score imbalance(const int pieceCount[][PIECE_TYPE_NB]) {
|
||||
|
||||
constexpr Color Them = ~Us;
|
||||
|
||||
Score bonus = SCORE_ZERO;
|
||||
|
||||
// Second-degree polynomial material imbalance, by Tord Romstad
|
||||
for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
||||
{
|
||||
if (!pieceCount[Us][pt1])
|
||||
continue;
|
||||
|
||||
int v = QuadraticOurs[pt1][pt1] * pieceCount[Us][pt1];
|
||||
|
||||
for (int pt2 = NO_PIECE_TYPE; pt2 < pt1; ++pt2)
|
||||
v += QuadraticOurs[pt1][pt2] * pieceCount[Us][pt2]
|
||||
+ QuadraticTheirs[pt1][pt2] * pieceCount[Them][pt2];
|
||||
|
||||
bonus += pieceCount[Us][pt1] * v;
|
||||
}
|
||||
|
||||
return bonus;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Material {
|
||||
|
||||
|
||||
/// Material::probe() looks up the current position's material configuration in
|
||||
/// the material hash table. It returns a pointer to the Entry if the position
|
||||
/// is found. Otherwise a new Entry is computed and stored there, so we don't
|
||||
/// have to recompute all when the same material configuration occurs again.
|
||||
|
||||
Entry* probe(const Position& pos) {
|
||||
|
||||
Key key = pos.material_key();
|
||||
Entry* e = pos.this_thread()->materialTable[key];
|
||||
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
std::memset(e, 0, sizeof(Entry));
|
||||
e->key = key;
|
||||
e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
|
||||
|
||||
Value npm_w = pos.non_pawn_material(WHITE);
|
||||
Value npm_b = pos.non_pawn_material(BLACK);
|
||||
Value npm = std::clamp(npm_w + npm_b, EndgameLimit, MidgameLimit);
|
||||
|
||||
// Map total non-pawn material into [PHASE_ENDGAME, PHASE_MIDGAME]
|
||||
e->gamePhase = Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit));
|
||||
|
||||
// Let's look if we have a specialized evaluation function for this particular
|
||||
// material configuration. Firstly we look for a fixed configuration one, then
|
||||
// for a generic one if the previous search failed.
|
||||
if ((e->evaluationFunction = Endgames::probe<Value>(key)) != nullptr)
|
||||
return e;
|
||||
|
||||
for (Color c : { WHITE, BLACK })
|
||||
if (is_KXK(pos, c))
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKXK[c];
|
||||
return e;
|
||||
}
|
||||
|
||||
// OK, we didn't find any special evaluation function for the current material
|
||||
// configuration. Is there a suitable specialized scaling function?
|
||||
const auto* sf = Endgames::probe<ScaleFactor>(key);
|
||||
|
||||
if (sf)
|
||||
{
|
||||
e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned
|
||||
return e;
|
||||
}
|
||||
|
||||
// We didn't find any specialized scaling function, so fall back on generic
|
||||
// ones that refer to more than one material distribution. Note that in this
|
||||
// case we don't return after setting the function.
|
||||
for (Color c : { WHITE, BLACK })
|
||||
{
|
||||
if (is_KBPsK(pos, c))
|
||||
e->scalingFunction[c] = &ScaleKBPsK[c];
|
||||
|
||||
else if (is_KQKRPs(pos, c))
|
||||
e->scalingFunction[c] = &ScaleKQKRPs[c];
|
||||
}
|
||||
|
||||
if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN)) // Only pawns on the board
|
||||
{
|
||||
if (!pos.count<PAWN>(BLACK))
|
||||
{
|
||||
assert(pos.count<PAWN>(WHITE) >= 2);
|
||||
|
||||
e->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
|
||||
}
|
||||
else if (!pos.count<PAWN>(WHITE))
|
||||
{
|
||||
assert(pos.count<PAWN>(BLACK) >= 2);
|
||||
|
||||
e->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
|
||||
}
|
||||
else if (pos.count<PAWN>(WHITE) == 1 && pos.count<PAWN>(BLACK) == 1)
|
||||
{
|
||||
// This is a special case because we set scaling functions
|
||||
// for both colors instead of only one.
|
||||
e->scalingFunction[WHITE] = &ScaleKPKP[WHITE];
|
||||
e->scalingFunction[BLACK] = &ScaleKPKP[BLACK];
|
||||
}
|
||||
}
|
||||
|
||||
// Zero or just one pawn makes it difficult to win, even with a small material
|
||||
// advantage. This catches some trivial draws like KK, KBK and KNK and gives a
|
||||
// drawish scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
|
||||
if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
|
||||
e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||
npm_b <= BishopValueMg ? 4 : 14);
|
||||
|
||||
if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
|
||||
e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||
npm_w <= BishopValueMg ? 4 : 14);
|
||||
|
||||
// Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
|
||||
// for the bishop pair "extended piece", which allows us to be more flexible
|
||||
// in defining bishop pair bonuses.
|
||||
const int pieceCount[COLOR_NB][PIECE_TYPE_NB] = {
|
||||
{ pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
|
||||
pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
|
||||
{ pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
|
||||
pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
|
||||
|
||||
e->score = (imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16;
|
||||
return e;
|
||||
}
|
||||
|
||||
} // namespace Material
|
||||
|
||||
} // namespace Stockfish
|
|
@ -1,71 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef MATERIAL_H_INCLUDED
|
||||
#define MATERIAL_H_INCLUDED
|
||||
|
||||
#include "endgame.h"
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace Stockfish::Material {
|
||||
|
||||
/// Material::Entry contains various information about a material configuration.
|
||||
/// It contains a material imbalance evaluation, a function pointer to a special
|
||||
/// endgame evaluation function (which in most cases is nullptr, meaning that the
|
||||
/// standard evaluation function will be used), and scale factors.
|
||||
///
|
||||
/// The scale factors are used to scale the evaluation score up or down. For
|
||||
/// instance, in KRB vs KR endgames, the score is scaled down by a factor of 4,
|
||||
/// which will result in scores of absolute value less than one pawn.
|
||||
|
||||
struct Entry {
|
||||
|
||||
Score imbalance() const { return score; }
|
||||
Phase game_phase() const { return (Phase)gamePhase; }
|
||||
bool specialized_eval_exists() const { return evaluationFunction != nullptr; }
|
||||
Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); }
|
||||
|
||||
// scale_factor() takes a position and a color as input and returns a scale factor
|
||||
// for the given color. We have to provide the position in addition to the color
|
||||
// because the scale factor may also be a function which should be applied to
|
||||
// the position. For instance, in KBP vs K endgames, the scaling function looks
|
||||
// for rook pawns and wrong-colored bishops.
|
||||
ScaleFactor scale_factor(const Position& pos, Color c) const {
|
||||
ScaleFactor sf = scalingFunction[c] ? (*scalingFunction[c])(pos)
|
||||
: SCALE_FACTOR_NONE;
|
||||
return sf != SCALE_FACTOR_NONE ? sf : ScaleFactor(factor[c]);
|
||||
}
|
||||
|
||||
Key key;
|
||||
const EndgameBase<Value>* evaluationFunction;
|
||||
const EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB]; // Could be one for each
|
||||
// side (e.g. KPKP, KBPsK)
|
||||
Score score;
|
||||
int16_t gamePhase;
|
||||
uint8_t factor[COLOR_NB];
|
||||
};
|
||||
|
||||
using Table = HashTable<Entry, 8192>;
|
||||
|
||||
Entry* probe(const Position& pos);
|
||||
|
||||
} // namespace Stockfish::Material
|
||||
|
||||
#endif // #ifndef MATERIAL_H_INCLUDED
|
|
@ -137,8 +137,7 @@ namespace Stockfish::Eval::NNUE {
|
|||
}
|
||||
|
||||
void hint_common_parent_position(const Position& pos) {
|
||||
if (Eval::useNNUE)
|
||||
featureTransformer->hint_common_access(pos);
|
||||
featureTransformer->hint_common_access(pos);
|
||||
}
|
||||
|
||||
// Evaluation function. Perform differential calculation.
|
||||
|
|
305
src/pawns.cpp
305
src/pawns.cpp
|
@ -1,305 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "pawns.h"
|
||||
#include "position.h"
|
||||
#include "thread.h"
|
||||
|
||||
namespace Stockfish {
|
||||
|
||||
namespace {
|
||||
|
||||
#define V Value
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
// Pawn penalties
|
||||
constexpr Score Backward = S( 6, 19);
|
||||
constexpr Score Doubled = S(11, 51);
|
||||
constexpr Score DoubledEarly = S(17, 7);
|
||||
constexpr Score Isolated = S( 1, 20);
|
||||
constexpr Score WeakLever = S( 2, 57);
|
||||
constexpr Score WeakUnopposed = S(15, 18);
|
||||
|
||||
// Bonus for blocked pawns at 5th or 6th rank
|
||||
constexpr Score BlockedPawn[2] = { S(-19, -8), S(-7, 3) };
|
||||
|
||||
constexpr Score BlockedStorm[RANK_NB] = {
|
||||
S(0, 0), S(0, 0), S(64, 75), S(-3, 14), S(-12, 19), S(-7, 4), S(-10, 5)
|
||||
};
|
||||
|
||||
// Connected pawn bonus
|
||||
constexpr int Connected[RANK_NB] = { 0, 3, 7, 7, 15, 54, 86 };
|
||||
|
||||
// Strength of pawn shelter for our king by [distance from edge][rank].
|
||||
// RANK_1 = 0 is used for files where we have no pawn, or pawn is behind our king.
|
||||
constexpr Value ShelterStrength[int(FILE_NB) / 2][RANK_NB] = {
|
||||
{ V(-2), V(85), V(95), V(53), V(39), V(23), V(25) },
|
||||
{ V(-55), V(64), V(32), V(-55), V(-30), V(-11), V(-61) },
|
||||
{ V(-11), V(75), V(19), V(-6), V(26), V(9), V(-47) },
|
||||
{ V(-41), V(-11), V(-27), V(-58), V(-42), V(-66), V(-163) }
|
||||
};
|
||||
|
||||
// Danger of enemy pawns moving toward our king by [distance from edge][rank].
|
||||
// RANK_1 = 0 is used for files where the enemy has no pawn, or their pawn
|
||||
// is behind our king. Note that UnblockedStorm[0][1-2] accommodate opponent pawn
|
||||
// on edge, likely blocked by our king.
|
||||
constexpr Value UnblockedStorm[int(FILE_NB) / 2][RANK_NB] = {
|
||||
{ V(94), V(-280), V(-170), V(90), V(59), V(47), V(53) },
|
||||
{ V(43), V(-17), V(128), V(39), V(26), V(-17), V(15) },
|
||||
{ V(-9), V(62), V(170), V(34), V(-5), V(-20), V(-11) },
|
||||
{ V(-27), V(-19), V(106), V(10), V(2), V(-13), V(-24) }
|
||||
};
|
||||
|
||||
|
||||
// KingOnFile[semi-open Us][semi-open Them] contains bonuses/penalties
|
||||
// for king when the king is on a semi-open or open file.
|
||||
constexpr Score KingOnFile[2][2] = {{ S(-18,11), S(-6,-3) },
|
||||
{ S( 0, 0), S( 5,-4) }};
|
||||
|
||||
#undef S
|
||||
#undef V
|
||||
|
||||
|
||||
/// evaluate() calculates a score for the static pawn structure of the given position.
|
||||
/// We cannot use the location of pieces or king in this function, as the evaluation
|
||||
/// of the pawn structure will be stored in a small cache for speed reasons, and will
|
||||
/// be re-used even when the pieces have moved.
|
||||
|
||||
template<Color Us>
|
||||
Score evaluate(const Position& pos, Pawns::Entry* e) {
|
||||
|
||||
constexpr Color Them = ~Us;
|
||||
constexpr Direction Up = pawn_push(Us);
|
||||
constexpr Direction Down = -Up;
|
||||
|
||||
Bitboard neighbours, stoppers, support, phalanx, opposed;
|
||||
Bitboard lever, leverPush, blocked;
|
||||
Square s;
|
||||
bool backward, passed, doubled;
|
||||
Score score = SCORE_ZERO;
|
||||
Bitboard b = pos.pieces(Us, PAWN);
|
||||
|
||||
Bitboard ourPawns = pos.pieces( Us, PAWN);
|
||||
Bitboard theirPawns = pos.pieces(Them, PAWN);
|
||||
|
||||
Bitboard doubleAttackThem = pawn_double_attacks_bb<Them>(theirPawns);
|
||||
|
||||
e->passedPawns[Us] = 0;
|
||||
e->kingSquares[Us] = SQ_NONE;
|
||||
e->pawnAttacks[Us] = e->pawnAttacksSpan[Us] = pawn_attacks_bb<Us>(ourPawns);
|
||||
e->blockedCount += popcount(shift<Up>(ourPawns) & (theirPawns | doubleAttackThem));
|
||||
|
||||
// Loop through all pawns of the current color and score each pawn
|
||||
while (b)
|
||||
{
|
||||
s = pop_lsb(b);
|
||||
|
||||
assert(pos.piece_on(s) == make_piece(Us, PAWN));
|
||||
|
||||
Rank r = relative_rank(Us, s);
|
||||
|
||||
// Flag the pawn
|
||||
opposed = theirPawns & forward_file_bb(Us, s);
|
||||
blocked = theirPawns & (s + Up);
|
||||
stoppers = theirPawns & passed_pawn_span(Us, s);
|
||||
lever = theirPawns & pawn_attacks_bb(Us, s);
|
||||
leverPush = theirPawns & pawn_attacks_bb(Us, s + Up);
|
||||
doubled = ourPawns & (s - Up);
|
||||
neighbours = ourPawns & adjacent_files_bb(s);
|
||||
phalanx = neighbours & rank_bb(s);
|
||||
support = neighbours & rank_bb(s - Up);
|
||||
|
||||
if (doubled)
|
||||
{
|
||||
// Additional doubled penalty if none of their pawns is fixed
|
||||
if (!(ourPawns & shift<Down>(theirPawns | pawn_attacks_bb<Them>(theirPawns))))
|
||||
score -= DoubledEarly;
|
||||
}
|
||||
|
||||
// A pawn is backward when it is behind all pawns of the same color on
|
||||
// the adjacent files and cannot safely advance.
|
||||
backward = !(neighbours & forward_ranks_bb(Them, s + Up))
|
||||
&& (leverPush | blocked);
|
||||
|
||||
// Compute additional span if pawn is not backward nor blocked
|
||||
if (!backward && !blocked)
|
||||
e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);
|
||||
|
||||
// A pawn is passed if one of the three following conditions is true:
|
||||
// (a) there is no stoppers except some levers
|
||||
// (b) the only stoppers are the leverPush, but we outnumber them
|
||||
// (c) there is only one front stopper which can be levered.
|
||||
// (Refined in Evaluation::passed)
|
||||
passed = !(stoppers ^ lever)
|
||||
|| ( !(stoppers ^ leverPush)
|
||||
&& popcount(phalanx) >= popcount(leverPush))
|
||||
|| ( stoppers == blocked && r >= RANK_5
|
||||
&& (shift<Up>(support) & ~(theirPawns | doubleAttackThem)));
|
||||
|
||||
passed &= !(forward_file_bb(Us, s) & ourPawns);
|
||||
|
||||
// Passed pawns will be properly scored later in evaluation when we have
|
||||
// full attack info.
|
||||
if (passed)
|
||||
e->passedPawns[Us] |= s;
|
||||
|
||||
// Score this pawn
|
||||
if (support | phalanx)
|
||||
{
|
||||
int v = Connected[r] * (2 + bool(phalanx) - bool(opposed))
|
||||
+ 22 * popcount(support);
|
||||
|
||||
score += make_score(v, v * (r - 2) / 4);
|
||||
}
|
||||
|
||||
else if (!neighbours)
|
||||
{
|
||||
if ( opposed
|
||||
&& (ourPawns & forward_file_bb(Them, s))
|
||||
&& !(theirPawns & adjacent_files_bb(s)))
|
||||
score -= Doubled;
|
||||
else
|
||||
score -= Isolated
|
||||
+ WeakUnopposed * !opposed;
|
||||
}
|
||||
|
||||
else if (backward)
|
||||
score -= Backward
|
||||
+ WeakUnopposed * !opposed * bool(~(FileABB | FileHBB) & s);
|
||||
|
||||
if (!support)
|
||||
score -= Doubled * doubled
|
||||
+ WeakLever * more_than_one(lever);
|
||||
|
||||
if (blocked && r >= RANK_5)
|
||||
score += BlockedPawn[r - RANK_5];
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Pawns {
|
||||
|
||||
|
||||
/// Pawns::probe() looks up the current position's pawns configuration in
|
||||
/// the pawns hash table. It returns a pointer to the Entry if the position
|
||||
/// is found. Otherwise a new Entry is computed and stored there, so we don't
|
||||
/// have to recompute all when the same pawns configuration occurs again.
|
||||
|
||||
Entry* probe(const Position& pos) {
|
||||
|
||||
Key key = pos.pawn_key();
|
||||
Entry* e = pos.this_thread()->pawnsTable[key];
|
||||
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
e->key = key;
|
||||
e->blockedCount = 0;
|
||||
e->scores[WHITE] = evaluate<WHITE>(pos, e);
|
||||
e->scores[BLACK] = evaluate<BLACK>(pos, e);
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
/// Entry::evaluate_shelter() calculates the shelter bonus and the storm
|
||||
/// penalty for a king, looking at the king file and the two closest files.
|
||||
|
||||
template<Color Us>
|
||||
Score Entry::evaluate_shelter(const Position& pos, Square ksq) const {
|
||||
|
||||
constexpr Color Them = ~Us;
|
||||
|
||||
Bitboard b = pos.pieces(PAWN) & ~forward_ranks_bb(Them, ksq);
|
||||
Bitboard ourPawns = b & pos.pieces(Us) & ~pawnAttacks[Them];
|
||||
Bitboard theirPawns = b & pos.pieces(Them);
|
||||
|
||||
Score bonus = make_score(5, 5);
|
||||
|
||||
File center = std::clamp(file_of(ksq), FILE_B, FILE_G);
|
||||
for (File f = File(center - 1); f <= File(center + 1); ++f)
|
||||
{
|
||||
b = ourPawns & file_bb(f);
|
||||
int ourRank = b ? relative_rank(Us, frontmost_sq(Them, b)) : 0;
|
||||
|
||||
b = theirPawns & file_bb(f);
|
||||
int theirRank = b ? relative_rank(Us, frontmost_sq(Them, b)) : 0;
|
||||
|
||||
int d = edge_distance(f);
|
||||
bonus += make_score(ShelterStrength[d][ourRank], 0);
|
||||
|
||||
if (ourRank && (ourRank == theirRank - 1))
|
||||
bonus -= BlockedStorm[theirRank];
|
||||
else
|
||||
bonus -= make_score(UnblockedStorm[d][theirRank], 0);
|
||||
}
|
||||
|
||||
// King On File
|
||||
bonus -= KingOnFile[pos.is_on_semiopen_file(Us, ksq)][pos.is_on_semiopen_file(Them, ksq)];
|
||||
|
||||
return bonus;
|
||||
}
|
||||
|
||||
|
||||
/// Entry::do_king_safety() calculates a bonus for king safety. It is called only
|
||||
/// when king square changes, which is about 20% of total king_safety() calls.
|
||||
|
||||
template<Color Us>
|
||||
Score Entry::do_king_safety(const Position& pos) {
|
||||
|
||||
Square ksq = pos.square<KING>(Us);
|
||||
kingSquares[Us] = ksq;
|
||||
castlingRights[Us] = pos.castling_rights(Us);
|
||||
auto compare = [](Score a, Score b) { return mg_value(a) < mg_value(b); };
|
||||
|
||||
Score shelter = evaluate_shelter<Us>(pos, ksq);
|
||||
|
||||
// If we can castle use the bonus after castling if it is bigger
|
||||
|
||||
if (pos.can_castle(Us & KING_SIDE))
|
||||
shelter = std::max(shelter, evaluate_shelter<Us>(pos, relative_square(Us, SQ_G1)), compare);
|
||||
|
||||
if (pos.can_castle(Us & QUEEN_SIDE))
|
||||
shelter = std::max(shelter, evaluate_shelter<Us>(pos, relative_square(Us, SQ_C1)), compare);
|
||||
|
||||
// In endgame we like to bring our king near our closest pawn
|
||||
Bitboard pawns = pos.pieces(Us, PAWN);
|
||||
int minPawnDist = 6;
|
||||
|
||||
if (pawns & attacks_bb<KING>(ksq))
|
||||
minPawnDist = 1;
|
||||
else while (pawns)
|
||||
minPawnDist = std::min(minPawnDist, distance(ksq, pop_lsb(pawns)));
|
||||
|
||||
return shelter - make_score(0, 16 * minPawnDist);
|
||||
}
|
||||
|
||||
// Explicit template instantiation
|
||||
template Score Entry::do_king_safety<WHITE>(const Position& pos);
|
||||
template Score Entry::do_king_safety<BLACK>(const Position& pos);
|
||||
|
||||
} // namespace Pawns
|
||||
|
||||
} // namespace Stockfish
|
70
src/pawns.h
70
src/pawns.h
|
@ -1,70 +0,0 @@
|
|||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PAWNS_H_INCLUDED
|
||||
#define PAWNS_H_INCLUDED
|
||||
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace Stockfish::Pawns {
|
||||
|
||||
/// Pawns::Entry contains various information about a pawn structure. A lookup
|
||||
/// to the pawn hash table (performed by calling the probe function) returns a
|
||||
/// pointer to an Entry object.
|
||||
|
||||
struct Entry {
|
||||
|
||||
Score pawn_score(Color c) const { return scores[c]; }
|
||||
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
|
||||
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
|
||||
Bitboard pawn_attacks_span(Color c) const { return pawnAttacksSpan[c]; }
|
||||
int passed_count() const { return popcount(passedPawns[WHITE] | passedPawns[BLACK]); }
|
||||
int blocked_count() const { return blockedCount; }
|
||||
|
||||
template<Color Us>
|
||||
Score king_safety(const Position& pos) {
|
||||
return kingSquares[Us] == pos.square<KING>(Us) && castlingRights[Us] == pos.castling_rights(Us)
|
||||
? kingSafety[Us] : (kingSafety[Us] = do_king_safety<Us>(pos));
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
Score do_king_safety(const Position& pos);
|
||||
|
||||
template<Color Us>
|
||||
Score evaluate_shelter(const Position& pos, Square ksq) const;
|
||||
|
||||
Key key;
|
||||
Score scores[COLOR_NB];
|
||||
Bitboard passedPawns[COLOR_NB];
|
||||
Bitboard pawnAttacks[COLOR_NB];
|
||||
Bitboard pawnAttacksSpan[COLOR_NB];
|
||||
Square kingSquares[COLOR_NB];
|
||||
Score kingSafety[COLOR_NB];
|
||||
int castlingRights[COLOR_NB];
|
||||
int blockedCount;
|
||||
};
|
||||
|
||||
using Table = HashTable<Entry, 131072>;
|
||||
|
||||
Entry* probe(const Position& pos);
|
||||
|
||||
} // namespace Stockfish::Pawns
|
||||
|
||||
#endif // #ifndef PAWNS_H_INCLUDED
|
|
@ -751,13 +751,10 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||
else
|
||||
st->nonPawnMaterial[them] -= PieceValue[MG][captured];
|
||||
|
||||
if (Eval::useNNUE)
|
||||
{
|
||||
dp.dirty_num = 2; // 1 piece moved, 1 piece captured
|
||||
dp.piece[1] = captured;
|
||||
dp.from[1] = capsq;
|
||||
dp.to[1] = SQ_NONE;
|
||||
}
|
||||
dp.dirty_num = 2; // 1 piece moved, 1 piece captured
|
||||
dp.piece[1] = captured;
|
||||
dp.from[1] = capsq;
|
||||
dp.to[1] = SQ_NONE;
|
||||
|
||||
// Update board and piece lists
|
||||
remove_piece(capsq);
|
||||
|
@ -765,7 +762,6 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||
// Update material hash key and prefetch access to materialTable
|
||||
k ^= Zobrist::psq[captured][capsq];
|
||||
st->materialKey ^= Zobrist::psq[captured][pieceCount[captured]];
|
||||
prefetch(thisThread->materialTable[st->materialKey]);
|
||||
|
||||
// Reset rule 50 counter
|
||||
st->rule50 = 0;
|
||||
|
@ -792,12 +788,9 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||
// Move the piece. The tricky Chess960 castling is handled earlier
|
||||
if (type_of(m) != CASTLING)
|
||||
{
|
||||
if (Eval::useNNUE)
|
||||
{
|
||||
dp.piece[0] = pc;
|
||||
dp.from[0] = from;
|
||||
dp.to[0] = to;
|
||||
}
|
||||
dp.piece[0] = pc;
|
||||
dp.from[0] = from;
|
||||
dp.to[0] = to;
|
||||
|
||||
move_piece(from, to);
|
||||
}
|
||||
|
@ -823,15 +816,12 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||
remove_piece(to);
|
||||
put_piece(promotion, to);
|
||||
|
||||
if (Eval::useNNUE)
|
||||
{
|
||||
// Promoting pawn to SQ_NONE, promoted piece from SQ_NONE
|
||||
dp.to[0] = SQ_NONE;
|
||||
dp.piece[dp.dirty_num] = promotion;
|
||||
dp.from[dp.dirty_num] = SQ_NONE;
|
||||
dp.to[dp.dirty_num] = to;
|
||||
dp.dirty_num++;
|
||||
}
|
||||
// Promoting pawn to SQ_NONE, promoted piece from SQ_NONE
|
||||
dp.to[0] = SQ_NONE;
|
||||
dp.piece[dp.dirty_num] = promotion;
|
||||
dp.from[dp.dirty_num] = SQ_NONE;
|
||||
dp.to[dp.dirty_num] = to;
|
||||
dp.dirty_num++;
|
||||
|
||||
// Update hash keys
|
||||
k ^= Zobrist::psq[pc][to] ^ Zobrist::psq[promotion][to];
|
||||
|
@ -961,7 +951,7 @@ void Position::do_castling(Color us, Square from, Square& to, Square& rfrom, Squ
|
|||
rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1);
|
||||
to = relative_square(us, kingSide ? SQ_G1 : SQ_C1);
|
||||
|
||||
if (Do && Eval::useNNUE)
|
||||
if (Do)
|
||||
{
|
||||
auto& dp = st->dirtyPiece;
|
||||
dp.piece[0] = make_piece(us, KING);
|
||||
|
|
|
@ -25,9 +25,7 @@
|
|||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "material.h"
|
||||
#include "movepick.h"
|
||||
#include "pawns.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "thread_win32_osx.h"
|
||||
|
@ -57,8 +55,6 @@ public:
|
|||
void wait_for_search_finished();
|
||||
size_t id() const { return idx; }
|
||||
|
||||
Pawns::Table pawnsTable;
|
||||
Material::Table materialTable;
|
||||
size_t pvIdx, pvLast;
|
||||
std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
|
||||
int selDepth, nmpMinPly;
|
||||
|
|
|
@ -43,7 +43,6 @@ static void on_hash_size(const Option& o) { TT.resize(size_t(o)); }
|
|||
static void on_logger(const Option& o) { start_logger(o); }
|
||||
static void on_threads(const Option& o) { Threads.set(size_t(o)); }
|
||||
static void on_tb_path(const Option& o) { Tablebases::init(o); }
|
||||
static void on_use_NNUE(const Option&) { Eval::NNUE::init(); }
|
||||
static void on_eval_file(const Option&) { Eval::NNUE::init(); }
|
||||
|
||||
/// Our case insensitive less() function as required by UCI protocol
|
||||
|
@ -79,7 +78,6 @@ void init(OptionsMap& o) {
|
|||
o["SyzygyProbeDepth"] << Option(1, 1, 100);
|
||||
o["Syzygy50MoveRule"] << Option(true);
|
||||
o["SyzygyProbeLimit"] << Option(7, 0, 7);
|
||||
o["Use NNUE"] << Option(true, on_use_NNUE);
|
||||
o["EvalFile"] << Option(EvalFileDefaultName, on_eval_file);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue