Belofte  version 2.1.5
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 
11 constexpr auto INFINITE_DEPTH = 50;
12 constexpr auto DEFAULT_DEPTH = 1;
13 constexpr auto QSDEPTH_MULTIPLYER = 2;
14 constexpr auto QS_DEPTHEXTENSION = 5;
15 constexpr auto MINIMAL_DEPTH_COMPLETED = 1;
16 constexpr auto TIME_OVERFLOWMULTIPLYER = 3;
17 
18 constexpr auto TIME_UNDERFLOWMULTIPLYER = 2;
19 // formula time-elapsed > multiplier * movetime / divider
20 // 1,3 equals 33% 1,2 equals 50%, 2,3 equals 66%
21 constexpr auto TIME_UNDERFLOWDEVIDER = 5;
22 
23 /// time lost in between UI go command and bestmove return
24 /// TODO: replace based on actual measurements
25 constexpr auto TIME_LOSTINENGINE = 50;
26 constexpr auto TIME_LASTMOVEMARGIN = 100;
27 
28 constexpr auto DEFAULT_MOVETIME = 3000 + TIME_LOSTINENGINE;
29 
30 /** Implements clock
31  * n seconds per game - setGameTime(seconds * 1000)
32  * n seconds per game, increment inc seconds per move
33  * - setGameTime(seconds * 1000, inc * 1000)
34  * n seconds per move - setMoveTime(seconds * 1000)
35  * n seconds per x moves - setMoveTime(seconds * 1000, x)
36  * n seconds per x moves, increment inc seconds per move
37  * - setMoveTime(seconds * 1000, x, inc * 1000)
38  * infinite - setInfinite()
39  * mate search - setMateSearch(depth)
40  *
41  * restrict to depth x - setDepth(depth)
42  * @todo restrict to n nodes - setNodes(nodes)
43  * @todo restrict to moves - setMoves(moves)
44  *
45  * start pondering - setPondering()
46  */
47 
50 
51 class bLevel final {
52 public:
53  bLevel();
54  ~bLevel();
55 
56  bLevel& operator=(bLevel const&) = default;
57 
58  // no copy or move ctor nor assignment defined
59  bLevel(bLevel const&) = delete;
60  bLevel(bLevel&&) = delete;
61  bLevel& operator=(bLevel&&) = delete;
62 
63  operator std::string() const;
64 
65  void flagLevelChange(); /// new level or new game
66 
67  // specific levels
68  void setGameTime(int const msPerGame);
69  void setGameTime(int const msPerGame, int const msIncrementPerMove);
70  void setMoveTime(int const msPerMove);
71  void setMoveTime(int const msPerGame, int const nMoves);
72  void setMoveTime(int const msPerGame, int const nMoves, int const msIncrementPerMove);
73  void setInfinite();
74  void setMateSearch(depth_t const d);
75 
76  // level modifiers, before start of search
77  void setMoves(std::string movelist);
78  void setNodes(int64_t const n);
79  void initDepths(depth_t const d);
80  void setPondering();
81  void clearPondering();
82  bool isPondering() const;
83 
84  // level info
85  LevelType getType() const;
86  depth_t getDepth() const;
87 
88  // level search decisions
89  void setDepth(depth_t const d);
90  bool depthReached(depth_t const d) const;
91  bool qsDepthReached(depth_t const d) const;
92  bool stoppingSearch(int64_t const nodes, long const nTimeElapsed) const;
93  bool stillTimeLeft(depth_t const d, long const nTimeElapsed) const;
94 
95  // level update
96  void setRemainingTime(int const msPerGame);
97  void setMovePlayed();
98  void undoMovePlayed();
99  void setEstimatedMovesLeft(int const n) { m_movesleftingame = n; }
100 
101 private:
102  void recalibrateTime();
103  std::string prettyDepth(depth_t const d) const;
104  std::string prettyMoves(int const d) const;
105 
106  LevelType m_oType;
107  depth_t m_nDepth = DEFAULT_DEPTH;
109 
110  int m_nTimeForMove = 0; /// indicative time for searching
111  int m_nTimeLeftForGame = 0; /// total time left for n moves or game
112  int m_nRemainingMovesForPeriod = 0; /// total time left for next time control
113  int m_movesleftingame = 32; /// estimated number of moves left in game (for some time controls)
114 
115  // aborting time control
116  int m_nMaxTimeForMove = 0; /// time when to abort search
117  int m_nEstAllowNextIterationTime = 0; /// indicative time for next iteration
118  int m_nAbsoluteAbortTime = 0; /// time after which an abort if forced
119 
120  // level as set initially
121  bool m_levelchanged = true;
122  bool m_pondering = false;
123  int64_t m_nNodes = 0; /// maximum number of nodes allowed
124  int m_timeForGame = 0; /// total time per game set
125  int m_movesPerPeriod = 0; /// number of moves per period
126  int m_incrementPerMove = 0; /// number of milliseconds added each move
127 };
128 
129 #endif // defined LEVEL_H
130 
131 // eof
int_fast8_t depth_t
Definition: belofte.h:105
Definition: level.h:51
void clearPondering()
Definition: level.cpp:270
void setDepth(depth_t const d)
Definition: level.cpp:238
void setGameTime(int const msPerGame)
new level or new game
Definition: level.cpp:40
void setInfinite()
Definition: level.cpp:30
void setRemainingTime(int const msPerGame)
xboard issues time command to update available time
Definition: level.cpp:141
void setEstimatedMovesLeft(int const n)
Definition: level.h:99
void setNodes(int64_t const n)
Definition: level.cpp:255
void setMovePlayed()
Definition: level.cpp:151
void setMoveTime(int const msPerMove)
Definition: level.cpp:72
bLevel()
Definition: level.cpp:12
bLevel & operator=(bLevel &&)=delete
depth_t getDepth() const
Definition: level.cpp:233
void setMateSearch(depth_t const d)
set level to search mate in n different from bruteforce search which is non optimised
Definition: level.cpp:129
void setMoves(std::string movelist)
bool isPondering() const
Definition: level.cpp:275
~bLevel()
Definition: level.cpp:19
bool qsDepthReached(depth_t const d) const
Definition: level.cpp:287
LevelType getType() const
Definition: level.cpp:260
void setPondering()
Definition: level.cpp:265
void flagLevelChange()
Definition: level.cpp:25
bool stoppingSearch(int64_t const nodes, long const nTimeElapsed) const
Stop search required ?
Definition: level.cpp:298
void initDepths(depth_t const d)
Definition: level.cpp:243
bool stillTimeLeft(depth_t const d, long const nTimeElapsed) const
Do we still have time to do another iteration ?
Definition: level.cpp:320
void undoMovePlayed()
used for recalibrating time in case for undo move in xboard moves per period
Definition: level.cpp:163
bLevel & operator=(bLevel const &)=default
bool depthReached(depth_t const d) const
Definition: level.cpp:282
bLevel(bLevel const &)=delete
bLevel(bLevel &&)=delete
constexpr auto TIME_OVERFLOWMULTIPLYER
Definition: level.h:16
constexpr auto TIME_UNDERFLOWDEVIDER
Definition: level.h:21
constexpr auto TIME_UNDERFLOWMULTIPLYER
Definition: level.h:18
constexpr auto MINIMAL_DEPTH_COMPLETED
Definition: level.h:15
constexpr auto INFINITE_DEPTH
Definition: level.h:11
constexpr auto QS_DEPTHEXTENSION
Definition: level.h:14
constexpr auto QSDEPTH_MULTIPLYER
Definition: level.h:13
constexpr auto DEFAULT_MOVETIME
Definition: level.h:28
constexpr auto TIME_LOSTINENGINE
time lost in between UI go command and bestmove return
Definition: level.h:25
LevelType
Implements clock n seconds per game - setGameTime(seconds * 1000) n seconds per game,...
Definition: level.h:48
@ FicherTimeControl
constexpr auto DEFAULT_DEPTH
Definition: level.h:12
constexpr auto TIME_LASTMOVEMARGIN
Definition: level.h:26