From e407a4f269ba4389f31e9bb71fd8b944e3056ced Mon Sep 17 00:00:00 2001 From: Carlos Esparza Date: Fri, 28 Feb 2025 17:54:46 -0800 Subject: [PATCH] Simplify risk_tolerance + avoid overflow passed simplification STC: LLR: 2.93 (-2.94,2.94) <-1.75,0.25> Total: 73984 W: 19058 L: 18879 D: 36047 Ptnml(0-2): 232, 8735, 18866, 8940, 219 https://tests.stockfishchess.org/tests/view/67c269a38200cf1034c9baf9 passed simplification LTC: LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 39288 W: 10033 L: 9833 D: 19422 Ptnml(0-2): 14, 4168, 11086, 4356, 20 https://tests.stockfishchess.org/tests/view/67c34f8c8200cf1034c9bda1 closes https://github.com/official-stockfish/Stockfish/pull/5919 Bench: 2050046 --- src/search.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index bbd43ed69..bacd63c95 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -99,28 +100,28 @@ int correction_value(const Worker& w, const Position& pos, const Stack* const ss int risk_tolerance(const Position& pos, Value v) { // Returns (some constant of) second derivative of sigmoid. static constexpr auto sigmoid_d2 = [](int x, int y) { - return -355752 * x / (x * x + 3 * y * y); + return 644800 * x / ((x * x + 3 * y * y) * y); }; - int material = (67 * pos.count() + 182 * pos.count() + 182 * pos.count() - + 337 * pos.count() + 553 * pos.count()) - / 64; - - int m = std::clamp(material, 17, 78); + int m = (67 * pos.count() + 182 * pos.count() + 182 * pos.count() + + 337 * pos.count() + 553 * pos.count()) + / 64; // a and b are the crude approximation of the wdl model. // The win rate is: 1/(1+exp((a-v)/b)) // The loss rate is 1/(1+exp((v+a)/b)) - int a = ((-m * 3037 / 256 + 2270) * m / 256 - 637) * m / 256 + 413; - int b = ((m * 7936 / 256 - 2255) * m / 256 + 319) * m / 256 + 83; + int a = 356; + int b = ((65 * m - 3172) * m + 240578) / 2048; + // guard against overflow + assert(abs(v) + a <= std::numeric_limits::max() / 644800); // The risk utility is therefore d/dv^2 (1/(1+exp(-(v-a)/b)) -1/(1+exp(-(-v-a)/b))) // -115200x/(x^2+3) = -345600(ab) / (a^2+3b^2) (multiplied by some constant) (second degree pade approximant) int winning_risk = sigmoid_d2(v - a, b); - int losing_risk = -sigmoid_d2(-v - a, b); + int losing_risk = sigmoid_d2(v + a, b); - return (winning_risk + losing_risk) * 58 / b; + return -(winning_risk + losing_risk) * 32; } // Add correctionHistory value to raw staticEval and guarantee evaluation @@ -1192,7 +1193,7 @@ moves_loop: // When in check, search starts here r -= std::abs(correctionValue) / 29696; - if (PvNode && !is_decisive(bestValue)) + if (PvNode && std::abs(bestValue) <= 2000) r -= risk_tolerance(pos, bestValue); // Increase reduction for cut nodes