Skip to content

Commit

Permalink
simplify LMR condition
Browse files Browse the repository at this point in the history
test r15
  • Loading branch information
dhbloo committed Jun 12, 2024
1 parent 1bec5d0 commit 16fc854
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
21 changes: 0 additions & 21 deletions Rapfi/search/ab/parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,6 @@ constexpr Value qvcfDeltaMargin(Depth d) // note: d <= 0
return Value(std::max(QVCFBias[R] + QVCFScale[R] * int(d), 768));
}

/// LMR move count. For non-PV all node, moves that exceeds late move count
/// will be searched will late move reduction even without other condition.
template <Rule R>
constexpr int lateMoveCount(Depth d, bool improving)
{
constexpr float LMC[RULE_NB][2] = {
{0.82f, 1.48f},
{0.83f, 0.89f},
{0.70f, 1.65f},
};
return 1 + 2 * improving + int(LMC[R][improving] * d);
}

/// Extension for full-depth search when reduced LMR search fails high
template <Rule R>
constexpr int
Expand Down Expand Up @@ -263,12 +250,4 @@ inline int policyPruningScore(Depth d)
return PPBias[R] - int(d * PPScale[R]);
}

/// Policy reduction score at given depth. Moves lower than this will do lmr.
template <Rule R>
inline int policyReductionScore(Depth d)
{
constexpr int PRBias[RULE_NB] = {489, 535, 520};
return PRBias[R];
}

} // namespace Search::AB
36 changes: 16 additions & 20 deletions Rapfi/search/ab/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
searchData->rootDepth,
move);

// Initialize various heruistic information
// Initialize heruistic information
ss->moveCount = ++moveCount;
ss->moveP4[BLACK] = board.cell(move).pattern4[BLACK];
ss->moveP4[WHITE] = board.cell(move).pattern4[WHITE];
Expand Down Expand Up @@ -1101,22 +1101,14 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
board.move<Rule>(move);
TT.prefetch(board.zobristKey());

bool doFullDepthSearch;
// Step 15. Late move reduction (LMR). Moves are searched with a reduced
// depth and will be re-searched at full depth if fail high.
if (depth > 2 && moveCount > 1 + 2 * RootNode
&& (!importantMove // do LMR for non important move
|| distract // do LMR for distract move
|| cutNode // do LMR for all moves in cut node
|| moveCount >= lateMoveCount<Rule>(depth, improvement > 0) // do LMR for late move
|| mp.hasPolicyScore() // do LMR for low policy
&& mp.curMoveScore() < policyReductionScore<Rule>(depth))) {
Value delta = beta - alpha;
Depth r = reduction<Rule, PvNode>(searcher->reductions,
if (depth >= 2 && moveCount > 1 + RootNode) {
Depth r = reduction<Rule, PvNode>(searcher->reductions,
depth,
moveCount,
improvement,
delta,
beta - alpha,
searchData->rootDelta);

// Policy based reduction
Expand Down Expand Up @@ -1164,10 +1156,8 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
r -= ss->statScore * (1.0f / (12633 + 4055 * (depth > 7)));

// Allow LMR to do deeper search in some circumstances
int deeper = r < -1 && moveCount <= 4 ? 1 : 0;

// Clamp the LMR depth to newDepth (no depth less than one)
Depth d = std::max(std::min(newDepth - r, newDepth + deeper), 1.0f);
Depth d = std::max(std::min(newDepth - r, newDepth + 1), 1.0f);

value = -search<Rule, NonPV>(board, ss + 1, -(alpha + 1), -alpha, d, true);

Expand All @@ -1179,16 +1169,22 @@ Value search(Board &board, SearchStack *ss, Value alpha, Value beta, Depth depth
else
ss->extraExtension += std::max(ext - 1.0f, 0.0f);
newDepth += ext;
}

doFullDepthSearch = (value > alpha && d < newDepth);
if (d < newDepth)
value = -search<Rule, NonPV>(board,
ss + 1,
-(alpha + 1),
-alpha,
newDepth,
!cutNode);
}
}
else
doFullDepthSearch = !PvNode || moveCount > 1;

// Step 16. Full depth search when LMR is skipped or fails high
if (doFullDepthSearch)
else if (!PvNode || moveCount > 1) {
// If expected reduction is high, we reduce search depth by 1 here
value = -search<Rule, NonPV>(board, ss + 1, -(alpha + 1), -alpha, newDepth, !cutNode);
}

// For balance move mode, we also check if a move can trigger beta cut
// (which is too good to be balanced), so we can safely discard this move.
Expand Down

0 comments on commit 16fc854

Please sign in to comment.