io-chess
UCI chess engine
Loading...
Searching...
No Matches
ISearch.h
Go to the documentation of this file.
1#pragma once
2
10
11#include "../Types.h"
12#include <atomic>
13#include <chrono>
14#include <cstdint>
15#include <functional>
16#include <string>
17#include <vector>
18
27 std::atomic<bool> stop{false};
28 std::atomic<bool> disableTablebase{false};
29 std::atomic<uint64_t> totalNodes{0};
30 std::atomic<int> selDepth{0};
31 std::atomic<uint64_t> tbHits{0};
32 std::atomic<uint64_t> ttHits{0};
33
34 // =========================================================================
35 // LAZY SMP: Shared state for thread coordination
36 // =========================================================================
37
38 std::atomic<int> mainThreadScore{0};
39 std::atomic<int> mainThreadDepth{0};
40 std::atomic<int> targetDepth{0};
41 int numThreads{1};
42
43 // Time management (written by Main, read by Helpers)
44 std::atomic<int64_t> startTimeNs{0};
45 int64_t softLimit = 0;
46 int64_t hardLimit = 0;
47
52 struct SearchConfig {
53 int razorMarginD1 = 350;
54 int razorMarginD2 = 700;
55 int razorMarginD3 = 900;
56 int futilityMargin = 200;
58 int lmpBase = 3;
59 int lmrThreshold = 4;
60 int nullMoveR = 3;
61 int deltaMargin = 200;
62 int singularMargin = 50;
65 int corrWeight = 10;
66 bool enableCorrHist = true;
68
69 // Lazy Eval Tuning
74 bool enableLazyEval = false;
75
97
102 struct VizNode {
103 uint64_t parentHash;
104 uint64_t childHash;
105 std::string move;
108 int depth;
109 int ply;
111 uint64_t subtreeNodes;
112 bool isPV;
114 };
115
116 std::vector<VizNode> vizTree;
117 bool exportTree = false;
119};
120
126 int wtime = 0;
127 int btime = 0;
128 int winc = 0;
129 int binc = 0;
130 int movestogo = 0;
132 int nodes = 0;
133 int movetime = 0;
134 bool infinite = false;
135 bool ponder = false;
136 int multiPV = 1;
137
138 std::vector<Move> searchMoves;
139};
140
145using InfoCallback = std::function<void(int depth, int score, int nodes,
146 int nps, const std::vector<Move> &pv)>;
147
155class ISearch {
156public:
157 virtual ~ISearch() = default;
158
169 virtual Move startSearch(Board &root, const SearchParams &params) = 0;
170
176 virtual void stop() = 0;
177
183 virtual bool isSearching() const = 0;
184
190 virtual void setInfoCallback(InfoCallback callback) = 0;
191
197 virtual uint64_t getNodes() const = 0;
198};
std::function< void(int depth, int score, int nodes, int nps, const std::vector< Move > &pv)> InfoCallback
Callback function type for sending UCI 'info' updates during search.
Definition ISearch.h:145
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
Abstract interface for search algorithms.
Definition ISearch.h:155
virtual void stop()=0
Asynchronously signals the search to stop immediately.
virtual void setInfoCallback(InfoCallback callback)=0
Sets the callback function for periodic information updates.
virtual ~ISearch()=default
virtual bool isSearching() const =0
Checks if the search algorithm is currently running.
virtual uint64_t getNodes() const =0
Retrieves the total number of nodes evaluated by the search algorithm.
virtual Move startSearch(Board &root, const SearchParams &params)=0
Starts the search process on the given root board.
constexpr int DEFAULT_DEPTH
Default maximum depth for search.
Definition Types.h:48
Parameters defining the constraints for a search operation.
Definition ISearch.h:125
int binc
Black increment per move in ms.
Definition ISearch.h:129
int btime
Black time remaining in ms.
Definition ISearch.h:127
int wtime
White time remaining in ms.
Definition ISearch.h:126
int depth
Target depth limit.
Definition ISearch.h:131
int movetime
Fixed time per move in ms.
Definition ISearch.h:133
int movestogo
Moves until next time control.
Definition ISearch.h:130
int multiPV
Number of principal variations to search (1 = normal, >1 = analysis mode).
Definition ISearch.h:136
bool infinite
True if search should run until explicitly stopped.
Definition ISearch.h:134
bool ponder
True if thinking during the opponent's time (wait for ponderhit/stop).
Definition ISearch.h:135
int winc
White increment per move in ms.
Definition ISearch.h:128
int nodes
Node limit (0 = unlimited).
Definition ISearch.h:132
std::vector< Move > searchMoves
Specific root moves to search (if empty, search all legal moves).
Definition ISearch.h:138
Runtime tunable parameters for the search algorithm.
Definition ISearch.h:52
bool enableCorrHist
Flag to enable or disable the correction history heuristic.
Definition ISearch.h:66
int razorMarginD3
Razoring margin at depth 3.
Definition ISearch.h:55
int razorMarginD2
Razoring margin at depth 2.
Definition ISearch.h:54
int futilityMargin
Futility pruning margin.
Definition ISearch.h:56
int deltaMargin
Delta pruning margin.
Definition ISearch.h:61
int reverseFutilityMargin
Reverse futility (static null move) margin.
Definition ISearch.h:57
int singularMargin
Singular extension margin.
Definition ISearch.h:62
int nullMoveR
Null Move Pruning depth reduction factor.
Definition ISearch.h:60
int singularMinDepth
Minimum depth required for singular extensions.
Definition ISearch.h:63
int lmpBase
Late Move Pruning base threshold.
Definition ISearch.h:58
int lmrThreshold
Late Move Reduction threshold.
Definition ISearch.h:59
int corrWeight
Weight applied to the correction history heuristic.
Definition ISearch.h:65
int internalPruningMargin
Margin for internal node pruning.
Definition ISearch.h:64
int razorMarginD1
Razoring margin at depth 1.
Definition ISearch.h:53
Represents a single node in the search tree for export and visualization.
Definition ISearch.h:102
int staticEval
NN evaluation at node (before search).
Definition ISearch.h:106
uint64_t subtreeNodes
Nodes searched below this move.
Definition ISearch.h:111
int depth
Search depth remaining.
Definition ISearch.h:108
int searchScore
Minimax backed-up score (after search).
Definition ISearch.h:107
uint64_t childHash
Zobrist hash of the child position.
Definition ISearch.h:104
bool isPV
True if on principal variation.
Definition ISearch.h:112
uint64_t parentHash
Zobrist hash of the parent position.
Definition ISearch.h:103
PruneReason pruneReason
Why this node was pruned.
Definition ISearch.h:113
std::string move
UCI string representation of the move.
Definition ISearch.h:105
int ply
Ply from root (0 = root).
Definition ISearch.h:109
int moveOrder
Order this move was tried (0 = first, 1 = second, etc.).
Definition ISearch.h:110
Thread-safe shared state for search coordination and Lazy SMP.
Definition ISearch.h:26
std::atomic< uint64_t > ttHits
Total transposition table hits across all threads.
Definition ISearch.h:32
int exportTreeDepth
Maximum depth to record nodes for tree visualization.
Definition ISearch.h:118
struct SearchSharedData::SearchConfig config
int64_t softLimit
Soft time limit in milliseconds.
Definition ISearch.h:45
std::atomic< bool > stop
Global flag to immediately stop all search threads.
Definition ISearch.h:27
bool exportTree
Flag to enable tree export visualization.
Definition ISearch.h:117
std::atomic< int > mainThreadDepth
Main thread's completed depth (used by helpers for early termination).
Definition ISearch.h:39
int lazyEvalMaxDepth
Maximum depth to apply lazy evaluation.
Definition ISearch.h:70
std::atomic< int > targetDepth
Target search depth (set by main thread at search start).
Definition ISearch.h:40
int64_t hardLimit
Hard time limit in milliseconds.
Definition ISearch.h:46
std::atomic< uint64_t > tbHits
Total tablebase hits across all threads.
Definition ISearch.h:31
std::vector< VizNode > vizTree
Stores edges for top layers of the search tree.
Definition ISearch.h:116
bool enableLazyEval
Flag to enable or disable lazy evaluation.
Definition ISearch.h:74
std::atomic< uint64_t > totalNodes
Global node count across all threads.
Definition ISearch.h:29
std::atomic< int > mainThreadScore
Main thread's current score (used by helpers for aspiration windows).
Definition ISearch.h:38
std::atomic< int64_t > startTimeNs
Search start time in steady_clock nanoseconds (atomic for ponderhit updates).
Definition ISearch.h:44
int lazyEvalBaseMargin
Base margin for lazy evaluation (approx 1 Sigma of residual error).
Definition ISearch.h:71
PruneReason
Enumeration of reasons why a node might be pruned (used for tree visualization).
Definition ISearch.h:80
@ RAZOR
Razoring (drop to qsearch).
Definition ISearch.h:86
@ NULL_MOVE
Null Move Pruning cutoff.
Definition ISearch.h:88
@ INTERNAL
Internal Node Pruning (Eval Margin).
Definition ISearch.h:89
@ DRAW
Draw by repetition or 50-move rule.
Definition ISearch.h:85
@ TT_CUTOFF
Transposition table cutoff.
Definition ISearch.h:92
@ SEE
Static Exchange Evaluation pruning.
Definition ISearch.h:84
@ SINGULAR_SKIP
Skipped during singular extension search.
Definition ISearch.h:95
@ LMP
Late Move Pruning.
Definition ISearch.h:82
@ TB_CUTOFF
Tablebase cutoff.
Definition ISearch.h:91
@ BETA_CUTOFF
Normal beta cutoff.
Definition ISearch.h:93
@ FUTILITY
Futility Pruning.
Definition ISearch.h:83
@ DEPTH_LIMIT
Beyond exportTreeDepth and not PV.
Definition ISearch.h:94
@ NONE
Not pruned - fully searched.
Definition ISearch.h:81
@ LAZY_EVAL
Lazy Eval skip (fast score sufficient).
Definition ISearch.h:90
@ REVERSE_FUTILITY
Reverse Futility / Static Null Move.
Definition ISearch.h:87
std::atomic< bool > disableTablebase
Disables tablebase probing (e.g., when Syzygy coverage is incomplete).
Definition ISearch.h:28
int lazyEvalDepthMargin
Depth multiplier margin for lazy evaluation.
Definition ISearch.h:72
std::atomic< int > selDepth
Maximum selective depth reached by any thread.
Definition ISearch.h:30
int numThreads
Total number of active search threads.
Definition ISearch.h:41
int lazyEvalMinMargin
Absolute minimum margin for lazy evaluation.
Definition ISearch.h:73