Belofte version 2.1.9
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
29//-----------------------------------------------------------------------
30
31#if defined(_DEBUG)
32#define DEBUG_sendInfoSearching(b, depth, msg, sc) sendInfoSearching(b, depth, msg, sc)
33#define DEBUG_sendInfoSearchingNS(b, depth, msg) sendInfoSearching(b, depth, msg)
34#else
35#define DEBUG_sendInfoSearching(b, depth, msg, sc)
36#define DEBUG_sendInfoSearchingNS(b, depth, msg)
37#endif
38
39#if defined(BELOFTE_NOUNICODE)
40
41#define ALPHA "alpha"
42#define BETA "beta"
43#define ALPHABETA "alpha-beta"
44#define BE_SEARCH "search"
45#define BE_BEST "best"
46#define BE_INF "INF."
47#define BE_UNDEFINED "UNDEF."
48#define BE_DRAW "1/2-1/2"
49#if defined(_DEBUG)
50#define GTOREQUAL " >= "
51#define CHECKSYMBOL " check"
52#endif
53
54#else
55
56#define ALPHABETA "𝛼𝛽"
57#define ALPHA "𝛼"
58#define BETA "𝛽"
59#define BE_SEARCH "β…€"
60#define BE_BEST "↑"
61#define BE_INF "∞"
62#define BE_UNDEFINED "*"
63#define BE_DRAW "Β½-Β½"
64#if defined(_DEBUG)
65#define GTOREQUAL " β‰₯ "
66#define CHECKSYMBOL " †"
67#endif
68
69#endif
70
71//-----------------------------------------------------------------------
72
74public:
75 explicit bSearchAlgorithm(std::string const& s)
76 : m_aborting{false}
77 , m_name{s}
78 , m_levelptr{nullptr}
79 , m_appei{nullptr}
80 , m_evalptr{nullptr}
81 {}
83 { m_levelptr = nullptr; }
84
85 // no copy or move ctor nor assignment defined
90
91 constexpr int64_t getNodes() const
92 { return m_leafnodes; }
93 constexpr int64_t getNonLeafNodes()
94 const { return m_nonleafnodes; }
95
96 void StartSearch(bScore const sc);
98 { m_aborting = false;
99 ClockEnd();
100 }
101 inline void InterruptSearch()
102 { m_aborting = true; }
103
104 void SearchBestMove(bBoard& b, bMoveList& ml);
105
106 void sendInfoSearching(bBoard const& b, depth_t const nDepth,
107 std::string const& comment) const;
108 bScore sendInfoSearching(bBoard const& b, depth_t const nDepth,
109 std::string const& comment,
110 bScore const sc) const;
111
113
114 constexpr bool isNoBench() const
115 { return m_noBench; }
116 inline void setBench()
117 { m_noBench = false; }
118 inline void clearBench()
119 { m_noBench = true; }
120
121 operator std::string() const&
122 { return const_cast<std::string const&>(m_name); }
123
124 int64_t m_leafnodes = 0LL;
125 int64_t m_nonleafnodes = 0LL;
126 bool m_iterativesearch = false; // access required from bench command
127
128protected:
129 virtual bScore CalcBestMove(bBoard& b, bMoveList& ml) = 0;
130 void CheckIfAbortingSearch() const;
131
132 inline void adjustMaxSearchedDepth(depth_t const nDepth)
133 { if (nDepth > m_maxDepth) m_maxDepth = nDepth; }
134 constexpr depth_t getMaxSearchedDepth() const
135 { return m_maxDepth; }
137 { m_maxDepth = 0; }
138
139 inline void setLevel(bLevel* l)
140 { m_levelptr = l; }
141 inline bLevel* getLevel()
142 { return m_levelptr; }
143
144 void sendInfoCurrMove(bBoard const& b,
145 depth_t const nCurDepth,
146 bMove const& m,
147 movenum_t const moveid) const;
148
149private:
150 void SearchBestMoveIterative(bBoard& b, bMoveList& ml);
151 void dumpMoveList(bBoard const& b, depth_t const iDepth) const;
152
153 depth_t m_maxDepth = 0;
154 bool m_noBench = true; // set to false during bench or perft execution
155 int m_postlevel = 0;
156 bScore m_minimizing = 1; // set to -1 if searching started for black
157 std::atomic<bool> m_aborting;
158 std::string m_name;
159 bLevel* m_levelptr;
160 engineInterface* m_appei;
161 bPositionEvaluation* m_evalptr;
162};
163
164//-----------------------------------------------------------------------
165
166#endif // defined SEARCH_H
167
168// eof
uint_fast8_t movenum_t
Definition belofte.h:100
int_fast8_t depth_t
Definition belofte.h:103
void ClockEnd()
Definition util.cpp:37
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:132
constexpr int64_t getNodes() const
Definition search.h:91
void clearBench()
Definition search.h:118
void StartSearch(bScore const sc)
Definition search.cpp:157
void sendInfoCurrMove(bBoard const &b, depth_t const nCurDepth, bMove const &m, movenum_t const moveid) const
Definition search.cpp:223
void StopSearch()
Definition search.h:97
bool m_iterativesearch
Definition search.h:126
void initMaxSearchedDepth()
Definition search.h:136
void InterruptSearch()
Definition search.h:101
constexpr bool isNoBench() const
Definition search.h:114
~bSearchAlgorithm() override
Definition search.h:82
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:141
int64_t m_nonleafnodes
Definition search.h:125
bSearchAlgorithm(std::string const &s)
Definition search.h:75
bSearchAlgorithm(bSearchAlgorithm const &)=delete
bSearchAlgorithm(bSearchAlgorithm &&)=delete
constexpr depth_t getMaxSearchedDepth() const
Definition search.h:134
int64_t m_leafnodes
Definition search.h:124
void setLevel(bLevel *l)
Definition search.h:139
bSearchAlgorithm & operator=(bSearchAlgorithm const &)=delete
void setBench()
Definition search.h:116
bScore RetrieveBoardEvaluation(bBoard &b, gameResult_t const gr=GR_UNSET) const
Get score of board, eventually from cache.
Definition search.cpp:242
void CheckIfAbortingSearch() const
Definition search.cpp:168
void sendInfoSearching(bBoard const &b, depth_t const nDepth, std::string const &comment) const
Definition search.cpp:191
bSearchAlgorithm & operator=(bSearchAlgorithm &&)=delete
constexpr int64_t getNonLeafNodes() const
Definition search.h:93
virtual bScore CalcBestMove(bBoard &b, bMoveList &ml)=0
implementation of user interface
@ GR_UNSET
Definition eval.h:35
enum gameResult gameResult_t
Definition eval.h:43
int16_t bScore
Definition eval.h:11