io-chess
UCI chess engine
Loading...
Searching...
No Matches
UCI.h
Go to the documentation of this file.
1#pragma once
2
10
11#include "UciOptions.h"
12
13#include "../Types.h"
15
16#include "../eval/Evaluator.h"
17#include "../search/ISearch.h"
18#include "../search/MCTS.h"
19#include "../search/Negamax.h"
21#include "../search/TT.h"
23#include <atomic>
24#include <memory>
25#include <mutex>
26#include <sstream>
27#include <string>
28#include <thread>
29
30// Engine constant for versioning
31static constexpr const char *ENGINE_VERSION = "1.0";
32
41private:
42 // Engine name and author
43 static constexpr const char *ENGINE_NAME = "io-chess-engine";
44 static constexpr const char *ENGINE_AUTHOR = "Alessandro Gobbetti!";
45
46 // Persistent Resources (loaded once)
47 std::vector<std::unique_ptr<Evaluator>> evaluators_;
48 std::vector<std::unique_ptr<IEvaluator>> evalCtxs_;
50 std::unique_ptr<PolyglotBook> book_;
51
52 // Threading and Search State
53 std::vector<std::thread> threadPool_;
54 std::shared_ptr<SearchSharedData> searchData_;
55
56 // The active search algorithm
57 std::unique_ptr<ISearch> searcher_;
58 std::thread searchThread_;
59 std::atomic<bool> searchRunning_{false};
60 std::atomic<bool> searchMayRunForever_{false};
61 std::atomic<bool> pondering_{false};
62 std::atomic<bool> ponderResultReady_{false};
66 std::mutex ponderMutex_;
67
68 // Current position
70
71 // Options
73 std::unique_ptr<PolyglotBook> book2_;
75 static constexpr int MAX_BOOK_MISSES = 3;
76
77 std::string pendingTTLoadFile_;
78 uint64_t ttSnapshotCounter_ = 0;
79
80 // Per-search metric baselines (used to report deltas in info lines)
82 uint64_t searchBaseTbHits_ = 0;
83 uint64_t searchBaseTtHits_ = 0;
84
85public:
89 explicit UciProtocol(const std::string &modelPath, bool useSimpleEval = false,
90 const std::string &tbPath = "",
91 const std::string &bookPath = "",
92 const std::string &bookPath2 = "", int evalThreads = 0);
94
98 void loop();
99
100 // --- Callbacks for options ---
101
103 void resizeHash(size_t mb);
104
106 void updateThreads(int numThreads);
107
109 void reinitEngine();
110
112 void updateEvalContexts();
113
115 void initTablebase();
116
118 void loadBooks();
119
121 void setTTDisabled(bool disable);
122
124 void setPendingTTLoadFile(const std::string& file) { pendingTTLoadFile_ = file; }
125
126private:
127 // --- UCI command handlers ---
128
130 void handleUci();
131
133 void handleIsReady();
134
136 void handleSetOption(std::istringstream &is);
137
139 void handleUciNewGame();
140
142 void handlePosition(std::istringstream &is);
143
145 void handleGo(std::istringstream &is);
146
148 void handlePonderHit();
149
151 void handleStop();
152
154 void handleQuit();
155
157 void handleEval();
158
159 // --- Helper functions ---
160
162 void initializeEngine();
163
165 static std::string formatScore(int score);
166
168 void sendInfo(int depth, int score, int nodes, int nps,
169 const std::vector<Move> &pv, const Board &board);
170
172 void sendBestMove(Move move, Move ponderMove = Move(Move::NO_MOVE));
173
175 void saveTTSnapshotForMove(const std::string &moveUci);
176
178 static std::string sanitizeForFilename(const std::string &value);
179};
Runtime evaluator manager.
Search interface and shared data structures.
Monte Carlo Tree Search implementation.
Alpha-Beta search implementation with enhancements.
Transposition Table implementation using a 3+1 Cluster Architecture.
Syzygy tablebase probing via Fathom.
Sophisticated time allocation and management for the chess engine.
Common type definitions and constants for the chess engine.
chess::Move Move
Alias for chess::Move.
Definition Types.h:15
chess::Board Board
Alias for chess::Board.
Definition Types.h:14
static constexpr const char * ENGINE_VERSION
Definition UCI.h:31
Management of UCI engine options.
The main Transposition Table.
Definition TT.h:145
uint64_t ttSnapshotCounter_
Definition UCI.h:78
void handleIsReady()
Handles the 'isready' command (initializes lazy resources).
Definition UCI.cpp:198
uint64_t searchBaseTtHits_
Definition UCI.h:83
void handlePosition(std::istringstream &is)
Handles the 'position' command to set up the board state.
Definition UCI.cpp:232
SearchParams ponderParams_
Time params from go ponder for ponderhit use.
Definition UCI.h:65
void handleUci()
Handles the 'uci' command (sends id and options).
Definition UCI.cpp:191
void saveTTSnapshotForMove(const std::string &moveUci)
Dumps a snapshot of the Transposition Table to disk (for debugging).
Definition UCI.cpp:923
void initializeEngine()
Performs heavy lazy initialization of networks and threads.
Definition UCI.cpp:743
std::string pendingTTLoadFile_
Definition UCI.h:77
void handleEval()
Handles the custom 'eval' command to print the static evaluation of the current position.
Definition UCI.cpp:972
std::vector< std::thread > threadPool_
Definition UCI.h:53
void sendBestMove(Move move, Move ponderMove=Move(Move::NO_MOVE))
Sends the 'bestmove' UCI string to conclude a search.
Definition UCI.cpp:909
~UciProtocol()
Definition UCI.cpp:72
uint64_t searchBaseTbHits_
Definition UCI.h:82
std::atomic< bool > ponderResultReady_
Definition UCI.h:62
void handleSetOption(std::istringstream &is)
Handles the 'setoption' command to configure engine parameters.
Definition UCI.cpp:209
uint64_t searchBaseFullRebuilds_
Definition UCI.h:81
void resizeHash(size_t mb)
Resizes the transposition table memory block.
Definition UCI.cpp:991
std::atomic< bool > pondering_
Definition UCI.h:61
void updateEvalContexts()
Refreshes the thread-local evaluation contexts.
Definition UCI.cpp:1007
void setPendingTTLoadFile(const std::string &file)
Queues a saved TT snapshot file to be loaded before the next search.
Definition UCI.h:124
std::unique_ptr< ISearch > searcher_
Definition UCI.h:57
void updateThreads(int numThreads)
Restarts the search thread pool with the specified number of threads.
Definition UCI.cpp:995
std::vector< std::unique_ptr< IEvaluator > > evalCtxs_
Definition UCI.h:48
int consecutiveBookMisses_
Definition UCI.h:74
void sendInfo(int depth, int score, int nodes, int nps, const std::vector< Move > &pv, const Board &board)
Sends search progress information via 'info' UCI strings.
Definition UCI.cpp:855
std::atomic< bool > searchMayRunForever_
Definition UCI.h:60
UciProtocol(const std::string &modelPath, bool useSimpleEval=false, const std::string &tbPath="", const std::string &bookPath="", const std::string &bookPath2="", int evalThreads=0)
Constructs the UCI Protocol handler.
Definition UCI.cpp:43
std::shared_ptr< SearchSharedData > searchData_
Definition UCI.h:54
static std::string sanitizeForFilename(const std::string &value)
Sanitizes a UCI string to be safe for filesystem filenames.
Definition UCI.cpp:958
static constexpr const char * ENGINE_NAME
Definition UCI.h:43
void loop()
The main blocking loop that reads commands from standard input.
Definition UCI.cpp:80
void handleStop()
Handles the 'stop' command to halt the current search immediately.
Definition UCI.cpp:728
std::vector< std::unique_ptr< Evaluator > > evaluators_
Definition UCI.h:47
std::atomic< bool > searchRunning_
Definition UCI.h:59
static constexpr const char * ENGINE_AUTHOR
Definition UCI.h:44
std::unique_ptr< PolyglotBook > book2_
Definition UCI.h:73
void reinitEngine()
Performs a full re-initialization of the engine and evaluator state.
Definition UCI.cpp:1001
void handlePonderHit()
Handles the 'ponderhit' command (converts a ponder search to a normal search).
Definition UCI.cpp:641
Board currentBoard_
Definition UCI.h:69
void loadBooks()
Reloads the Polyglot opening books from the configured paths.
Definition UCI.cpp:1022
void handleQuit()
Handles the 'quit' command to exit the engine process.
Definition UCI.cpp:741
std::thread searchThread_
Definition UCI.h:58
static std::string formatScore(int score)
Formats an internal centipawn/mate score into a UCI-compliant string.
Definition UCI.cpp:830
void setTTDisabled(bool disable)
Disables or enables the transposition table globally.
Definition UCI.cpp:1038
void initTablebase()
Reloads the Syzygy tablebases from the configured path.
Definition UCI.cpp:1016
void handleGo(std::istringstream &is)
Handles the 'go' command to start a search with specified parameters.
Definition UCI.cpp:270
std::mutex ponderMutex_
Definition UCI.h:66
static constexpr int MAX_BOOK_MISSES
Definition UCI.h:75
UciOptions options_
Definition UCI.h:72
void handleUciNewGame()
Handles the 'ucinewgame' command (clears TT and search state).
Definition UCI.cpp:226
std::unique_ptr< PolyglotBook > book_
Definition UCI.h:50
Move ponderResultMove_
Definition UCI.h:63
Move ponderMove_
Expected opponent reply (2nd move in PV).
Definition UCI.h:64
TranspositionTable tt_
Definition UCI.h:49
Polyglot opening book parser and probe utility.
Parameters defining the constraints for a search operation.
Definition ISearch.h:125
Holds all configurable engine parameters.
Definition UciOptions.h:19