]> git.sesse.net Git - stockfish/commitdiff
Introduce Optimism
authorStéphane Nicolet <cassio@free.fr>
Fri, 19 Nov 2021 19:04:35 +0000 (20:04 +0100)
committerStéphane Nicolet <cassio@free.fr>
Sun, 21 Nov 2021 20:18:08 +0000 (21:18 +0100)
Current master implements a scaling of the raw NNUE output value with a formula
equivalent to 'eval = alpha * NNUE_output', where the scale factor alpha varies
between 1.8 (for early middle game) and 0.9 (for pure endgames). This feature
allows Stockfish to keep material on the board when she thinks she has the advantage,
and to seek exchanges and simplifications when she thinks she has to defend.

This patch slightly offsets the turning point between these two strategies, by adding
to Stockfish's evaluation a small "optimism" value before actually doing the scaling.
The effect is that SF will play a little bit more risky, trying to keep the tension a
little bit longer when she is defending, and keeping even more material on the board
when she has an advantage.

We note that this patch is similar in spirit to the old "Contempt" idea we used to have
in classical Stockfish, but this implementation differs in two key points:

  a) it has been tested as an Elo-gainer against master;

  b) the values output by the search are not changed on average by the implementation
     (in other words, the optimism value changes the tension/exchange strategy, but a
     displayed value of 1.0 pawn has the same signification before and after the patch).

See the old comment https://github.com/official-stockfish/Stockfish/pull/1361#issuecomment-359165141
for some images illustrating the ideas.

-------

finished yellow at STC:
LLR: -2.94 (-2.94,2.94) <0.00,2.50>
Total: 165048 W: 41705 L: 41611 D: 81732
Ptnml(0-2): 565, 18959, 43245, 19327, 428
https://tests.stockfishchess.org/tests/view/61942a3dcd645dc8291c876b

passed LTC:
LLR: 2.95 (-2.94,2.94) <0.50,3.00>
Total: 121656 W: 30762 L: 30287 D: 60607
Ptnml(0-2): 87, 12558, 35032, 13095, 56
https://tests.stockfishchess.org/tests/view/61962c58cd645dc8291c8877

-------

How to continue from there?

a) the shape (slope and amplitude) of the sigmoid used to compute the optimism value
   could be tweaked to try to gain more Elo, so the parameters of the sigmoid function
   in line 391 of search.cpp could be tuned with SPSA. Manual tweaking is also possible
   using this Desmos page: https://www.desmos.com/calculator/jhh83sqq92

b) in a similar vein, with two recents patches affecting the scaling of the NNUE
   evaluation in evaluate.cpp, now could be a good time to try a round of SPSA tuning
   of the NNUE network;

c) this patch will tend to keep tension in middlegame a little bit longer, so any
   patch improving the defensive aspect of play via search extensions in risky,
   tactical positions would be welcome.

-------

closes https://github.com/official-stockfish/Stockfish/pull/3797

Bench: 6184852

src/evaluate.cpp
src/misc.h
src/search.cpp
src/thread.h

index 9fdadbb74f3e40b9e898a01067cdf80c4c396c82..c1d3d159b6fa0de652da30c638c2823bf2f99cdf 100644 (file)
@@ -1091,11 +1091,15 @@ Value Eval::evaluate(const Position& pos) {
       v = Evaluation<NO_TRACE>(pos).value();          // classical
   else
   {
-      int scale =   898
-                  + 24 * pos.count<PAWN>()
-                  + 33 * pos.non_pawn_material() / 1024;
+       int scale =   898
+                   + 24 * pos.count<PAWN>()
+                   + 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);
@@ -1127,8 +1131,11 @@ std::string Eval::trace(Position& pos) {
 
   std::memset(scores, 0, sizeof(scores));
 
-  pos.this_thread()->trend = SCORE_ZERO; // Reset any dynamic contempt
-  pos.this_thread()->bestValue = VALUE_ZERO; // Reset bestValue for lazyEval
+  // Reset any global variable used in eval
+  pos.this_thread()->trend           = SCORE_ZERO;
+  pos.this_thread()->bestValue       = VALUE_ZERO;
+  pos.this_thread()->optimism[WHITE] = VALUE_ZERO;
+  pos.this_thread()->optimism[BLACK] = VALUE_ZERO;
 
   v = Evaluation<TRACE>(pos).value();
 
index 718e5558178fafbca9edba04fbb64d0fb03e4c9a..c17441306c1a5a621579233b25441342712e68e0 100644 (file)
@@ -138,6 +138,33 @@ private:
   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 <https://www.desmos.com/calculator/jhh83sqq92> 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).
index fabb0ff07ac7398404f33bcd432cfa47a24754d8..1dfadd21ab6b0009fc2817754036d130e7a1c00e 100644 (file)
@@ -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, 14464, 256);
+              optimism[ us] = Value(opt);
+              optimism[~us] = -optimism[us];
           }
 
           // Start with a small aspiration window and, in the case of a fail
index 387937390c18b1381fccc2e18755b69f3f73c2c9..cd206faab987c71ee5bcc41406b2e42d75dab3eb 100644 (file)
@@ -68,6 +68,7 @@ public:
   int selDepth, nmpMinPly;
   Color nmpColor;
   ExplosionState state;
+  Value optimism[COLOR_NB];
 
   Position rootPos;
   StateInfo rootState;