mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 05:07:46 +00:00
a5a89b27c8
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
141 lines
3.8 KiB
C++
141 lines
3.8 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef THREAD_H_INCLUDED
|
|
#define THREAD_H_INCLUDED
|
|
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "material.h"
|
|
#include "movepick.h"
|
|
#include "pawns.h"
|
|
#include "position.h"
|
|
#include "search.h"
|
|
#include "thread_win32_osx.h"
|
|
|
|
namespace Stockfish {
|
|
|
|
/// Thread class keeps together all the thread-related stuff. We use
|
|
/// per-thread pawn and material hash tables so that once we get a
|
|
/// pointer to an entry its life time is unlimited and we don't have
|
|
/// to care about someone changing the entry under our feet.
|
|
|
|
class Thread {
|
|
|
|
std::mutex mutex;
|
|
std::condition_variable cv;
|
|
size_t idx;
|
|
bool exit = false, searching = true; // Set before starting std::thread
|
|
NativeThread stdThread;
|
|
|
|
public:
|
|
explicit Thread(size_t);
|
|
virtual ~Thread();
|
|
virtual void search();
|
|
void clear();
|
|
void idle_loop();
|
|
void start_searching();
|
|
void wait_for_search_finished();
|
|
size_t id() const { return idx; }
|
|
|
|
Pawns::Table pawnsTable;
|
|
Material::Table materialTable;
|
|
size_t pvIdx, pvLast;
|
|
RunningAverage doubleExtensionAverage[COLOR_NB];
|
|
uint64_t nodesLastExplosive;
|
|
uint64_t nodesLastNormal;
|
|
std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
|
|
Value bestValue;
|
|
int selDepth, nmpMinPly;
|
|
Color nmpColor;
|
|
ExplosionState state;
|
|
Value optimism[COLOR_NB];
|
|
|
|
Position rootPos;
|
|
StateInfo rootState;
|
|
Search::RootMoves rootMoves;
|
|
Depth rootDepth, completedDepth;
|
|
Value rootDelta;
|
|
CounterMoveHistory counterMoves;
|
|
ButterflyHistory mainHistory;
|
|
LowPlyHistory lowPlyHistory;
|
|
CapturePieceToHistory captureHistory;
|
|
ContinuationHistory continuationHistory[2][2];
|
|
Score trend;
|
|
};
|
|
|
|
|
|
/// MainThread is a derived class specific for main thread
|
|
|
|
struct MainThread : public Thread {
|
|
|
|
using Thread::Thread;
|
|
|
|
void search() override;
|
|
void check_time();
|
|
|
|
double previousTimeReduction;
|
|
Value bestPreviousScore;
|
|
Value iterValue[4];
|
|
int callsCnt;
|
|
bool stopOnPonderhit;
|
|
std::atomic_bool ponder;
|
|
};
|
|
|
|
|
|
/// ThreadPool struct handles all the threads-related stuff like init, starting,
|
|
/// parking and, most importantly, launching a thread. All the access to threads
|
|
/// is done through this class.
|
|
|
|
struct ThreadPool : public std::vector<Thread*> {
|
|
|
|
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
|
|
void clear();
|
|
void set(size_t);
|
|
|
|
MainThread* main() const { return static_cast<MainThread*>(front()); }
|
|
uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
|
|
uint64_t tb_hits() const { return accumulate(&Thread::tbHits); }
|
|
Thread* get_best_thread() const;
|
|
void start_searching();
|
|
void wait_for_search_finished() const;
|
|
|
|
std::atomic_bool stop, increaseDepth;
|
|
|
|
private:
|
|
StateListPtr setupStates;
|
|
|
|
uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const {
|
|
|
|
uint64_t sum = 0;
|
|
for (Thread* th : *this)
|
|
sum += (th->*member).load(std::memory_order_relaxed);
|
|
return sum;
|
|
}
|
|
};
|
|
|
|
extern ThreadPool Threads;
|
|
|
|
} // namespace Stockfish
|
|
|
|
#endif // #ifndef THREAD_H_INCLUDED
|