io-chess
UCI chess engine
Loading...
Searching...
No Matches
UciProtocol Class Reference

Main engine controller implementing the UCI protocol. More...

#include <UCI.h>

Collaboration diagram for UciProtocol:

Public Member Functions

 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.
 ~UciProtocol ()
void loop ()
 The main blocking loop that reads commands from standard input.
void resizeHash (size_t mb)
 Resizes the transposition table memory block.
void updateThreads (int numThreads)
 Restarts the search thread pool with the specified number of threads.
void reinitEngine ()
 Performs a full re-initialization of the engine and evaluator state.
void updateEvalContexts ()
 Refreshes the thread-local evaluation contexts.
void initTablebase ()
 Reloads the Syzygy tablebases from the configured path.
void loadBooks ()
 Reloads the Polyglot opening books from the configured paths.
void setTTDisabled (bool disable)
 Disables or enables the transposition table globally.
void setPendingTTLoadFile (const std::string &file)
 Queues a saved TT snapshot file to be loaded before the next search.

Private Member Functions

void handleUci ()
 Handles the 'uci' command (sends id and options).
void handleIsReady ()
 Handles the 'isready' command (initializes lazy resources).
void handleSetOption (std::istringstream &is)
 Handles the 'setoption' command to configure engine parameters.
void handleUciNewGame ()
 Handles the 'ucinewgame' command (clears TT and search state).
void handlePosition (std::istringstream &is)
 Handles the 'position' command to set up the board state.
void handleGo (std::istringstream &is)
 Handles the 'go' command to start a search with specified parameters.
void handlePonderHit ()
 Handles the 'ponderhit' command (converts a ponder search to a normal search).
void handleStop ()
 Handles the 'stop' command to halt the current search immediately.
void handleQuit ()
 Handles the 'quit' command to exit the engine process.
void handleEval ()
 Handles the custom 'eval' command to print the static evaluation of the current position.
void initializeEngine ()
 Performs heavy lazy initialization of networks and threads.
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.
void sendBestMove (Move move, Move ponderMove=Move(Move::NO_MOVE))
 Sends the 'bestmove' UCI string to conclude a search.
void saveTTSnapshotForMove (const std::string &moveUci)
 Dumps a snapshot of the Transposition Table to disk (for debugging).

Static Private Member Functions

static std::string formatScore (int score)
 Formats an internal centipawn/mate score into a UCI-compliant string.
static std::string sanitizeForFilename (const std::string &value)
 Sanitizes a UCI string to be safe for filesystem filenames.

Private Attributes

std::vector< std::unique_ptr< Evaluator > > evaluators_
std::vector< std::unique_ptr< IEvaluator > > evalCtxs_
TranspositionTable tt_
std::unique_ptr< PolyglotBookbook_
std::vector< std::thread > threadPool_
std::shared_ptr< SearchSharedDatasearchData_
std::unique_ptr< ISearchsearcher_
std::thread searchThread_
std::atomic< bool > searchRunning_ {false}
std::atomic< bool > searchMayRunForever_ {false}
std::atomic< bool > pondering_ {false}
std::atomic< bool > ponderResultReady_ {false}
Move ponderResultMove_ {}
Move ponderMove_ {}
 Expected opponent reply (2nd move in PV).
SearchParams ponderParams_ {}
 Time params from go ponder for ponderhit use.
std::mutex ponderMutex_
Board currentBoard_
UciOptions options_
std::unique_ptr< PolyglotBookbook2_
int consecutiveBookMisses_ = 0
std::string pendingTTLoadFile_
uint64_t ttSnapshotCounter_ = 0
uint64_t searchBaseFullRebuilds_ = 0
uint64_t searchBaseTbHits_ = 0
uint64_t searchBaseTtHits_ = 0

Static Private Attributes

static constexpr const char * ENGINE_NAME = "io-chess-engine"
static constexpr const char * ENGINE_AUTHOR = "Alessandro Gobbetti!"
static constexpr int MAX_BOOK_MISSES = 3

Detailed Description

Main engine controller implementing the UCI protocol.

Orchestrates parsing input from standard input, managing engine options, starting/stopping search threads, and reporting results to standard output.

Constructor & Destructor Documentation

◆ UciProtocol()

UciProtocol::UciProtocol ( const std::string & modelPath,
bool useSimpleEval = false,
const std::string & tbPath = "",
const std::string & bookPath = "",
const std::string & bookPath2 = "",
int evalThreads = 0 )
explicit

Constructs the UCI Protocol handler.

Here is the call graph for this function:

◆ ~UciProtocol()

UciProtocol::~UciProtocol ( )

Member Function Documentation

◆ formatScore()

std::string UciProtocol::formatScore ( int score)
staticprivate

Formats an internal centipawn/mate score into a UCI-compliant string.

Here is the caller graph for this function:

◆ handleEval()

void UciProtocol::handleEval ( )
private

Handles the custom 'eval' command to print the static evaluation of the current position.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleGo()

void UciProtocol::handleGo ( std::istringstream & is)
private

Handles the 'go' command to start a search with specified parameters.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleIsReady()

void UciProtocol::handleIsReady ( )
private

Handles the 'isready' command (initializes lazy resources).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handlePonderHit()

void UciProtocol::handlePonderHit ( )
private

Handles the 'ponderhit' command (converts a ponder search to a normal search).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handlePosition()

void UciProtocol::handlePosition ( std::istringstream & is)
private

Handles the 'position' command to set up the board state.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleQuit()

void UciProtocol::handleQuit ( )
private

Handles the 'quit' command to exit the engine process.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleSetOption()

void UciProtocol::handleSetOption ( std::istringstream & is)
private

Handles the 'setoption' command to configure engine parameters.

Here is the caller graph for this function:

