mirror of
https://github.com/sockspls/badfish
synced 2025-04-29 16:23:09 +00:00
Refactor get_best_thread
Make get_best_thread function easier to understand. Passed non-reg SMP STC: https://tests.stockfishchess.org/tests/view/65a91c6679aa8af82b975500 LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 186000 W: 46379 L: 46325 D: 93296 Ptnml(0-2): 269, 21374, 49634, 21480, 243 closes https://github.com/official-stockfish/Stockfish/pull/5001 No functional change
This commit is contained in:
parent
e860f620aa
commit
ad9fcbc496
1 changed files with 38 additions and 21 deletions
|
@ -210,8 +210,9 @@ void ThreadPool::start_thinking(const OptionsMap& options,
|
|||
|
||||
Thread* ThreadPool::get_best_thread() const {
|
||||
|
||||
Thread* bestThread = threads.front();
|
||||
std::unordered_map<Move, int64_t, Move::MoveHash> votes;
|
||||
|
||||
Thread* bestThread = threads.front();
|
||||
Value minScore = VALUE_NONE;
|
||||
|
||||
// Find the minimum score of all threads
|
||||
|
@ -219,40 +220,56 @@ Thread* ThreadPool::get_best_thread() const {
|
|||
minScore = std::min(minScore, th->worker->rootMoves[0].score);
|
||||
|
||||
// Vote according to score and depth, and select the best thread
|
||||
auto thread_value = [minScore](Thread* th) {
|
||||
auto thread_voting_value = [minScore](Thread* th) {
|
||||
return (th->worker->rootMoves[0].score - minScore + 14) * int(th->worker->completedDepth);
|
||||
};
|
||||
|
||||
for (Thread* th : threads)
|
||||
votes[th->worker->rootMoves[0].pv[0]] += thread_value(th);
|
||||
votes[th->worker->rootMoves[0].pv[0]] += thread_voting_value(th);
|
||||
|
||||
for (Thread* th : threads)
|
||||
if (bestThread->worker->rootMoves[0].score >= VALUE_TB_WIN_IN_MAX_PLY)
|
||||
{
|
||||
const auto bestThreadScore = bestThread->worker->rootMoves[0].score;
|
||||
const auto newThreadScore = th->worker->rootMoves[0].score;
|
||||
|
||||
const auto bestThreadPV = bestThread->worker->rootMoves[0].pv;
|
||||
const auto newThreadPV = th->worker->rootMoves[0].pv;
|
||||
|
||||
const auto bestThreadMoveVote = votes[bestThreadPV[0]];
|
||||
const auto newThreadMoveVote = votes[newThreadPV[0]];
|
||||
|
||||
|
||||
const bool bestThreadInProvenWin = bestThreadScore >= VALUE_TB_WIN_IN_MAX_PLY;
|
||||
const bool newThreadInProvenWin = newThreadScore >= VALUE_TB_WIN_IN_MAX_PLY;
|
||||
|
||||
const bool bestThreadInProvenLoss =
|
||||
bestThreadScore != -VALUE_INFINITE && bestThreadScore <= VALUE_TB_LOSS_IN_MAX_PLY;
|
||||
const bool newThreadInProvenLoss =
|
||||
newThreadScore != -VALUE_INFINITE && newThreadScore <= VALUE_TB_LOSS_IN_MAX_PLY;
|
||||
|
||||
// Note that we make sure not to pick a thread with truncated-PV for better viewer experience.
|
||||
const bool betterVotingValue =
|
||||
thread_voting_value(th) * int(newThreadPV.size() > 2)
|
||||
> thread_voting_value(bestThread) * int(bestThreadPV.size() > 2);
|
||||
|
||||
if (bestThreadInProvenWin)
|
||||
{
|
||||
// Make sure we pick the shortest mate / TB conversion
|
||||
if (th->worker->rootMoves[0].score > bestThread->worker->rootMoves[0].score)
|
||||
if (newThreadScore > bestThreadScore)
|
||||
bestThread = th;
|
||||
}
|
||||
else if (bestThread->worker->rootMoves[0].score != -VALUE_INFINITE
|
||||
&& bestThread->worker->rootMoves[0].score <= VALUE_TB_LOSS_IN_MAX_PLY)
|
||||
else if (bestThreadInProvenLoss)
|
||||
{
|
||||
// Make sure we pick the shortest mated / TB conversion
|
||||
if (th->worker->rootMoves[0].score != -VALUE_INFINITE
|
||||
&& th->worker->rootMoves[0].score < bestThread->worker->rootMoves[0].score)
|
||||
if (newThreadInProvenLoss && newThreadScore < bestThreadScore)
|
||||
bestThread = th;
|
||||
}
|
||||
else if (th->worker->rootMoves[0].score >= VALUE_TB_WIN_IN_MAX_PLY
|
||||
|| (th->worker->rootMoves[0].score != -VALUE_INFINITE
|
||||
&& th->worker->rootMoves[0].score <= VALUE_TB_LOSS_IN_MAX_PLY)
|
||||
|| (th->worker->rootMoves[0].score > VALUE_TB_LOSS_IN_MAX_PLY
|
||||
&& (votes[th->worker->rootMoves[0].pv[0]]
|
||||
> votes[bestThread->worker->rootMoves[0].pv[0]]
|
||||
|| (votes[th->worker->rootMoves[0].pv[0]]
|
||||
== votes[bestThread->worker->rootMoves[0].pv[0]]
|
||||
&& thread_value(th) * int(th->worker->rootMoves[0].pv.size() > 2)
|
||||
> thread_value(bestThread)
|
||||
* int(bestThread->worker->rootMoves[0].pv.size() > 2)))))
|
||||
else if (newThreadInProvenWin || newThreadInProvenLoss
|
||||
|| (newThreadScore > VALUE_TB_LOSS_IN_MAX_PLY
|
||||
&& (newThreadMoveVote > bestThreadMoveVote
|
||||
|| (newThreadMoveVote == bestThreadMoveVote && betterVotingValue))))
|
||||
bestThread = th;
|
||||
}
|
||||
|
||||
return bestThread;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue