Skip to content

Commit

Permalink
Merge pull request #144 from Joachim26/Main_SFNNv6_7commits0307
Browse files Browse the repository at this point in the history
Main_SFNNv6_7commits0307
  • Loading branch information
Joachim26 authored Mar 7, 2024
2 parents a02f3b4 + e516116 commit 0721fd2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
63 changes: 37 additions & 26 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ using namespace Search;

namespace {


// Futility margin
Value futility_margin(Depth d, bool noTtCutNode, bool improving) {
Value futilityMult = 117 - 44 * noTtCutNode;
return (futilityMult * d - 3 * futilityMult / 2 * improving);
Value futility_margin(Depth d, bool noTtCutNode, bool improving, bool oppWorsening) {
Value futilityMult = 117 - 44 * noTtCutNode;
Value improvingDeduction = 3 * improving * futilityMult / 2;
Value worseningDeduction = (331 + 45 * improving) * oppWorsening * futilityMult / 1024;

return futilityMult * d - improvingDeduction - worseningDeduction;
}

constexpr int futility_move_count(bool improving, Depth depth) {
Expand Down Expand Up @@ -190,7 +192,7 @@ void Search::Worker::start_searching() {
Skill skill =
Skill(options["Skill Level"], options["UCI_LimitStrength"] ? int(options["UCI_Elo"]) : 0);

if (int(options["MultiPV"]) == 1 && !limits.depth && !skill.enabled()
if (int(options["MultiPV"]) == 1 && !limits.depth && !limits.mate && !skill.enabled()
&& rootMoves[0].pv[0] != Move::none())
bestThread = threads.get_best_thread()->worker.get();

Expand Down Expand Up @@ -402,14 +404,18 @@ void Search::Worker::iterative_deepening() {
lastBestMoveDepth = rootDepth;
}

// Have we found a "mate in x"?
if (limits.mate && bestValue >= VALUE_MATE_IN_MAX_PLY
&& VALUE_MATE - bestValue <= 2 * limits.mate)
threads.stop = true;

if (!mainThread)
continue;

// Have we found a "mate in x"?
if (limits.mate && rootMoves[0].score == rootMoves[0].uciScore
&& ((rootMoves[0].score >= VALUE_MATE_IN_MAX_PLY
&& VALUE_MATE - rootMoves[0].score <= 2 * limits.mate)
|| (rootMoves[0].score != -VALUE_INFINITE
&& rootMoves[0].score <= VALUE_MATED_IN_MAX_PLY
&& VALUE_MATE + rootMoves[0].score <= 2 * limits.mate)))
threads.stop = true;

// If the skill level is enabled and time is up, pick a sub-optimal best move
if (skill.enabled() && skill.time_to_pick(rootDepth))
skill.pick_best(rootMoves, multiPV);
Expand All @@ -428,15 +434,15 @@ void Search::Worker::iterative_deepening() {
int nodesEffort = effort[bestmove.from_sq()][bestmove.to_sq()] * 100
/ std::max(size_t(1), size_t(nodes));

double fallingEval = (66 + 14 * (mainThread->bestPreviousAverageScore - bestValue)
+ 6 * (mainThread->iterValue[iterIdx] - bestValue))
/ 616.6;
fallingEval = std::clamp(fallingEval, 0.51, 1.51);
double fallingEval = (1067 + 223 * (mainThread->bestPreviousAverageScore - bestValue)
+ 97 * (mainThread->iterValue[iterIdx] - bestValue))
/ 10000.0;
fallingEval = std::clamp(fallingEval, 0.580, 1.667);

// If the bestMove is stable over several iterations, reduce time accordingly
timeReduction = lastBestMoveDepth + 8 < completedDepth ? 1.56 : 0.69;
double reduction = (1.4 + mainThread->previousTimeReduction) / (2.17 * timeReduction);
double bestMoveInstability = 1 + 1.79 * totBestMoveChanges / threads.size();
timeReduction = lastBestMoveDepth + 8 < completedDepth ? 1.495 : 0.687;
double reduction = (1.48 + mainThread->previousTimeReduction) / (2.17 * timeReduction);
double bestMoveInstability = 1 + 1.88 * totBestMoveChanges / threads.size();

double totalTime =
mainThread->tm.optimum() * fallingEval * reduction * bestMoveInstability;
Expand All @@ -445,8 +451,8 @@ void Search::Worker::iterative_deepening() {
if (rootMoves.size() == 1)
totalTime = std::min(500.0, totalTime);

if (completedDepth >= 10 && nodesEffort >= 95
&& mainThread->tm.elapsed(threads.nodes_searched()) > totalTime * 3 / 4
if (completedDepth >= 10 && nodesEffort >= 97
&& mainThread->tm.elapsed(threads.nodes_searched()) > totalTime * 0.739
&& !mainThread->ponder)
{
threads.stop = true;
Expand All @@ -463,7 +469,7 @@ void Search::Worker::iterative_deepening() {
threads.stop = true;
}
else if (!mainThread->ponder
&& mainThread->tm.elapsed(threads.nodes_searched()) > totalTime * 0.50)
&& mainThread->tm.elapsed(threads.nodes_searched()) > totalTime * 0.506)
threads.increaseDepth = false;
else
threads.increaseDepth = true;
Expand Down Expand Up @@ -538,7 +544,7 @@ Value Search::Worker::search(
Move ttMove, move, excludedMove, bestMove;
Depth extension, newDepth;
Value bestValue, value, ttValue, eval, maxValue, probCutBeta;
bool givesCheck, improving, priorCapture;
bool givesCheck, improving, priorCapture, opponentWorsening;
bool capture, moveCountPruning, ttCapture;
Piece movedPiece;
int moveCount, captureCount, quietCount;
Expand Down Expand Up @@ -619,7 +625,7 @@ Value Search::Worker::search(
update_quiet_stats(pos, ss, *this, ttMove, stat_bonus(depth));

// Extra penalty for early quiet moves of
// the previous ply (~0 Elo on STC, ~2 Elo on LTC).
// the previous ply (~1 Elo on STC, ~2 Elo on LTC)
if (prevSq != SQ_NONE && (ss - 1)->moveCount <= 2 && !priorCapture)
update_continuation_histories(ss - 1, pos.piece_on(prevSq), prevSq,
-stat_malus(depth + 1));
Expand Down Expand Up @@ -747,6 +753,8 @@ Value Search::Worker::search(
? ss->staticEval > (ss - 2)->staticEval
: (ss - 4)->staticEval != VALUE_NONE && ss->staticEval > (ss - 4)->staticEval;

opponentWorsening = ss->staticEval + (ss - 1)->staticEval > 2 && (depth != 2 || !improving);

// Step 7. Razoring (~1 Elo)
// If eval is really low check with qsearch if it can exceed alpha, if it can't,
// return a fail low.
Expand All @@ -761,7 +769,7 @@ Value Search::Worker::search(
// Step 8. Futility pruning: child node (~40 Elo)
// The depth condition is important for mate finding.
if (!ss->ttPv && depth < 11
&& eval - futility_margin(depth, cutNode && !ss->ttHit, improving)
&& eval - futility_margin(depth, cutNode && !ss->ttHit, improving, opponentWorsening)
- (ss - 1)->statScore / 314
>= beta
&& eval >= beta && eval < 30016 // smaller than TB wins
Expand Down Expand Up @@ -1043,6 +1051,9 @@ Value Search::Worker::search(
extension = 2 + (value < singularBeta - 78 && !ttCapture);
depth += depth < 16;
}
if (PvNode && !ttCapture && ss->multipleExtensions <= 5
&& value < singularBeta - 50)
extension = 2;
}

// Multi-cut pruning
Expand Down Expand Up @@ -1072,7 +1083,7 @@ Value Search::Worker::search(
extension = -1;
}

// Recapture extensions (~1 Elo)
// Recapture extensions (~0 Elo on STC, ~1 Elo on LTC)
else if (PvNode && move == ttMove && move.to_sq() == prevSq
&& thisThread->captureHistory[movedPiece][move.to_sq()]
[type_of(pos.piece_on(move.to_sq()))]
Expand Down Expand Up @@ -1110,7 +1121,7 @@ Value Search::Worker::search(
if (ttCapture)
r++;

// Decrease reduction for PvNodes (~3 Elo)
// Decrease reduction for PvNodes (~0 Elo on STC, ~2 Elo on LTC)
if (PvNode)
r--;

Expand Down Expand Up @@ -1172,7 +1183,7 @@ Value Search::Worker::search(
// Step 18. Full-depth search when LMR is skipped
else if (!PvNode || moveCount > 1)
{
// Increase reduction if ttMove is not present (~1 Elo)
// Increase reduction if ttMove is not present (~6 Elo)
if (!ttMove)
r += 2;

Expand Down
18 changes: 9 additions & 9 deletions src/timeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,22 @@ void TimeManagement::init(Search::LimitsType& limits,
timeLeft = slowMover * timeLeft / 100;

// x basetime (+ z increment)
// If there is a healthy increment, timeLeft can exceed actual available
// game time for the current move, so also cap to 20% of available game time.
// If there is a healthy increment, timeLeft can exceed the actual available
// game time for the current move, so also cap to a percentage of available game time.
if (limits.movestogo == 0)
{
// Use extra time with larger increments
double optExtra = std::clamp(1.0 + 12.5 * limits.inc[us] / limits.time[us], 1.0, 1.11);
double optExtra = limits.inc[us] < 500 ? 1.0 : 1.13;

// Calculate time constants based on current time left.
double optConstant =
std::min(0.00334 + 0.0003 * std::log10(limits.time[us] / 1000.0), 0.0049);
double maxConstant = std::max(3.4 + 3.0 * std::log10(limits.time[us] / 1000.0), 2.76);
std::min(0.00308 + 0.000319 * std::log10(limits.time[us] / 1000.0), 0.00506);
double maxConstant = std::max(3.39 + 3.01 * std::log10(limits.time[us] / 1000.0), 2.93);

optScale = std::min(0.0120 + std::pow(ply + 3.1, 0.44) * optConstant,
0.21 * limits.time[us] / double(timeLeft))
optScale = std::min(0.0122 + std::pow(ply + 2.95, 0.462) * optConstant,
0.213 * limits.time[us] / double(timeLeft))
* optExtra;
maxScale = std::min(6.9, maxConstant + ply / 12.2);
maxScale = std::min(6.64, maxConstant + ply / 12.0);
}

// x moves in y seconds (+ z increment)
Expand All @@ -122,7 +122,7 @@ void TimeManagement::init(Search::LimitsType& limits,
// Limit the maximum possible time for this move
optimumTime = TimePoint(optScale * timeLeft);
maximumTime =
TimePoint(std::min(0.84 * limits.time[us] - moveOverhead, maxScale * optimumTime)) - 10;
TimePoint(std::min(0.825 * limits.time[us] - moveOverhead, maxScale * optimumTime)) - 10;

if (options["Ponder"])
optimumTime += optimumTime / 4;
Expand Down

0 comments on commit 0721fd2

Please sign in to comment.