Belofte version 2.2.0
A promising chess program using the UCI or Winboard interface
search.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: search.h
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#if !defined(SEARCH_H)
9#define SEARCH_H
10
11class gameEndException : public std::exception
12{
13 const char * what () const noexcept override
14 { return "Game ended"; }
15};
16
17class noMoveFoundException : public std::exception
18{
19 const char * what () const noexcept override
20 { return "Nothing found"; }
21};
22
23class searchAbortedException : public std::exception
24{
25 const char * what () const noexcept override
26 { return "Aborting search"; }
27};
28
29class treeLogicException : public std::exception
30{
31public:
33 : m_msg("tree logic exception, should not occur")
34 {}
35 explicit treeLogicException(std::string const& msg)
36 : m_msg(msg)
37 {}
38
39 const char* what() const noexcept override {
40 return m_msg.c_str();
41 }
42
43private:
44 std::string m_msg;
45};
46
47//-----------------------------------------------------------------------
48
49#if defined(_DEBUG)
50#define DEBUG_sendInfoSearching(b, depth, msg, sc) sendInfoSearching(b, depth, msg, sc)
51#define DEBUG_sendInfoSearchingNS(b, depth, msg) sendInfoSearching(b, depth, msg)
52#else
53#define DEBUG_sendInfoSearching(b, depth, msg, sc)
54#define DEBUG_sendInfoSearchingNS(b, depth, msg)
55#endif
56
57#if defined(BELOFTE_NOUNICODE)
58
59#define ALPHA "alpha"
60#define BETA "beta"
61#define ALPHABETA "alpha-beta"
62#define BE_INF "INF."
63#define BE_DRAW "1/2-1/2"
64
65#if defined(_DEBUG)
66#define BE_UNDEFINED "UNDEF."
67#define GTOREQUAL " >= "
68#endif
69
70#else
71
72#define ALPHA "𝛼"
73#define BETA "𝛽"
74#define ALPHABETA "𝛼𝛽"
75#define BE_INF "∞"
76#define BE_DRAW "Β½-Β½"
77
78#if defined(_DEBUG)
79#define BE_UNDEFINED "*"
80#define GTOREQUAL " β‰₯ "
81#endif
82
83#endif
84
85#if !defined(_DEBUG)
86#define BE_UNDEFINED ""
87#endif
88
89//-----------------------------------------------------------------------
90
92public:
93 explicit bSearchAlgorithm(std::string const& s)
94 : m_aborting{false}
95 , m_name{s}
96 , m_levelptr{nullptr}
97 , m_appei{nullptr}
98 , m_evalptr{nullptr}
99 {}
101 { m_levelptr = nullptr; }
102
103 // no copy or move ctor nor assignment defined
108
109 constexpr int64_t getNodes() const
110 { return m_leafnodes; }
111 constexpr int64_t getNonLeafNodes()
112 const { return m_nonleafnodes; }
113
114 void StartSearch(bScore const sc);
115 void StopSearch();
116 inline void InterruptSearch()
117 { m_aborting = true; }
118
119 void SearchBestMove(bBoard& b, bMoveList& ml);
120
121 void sendInfoSearching(bBoard const& b, depth_t const nDepth,
122 std::string const& comment) const;
123 bScore sendInfoSearching(bBoard const& b, depth_t const nDepth,
124 std::string const& comment,
125 bScore const sc) const;
126
128 gameResult_t const gr, bool const bRecalcFirst) const;
130 gameResult_t const gr, bool const bRecalcFirst) const;
131
132 constexpr bool isNoBench() const
133 { return m_noBench; }
134 constexpr bool isBench() const
135 { return !m_noBench; }
136 inline void setBench()
137 { m_noBench = false; }
138 inline void clearBench()
139 { m_noBench = true; }
140
141 operator std::string() const&
142 { return const_cast<std::string const&>(m_name); }
143
144 int64_t m_leafnodes = 0LL;
145 int64_t m_nonleafnodes = 0LL;
146 bool m_iterativesearch = false; // access required from bench command
147
148protected:
149 virtual bScore CalcBestMove(bBoard& b, bMoveList& ml) = 0;
150 void CheckIfAbortingSearch() const;
151
152 inline void adjustMaxSearchedDepth(depth_t const nDepth)
153 { if (nDepth > m_maxDepth) m_maxDepth = nDepth; }
154 constexpr depth_t getMaxSearchedDepth() const
155 { return m_maxDepth; }
157 { m_maxDepth = 0; }
158
159 inline void setLevel(bLevel* l)
160 { m_levelptr = l; }
161 inline bLevel* getLevel()
162 { return m_levelptr; }
163
164 void sendInfoCurrMove(bBoard const& b,
165 depth_t const nCurDepth,
166 bMove const& m,
167 movenum_t const moveid) const;
168
169private:
170 void SearchBestMoveIterative(bBoard& b, bMoveList& ml);
171 void dumpMoveList(bMoveList const& ml, depth_t const iDepth) const;
172
173 depth_t m_maxDepth = 0;
174 bool m_noBench = true; // set to false during bench or perft execution
175 int m_postlevel = 0;
176 bScore m_minimizing = 1; // set to -1 if searching started for black
177 std::atomic<bool> m_aborting;
178 std::string m_name;
179 bLevel* m_levelptr;
180 engineInterface* m_appei;
181 bPositionEvaluation* m_evalptr;
182};
183
184//-----------------------------------------------------------------------
185
186#endif // defined SEARCH_H
187
188// eof
uint_fast8_t movenum_t
Definition belofte.h:103
int_fast8_t depth_t
Definition belofte.h:106
TimedExecution()
implementation of timing functions
Definition util.cpp:17
board
Definition board.h:45
Definition level.h:48
Definition move.h:13
void adjustMaxSearchedDepth(depth_t const nDepth)
Definition search.h:152
constexpr int64_t getNodes() const
Definition search.h:109
void clearBench()
Definition search.h:138
void StartSearch(bScore const sc)
Definition search.cpp:211
void sendInfoCurrMove(bBoard const &b, depth_t const nCurDepth, bMove const &m, movenum_t const moveid) const
Definition search.cpp:282
void StopSearch()
Definition search.cpp:231
bool m_iterativesearch
Definition search.h:146
bScore RetrieveBoardEvaluationForWhite(bBoard &b, gameResult_t const gr, bool const bRecalcFirst) const
Get score of board, eventually from cache, from whites view.
Definition search.cpp:323
void initMaxSearchedDepth()
Definition search.h:156
void InterruptSearch()
Definition search.h:116
constexpr bool isNoBench() const
Definition search.h:132
~bSearchAlgorithm() override
Definition search.h:100
void SearchBestMove(bBoard &b, bMoveList &ml)
Generic search, will call (non-)recursive method per algorithm only when there are moves to be played...
Definition search.cpp:18
bLevel * getLevel()
Definition search.h:161
int64_t m_nonleafnodes
Definition search.h:145
bSearchAlgorithm(std::string const &s)
Definition search.h:93
bSearchAlgorithm(bSearchAlgorithm const &)=delete
bSearchAlgorithm(bSearchAlgorithm &&)=delete
constexpr depth_t getMaxSearchedDepth() const
Definition search.h:154
constexpr bool isBench() const
Definition search.h:134
int64_t m_leafnodes
Definition search.h:144
bScore RetrieveBoardEvaluation(bBoard &b, gameResult_t const gr, bool const bRecalcFirst) const
Get score of board, eventually from cache.
Definition search.cpp:302
void setLevel(bLevel *l)
Definition search.h:159
bSearchAlgorithm & operator=(bSearchAlgorithm const &)=delete
void setBench()
Definition search.h:136
void CheckIfAbortingSearch() const
Definition search.cpp:223
void sendInfoSearching(bBoard const &b, depth_t const nDepth, std::string const &comment) const
Definition search.cpp:250
bSearchAlgorithm & operator=(bSearchAlgorithm &&)=delete
constexpr int64_t getNonLeafNodes() const
Definition search.h:111
virtual bScore CalcBestMove(bBoard &b, bMoveList &ml)=0
implementation of user interface
treeLogicException(std::string const &msg)
Definition search.h:35
const char * what() const noexcept override
Definition search.h:39
enum gameResult gameResult_t
Definition eval.h:40
int16_t bScore
Definition eval.h:11