small refactor | bench 1181803
This commit is contained in:
1 parent
00f0d06381
commit
7db4fcf59c
3 files changed
+50
-80
No files matched your search
+48
-79
@@ -81,7 +81,7 @@ static int AdjustEval(Board &board, SearchContext* ctx, int eval) {
|
|||||||
return std::clamp(eval + corrhist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1);
|
return std::clamp(eval + corrhist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsThreefold(Board &board, SearchContext* ctx) {
|
static bool IsTwoFold(Board &board, SearchContext* ctx) {
|
||||||
for (int i = 0; i < board.positionIndex; i++) {
|
for (int i = 0; i < board.positionIndex; i++) {
|
||||||
if (ctx->positionHistory[i] == board.hashKey) {
|
if (ctx->positionHistory[i] == board.hashKey) {
|
||||||
// repetition found
|
// repetition found
|
||||||
@@ -103,7 +103,7 @@ static bool IsInsuffMat(Board &board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool IsDraw(Board &board, SearchContext* ctx) {
|
bool IsDraw(Board &board, SearchContext* ctx) {
|
||||||
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx);
|
return IsFifty(board) || IsInsuffMat(board) || IsTwoFold(board, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool isPV>
|
template <bool isPV>
|
||||||
@@ -324,7 +324,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
alpha = bestScore;
|
alpha = bestScore;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int fpScore = bestScore + 100;
|
const int fpScore = bestScore + qsFPMargin;
|
||||||
|
|
||||||
MovePicker<Noisy> mp(board, ctx, ply, entry.bestMove);
|
MovePicker<Noisy> mp(board, ctx, ply, entry.bestMove);
|
||||||
|
|
||||||
@@ -332,11 +332,13 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
|
|
||||||
int nodeType = AllNode;
|
int nodeType = AllNode;
|
||||||
|
|
||||||
|
const bool inCheck = board.InCheck();
|
||||||
|
|
||||||
Move currMove;
|
Move currMove;
|
||||||
while ((currMove = mp.Next())) {
|
while ((currMove = mp.Next())) {
|
||||||
if (!IsLoss(bestScore)) {
|
if (!IsLoss(bestScore)) {
|
||||||
// QS FP
|
// QS FP
|
||||||
if (!board.InCheck() && currMove.IsCapture() &&
|
if (!inCheck && currMove.IsCapture() &&
|
||||||
fpScore <= alpha && !SEE(board, currMove, 1)) {
|
fpScore <= alpha && !SEE(board, currMove, 1)) {
|
||||||
|
|
||||||
bestScore = std::max(bestScore, fpScore);
|
bestScore = std::max(bestScore, fpScore);
|
||||||
@@ -427,7 +429,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
int ttAdjustedEval = staticEval;
|
int ttAdjustedEval = staticEval;
|
||||||
|
|
||||||
if (!ctx->excluded && !board.InCheck() && ttHit &&
|
const bool inCheck = board.InCheck();
|
||||||
|
|
||||||
|
if (!ctx->excluded && !inCheck && ttHit &&
|
||||||
((entry.nodeType == PV) ||
|
((entry.nodeType == PV) ||
|
||||||
(entry.nodeType == AllNode && entry.score <= staticEval) ||
|
(entry.nodeType == AllNode && entry.score <= staticEval) ||
|
||||||
(entry.nodeType == CutNode && entry.score >= staticEval))) {
|
(entry.nodeType == CutNode && entry.score >= staticEval))) {
|
||||||
@@ -439,7 +443,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
const bool improving = [&]
|
const bool improving = [&]
|
||||||
{
|
{
|
||||||
if (board.InCheck())
|
if (inCheck)
|
||||||
return false;
|
return false;
|
||||||
if (ply > 1 && ctx->ss[ply - 2].eval != ScoreNone)
|
if (ply > 1 && ctx->ss[ply - 2].eval != ScoreNone)
|
||||||
return staticEval > ctx->ss[ply - 2].eval;
|
return staticEval > ctx->ss[ply - 2].eval;
|
||||||
@@ -449,7 +453,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
return true;
|
return true;
|
||||||
}();
|
}();
|
||||||
|
|
||||||
if (!board.InCheck() && !ctx->excluded) {
|
if (!inCheck && !ctx->excluded) {
|
||||||
if (ply) {
|
if (ply) {
|
||||||
// Reverse Futility Pruning
|
// Reverse Futility Pruning
|
||||||
int margin = rfpBase + rfpMargin * (depth - improving);
|
int margin = rfpBase + rfpMargin * (depth - improving);
|
||||||
@@ -591,12 +595,8 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
// that a quiet move will help us so we skip them
|
// that a quiet move will help us so we skip them
|
||||||
int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()][sourceThreatened][targetThreatened];
|
int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()][sourceThreatened][targetThreatened];
|
||||||
|
|
||||||
if (ply > 0) {
|
if (ply > 0) historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 1);
|
||||||
historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 1);
|
if (ply > 1) historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 2);
|
||||||
|
|
||||||
if (ply > 1)
|
|
||||||
historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
int margin = fpMargin * (lmrDepth + improving) + historyScore / 32;
|
int margin = fpMargin * (lmrDepth + improving) + historyScore / 32;
|
||||||
|
|
||||||
@@ -664,7 +664,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
} else if (cutnode) {
|
} else if (cutnode) {
|
||||||
extension--;
|
extension--;
|
||||||
}
|
}
|
||||||
} else if (depth <= 7 && !board.InCheck() && staticEval <= alpha - ldseMargin && entry.nodeType == CutNode) {
|
} else if (depth <= 7 && !inCheck && staticEval <= alpha - ldseMargin && entry.nodeType == CutNode) {
|
||||||
extension++;
|
extension++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -728,7 +728,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
// Fail high (beta cutoff)
|
// Fail high (beta cutoff)
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
const int bonusDepth = depth + (!board.InCheck() && staticEval <= alpha);
|
const int bonusDepth = depth + (!inCheck && staticEval <= alpha);
|
||||||
int historyBonus = historyBonusMultiplier * bonusDepth - historyBonusSub;
|
int historyBonus = historyBonusMultiplier * bonusDepth - historyBonusSub;
|
||||||
int historyMalus = historyMalusMultiplier * bonusDepth - historyMalusSub;
|
int historyMalus = historyMalusMultiplier * bonusDepth - historyMalusSub;
|
||||||
int contHistoryBonus = contHistoryBonusMultiplier * bonusDepth - contHistoryBonusSub;
|
int contHistoryBonus = contHistoryBonusMultiplier * bonusDepth - contHistoryBonusSub;
|
||||||
@@ -736,80 +736,49 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
int captHistoryBonus = captHistoryBonusMultiplier * bonusDepth - captHistoryBonusSub;
|
int captHistoryBonus = captHistoryBonusMultiplier * bonusDepth - captHistoryBonusSub;
|
||||||
int captHistoryMalus = captHistoryMalusMultiplier * bonusDepth - captHistoryMalusSub;
|
int captHistoryMalus = captHistoryMalusMultiplier * bonusDepth - captHistoryMalusSub;
|
||||||
|
|
||||||
|
auto UpdateCaptHist = [&](Move move, int bonus) {
|
||||||
|
int movingPiece = board.GetPieceType(move.MoveFrom());
|
||||||
|
int capturedPiece = move.GetFlags() == epCapture ? Pawn : board.GetPieceType(move.MoveTo());
|
||||||
|
ctx->capthist.Update(board.sideToMove, movingPiece, capturedPiece, move.MoveTo(), bonus);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto UpdateContHistNPly = [&](int n) {
|
||||||
|
int prevType = ctx->ss[ply - n].pieceType;
|
||||||
|
int prevTo = ctx->ss[ply - n].moveTo;
|
||||||
|
bool otherColor = ctx->ss[ply - n].side;
|
||||||
|
int pieceType = ctx->ss[ply].pieceType;
|
||||||
|
int to = ctx->ss[ply].moveTo;
|
||||||
|
|
||||||
|
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, contHistoryBonus);
|
||||||
|
|
||||||
|
for (int i = 0; i < seenQuietsCount - 1; i++) {
|
||||||
|
int pt = board.GetPieceType(seenQuiets[i].MoveFrom());
|
||||||
|
int t = seenQuiets[i].MoveTo();
|
||||||
|
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pt, t, -contHistoryMalus);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (!currMove.IsCapture()) {
|
if (!currMove.IsCapture()) {
|
||||||
ctx->killerMoves[ply] = currMove;
|
ctx->killerMoves[ply] = currMove;
|
||||||
|
|
||||||
ctx->history.Update(board.sideToMove, currMove, sourceThreatened, targetThreatened, historyBonus);
|
ctx->history.Update(board.sideToMove, currMove, sourceThreatened, targetThreatened, historyBonus);
|
||||||
|
|
||||||
if (ply > 0) {
|
if (ply > 0) UpdateContHistNPly(1);
|
||||||
int prevType = ctx->ss[ply-1].pieceType;
|
if (ply > 1) UpdateContHistNPly(2);
|
||||||
int prevTo = ctx->ss[ply-1].moveTo;
|
|
||||||
int pieceType = ctx->ss[ply].pieceType;
|
|
||||||
int to = ctx->ss[ply].moveTo;
|
|
||||||
bool otherColor = ctx->ss[ply-1].side;
|
|
||||||
|
|
||||||
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, contHistoryBonus);
|
|
||||||
|
|
||||||
// Malus
|
|
||||||
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
|
||||||
int pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom());
|
|
||||||
int to = seenQuiets[moveIndex].MoveTo();
|
|
||||||
|
|
||||||
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, -contHistoryMalus);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ply > 1) {
|
|
||||||
prevType = ctx->ss[ply-2].pieceType;
|
|
||||||
prevTo = ctx->ss[ply-2].moveTo;
|
|
||||||
otherColor = ctx->ss[ply-2].side;
|
|
||||||
|
|
||||||
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, contHistoryBonus);
|
|
||||||
|
|
||||||
// Malus
|
|
||||||
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
|
||||||
int pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom());
|
|
||||||
int to = seenQuiets[moveIndex].MoveTo();
|
|
||||||
|
|
||||||
ctx->conthist.Update(board.sideToMove, otherColor, prevType, prevTo, pieceType, to, -contHistoryMalus);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Malus
|
// Malus
|
||||||
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
for (int i = 0; i < seenQuietsCount - 1; i++) {
|
||||||
sourceThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[moveIndex].MoveFrom());
|
sourceThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[i].MoveFrom());
|
||||||
targetThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[moveIndex].MoveTo());
|
targetThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[i].MoveTo());
|
||||||
|
ctx->history.Update(board.sideToMove, seenQuiets[i], sourceThreatened, targetThreatened, -historyMalus);
|
||||||
ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], sourceThreatened, targetThreatened, -historyMalus);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
int movingPiece = board.GetPieceType(currMove.MoveFrom());
|
UpdateCaptHist(currMove, captHistoryBonus);
|
||||||
int capturedPiece = board.GetPieceType(currMove.MoveTo());
|
|
||||||
|
|
||||||
if (currMove.GetFlags() == epCapture) {
|
|
||||||
capturedPiece = Pawn;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->capthist.Update(board.sideToMove, movingPiece, capturedPiece, currMove.MoveTo(), captHistoryBonus);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capthist malus
|
// Capthist malus
|
||||||
for (int moveIndex = 0; moveIndex < seenCapturesCount - currMove.IsCapture(); moveIndex++) {
|
for (int i = 0; i < seenCapturesCount - currMove.IsCapture(); i++) {
|
||||||
int movingPiece = board.GetPieceType(seenCaptures[moveIndex].MoveFrom());
|
UpdateCaptHist(seenCaptures[i], -captHistoryMalus);
|
||||||
int capturedPiece = board.GetPieceType(seenCaptures[moveIndex].MoveTo());
|
|
||||||
|
|
||||||
if (seenCaptures[moveIndex].GetFlags() == epCapture) {
|
|
||||||
capturedPiece = Pawn;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (movingPiece == nullPieceType || capturedPiece == nullPieceType) {
|
|
||||||
std::cout << "asd\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->capthist.Update(board.sideToMove, movingPiece, capturedPiece,
|
|
||||||
seenCaptures[moveIndex].MoveTo(), -captHistoryMalus);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ctx->excluded)
|
if (!ctx->excluded)
|
||||||
@@ -819,7 +788,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (moveSeen == 0) {
|
if (moveSeen == 0) {
|
||||||
if (board.InCheck()) { // checkmate
|
if (inCheck) { // checkmate
|
||||||
return -MATE_SCORE + ply;
|
return -MATE_SCORE + ply;
|
||||||
} else { // stalemate
|
} else { // stalemate
|
||||||
return 0;
|
return 0;
|
||||||
@@ -829,7 +798,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
if (searchStopped) return 0;
|
if (searchStopped) return 0;
|
||||||
if (!ctx->excluded) {
|
if (!ctx->excluded) {
|
||||||
|
|
||||||
if (!board.InCheck() && ((results.bestMove.IsQuiet() || !results.bestMove))
|
if (!inCheck && ((results.bestMove.IsQuiet() || !results.bestMove))
|
||||||
&& !(nodeType == AllNode && results.score >= staticEval)) {
|
&& !(nodeType == AllNode && results.score >= staticEval)) {
|
||||||
|
|
||||||
int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT);
|
int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT);
|
||||||
|
|||||||
+1
-1
@@ -178,7 +178,7 @@ public:
|
|||||||
std::cout << termcolor::bright_cyan;
|
std::cout << termcolor::bright_cyan;
|
||||||
table[n][i].PrintMove();
|
table[n][i].PrintMove();
|
||||||
std::cout << termcolor::reset;
|
std::cout << termcolor::reset;
|
||||||
|
|
||||||
if (isEven) {
|
if (isEven) {
|
||||||
std::cout << termcolor::color<247>;
|
std::cout << termcolor::color<247>;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
X_INT(rfpBase, 21, 0, 100) \
|
X_INT(rfpBase, 21, 0, 100) \
|
||||||
X_INT(rfpMargin, 97, 50, 200) \
|
X_INT(rfpMargin, 97, 50, 200) \
|
||||||
X_INT(fpMargin, 86, 50, 200) \
|
X_INT(fpMargin, 86, 50, 200) \
|
||||||
|
X_INT(qsFPMargin, 100, 50, 200) \
|
||||||
X_INT(probcutBetaMargin, 190, 100, 300) \
|
X_INT(probcutBetaMargin, 190, 100, 300) \
|
||||||
X_INT(razoringScalar, 195, 80, 320) \
|
X_INT(razoringScalar, 195, 80, 320) \
|
||||||
X_INT(doubleExtMargin, 32, 8, 64) \
|
X_INT(doubleExtMargin, 32, 8, 64) \
|
||||||
|
|||||||
Reference in new issue
Block a user