◆ handleStop()

void UciProtocol::handleStop ( )
private

Handles the 'stop' command to halt the current search immediately.

Here is the caller graph for this function:

◆ handleUci()

void UciProtocol::handleUci ( )
private

Handles the 'uci' command (sends id and options).

Here is the caller graph for this function:

◆ handleUciNewGame()

void UciProtocol::handleUciNewGame ( )
private

Handles the 'ucinewgame' command (clears TT and search state).

Here is the caller graph for this function:

◆ initializeEngine()

void UciProtocol::initializeEngine ( )
private

Performs heavy lazy initialization of networks and threads.

Here is the caller graph for this function:

◆ initTablebase()

void UciProtocol::initTablebase ( )

Reloads the Syzygy tablebases from the configured path.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadBooks()

void UciProtocol::loadBooks ( )

Reloads the Polyglot opening books from the configured paths.

Here is the caller graph for this function:

◆ loop()

void UciProtocol::loop ( )

The main blocking loop that reads commands from standard input.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reinitEngine()

void UciProtocol::reinitEngine ( )

Performs a full re-initialization of the engine and evaluator state.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resizeHash()

void UciProtocol::resizeHash ( size_t mb)

Resizes the transposition table memory block.

Here is the caller graph for this function:

◆ sanitizeForFilename()

std::string UciProtocol::sanitizeForFilename ( const std::string & value)
staticprivate

Sanitizes a UCI string to be safe for filesystem filenames.

Here is the caller graph for this function:

◆ saveTTSnapshotForMove()

void UciProtocol::saveTTSnapshotForMove ( const std::string & moveUci)
private

Dumps a snapshot of the Transposition Table to disk (for debugging).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendBestMove()

void UciProtocol::sendBestMove ( Move move,
Move ponderMove = Move(Move::NO_MOVE) )
private

Sends the 'bestmove' UCI string to conclude a search.

Here is the caller graph for this function:

◆ sendInfo()

void UciProtocol::sendInfo ( int depth,
int score,
int nodes,
int nps,
const std::vector< Move > & pv,
const Board & board )
private

Sends search progress information via 'info' UCI strings.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setPendingTTLoadFile()

void UciProtocol::setPendingTTLoadFile ( const std::string & file)
inline

Queues a saved TT snapshot file to be loaded before the next search.

Here is the caller graph for this function:

◆ setTTDisabled()

void UciProtocol::setTTDisabled ( bool disable)

Disables or enables the transposition table globally.

Here is the caller graph for this function:

◆ updateEvalContexts()

void UciProtocol::updateEvalContexts ( )

Refreshes the thread-local evaluation contexts.

Here is the caller graph for this function:

◆ updateThreads()

void UciProtocol::updateThreads ( int numThreads)

Restarts the search thread pool with the specified number of threads.

Here is the caller graph for this function:

Member Data Documentation

◆ book2_

std::unique_ptr<PolyglotBook> UciProtocol::book2_
private

◆ book_

std::unique_ptr<PolyglotBook> UciProtocol::book_
private

◆ consecutiveBookMisses_

int UciProtocol::consecutiveBookMisses_ = 0
private

◆ currentBoard_

Board UciProtocol::currentBoard_
private

◆ ENGINE_AUTHOR

const char* UciProtocol::ENGINE_AUTHOR = "Alessandro Gobbetti!"
staticconstexprprivate

◆ ENGINE_NAME

const char* UciProtocol::ENGINE_NAME = "io-chess-engine"
staticconstexprprivate

◆ evalCtxs_

std::vector<std::unique_ptr<IEvaluator> > UciProtocol::evalCtxs_
private

◆ evaluators_

std::vector<std::unique_ptr<Evaluator> > UciProtocol::evaluators_
private

◆ MAX_BOOK_MISSES

int UciProtocol::MAX_BOOK_MISSES = 3
staticconstexprprivate

◆ options_

UciOptions UciProtocol::options_
private

◆ pendingTTLoadFile_

std::string UciProtocol::pendingTTLoadFile_
private

◆ pondering_

std::atomic<bool> UciProtocol::pondering_ {false}
private

◆ ponderMove_

Move UciProtocol::ponderMove_ {}
private

Expected opponent reply (2nd move in PV).

◆ ponderMutex_

std::mutex UciProtocol::ponderMutex_
private

◆ ponderParams_

SearchParams UciProtocol::ponderParams_ {}
private

Time params from go ponder for ponderhit use.

◆ ponderResultMove_

Move UciProtocol::ponderResultMove_ {}
private

◆ ponderResultReady_

std::atomic<bool> UciProtocol::ponderResultReady_ {false}
private

◆ searchBaseFullRebuilds_

uint64_t UciProtocol::searchBaseFullRebuilds_ = 0
private

◆ searchBaseTbHits_

uint64_t UciProtocol::searchBaseTbHits_ = 0
private

◆ searchBaseTtHits_

uint64_t UciProtocol::searchBaseTtHits_ = 0
private

◆ searchData_

std::shared_ptr<SearchSharedData> UciProtocol::searchData_
private

◆ searcher_

std::unique_ptr<ISearch> UciProtocol::searcher_
private

◆ searchMayRunForever_

std::atomic<bool> UciProtocol::searchMayRunForever_ {false}
private

◆ searchRunning_

std::atomic<bool> UciProtocol::searchRunning_ {false}
private

◆ searchThread_

std::thread UciProtocol::searchThread_
private

◆ threadPool_

std::vector<std::thread> UciProtocol::threadPool_
private

◆ tt_

TranspositionTable UciProtocol::tt_
private

◆ ttSnapshotCounter_

uint64_t UciProtocol::ttSnapshotCounter_ = 0
private

The documentation for this class was generated from the following files: