diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 9fdadbb74f3..c140a7e43ce 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -1091,11 +1091,15 @@ Value Eval::evaluate(const Position& pos) { v = Evaluation(pos).value(); // classical else { - int scale = 898 - + 24 * pos.count() - + 33 * pos.non_pawn_material() / 1024; + int scale = 898 + + 24 * pos.count() + + 33 * pos.non_pawn_material() / 1024; - v = NNUE::evaluate(pos, true) * scale / 1024; // NNUE + Value nnue = NNUE::evaluate(pos, true); // NNUE + Color stm = pos.side_to_move(); + Value optimism = pos.this_thread()->optimism[stm]; + + v = (nnue + optimism) * scale / 1024 - optimism; if (pos.is_chess960()) v += fix_FRC(pos); diff --git a/src/misc.h b/src/misc.h index 718e5558178..f75e7e4e74f 100644 --- a/src/misc.h +++ b/src/misc.h @@ -138,6 +138,33 @@ class ValueList { std::size_t size_ = 0; }; + +/// sigmoid(t, x0, y0, C, P, Q) implements a sigmoid-like function +/// using only integers, with the following properties: +/// +/// - sigmoid is centered in (x0, y0) +/// - sigmoid has amplitude [-P/Q , P/Q] instead of [-1 , +1] +/// - limit is (y0 - P/Q) when t tends to -infinity +/// - limit is (y0 + P/Q) when t tends to +infinity +/// - the slope can be adjusted using C > 0, smaller C giving a steeper sigmoid +/// - the slope of the sigmoid when t = x0 is P/(Q*C) +/// - sigmoid is increasing with t when P > 0 and Q > 0 +/// - to get a decreasing sigmoid, call with -t, or change sign of P +/// - mean value of the sigmoid is y0 +/// +/// Use to draw the sigmoid + +inline int64_t sigmoid(int64_t t, int64_t x0, + int64_t y0, + int64_t C, + int64_t P, + int64_t Q) +{ + assert(C > 0); + return y0 + P * (t-x0) / (Q * (std::abs(t-x0) + C)) ; +} + + /// xorshift64star Pseudo-Random Number Generator /// This class is based on original code written and dedicated /// to the public domain by Sebastiano Vigna (2014). diff --git a/src/search.cpp b/src/search.cpp index fabb0ff07ac..fa2f506bc1e 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -334,8 +334,10 @@ void Thread::search() { nodesLastExplosive = nodes; nodesLastNormal = nodes; - state = EXPLOSION_NONE; - trend = SCORE_ZERO; + state = EXPLOSION_NONE; + trend = SCORE_ZERO; + optimism[ us] = Value(25); + optimism[~us] = -optimism[us]; int searchAgainCounter = 0; @@ -381,11 +383,14 @@ void Thread::search() { alpha = std::max(prev - delta,-VALUE_INFINITE); beta = std::min(prev + delta, VALUE_INFINITE); - // Adjust trend based on root move's previousScore (dynamic contempt) - int tr = 113 * prev / (abs(prev) + 147); - + // Adjust trend and optimism based on root move's previousScore + int tr = sigmoid(prev, 0, 0, 147, 113, 1); trend = (us == WHITE ? make_score(tr, tr / 2) : -make_score(tr, tr / 2)); + + int opt = sigmoid(prev, 0, 25, 147, 113, 2); + optimism[ us] = Value(opt); + optimism[~us] = -optimism[us]; } // Start with a small aspiration window and, in the case of a fail diff --git a/src/thread.h b/src/thread.h index 387937390c1..cd206faab98 100644 --- a/src/thread.h +++ b/src/thread.h @@ -68,6 +68,7 @@ class Thread { int selDepth, nmpMinPly; Color nmpColor; ExplosionState state; + Value optimism[COLOR_NB]; Position rootPos; StateInfo rootState;