Belofte version 2.1.9
A promising chess program using the UCI or Winboard interface
level.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: level.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(LEVEL_H)
9#define LEVEL_H
10
11constexpr depth_t INFINITE_DEPTH = 50;
12constexpr depth_t DEFAULT_DEPTH = 5;
14constexpr auto MINIMAL_DEPTH_COMPLETED = 1;
15constexpr auto TIME_OVERFLOWMULTIPLYER = 2;
16
17constexpr auto TIME_UNDERFLOWMULTIPLYER = 2;
18// formula time-elapsed > multiplier * movetime / divider
19// 1,3 equals 33% 1,2 equals 50%, 2,3 equals 66%
20constexpr auto TIME_UNDERFLOWDEVIDER = 2;
21
22/// time lost in between UI go command and bestmove return
23/// @todo replace based on actual measurements
24constexpr auto TIME_LOSTINENGINE = 50;
25constexpr auto TIME_LASTMOVEMARGIN = 100;
26
27/** Implements clock
28 * n seconds per game - setGameTime(seconds * 1000)
29 * n seconds per game, increment inc seconds per move
30 * - setGameTime(seconds * 1000, inc * 1000)
31 * n seconds per move - setMoveTime(seconds * 1000)
32 * n seconds per x moves - setMoveTime(seconds * 1000, x)
33 * n seconds per x moves, increment inc seconds per move
34 * - setMoveTime(seconds * 1000, x, inc * 1000)
35 * infinite - setInfinite()
36 * mate search - setMateSearch(depth)
37 *
38 * restrict to depth x - setDepthCommand(depth)
39 * @todo restrict to n nodes - setNodes(nodes)
40 * @todo restrict to moves - setMoves(moves)
41 *
42 * start pondering - setPondering()
43 */
44
47
48class bLevel {
49public:
51 : m_oType{LevelType::Unset}
52 , m_nSearchDepth{DEFAULT_DEPTH}
53 , m_nQSDepth{DEFAULT_DEPTH + QS_DEPTHEXTENSION}
54 , m_nMaxDepth{INFINITE_DEPTH}
55 { flagLevelChanged(); }
57 {}
58
59 bLevel(bLevel const&) = default;
60 bLevel& operator=(bLevel const&) = default;
61
62 // no move ctor or assignment defined
63 bLevel(bLevel&&) = delete;
64 bLevel& operator=(bLevel&&) = delete;
65
66 constexpr bool hasLevelChanged() const
67 { return m_levelchanged; }
68 inline void flagLevelChanged()
69 { m_levelchanged = true; } /// new level or new game
70 inline void clearLevelChanged()
71 { m_levelchanged = false; }
72
73 // specific levels
74 void setGameTime(int const msPerGame);
75 void setGameTime(int const msPerGame, int const msIncrementPerMove);
76 void setMoveTime(int const msPerMove);
77 void setMoveTime(int const msPerGame, int const nMoves);
78 void setMoveTime(int const msPerGame, int const nMoves, int const msIncrementPerMove);
79 void setInfinite();
80 void setMateSearch(depth_t const d);
82
83 // level modifiers, before start of search
84 void setMoves(std::string movelist);
85 inline void setNodes(int64_t const n)
86 { m_nNodes = n; }
87 inline void setPondering()
88 { m_pondering = true; }
89 inline void clearPondering()
90 { m_pondering = false; }
91 constexpr bool isPondering() const
92 { return m_pondering; }
93
94 // level info
95 constexpr LevelType getType() const
96 { return m_oType; }
97 void setSearchDepth(depth_t const d); // also adjusts QS depth
98 constexpr depth_t getSearchDepth() const
99 { return m_nSearchDepth; }
100 inline void setQSDepth(depth_t const d)
101 { m_nQSDepth = d; }
102 constexpr depth_t getQSDepth() const
103 { return m_nQSDepth; }
104 inline void setMaxDepth(depth_t const d)
105 { m_nMaxDepth = d; }
106 constexpr depth_t getMaxDepth() const
107 { return m_nMaxDepth; }
108
109 // level search decisions
110 void setDepthCommand(depth_t const d);
111 bool searchDepthReached(depth_t const d) const;
112 bool qsDepthReached(depth_t const d) const;
113 bool stoppingSearch(long const nTimeElapsed) const;
114 bool stillTimeLeft(depth_t const d, long const nTimeElapsed) const;
115
116 // level update
117 void setRemainingTime(int const msPerGame);
118 void setMovePlayed();
119 void undoMovePlayed();
120
121 operator std::string() const;
122
123private:
124 void recalibrateTime();
125 void adjustQSDepth();
126 std::string prettyDepth(depth_t const d) const;
127 std::string prettyMoves(int const d) const;
128
129 LevelType m_oType;
130 depth_t m_nSearchDepth;
131 depth_t m_nQSDepth;
132 depth_t m_nMaxDepth;
133
134 int m_nTimeForMove = 0; /// indicative time for searching
135 int m_nTimeLeftForGame = 0; /// total time left for n moves or game
136 int m_nRemainingMovesForPeriod = 0; /// total time left for next time control
137
138 // aborting time control
139 int m_nMaxTimeForMove = 0; /// time when to abort search
140 int m_nEstAllowNextIterationTime = 0; /// indicative time for next iteration
141 int m_nAbsoluteAbortTime = 0; /// time after which an abort if forced
142
143 // level as set initially
144 bool m_levelchanged = true;
145 bool m_pondering = false;
146 int64_t m_nNodes = 0; /// maximum number of nodes allowed
147 int m_timeForGame = 0; /// total time per game set
148 int m_movesPerPeriod = 0; /// number of moves per period
149 int m_incrementPerMove = 0; /// number of milliseconds added each move
150};
151
152#endif // defined LEVEL_H
153
154// eof
int_fast8_t depth_t
Definition belofte.h:103
bool searchDepthReached(depth_t const d) const
Definition level.cpp:245
void clearPondering()
Definition level.h:89
void flagLevelChanged()
Definition level.h:68
bLevel & operator=(bLevel &&)=delete
void setGameTime(int const msPerGame)
Definition level.cpp:23
constexpr depth_t getSearchDepth() const
Definition level.h:98
void setMoreTimeRequired()
increase gradually the allowed time for move, first move to max time for move, then move to abort tim...
Definition level.cpp:297
void setQSDepth(depth_t const d)
Definition level.h:100
void setInfinite()
Definition level.cpp:12
void setRemainingTime(int const msPerGame)
xboard issues time command to update available time
Definition level.cpp:133
void setNodes(int64_t const n)
Definition level.h:85
void setMovePlayed()
Definition level.cpp:143
void setMoveTime(int const msPerMove)
Definition level.cpp:59
void setSearchDepth(depth_t const d)
Definition level.cpp:232
bLevel()
Definition level.h:50
constexpr bool hasLevelChanged() const
Definition level.h:66
bLevel & operator=(bLevel const &)=default
void setMateSearch(depth_t const d)
Definition level.cpp:119
void setMoves(std::string movelist)
constexpr depth_t getQSDepth() const
Definition level.h:102
constexpr LevelType getType() const
Definition level.h:95
constexpr bool isPondering() const
Definition level.h:91
~bLevel()
Definition level.h:56
bool qsDepthReached(depth_t const d) const
Definition level.cpp:250
bool stoppingSearch(long const nTimeElapsed) const
Stop search required ?
Definition level.cpp:260
void setPondering()
Definition level.h:87
void clearLevelChanged()
new level or new game
Definition level.h:70
bLevel(bLevel const &)=default
void setMaxDepth(depth_t const d)
Definition level.h:104
bool stillTimeLeft(depth_t const d, long const nTimeElapsed) const
Do we still have time to do another iteration ?
Definition level.cpp:280
void setDepthCommand(depth_t const d)
Definition level.cpp:226
void undoMovePlayed()
used for recalibrating time in case for undo move in xboard moves per period
Definition level.cpp:156
constexpr depth_t getMaxDepth() const
Definition level.h:106
bLevel(bLevel &&)=delete
constexpr auto TIME_OVERFLOWMULTIPLYER
Definition level.h:15
constexpr auto TIME_UNDERFLOWDEVIDER
Definition level.h:20
constexpr depth_t QS_DEPTHEXTENSION
Definition level.h:13
constexpr auto TIME_UNDERFLOWMULTIPLYER
Definition level.h:17
constexpr auto MINIMAL_DEPTH_COMPLETED
Definition level.h:14
constexpr auto TIME_LOSTINENGINE
time lost in between UI go command and bestmove return
Definition level.h:24
constexpr depth_t DEFAULT_DEPTH
Definition level.h:12
constexpr depth_t INFINITE_DEPTH
Definition level.h:11
LevelType
Implements clock n seconds per game - setGameTime(seconds * 1000) n seconds per game,...
Definition level.h:45
@ TimeControl
Definition level.h:46
@ SuddenDeath
Definition level.h:46
@ SecondsPerMove
Definition level.h:45
@ FicherTimeControl
Definition level.h:46
@ MateSearch
Definition level.h:46
@ Infinite
Definition level.h:46
@ Unset
Definition level.h:45
constexpr auto TIME_LASTMOVEMARGIN
Definition level.h:25