mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 08:43:09 +00:00
Code style fixes in search.cpp
Some code style triviality. No functional change.
This commit is contained in:
parent
baab8be324
commit
4c57cf0ead
2 changed files with 128 additions and 130 deletions
198
src/search.cpp
198
src/search.cpp
|
@ -63,11 +63,11 @@ namespace {
|
||||||
enum NodeType { NonPV, PV };
|
enum NodeType { NonPV, PV };
|
||||||
|
|
||||||
// Sizes and phases of the skip-blocks, used for distributing search depths across the threads
|
// Sizes and phases of the skip-blocks, used for distributing search depths across the threads
|
||||||
const int skipSize[] = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
|
const int SkipSize[] = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
|
||||||
const int skipPhase[] = { 0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7 };
|
const int SkipPhase[] = { 0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||||
|
|
||||||
// Razoring and futility margin based on depth
|
// Razoring and futility margins
|
||||||
const int razor_margin = 600;
|
const int RazorMargin = 600;
|
||||||
Value futility_margin(Depth d) { return Value(150 * d / ONE_PLY); }
|
Value futility_margin(Depth d) { return Value(150 * d / ONE_PLY); }
|
||||||
|
|
||||||
// Futility and reductions lookup tables, initialized at startup
|
// Futility and reductions lookup tables, initialized at startup
|
||||||
|
@ -105,7 +105,7 @@ namespace {
|
||||||
Value value_from_tt(Value v, int ply);
|
Value value_from_tt(Value v, int ply);
|
||||||
void update_pv(Move* pv, Move move, Move* childPv);
|
void update_pv(Move* pv, Move move, Move* childPv);
|
||||||
void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus);
|
void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus);
|
||||||
void update_stats(const Position& pos, Stack* ss, Move move, Move* quiets, int quietsCnt, int bonus);
|
void update_quiet_stats(const Position& pos, Stack* ss, Move move, Move* quiets, int quietsCnt, int bonus);
|
||||||
void update_capture_stats(const Position& pos, Move move, Move* captures, int captureCnt, int bonus);
|
void update_capture_stats(const Position& pos, Move move, Move* captures, int captureCnt, int bonus);
|
||||||
|
|
||||||
// perft() is our utility to verify move generation. All the leaf nodes up
|
// perft() is our utility to verify move generation. All the leaf nodes up
|
||||||
|
@ -137,7 +137,7 @@ namespace {
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
/// Search::init() is called during startup to initialize various lookup tables
|
/// Search::init() is called at startup to initialize various lookup tables
|
||||||
|
|
||||||
void Search::init() {
|
void Search::init() {
|
||||||
|
|
||||||
|
@ -252,7 +252,7 @@ void MainThread::search() {
|
||||||
|
|
||||||
previousScore = bestThread->rootMoves[0].score;
|
previousScore = bestThread->rootMoves[0].score;
|
||||||
|
|
||||||
// Send new PV when needed
|
// Send again PV info if we have a new best thread
|
||||||
if (bestThread != this)
|
if (bestThread != this)
|
||||||
sync_cout << UCI::pv(bestThread->rootPos, bestThread->completedDepth, -VALUE_INFINITE, VALUE_INFINITE) << sync_endl;
|
sync_cout << UCI::pv(bestThread->rootPos, bestThread->completedDepth, -VALUE_INFINITE, VALUE_INFINITE) << sync_endl;
|
||||||
|
|
||||||
|
@ -287,10 +287,7 @@ void Thread::search() {
|
||||||
beta = VALUE_INFINITE;
|
beta = VALUE_INFINITE;
|
||||||
|
|
||||||
if (mainThread)
|
if (mainThread)
|
||||||
{
|
mainThread->bestMoveChanges = 0, mainThread->failedLow = false;
|
||||||
mainThread->failedLow = false;
|
|
||||||
mainThread->bestMoveChanges = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t multiPV = Options["MultiPV"];
|
size_t multiPV = Options["MultiPV"];
|
||||||
Skill skill(Options["Skill Level"]);
|
Skill skill(Options["Skill Level"]);
|
||||||
|
@ -302,21 +299,21 @@ void Thread::search() {
|
||||||
|
|
||||||
multiPV = std::min(multiPV, rootMoves.size());
|
multiPV = std::min(multiPV, rootMoves.size());
|
||||||
|
|
||||||
int contempt = Options["Contempt"] * PawnValueEg / 100; // From centipawns
|
int ct = Options["Contempt"] * PawnValueEg / 100; // From centipawns
|
||||||
Eval::Contempt = (us == WHITE ? make_score(contempt, contempt / 2)
|
Eval::Contempt = (us == WHITE ? make_score(ct, ct / 2)
|
||||||
: -make_score(contempt, contempt / 2));
|
: -make_score(ct, ct / 2));
|
||||||
|
|
||||||
// Iterative deepening loop until requested to stop or the target depth is reached
|
// Iterative deepening loop until requested to stop or the target depth is reached
|
||||||
while ( (rootDepth += ONE_PLY) < DEPTH_MAX
|
while ( (rootDepth += ONE_PLY) < DEPTH_MAX
|
||||||
&& !Threads.stop
|
&& !Threads.stop
|
||||||
&& !(Limits.depth && mainThread && rootDepth / ONE_PLY > Limits.depth))
|
&& !(Limits.depth && mainThread && rootDepth / ONE_PLY > Limits.depth))
|
||||||
{
|
{
|
||||||
// Distribute search depths across the threads
|
// Distribute search depths across the helper threads
|
||||||
if (idx)
|
if (idx > 0)
|
||||||
{
|
{
|
||||||
int i = (idx - 1) % 20;
|
int i = (idx - 1) % 20;
|
||||||
if (((rootDepth / ONE_PLY + rootPos.game_ply() + skipPhase[i]) / skipSize[i]) % 2)
|
if (((rootDepth / ONE_PLY + rootPos.game_ply() + SkipPhase[i]) / SkipSize[i]) % 2)
|
||||||
continue;
|
continue; // Retry with an incremented rootDepth
|
||||||
}
|
}
|
||||||
|
|
||||||
// Age out PV variability metric
|
// Age out PV variability metric
|
||||||
|
@ -341,18 +338,18 @@ void Thread::search() {
|
||||||
alpha = std::max(rootMoves[PVIdx].previousScore - delta,-VALUE_INFINITE);
|
alpha = std::max(rootMoves[PVIdx].previousScore - delta,-VALUE_INFINITE);
|
||||||
beta = std::min(rootMoves[PVIdx].previousScore + delta, VALUE_INFINITE);
|
beta = std::min(rootMoves[PVIdx].previousScore + delta, VALUE_INFINITE);
|
||||||
|
|
||||||
// Adjust contempt based on current situation
|
// Adjust contempt based on current bestValue
|
||||||
contempt = Options["Contempt"] * PawnValueEg / 100; // From centipawns
|
ct = Options["Contempt"] * PawnValueEg / 100 // From centipawns
|
||||||
contempt += bestValue > 500 ? 50: // Dynamic contempt
|
+ (bestValue > 500 ? 50: // Dynamic contempt
|
||||||
bestValue < -500 ? -50:
|
bestValue < -500 ? -50:
|
||||||
bestValue / 10;
|
bestValue / 10);
|
||||||
|
|
||||||
Eval::Contempt = (us == WHITE ? make_score(contempt, contempt / 2)
|
Eval::Contempt = (us == WHITE ? make_score(ct, ct / 2)
|
||||||
: -make_score(contempt, contempt / 2));
|
: -make_score(ct, ct / 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start with a small aspiration window and, in the case of a fail
|
// Start with a small aspiration window and, in the case of a fail
|
||||||
// high/low, re-search with a bigger window until we're not failing
|
// high/low, re-search with a bigger window until we don't fail
|
||||||
// high/low anymore.
|
// high/low anymore.
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
@ -366,9 +363,9 @@ void Thread::search() {
|
||||||
// search the already searched PV lines are preserved.
|
// search the already searched PV lines are preserved.
|
||||||
std::stable_sort(rootMoves.begin() + PVIdx, rootMoves.end());
|
std::stable_sort(rootMoves.begin() + PVIdx, rootMoves.end());
|
||||||
|
|
||||||
// If search has been stopped, we break immediately. Sorting and
|
// If search has been stopped, we break immediately. Sorting is
|
||||||
// writing PV back to TT is safe because RootMoves is still
|
// safe because RootMoves is still valid, although it refers to
|
||||||
// valid, although it refers to the previous iteration.
|
// the previous iteration.
|
||||||
if (Threads.stop)
|
if (Threads.stop)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -433,27 +430,26 @@ void Thread::search() {
|
||||||
skill.pick_best(multiPV);
|
skill.pick_best(multiPV);
|
||||||
|
|
||||||
// Do we have time for the next iteration? Can we stop searching now?
|
// Do we have time for the next iteration? Can we stop searching now?
|
||||||
if (Limits.use_time_management())
|
if ( Limits.use_time_management()
|
||||||
|
&& !Threads.stop
|
||||||
|
&& !Threads.stopOnPonderhit)
|
||||||
{
|
{
|
||||||
if (!Threads.stop && !Threads.stopOnPonderhit)
|
|
||||||
{
|
|
||||||
// Stop the search if only one legal move is available, or if all
|
|
||||||
// of the available time has been used
|
|
||||||
const int F[] = { mainThread->failedLow,
|
const int F[] = { mainThread->failedLow,
|
||||||
bestValue - mainThread->previousScore };
|
bestValue - mainThread->previousScore };
|
||||||
|
|
||||||
int improvingFactor = std::max(229, std::min(715, 357 + 119 * F[0] - 6 * F[1]));
|
int improvingFactor = std::max(229, std::min(715, 357 + 119 * F[0] - 6 * F[1]));
|
||||||
|
|
||||||
double unstablePvFactor = 1 + mainThread->bestMoveChanges;
|
// If the bestMove is stable over several iterations, reduce time accordingly
|
||||||
|
timeReduction = 1.0;
|
||||||
// if the bestMove is stable over several iterations, reduce time for this move,
|
|
||||||
// the longer the move has been stable, the more.
|
|
||||||
// Use part of the gained time from a previous stable move for the current move.
|
|
||||||
timeReduction = 1;
|
|
||||||
for (int i : {3, 4, 5})
|
for (int i : {3, 4, 5})
|
||||||
if (lastBestMoveDepth * i < completedDepth)
|
if (lastBestMoveDepth * i < completedDepth)
|
||||||
timeReduction *= 1.3;
|
timeReduction *= 1.3;
|
||||||
|
|
||||||
|
// Use part of the gained time from a previous stable move for the current move
|
||||||
|
double unstablePvFactor = 1.0 + mainThread->bestMoveChanges;
|
||||||
unstablePvFactor *= std::pow(mainThread->previousTimeReduction, 0.51) / timeReduction;
|
unstablePvFactor *= std::pow(mainThread->previousTimeReduction, 0.51) / timeReduction;
|
||||||
|
|
||||||
|
// Stop the search if we have only one legal move, or if available time elapsed
|
||||||
if ( rootMoves.size() == 1
|
if ( rootMoves.size() == 1
|
||||||
|| Time.elapsed() > Time.optimum() * unstablePvFactor * improvingFactor / 605)
|
|| Time.elapsed() > Time.optimum() * unstablePvFactor * improvingFactor / 605)
|
||||||
{
|
{
|
||||||
|
@ -466,7 +462,6 @@ void Thread::search() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!mainThread)
|
if (!mainThread)
|
||||||
return;
|
return;
|
||||||
|
@ -527,8 +522,10 @@ namespace {
|
||||||
if (!rootNode)
|
if (!rootNode)
|
||||||
{
|
{
|
||||||
// Step 2. Check for aborted search and immediate draw
|
// Step 2. Check for aborted search and immediate draw
|
||||||
if (Threads.stop.load(std::memory_order_relaxed) || pos.is_draw(ss->ply) || ss->ply >= MAX_PLY)
|
if ( Threads.stop.load(std::memory_order_relaxed)
|
||||||
return ss->ply >= MAX_PLY && !inCheck ? evaluate(pos) : VALUE_DRAW;
|
|| pos.is_draw(ss->ply)
|
||||||
|
|| ss->ply >= MAX_PLY)
|
||||||
|
return (ss->ply >= MAX_PLY && !inCheck) ? evaluate(pos) : VALUE_DRAW;
|
||||||
|
|
||||||
// Step 3. Mate distance pruning. Even if we mate at the next move our score
|
// Step 3. Mate distance pruning. Even if we mate at the next move our score
|
||||||
// would be at best mate_in(ss->ply+1), but if alpha is already bigger because
|
// would be at best mate_in(ss->ply+1), but if alpha is already bigger because
|
||||||
|
@ -554,7 +551,7 @@ namespace {
|
||||||
// search to overwrite a previous full search TT value, so we use a different
|
// search to overwrite a previous full search TT value, so we use a different
|
||||||
// position key in case of an excluded move.
|
// position key in case of an excluded move.
|
||||||
excludedMove = ss->excludedMove;
|
excludedMove = ss->excludedMove;
|
||||||
posKey = pos.key() ^ Key(excludedMove << 16); // isn't a very good hash
|
posKey = pos.key() ^ Key(excludedMove << 16); // Isn't a very good hash
|
||||||
tte = TT.probe(posKey, ttHit);
|
tte = TT.probe(posKey, ttHit);
|
||||||
ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
|
ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
|
||||||
ttMove = rootNode ? thisThread->rootMoves[thisThread->PVIdx].pv[0]
|
ttMove = rootNode ? thisThread->rootMoves[thisThread->PVIdx].pv[0]
|
||||||
|
@ -574,7 +571,7 @@ namespace {
|
||||||
if (ttValue >= beta)
|
if (ttValue >= beta)
|
||||||
{
|
{
|
||||||
if (!pos.capture_or_promotion(ttMove))
|
if (!pos.capture_or_promotion(ttMove))
|
||||||
update_stats(pos, ss, ttMove, nullptr, 0, stat_bonus(depth));
|
update_quiet_stats(pos, ss, ttMove, nullptr, 0, stat_bonus(depth));
|
||||||
|
|
||||||
// Extra penalty for a quiet TT move in previous ply when it gets refuted
|
// Extra penalty for a quiet TT move in previous ply when it gets refuted
|
||||||
if ((ss-1)->moveCount == 1 && !pos.captured_piece())
|
if ((ss-1)->moveCount == 1 && !pos.captured_piece())
|
||||||
|
@ -591,7 +588,7 @@ namespace {
|
||||||
return ttValue;
|
return ttValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 4a. Tablebase probe
|
// Step 5. Tablebases probe
|
||||||
if (!rootNode && TB::Cardinality)
|
if (!rootNode && TB::Cardinality)
|
||||||
{
|
{
|
||||||
int piecesCount = pos.count<ALL_PIECES>();
|
int piecesCount = pos.count<ALL_PIECES>();
|
||||||
|
@ -638,13 +635,12 @@ namespace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 5. Evaluate the position statically
|
// Step 6. Evaluate the position statically
|
||||||
if (inCheck)
|
if (inCheck)
|
||||||
{
|
{
|
||||||
ss->staticEval = eval = VALUE_NONE;
|
ss->staticEval = eval = VALUE_NONE;
|
||||||
goto moves_loop;
|
goto moves_loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (ttHit)
|
else if (ttHit)
|
||||||
{
|
{
|
||||||
// Never assume anything on values stored in TT
|
// Never assume anything on values stored in TT
|
||||||
|
@ -658,7 +654,7 @@ namespace {
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
eval = ss->staticEval =
|
ss->staticEval = eval =
|
||||||
(ss-1)->currentMove != MOVE_NULL ? evaluate(pos)
|
(ss-1)->currentMove != MOVE_NULL ? evaluate(pos)
|
||||||
: -(ss-1)->staticEval + 2 * Eval::Tempo;
|
: -(ss-1)->staticEval + 2 * Eval::Tempo;
|
||||||
|
|
||||||
|
@ -669,34 +665,33 @@ namespace {
|
||||||
if (skipEarlyPruning || !pos.non_pawn_material(pos.side_to_move()))
|
if (skipEarlyPruning || !pos.non_pawn_material(pos.side_to_move()))
|
||||||
goto moves_loop;
|
goto moves_loop;
|
||||||
|
|
||||||
// Step 6. Razoring (skipped when in check)
|
// Step 7. Razoring (skipped when in check)
|
||||||
if ( !PvNode
|
if ( !PvNode
|
||||||
&& depth < 4 * ONE_PLY
|
&& depth < 4 * ONE_PLY
|
||||||
&& eval + razor_margin <= alpha)
|
&& eval + RazorMargin <= alpha)
|
||||||
{
|
{
|
||||||
if (depth <= ONE_PLY)
|
if (depth <= ONE_PLY)
|
||||||
return qsearch<NonPV, false>(pos, ss, alpha, alpha+1);
|
return qsearch<NonPV, false>(pos, ss, alpha, alpha+1);
|
||||||
|
|
||||||
Value ralpha = alpha - razor_margin;
|
Value ralpha = alpha - RazorMargin;
|
||||||
Value v = qsearch<NonPV, false>(pos, ss, ralpha, ralpha+1);
|
Value v = qsearch<NonPV, false>(pos, ss, ralpha, ralpha+1);
|
||||||
if (v <= ralpha)
|
if (v <= ralpha)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 7. Futility pruning: child node (skipped when in check)
|
// Step 8. Futility pruning: child node (skipped when in check)
|
||||||
if ( !rootNode
|
if ( !rootNode
|
||||||
&& depth < 7 * ONE_PLY
|
&& depth < 7 * ONE_PLY
|
||||||
&& eval - futility_margin(depth) >= beta
|
&& eval - futility_margin(depth) >= beta
|
||||||
&& eval < VALUE_KNOWN_WIN) // Do not return unproven wins
|
&& eval < VALUE_KNOWN_WIN) // Do not return unproven wins
|
||||||
return eval;
|
return eval;
|
||||||
|
|
||||||
// Step 8. Null move search with verification search (is omitted in PV nodes)
|
// Step 9. Null move search with verification search
|
||||||
if ( !PvNode
|
if ( !PvNode
|
||||||
&& eval >= beta
|
&& eval >= beta
|
||||||
&& ss->staticEval >= beta - 36 * depth / ONE_PLY + 225
|
&& ss->staticEval >= beta - 36 * depth / ONE_PLY + 225
|
||||||
&& (ss->ply >= thisThread->nmp_ply || ss->ply % 2 != thisThread->nmp_odd))
|
&& (ss->ply >= thisThread->nmp_ply || ss->ply % 2 != thisThread->nmp_odd))
|
||||||
{
|
{
|
||||||
|
|
||||||
assert(eval - beta >= 0);
|
assert(eval - beta >= 0);
|
||||||
|
|
||||||
// Null move dynamic reduction based on depth and value
|
// Null move dynamic reduction based on depth and value
|
||||||
|
@ -719,8 +714,8 @@ namespace {
|
||||||
if (abs(beta) < VALUE_KNOWN_WIN && (depth < 12 * ONE_PLY || thisThread->nmp_ply))
|
if (abs(beta) < VALUE_KNOWN_WIN && (depth < 12 * ONE_PLY || thisThread->nmp_ply))
|
||||||
return nullValue;
|
return nullValue;
|
||||||
|
|
||||||
// Do verification search at high depths
|
// Do verification search at high depths. Disable null move pruning
|
||||||
// disable null move pruning for side to move for the first part of the remaining search tree
|
// for side to move for the first part of the remaining search tree.
|
||||||
thisThread->nmp_ply = ss->ply + 3 * (depth-R) / 4;
|
thisThread->nmp_ply = ss->ply + 3 * (depth-R) / 4;
|
||||||
thisThread->nmp_odd = ss->ply % 2;
|
thisThread->nmp_odd = ss->ply % 2;
|
||||||
|
|
||||||
|
@ -734,17 +729,16 @@ namespace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 9. ProbCut (skipped when in check)
|
// Step 10. ProbCut (skipped when in check)
|
||||||
// If we have a good enough capture and a reduced search returns a value
|
// If we have a good enough capture and a reduced search returns a value
|
||||||
// much above beta, we can (almost) safely prune the previous move.
|
// much above beta, we can (almost) safely prune the previous move.
|
||||||
if ( !PvNode
|
if ( !PvNode
|
||||||
&& depth >= 5 * ONE_PLY
|
&& depth >= 5 * ONE_PLY
|
||||||
&& abs(beta) < VALUE_MATE_IN_MAX_PLY)
|
&& abs(beta) < VALUE_MATE_IN_MAX_PLY)
|
||||||
{
|
{
|
||||||
Value rbeta = std::min(beta + 200, VALUE_INFINITE);
|
|
||||||
|
|
||||||
assert(is_ok((ss-1)->currentMove));
|
assert(is_ok((ss-1)->currentMove));
|
||||||
|
|
||||||
|
Value rbeta = std::min(beta + 200, VALUE_INFINITE);
|
||||||
MovePicker mp(pos, ttMove, rbeta - ss->staticEval, &thisThread->captureHistory);
|
MovePicker mp(pos, ttMove, rbeta - ss->staticEval, &thisThread->captureHistory);
|
||||||
|
|
||||||
while ((move = mp.next_move()) != MOVE_NONE)
|
while ((move = mp.next_move()) != MOVE_NONE)
|
||||||
|
@ -754,6 +748,7 @@ namespace {
|
||||||
ss->contHistory = &thisThread->contHistory[pos.moved_piece(move)][to_sq(move)];
|
ss->contHistory = &thisThread->contHistory[pos.moved_piece(move)][to_sq(move)];
|
||||||
|
|
||||||
assert(depth >= 5 * ONE_PLY);
|
assert(depth >= 5 * ONE_PLY);
|
||||||
|
|
||||||
pos.do_move(move, st);
|
pos.do_move(move, st);
|
||||||
value = -search<NonPV>(pos, ss+1, -rbeta, -rbeta+1, depth - 4 * ONE_PLY, !cutNode, false);
|
value = -search<NonPV>(pos, ss+1, -rbeta, -rbeta+1, depth - 4 * ONE_PLY, !cutNode, false);
|
||||||
pos.undo_move(move);
|
pos.undo_move(move);
|
||||||
|
@ -762,19 +757,19 @@ namespace {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 10. Internal iterative deepening (skipped when in check)
|
// Step 11. Internal iterative deepening (skipped when in check)
|
||||||
if ( depth >= 6 * ONE_PLY
|
if ( depth >= 6 * ONE_PLY
|
||||||
&& !ttMove
|
&& !ttMove
|
||||||
&& (PvNode || ss->staticEval + 256 >= beta))
|
&& (PvNode || ss->staticEval + 256 >= beta))
|
||||||
{
|
{
|
||||||
Depth d = (3 * depth / (4 * ONE_PLY) - 2) * ONE_PLY;
|
Depth d = 3 * depth / 4 - 2 * ONE_PLY;
|
||||||
search<NT>(pos, ss, alpha, beta, d, cutNode, true);
|
search<NT>(pos, ss, alpha, beta, d, cutNode, true);
|
||||||
|
|
||||||
tte = TT.probe(posKey, ttHit);
|
tte = TT.probe(posKey, ttHit);
|
||||||
ttMove = ttHit ? tte->move() : MOVE_NONE;
|
ttMove = ttHit ? tte->move() : MOVE_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
moves_loop: // When in check search starts from here
|
moves_loop: // When in check, search starts from here
|
||||||
|
|
||||||
const PieceToHistory* contHist[] = { (ss-1)->contHistory, (ss-2)->contHistory, nullptr, (ss-4)->contHistory };
|
const PieceToHistory* contHist[] = { (ss-1)->contHistory, (ss-2)->contHistory, nullptr, (ss-4)->contHistory };
|
||||||
Move countermove = thisThread->counterMoves[pos.piece_on(prevSq)][prevSq];
|
Move countermove = thisThread->counterMoves[pos.piece_on(prevSq)][prevSq];
|
||||||
|
@ -796,8 +791,8 @@ moves_loop: // When in check search starts from here
|
||||||
ttCapture = false;
|
ttCapture = false;
|
||||||
pvExact = PvNode && ttHit && tte->bound() == BOUND_EXACT;
|
pvExact = PvNode && ttHit && tte->bound() == BOUND_EXACT;
|
||||||
|
|
||||||
// Step 11. Loop through moves
|
// Step 12. Loop through all pseudo-legal moves until no moves remain
|
||||||
// Loop through all pseudo-legal moves until no moves remain or a beta cutoff occurs
|
// or a beta cutoff occurs.
|
||||||
while ((move = mp.next_move(skipQuiets)) != MOVE_NONE)
|
while ((move = mp.next_move(skipQuiets)) != MOVE_NONE)
|
||||||
{
|
{
|
||||||
assert(is_ok(move));
|
assert(is_ok(move));
|
||||||
|
@ -818,7 +813,6 @@ moves_loop: // When in check search starts from here
|
||||||
sync_cout << "info depth " << depth / ONE_PLY
|
sync_cout << "info depth " << depth / ONE_PLY
|
||||||
<< " currmove " << UCI::move(move, pos.is_chess960())
|
<< " currmove " << UCI::move(move, pos.is_chess960())
|
||||||
<< " currmovenumber " << moveCount + thisThread->PVIdx << sync_endl;
|
<< " currmovenumber " << moveCount + thisThread->PVIdx << sync_endl;
|
||||||
|
|
||||||
if (PvNode)
|
if (PvNode)
|
||||||
(ss+1)->pv = nullptr;
|
(ss+1)->pv = nullptr;
|
||||||
|
|
||||||
|
@ -833,27 +827,26 @@ moves_loop: // When in check search starts from here
|
||||||
moveCountPruning = depth < 16 * ONE_PLY
|
moveCountPruning = depth < 16 * ONE_PLY
|
||||||
&& moveCount >= FutilityMoveCounts[improving][depth / ONE_PLY];
|
&& moveCount >= FutilityMoveCounts[improving][depth / ONE_PLY];
|
||||||
|
|
||||||
// Step 12. Singular and Gives Check Extensions
|
// Step 13. Extensions
|
||||||
|
|
||||||
// Singular extension search. If all moves but one fail low on a search of
|
// Singular extension search. If all moves but one fail low on a search
|
||||||
// (alpha-s, beta-s), and just one fails high on (alpha, beta), then that move
|
// of (alpha-s, beta-s), and just one fails high on (alpha, beta), then
|
||||||
// is singular and should be extended. To verify this we do a reduced search
|
// that move is singular and should be extended. To verify this we do a
|
||||||
// on all the other moves but the ttMove and if the result is lower than
|
// reduced search on on all the other moves but the ttMove and if the
|
||||||
// ttValue minus a margin then we will extend the ttMove.
|
// result is lower than ttValue minus a margin then we will extend the ttMove.
|
||||||
if ( singularExtensionNode
|
if ( singularExtensionNode
|
||||||
&& move == ttMove
|
&& move == ttMove
|
||||||
&& pos.legal(move))
|
&& pos.legal(move))
|
||||||
{
|
{
|
||||||
Value rBeta = std::max(ttValue - 2 * depth / ONE_PLY, -VALUE_MATE);
|
Value rBeta = std::max(ttValue - 2 * depth / ONE_PLY, -VALUE_MATE);
|
||||||
Depth d = (depth / (2 * ONE_PLY)) * ONE_PLY;
|
|
||||||
ss->excludedMove = move;
|
ss->excludedMove = move;
|
||||||
value = search<NonPV>(pos, ss, rBeta - 1, rBeta, d, cutNode, true);
|
value = search<NonPV>(pos, ss, rBeta - 1, rBeta, depth / 2, cutNode, true);
|
||||||
ss->excludedMove = MOVE_NONE;
|
ss->excludedMove = MOVE_NONE;
|
||||||
|
|
||||||
if (value < rBeta)
|
if (value < rBeta)
|
||||||
extension = ONE_PLY;
|
extension = ONE_PLY;
|
||||||
}
|
}
|
||||||
else if ( givesCheck
|
else if ( givesCheck // Check extension
|
||||||
&& !moveCountPruning
|
&& !moveCountPruning
|
||||||
&& pos.see_ge(move))
|
&& pos.see_ge(move))
|
||||||
extension = ONE_PLY;
|
extension = ONE_PLY;
|
||||||
|
@ -861,7 +854,7 @@ moves_loop: // When in check search starts from here
|
||||||
// Calculate new depth for this move
|
// Calculate new depth for this move
|
||||||
newDepth = depth - ONE_PLY + extension;
|
newDepth = depth - ONE_PLY + extension;
|
||||||
|
|
||||||
// Step 13. Pruning at shallow depth
|
// Step 14. Pruning at shallow depth
|
||||||
if ( !rootNode
|
if ( !rootNode
|
||||||
&& pos.non_pawn_material(pos.side_to_move())
|
&& pos.non_pawn_material(pos.side_to_move())
|
||||||
&& bestValue > VALUE_MATED_IN_MAX_PLY)
|
&& bestValue > VALUE_MATED_IN_MAX_PLY)
|
||||||
|
@ -920,10 +913,10 @@ moves_loop: // When in check search starts from here
|
||||||
ss->currentMove = move;
|
ss->currentMove = move;
|
||||||
ss->contHistory = &thisThread->contHistory[movedPiece][to_sq(move)];
|
ss->contHistory = &thisThread->contHistory[movedPiece][to_sq(move)];
|
||||||
|
|
||||||
// Step 14. Make the move
|
// Step 15. Make the move
|
||||||
pos.do_move(move, st, givesCheck);
|
pos.do_move(move, st, givesCheck);
|
||||||
|
|
||||||
// Step 15. Reduced depth search (LMR). If the move fails high it will be
|
// Step 16. Reduced depth search (LMR). If the move fails high it will be
|
||||||
// re-searched at full depth.
|
// re-searched at full depth.
|
||||||
if ( depth >= 3 * ONE_PLY
|
if ( depth >= 3 * ONE_PLY
|
||||||
&& moveCount > 1
|
&& moveCount > 1
|
||||||
|
@ -984,7 +977,7 @@ moves_loop: // When in check search starts from here
|
||||||
else
|
else
|
||||||
doFullDepthSearch = !PvNode || moveCount > 1;
|
doFullDepthSearch = !PvNode || moveCount > 1;
|
||||||
|
|
||||||
// Step 16. Full depth search when LMR is skipped or fails high
|
// Step 17. Full depth search when LMR is skipped or fails high
|
||||||
if (doFullDepthSearch)
|
if (doFullDepthSearch)
|
||||||
value = newDepth < ONE_PLY ?
|
value = newDepth < ONE_PLY ?
|
||||||
givesCheck ? -qsearch<NonPV, true>(pos, ss+1, -(alpha+1), -alpha)
|
givesCheck ? -qsearch<NonPV, true>(pos, ss+1, -(alpha+1), -alpha)
|
||||||
|
@ -1005,12 +998,12 @@ moves_loop: // When in check search starts from here
|
||||||
: - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, false);
|
: - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 17. Undo move
|
// Step 18. Undo move
|
||||||
pos.undo_move(move);
|
pos.undo_move(move);
|
||||||
|
|
||||||
assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);
|
assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);
|
||||||
|
|
||||||
// Step 18. Check for a new best move
|
// Step 19. Check for a new best move
|
||||||
// Finished searching the move. If a stop occurred, the return value of
|
// Finished searching the move. If a stop occurred, the return value of
|
||||||
// the search cannot be trusted, and we return immediately without
|
// the search cannot be trusted, and we return immediately without
|
||||||
// updating best move, PV and TT.
|
// updating best move, PV and TT.
|
||||||
|
@ -1068,10 +1061,14 @@ moves_loop: // When in check search starts from here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!captureOrPromotion && move != bestMove && quietCount < 64)
|
if (move != bestMove)
|
||||||
quietsSearched[quietCount++] = move;
|
{
|
||||||
else if (captureOrPromotion && move != bestMove && captureCount < 32)
|
if (captureOrPromotion && captureCount < 32)
|
||||||
capturesSearched[captureCount++] = move;
|
capturesSearched[captureCount++] = move;
|
||||||
|
|
||||||
|
else if (!captureOrPromotion && quietCount < 64)
|
||||||
|
quietsSearched[quietCount++] = move;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The following condition would detect a stop only after move loop has been
|
// The following condition would detect a stop only after move loop has been
|
||||||
|
@ -1096,7 +1093,7 @@ moves_loop: // When in check search starts from here
|
||||||
{
|
{
|
||||||
// Quiet best move: update move sorting heuristics
|
// Quiet best move: update move sorting heuristics
|
||||||
if (!pos.capture_or_promotion(bestMove))
|
if (!pos.capture_or_promotion(bestMove))
|
||||||
update_stats(pos, ss, bestMove, quietsSearched, quietCount, stat_bonus(depth));
|
update_quiet_stats(pos, ss, bestMove, quietsSearched, quietCount, stat_bonus(depth));
|
||||||
else
|
else
|
||||||
update_capture_stats(pos, bestMove, capturesSearched, captureCount, stat_bonus(depth));
|
update_capture_stats(pos, bestMove, capturesSearched, captureCount, stat_bonus(depth));
|
||||||
|
|
||||||
|
@ -1133,20 +1130,20 @@ moves_loop: // When in check search starts from here
|
||||||
|
|
||||||
const bool PvNode = NT == PV;
|
const bool PvNode = NT == PV;
|
||||||
|
|
||||||
assert(InCheck == bool(pos.checkers()));
|
|
||||||
assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
|
assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
|
||||||
assert(PvNode || (alpha == beta - 1));
|
assert(PvNode || (alpha == beta - 1));
|
||||||
assert(depth <= DEPTH_ZERO);
|
assert(depth <= DEPTH_ZERO);
|
||||||
assert(depth / ONE_PLY * ONE_PLY == depth);
|
assert(depth / ONE_PLY * ONE_PLY == depth);
|
||||||
|
assert(InCheck == bool(pos.checkers()));
|
||||||
|
|
||||||
Move pv[MAX_PLY+1];
|
Move pv[MAX_PLY+1];
|
||||||
StateInfo st;
|
StateInfo st;
|
||||||
TTEntry* tte;
|
TTEntry* tte;
|
||||||
Key posKey;
|
Key posKey;
|
||||||
Move ttMove, move, bestMove;
|
Move ttMove, move, bestMove;
|
||||||
|
Depth ttDepth;
|
||||||
Value bestValue, value, ttValue, futilityValue, futilityBase, oldAlpha;
|
Value bestValue, value, ttValue, futilityValue, futilityBase, oldAlpha;
|
||||||
bool ttHit, givesCheck, evasionPrunable;
|
bool ttHit, givesCheck, evasionPrunable;
|
||||||
Depth ttDepth;
|
|
||||||
int moveCount;
|
int moveCount;
|
||||||
|
|
||||||
if (PvNode)
|
if (PvNode)
|
||||||
|
@ -1156,13 +1153,14 @@ moves_loop: // When in check search starts from here
|
||||||
ss->pv[0] = MOVE_NONE;
|
ss->pv[0] = MOVE_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
ss->currentMove = bestMove = MOVE_NONE;
|
|
||||||
(ss+1)->ply = ss->ply + 1;
|
(ss+1)->ply = ss->ply + 1;
|
||||||
|
ss->currentMove = bestMove = MOVE_NONE;
|
||||||
moveCount = 0;
|
moveCount = 0;
|
||||||
|
|
||||||
// Check for an instant draw or if the maximum ply has been reached
|
// Check for an immediate draw or maximum ply reached
|
||||||
if (pos.is_draw(ss->ply) || ss->ply >= MAX_PLY)
|
if ( pos.is_draw(ss->ply)
|
||||||
return ss->ply >= MAX_PLY && !InCheck ? evaluate(pos) : VALUE_DRAW;
|
|| ss->ply >= MAX_PLY)
|
||||||
|
return (ss->ply >= MAX_PLY && !InCheck) ? evaluate(pos) : VALUE_DRAW;
|
||||||
|
|
||||||
assert(0 <= ss->ply && ss->ply < MAX_PLY);
|
assert(0 <= ss->ply && ss->ply < MAX_PLY);
|
||||||
|
|
||||||
|
@ -1403,9 +1401,9 @@ moves_loop: // When in check search starts from here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// update_stats() updates move sorting heuristics when a new quiet best move is found
|
// update_quiet_stats() updates move sorting heuristics when a new quiet best move is found
|
||||||
|
|
||||||
void update_stats(const Position& pos, Stack* ss, Move move,
|
void update_quiet_stats(const Position& pos, Stack* ss, Move move,
|
||||||
Move* quiets, int quietsCnt, int bonus) {
|
Move* quiets, int quietsCnt, int bonus) {
|
||||||
|
|
||||||
if (ss->killers[0] != move)
|
if (ss->killers[0] != move)
|
||||||
|
@ -1414,9 +1412,9 @@ moves_loop: // When in check search starts from here
|
||||||
ss->killers[0] = move;
|
ss->killers[0] = move;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color c = pos.side_to_move();
|
Color us = pos.side_to_move();
|
||||||
Thread* thisThread = pos.this_thread();
|
Thread* thisThread = pos.this_thread();
|
||||||
thisThread->mainHistory.update(c, move, bonus);
|
thisThread->mainHistory.update(us, move, bonus);
|
||||||
update_continuation_histories(ss, pos.moved_piece(move), to_sq(move), bonus);
|
update_continuation_histories(ss, pos.moved_piece(move), to_sq(move), bonus);
|
||||||
|
|
||||||
if (is_ok((ss-1)->currentMove))
|
if (is_ok((ss-1)->currentMove))
|
||||||
|
@ -1428,7 +1426,7 @@ moves_loop: // When in check search starts from here
|
||||||
// Decrease all the other played quiet moves
|
// Decrease all the other played quiet moves
|
||||||
for (int i = 0; i < quietsCnt; ++i)
|
for (int i = 0; i < quietsCnt; ++i)
|
||||||
{
|
{
|
||||||
thisThread->mainHistory.update(c, quiets[i], -bonus);
|
thisThread->mainHistory.update(us, quiets[i], -bonus);
|
||||||
update_continuation_histories(ss, pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
|
update_continuation_histories(ss, pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1468,16 +1466,15 @@ moves_loop: // When in check search starts from here
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
// check_time() is used to print debug info and, more importantly, to detect
|
/// MainThread::check_time() is used to print debug info and, more importantly,
|
||||||
// when we are out of available time and thus stop the search.
|
/// to detect when we are out of available time and thus stop the search.
|
||||||
|
|
||||||
void MainThread::check_time() {
|
void MainThread::check_time() {
|
||||||
|
|
||||||
if (--callsCnt > 0)
|
if (--callsCnt > 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// At low node count increase the checking rate to about 0.1% of nodes
|
// When using nodes, ensure checking rate is not lower than 0.1% of nodes
|
||||||
// otherwise use a default value.
|
|
||||||
callsCnt = Limits.nodes ? std::min(4096, int(Limits.nodes / 1024)) : 4096;
|
callsCnt = Limits.nodes ? std::min(4096, int(Limits.nodes / 1024)) : 4096;
|
||||||
|
|
||||||
static TimePoint lastInfoTime = now();
|
static TimePoint lastInfoTime = now();
|
||||||
|
@ -1491,7 +1488,7 @@ moves_loop: // When in check search starts from here
|
||||||
dbg_print();
|
dbg_print();
|
||||||
}
|
}
|
||||||
|
|
||||||
// An engine may not stop pondering until told so by the GUI
|
// We should not stop pondering until told so by the GUI
|
||||||
if (Threads.ponder)
|
if (Threads.ponder)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -1587,6 +1584,7 @@ bool RootMove::extract_ponder_from_tt(Position& pos) {
|
||||||
return pv.size() > 1;
|
return pv.size() > 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Tablebases::filter_root_moves(Position& pos, Search::RootMoves& rootMoves) {
|
void Tablebases::filter_root_moves(Position& pos, Search::RootMoves& rootMoves) {
|
||||||
|
|
||||||
RootInTB = false;
|
RootInTB = false;
|
||||||
|
|
|
@ -147,7 +147,7 @@ void ThreadPool::clear() {
|
||||||
|
|
||||||
main()->callsCnt = 0;
|
main()->callsCnt = 0;
|
||||||
main()->previousScore = VALUE_INFINITE;
|
main()->previousScore = VALUE_INFINITE;
|
||||||
main()->previousTimeReduction = 1;
|
main()->previousTimeReduction = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
|
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
|
||||||
|
|
Loading…
Add table
Reference in a new issue