Belofte version 2.2.0
A promising chess program using the UCI or Winboard interface
bel_debug.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: bel_debug.cpp
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#include "belofte.h"
9
10//-----------------------------------------------------------------------
11// NOLINTBEGIN
12
13void bel_debug::execute(std::string const& args)
14{
15 if (args == "on") {
16 App().setConfig("UCIdebug", 1);
17 App().bout.setLevel(99);
18 } else if (args == "off") {
19 App().setConfig("UCIdebug", 0);
20 App().bout.setLevel(0);
21 /// @todo add abstraction level on different debug commands
22 /// so that they do not need to be coded here (dynamic binding ?)
23 } else if (args == "show fen") {
24 App().bout << "info string fen " << Game()->getCurrentPosition().getFEN() << "\n";
25 } else if (args == "help") {
26 App().bout << show_help();
27 } else if (args == "gamehistory") {
28 App().bout << show_gamehistory();
29 } else if (args == "fenhistory") {
30 App().bout << show_fenhistory();
31 } else if (args == "poshistory") {
32 App().bout << show_poshistory();
33 } else if (args == "hashhistory") {
34 App().bout << show_hashhistory();
35 } else if (args == "autosave") {
36 App().bout << "not implemented yet\n";
37 } else if (args == "logging") {
38 App().bout << "not implemented yet\n";
39 } else if (args == "ml") {
40 bBoard const& b = Game()->getCurrentPosition();
41 bMoveList ml;
42 ml.generateMoves(b);
43 bPgnMoveList pml(b);
44 App().bout << "info string ml " << ml << "\n";
45 App().bout << "info string ml " << pml << "\n";
46 } else if (args == "sortmoves") {
47 bBoard const& b = Game()->getCurrentPosition();
48 bMoveList ml;
49 ml.generateMoves(b);
50 ml.sortMoves(false);
51 bPgnMoveList pml(b);
52 App().bout << "info string ml " << ml << "\n";
53 App().bout << "info string ml " << pml << "\n";
54 } else if (args == "setrunning") {
55 AppEI()->setRunning(true);
56 } else if (args == "wait") {
57 bel_debug::wait();
58 } else if (args == "invert") {
60 } else if (args == "log on") {
61 App().bout.attach(App().getName() + ".log");
62 } else if (args == "log off") {
63 App().bout.detach();
64 } else if (args == "search on") {
65 App().sout.attach(App().getName() + ".log");
66 } else if (args == "search off") {
67 App().sout.detach();
68 } else if (args == "verbose 0") {
69 /// @todo move to verbose command
70 App().sout.setLevel(0);
71 } else if (args == "verbose 1") {
72 App().sout.setLevel(1);
73 } else if (args == "verbose 2") {
74 App().sout.setLevel(2);
75 } else if (args == "verbose 3") {
76 App().sout.setLevel(3);
77 } else if (args == "verbose 4") {
78 App().sout.setLevel(4);
79 } else if (args == "verbose 5") {
80 App().sout.setLevel(5);
81 } else if (args == "verbose all") {
82 App().sout.setLevel(99);
83 } else if (args == "") {
84 App().bout.setLevel(50);
85 } else {
86 AppEI()->sendError("argument invalid", "debug " + args);
87 }
88}
89
90void bel_debug::info() const
91{
92 App().bout << "Info for: " << App().getName() << " - "
93 << "Version: " MYVERSION << "\n";
94 App().bout << "Built on: " MYRELEASEDATE " for " MYOS " - " MYPLATFORM << "\n";
95 App().bout << "Compiled by: " BELOFTE_COMPILER " with flags:";
96#if defined(CHRONO_MISSING)
97 App().bout << " CHRONO_MISSING";
98#endif
99#if defined(BELOFTE_NORANDOM)
100 App().bout << " BELOFTE_NORANDOM";
101#endif
102#if defined(BELOFTE_UCIMODE)
103 App().bout << " BELOFTE_UCIMODE";
104#endif
105#if defined(BELOFTE_NOSIGNALS)
106 App().bout << " BELOFTE_NOSIGNALS";
107#endif
108#if defined(BELOFTE_NOUNICODE)
109 App().bout << " BELOFTE_NOUNICODE";
110#endif
111#if defined(INCOMPLETE_C11)
112 App().bout << " INCOMPLETE_C11";
113#endif
114#if defined(NODIRENT)
115 App().bout << " NODIRENT";
116#endif
117#if defined(_WIN32) || defined(WIN32)
118 App().bout << " _WIN32";
119#endif
120#if defined(_DEBUG)
121 App().bout << " _DEBUG";
122#endif
123#if defined(NO_NPS_LOG)
124 App().bout << " NO_NPS_LOG";
125#endif
126 App().bout << "\n";
127
128 App().bout
129 << "Mode: " << AppEI()->operator std::string() << " - "
130 << "UI details: " << belofte::to_string(App().bout.getLevel())
131 << (App().bout.isAttached() ? " file " + App().bout.fn() : "")
132 << " - Search details: " << belofte::to_string(App().sout.getLevel())
133 << (App().sout.isAttached() ? " file: " + App().sout.fn() : "")
134 << "\n";
135
136 App().bout
137 << "Eval: " << Game()->getEval()->operator std::string()
138 << (App().getConfig("random", 0) ? " +-rnd" : "")
139 << " - "
140 << "Algo: " << Game()->getAlgorithm()->operator std::string() << "\n"
141 << Game()->getLevel().operator std::string();
142
143 App().bout.endl();
144}
145
147{
148 int64_t nNodes;
149 int64_t nNonLeafNodes;
150 int64_t nDuration;
151
152 search.m_iterativesearch = false;
153
154 Game()->newGame();
156 nNodes = Game()->DoPerft(search, nDepth);
157 nNonLeafNodes = search.getNonLeafNodes();
158 nDuration = search.getDurationMicroSec();
159
160 Game()->setFEN("n1n5/PPPk4/8/8/8/8/4Kppp/5N1N b - - 0 1");
161 nNodes += Game()->DoPerft(search, nDepth);
162 nNonLeafNodes += search.getNonLeafNodes();
163 nDuration += search.getDurationMicroSec();
164
165 App().bout(-1) << "bench " << search.operator std::string()
166 << " Time: " << belofte::prettyTime(nDuration / 1000LL)
167 << " Nodes: " << belofte::to_string(nNodes)
168 << " Total N.: " << belofte::to_string(nNonLeafNodes + nNodes)
169#if !defined(NO_NPS_LOG)
170 << " NPS: " << belofte::to_string(static_cast<int64_t>((nNonLeafNodes + nNodes) * 1000000LL / nDuration))
171#endif
172 ;
173 App().bout.endl();
174}
175
176/**
177 * @static Class static function
178 */
179void bel_debug::wait()
180{
181 if (App().m_reader.isBatchMode()) {
182 while (Game()->isSearching()) {
183 // wait 200 ms before testing again
185 }
186 } else {
187 AppEI()->sendError("Not in batch mode", "debug wait");
188 }
189}
190
191/**
192 * @static Class static function
193 */
194std::string bel_debug::show_help()
195{
196 std::stringstream os;
197 os << "debug help - this message" << "\n"
198 << "debug [off|on] - set level 50, 0 or 99, toggle UCI debug" << "\n"
199 << "debug verbose n - set level n (can be 0-5)" << "\n"
200 << "debug verbose all - set level 99" << "\n"
201 << "debug log on - activate logfile for ui protocol" << "\n"
202 << "debug log off - de-activate logfile for ui protocol" << "\n"
203 << "debug search on - activate logfile for search" << "\n"
204 << "debug search off - de-activate logfile for search" << "\n"
205 << "debug show fen - display FEN string of current position" << "\n"
206 << "debug ml - show moves calculated for current position" << "\n"
207 << "debug sortmoves - sort moves and show moves calculated" << "\n"
208 << "debug invert - invert colours" << "\n"
209 << "debug gamehistory - show complete game" << "\n"
210 << "debug fenhistory - show all fen strings of game" << "\n"
211 << "debug poshistory - show positions of game" << "\n"
212 << "debug setrunning - set xboard persistent running state" << "\n"
213 << "debug wait - wait for previous batch command to finish" << "\n";
214
215 return os.str();
216}
217
218/**
219 * @static Class static function
220 */
221std::string bel_debug::show_gamehistory()
222{
223 std::stringstream os;
224 positions_t const& positions = Game()->getPositions();
225
226 for (bBoard const& b : positions) {
227 bMoveList ml;
228 movenum_t n_moves = ml.generateMoves(b);
229 os << "\n";
230 os << b.getFEN() << "\n"
231 << b << "\n";
232 if (n_moves) {
233 // only print moves if there are any
234 bPgnMoveList pml(b);
235 os << ml << "\n"
236 << pml << "\n";
237 }
238 }
239
240 return os.str();
241}
242
243/**
244 * @static Class static function
245 */
246std::string bel_debug::show_poshistory()
247{
248 std::stringstream os;
249 positions_t const& positions = Game()->getPositions();
250
251 for (bBoard const& b : positions) {
252 os << b << "\n";
253 }
254
255 return os.str();
256}
257
258/**
259 * @static Class static function
260 */
261std::string bel_debug::show_fenhistory()
262{
263 std::stringstream os;
264 positions_t const& positions = Game()->getPositions();
265
266 for (bBoard const& b : positions) {
267 os << b.getFEN() << "\n";
268 }
269
270 return os.str();
271}
272
273/**
274 * @static Class static function
275 */
276std::string bel_debug::show_hashhistory()
277{
278 std::stringstream os;
279 positions_t const& positions = Game()->getPositions();
280
281 for (bBoard const& b : positions) {
282 os << b.getHashStr() << " : " << b.getFEN() << "\n";
283 }
284
285 return os.str();
286}
287
288// NOLINTEND
289// --------------------------------------------------------------------
290
291// eof
appInstance & App()
Definition belofte.cpp:242
engineInterface * AppEI()
Definition belofte.cpp:248
bGame * Game()
Definition belofte.cpp:253
This is the main include file, needs to be included before any other include.
#define MYRELEASEDATE
Definition belofte.h:45
uint_fast8_t movenum_t
Definition belofte.h:103
#define MYVERSION
Definition belofte.h:34
int_fast8_t depth_t
Definition belofte.h:106
static void sleep_ms(unsigned long ms)
Class static function Pause execution for a number of ms.
Definition util.cpp:132
long long getDurationMicroSec() const
Definition util.cpp:61
outputWriter sout
normal output
Definition belofte.h:179
static std::string getName()
Definition belofte.cpp:235
void setConfig(std::string const &s, int64_t v)
Definition belofte.cpp:194
outputWriter bout
Definition belofte.h:178
int64_t getConfig(std::string const &s, int64_t v)
Definition belofte.cpp:199
bFen getFEN() const
board
Definition board.h:45
void invertColours()
invert colours update kingpos, update colour to move, castle rights, ...
Definition board.cpp:181
bSearchAlgorithm * getAlgorithm() const
bPositionEvaluation * getEval() const
int64_t DoPerft(bSearchAlgorithm &sa, depth_t const d)
do perft search at depth in case of Perft algorithm, temporarily set evaltype to None
Definition game.cpp:139
positions_t & getPositions()
Definition game.h:61
void newGame()
Definition game.cpp:24
bLevel & getLevel()
Definition game.h:59
void setFEN(std::string const &fenstr)
Definition game.cpp:54
bBoard & getCurrentPosition()
Definition game.h:54
void setFENInitialPos()
Definition game.cpp:46
movenum_t generateMoves(bBasicBoard const &b)
generate moves if not yet generated
Definition movelist.cpp:417
void sortMoves(bool const bFastSort)
sort moves and update bestmove id if less than 5 moves, sort all if more than 5 moves,...
Definition movelist.cpp:369
bool m_iterativesearch
Definition search.h:146
constexpr int64_t getNonLeafNodes() const
Definition search.h:111
void info() const
Definition bel_debug.cpp:90
static void run_bench(bSearchAlgorithm &search, depth_t const nDepth)
static void execute(std::string const &args)
Definition bel_debug.cpp:13
virtual void setRunning(bool const r)
virtual void sendError(std::string const &error, std::string const &description)
bool isAttached() const
std::string const & fn() const
bool attach(std::string const &fn)
outputWriter & endl()
void setLevel(int const l)
std::vector< bBoard > positions_t
Definition game.h:15
#define BELOFTE_COMPILER
Definition myplatform.h:162
#define MYPLATFORM
no matching platform
Definition myplatform.h:73
#define MYOS
no matching OS
Definition myplatform.h:117
std::string to_string(int16_t value)
std::to_string not compatible on Mac OS (Apple LLVM version 5.0) provide generic utility function
Definition util.cpp:186
std::string prettyTime(long const nTime)
Definition util.cpp:302