diff --git a/source/search.cpp b/source/search.cpp index efc267f..8316fc9 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -25,7 +25,7 @@ void InitLMRTable() { static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { TTEntry *current = ctx->TT.GetRawEntry(board.hashKey); if (current->hashKey == board.hashKey && current->bestMove == move) { - return 50000; + return 100000; } if (move.IsCapture()) { @@ -36,15 +36,26 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) { targetType = Pawn; } - return 30000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; + return 50000 * ((SEE(board, move, -100))) + moveScoreTable[attackerType][targetType]; } else { if (ctx->killerMoves[0][ply] == move) { - return 20000; + return 41000; } else if (ctx->killerMoves[1][ply] == move) { - return 18000; + return 40000; } else { - // Max 16384 - return ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; + int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()]; + int conthistScore = 0; + + if (ply > 0) { + int prevType = ctx->ss[ply-1].pieceType; + int prevTo = ctx->ss[ply-1].moveTo; + int pieceType = board.GetPieceType(move.MoveFrom()); + int to = move.MoveTo(); + + conthistScore = ctx->conthist[board.sideToMove][prevType][prevTo][pieceType][to]; + } + + return 20000 + historyScore + conthistScore; } } @@ -258,6 +269,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; + ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom()); + ctx->ss[ply].moveTo = board.moveList[i].MoveTo(); + if (!SEE(board, board.moveList[i], 0)) continue; @@ -392,6 +406,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) { int lmpBase = 7; + int lmpThreshold = lmpBase + 4 * depth; if (moveSeen >= lmpThreshold) { @@ -413,6 +428,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC bool isLegal = copy.MakeMove(currMove); if (!isLegal) continue; + + ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom()); + ctx->ss[ply].moveTo = currMove.MoveTo(); + if (copy.positionIndex >= ctx->positionHistory.size()) { ctx->positionHistory.resize(copy.positionIndex + 100); } @@ -475,6 +494,24 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC ctx->history.Update(board.sideToMove, currMove, bonus); + if (ply > 0) { + int prevType = ctx->ss[ply-1].pieceType; + int prevTo = ctx->ss[ply-1].moveTo; + int pieceType = ctx->ss[ply].pieceType; + int to = ctx->ss[ply].moveTo; + + ctx->conthist.Update(board.sideToMove, prevType, prevTo, pieceType, to, bonus); + + // Malus + for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { + pieceType = board.GetPieceType(seenQuiets[moveIndex].MoveFrom()); + to = seenQuiets[moveIndex].MoveTo(); + + ctx->conthist.Update(board.sideToMove, prevType, prevTo, pieceType, to, -bonus); + } + } + + // Malus for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) { ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus); diff --git a/source/search.h b/source/search.h index 9041625..3d17c03 100644 --- a/source/search.h +++ b/source/search.h @@ -34,6 +34,27 @@ constexpr std::array SEEPieceValues = { void InitLMRTable(); +class ContHistory { +private: + // indexed by [stm][prev move piece][prev move to][piece][to] + MultiArray contHistMoves; +public: + void Update(bool stm, int prevType, int prevTo, int type, int to, int bonus) { + int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY); + + contHistMoves[stm][prevType][prevTo][type][to] += + clampedBonus - contHistMoves[stm][prevType][prevTo][type][to] * std::abs(clampedBonus) / MAX_HISTORY; + } + + void Clear() { + std::fill(&contHistMoves[0][0][0][0][0], &contHistMoves[0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0); + } + + auto& operator[](int index) { + return contHistMoves[index]; + } +}; + class History { private: MultiArray historyMoves; @@ -88,6 +109,9 @@ public: class Stack { public: int32_t eval = ScoreNone; + + int pieceType = -1; + int moveTo = -1; }; class SearchContext { @@ -103,6 +127,7 @@ public: PVLine pvLine; History history; + ContHistory conthist; std::array, 2> killerMoves{}; @@ -117,9 +142,11 @@ public: SearchContext(){ pvLine.Clear(); history.Clear(); + conthist.Clear(); TT.Clear(); sw.Restart(); killerMoves = {}; + ss = {}; positionHistory.reserve(1000); } }; diff --git a/source/uci.cpp b/source/uci.cpp index 0b1486e..9d9e982 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -139,7 +139,9 @@ void UCILoop(Board &board) { ctx->killerMoves = {}; ctx->history.Clear(); + ctx->conthist.Clear(); ctx->positionHistory = {}; + ctx->ss = {}; continue; }