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

Monte Carlo Tree Search engine. More...

#include <MCTS.h>

Inheritance diagram for MCTS:
Collaboration diagram for MCTS:

Public Member Functions

 MCTS (IEvaluator &eval)
Move startSearch (Board &root, const SearchParams &params) override
 Starts the search process on the given root board.
void stop () override
 Asynchronously signals the search to stop immediately.
bool isSearching () const override
 Checks if the search algorithm is currently running.
void setInfoCallback (InfoCallback callback) override
 Sets the callback function for periodic information updates.
uint64_t getNodes () const override
 Retrieves the total number of nodes evaluated by the search algorithm.
void setExplorationConstant (float c)
 Sets the exploration constant (Cp) used in the UCB calculation.
Public Member Functions inherited from ISearch
virtual ~ISearch ()=default

Private Member Functions

MCTSNodeselection (MCTSNode *node, Board &board)
void expansion (MCTSNode *node, Board &board)
float simulation (Board &board)
void backpropagation (MCTSNode *node, float value)
MCTSNodeselectBestChild (MCTSNode *node) const
MCTSNodeselectUCBChild (MCTSNode *node) const

Private Attributes

IEvaluatorevalCtx_
 The evaluator used for leaf node evaluation.
std::unique_ptr< MCTSNoderoot_
 Root node of the search tree.
std::atomic< bool > stopFlag_ {false}
 Signals the search to stop.
std::atomic< bool > searching_ {false}
 True while a search is active.
std::atomic< uint64_t > nodes_ {0}
 Total nodes visited.
float explorationConstant_ = 1.41f
 Weight of exploration in UCB formula.
int maxPlayouts_ = 100000
 Hard limit on search iterations.
InfoCallback infoCallback_
 Callback for UCI info updates.

Detailed Description

Monte Carlo Tree Search engine.

Implements the ISearch interface. Explores the game tree asymmetrically by repeatedly selecting, expanding, simulating, and backpropagating.

Constructor & Destructor Documentation

◆ MCTS()

MCTS::MCTS ( IEvaluator & eval)

Member Function Documentation

◆ backpropagation()

void MCTS::backpropagation ( MCTSNode * node,
float value )
private
Here is the caller graph for this function:

◆ expansion()

void MCTS::expansion ( MCTSNode * node,
Board & board )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNodes()

uint64_t MCTS::getNodes ( ) const
inlineoverridevirtual

Retrieves the total number of nodes evaluated by the search algorithm.

Returns
The node count.

Implements ISearch.

◆ isSearching()

bool MCTS::isSearching ( ) const
inlineoverridevirtual

Checks if the search algorithm is currently running.

Returns
True if a search is in progress.

Implements ISearch.

◆ selectBestChild()

MCTSNode * MCTS::selectBestChild ( MCTSNode * node) const
private
Here is the caller graph for this function:

◆ selection()

MCTSNode * MCTS::selection ( MCTSNode * node,
Board & board )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ selectUCBChild()

MCTSNode * MCTS::selectUCBChild ( MCTSNode * node) const
private
Here is the caller graph for this function:

◆ setExplorationConstant()

void MCTS::setExplorationConstant ( float c)
inline

Sets the exploration constant (Cp) used in the UCB calculation.

◆ setInfoCallback()

void MCTS::setInfoCallback ( InfoCallback callback)
inlineoverridevirtual

Sets the callback function for periodic information updates.

Parameters
callbackThe function to call with depth, score, node count, NPS, and PV.

Implements ISearch.

◆ simulation()

float MCTS::simulation ( Board & board)
private
Here is the caller graph for this function:

◆ startSearch()

Move MCTS::startSearch ( Board & root,
const SearchParams & params )
overridevirtual

Starts the search process on the given root board.

This is a blocking call that will execute the search algorithm until the stopping conditions in params (or stop()) are met.

Parameters
rootThe starting board position.
paramsThe time and depth constraints for the search.
Returns
The best move found.

Implements ISearch.

Here is the call graph for this function:

◆ stop()

void MCTS::stop ( )
inlineoverridevirtual

Asynchronously signals the search to stop immediately.

This method must be thread-safe.

Implements ISearch.

Member Data Documentation

◆ evalCtx_

IEvaluator& MCTS::evalCtx_
private

The evaluator used for leaf node evaluation.

◆ explorationConstant_

float MCTS::explorationConstant_ = 1.41f
private

Weight of exploration in UCB formula.

◆ infoCallback_

InfoCallback MCTS::infoCallback_
private

Callback for UCI info updates.

◆ maxPlayouts_

int MCTS::maxPlayouts_ = 100000
private

Hard limit on search iterations.

◆ nodes_

std::atomic<uint64_t> MCTS::nodes_ {0}
private

Total nodes visited.

◆ root_

std::unique_ptr<MCTSNode> MCTS::root_
private

Root node of the search tree.

◆ searching_

std::atomic<bool> MCTS::searching_ {false}
private

True while a search is active.

◆ stopFlag_

std::atomic<bool> MCTS::stopFlag_ {false}
private

Signals the search to stop.


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