12 #include "tactics/util.h"
15 /* Max net lag in seconds. TODO: estimate dynamically. */
16 #define MAX_NET_LAG 2.0
17 /* Minimal thinking time; in case reserved time gets smaller than MAX_NET_LAG,
18 * this makes sure we play minimally sensible moves even in massive time
19 * pressure; we still keep MAX_NET_LAG-MIN_THINK_WITH_LAG safety margin.
20 * Note that this affects only lag adjustmnet - if reserved time *before*
21 * lag adjustment gets too small, we still respect it and don't apply
22 * MIN_THINK_WITH_LAG. */
23 #define MIN_THINK_WITH_LAG (MAX_NET_LAG / 2)
24 /* Reserve 15% of byoyomi time as safety margin if risk of losing on time */
25 #define RESERVED_BYOYOMI_PERCENT 15
27 /* For safety, use at most 3 times the desired time on a single move
28 * in main time, 2 times in sudden death and 1.1 times in byoyomi. */
29 #define MAX_MAIN_TIME_EXTENSION 3.0
30 #define MAX_SUDDEN_DEATH_EXTENSION 2.0
31 #define MAX_BYOYOMI_TIME_EXTENSION 1.1
34 time_parse(struct time_info
*ti
, char *s
)
37 case '_': ti
->period
= TT_TOTAL
; s
++; break;
38 default: ti
->period
= TT_MOVE
; break;
43 ti
->len
.games
= atoi(++s
);
48 ti
->dim
= TD_WALLTIME
;
49 ti
->len
.t
.timer_start
= 0;
50 if (ti
->period
== TT_TOTAL
) {
51 ti
->len
.t
.main_time
= atof(s
);
52 ti
->len
.t
.byoyomi_time
= 0.0;
53 ti
->len
.t
.byoyomi_time_max
= 0.0;
54 ti
->len
.t
.byoyomi_periods
= 0;
55 ti
->len
.t
.byoyomi_stones
= 0;
56 ti
->len
.t
.byoyomi_stones_max
= 0;
57 } else { assert(ti
->period
== TT_MOVE
);
58 ti
->len
.t
.main_time
= 0.0;
59 ti
->len
.t
.byoyomi_time
= atof(s
);
60 ti
->len
.t
.byoyomi_time_max
= ti
->len
.t
.byoyomi_time
;
61 ti
->len
.t
.byoyomi_periods
= 1;
62 ti
->len
.t
.byoyomi_stones
= 1;
63 ti
->len
.t
.byoyomi_stones_max
= 1;
70 /* Update time settings according to gtp time_settings or kgs-time_settings command. */
72 time_settings(struct time_info
*ti
, int main_time
, int byoyomi_time
, int byoyomi_stones
, int byoyomi_periods
)
75 ti
->period
= TT_NULL
; // no time limit, rely on engine default
77 ti
->period
= main_time
> 0 ? TT_TOTAL
: TT_MOVE
;
78 ti
->dim
= TD_WALLTIME
;
79 ti
->len
.t
.timer_start
= 0;
80 ti
->len
.t
.main_time
= (double) main_time
;
81 ti
->len
.t
.byoyomi_time
= (double) byoyomi_time
;
82 ti
->len
.t
.byoyomi_periods
= byoyomi_periods
;
83 ti
->len
.t
.byoyomi_stones
= byoyomi_stones
;
84 ti
->len
.t
.canadian
= byoyomi_stones
> 0;
85 if (byoyomi_time
> 0) {
86 /* Normally, only one of byoyomi_periods and
87 * byoyomi_stones arguments will be > 0. However,
88 * our data structure uses generalized byoyomi
89 * specification that will assume "1 byoyomi period
90 * of N stones" for Canadian byoyomi and "N byoyomi
91 * periods of 1 stone" for Japanese byoyomi. */
92 if (ti
->len
.t
.byoyomi_periods
< 1)
93 ti
->len
.t
.byoyomi_periods
= 1;
94 if (ti
->len
.t
.byoyomi_stones
< 1)
95 ti
->len
.t
.byoyomi_stones
= 1;
97 assert(!ti
->len
.t
.byoyomi_periods
&& !ti
->len
.t
.byoyomi_stones
);
99 ti
->len
.t
.byoyomi_time_max
= ti
->len
.t
.byoyomi_time
;
100 ti
->len
.t
.byoyomi_stones_max
= ti
->len
.t
.byoyomi_stones
;
104 /* Update time information according to gtp time_left command.
105 * kgs doesn't give time_left for the first move, so make sure
106 * that just time_settings + time_stop_conditions still work. */
108 time_left(struct time_info
*ti
, int time_left
, int stones_left
)
110 assert(ti
->period
!= TT_NULL
);
111 ti
->dim
= TD_WALLTIME
;
113 if (!time_left
&& !stones_left
) {
114 /* Some GTP peers send time_left 0 0 at the end of main time. */
115 ti
->period
= TT_MOVE
;
116 ti
->len
.t
.main_time
= 0;
117 /* byoyomi_time kept fully charged. */
119 } else if (!stones_left
) {
121 ti
->period
= TT_TOTAL
;
122 ti
->len
.t
.main_time
= time_left
;
123 /* byoyomi_time kept fully charged. */
127 ti
->period
= TT_MOVE
;
128 ti
->len
.t
.main_time
= 0;
129 ti
->len
.t
.byoyomi_time
= time_left
;
130 if (ti
->len
.t
.canadian
) {
131 ti
->len
.t
.byoyomi_stones
= stones_left
;
133 // field misused by kgs
134 ti
->len
.t
.byoyomi_periods
= stones_left
;
139 /* Start our timer. kgs does this (correctly) on "play" not "genmove"
140 * unless we are making the first move of the game. */
142 time_start_timer(struct time_info
*ti
)
144 if (ti
->period
!= TT_NULL
&& ti
->dim
== TD_WALLTIME
)
145 ti
->len
.t
.timer_start
= time_now();
149 time_sub(struct time_info
*ti
, double interval
, bool new_move
)
151 assert(ti
->dim
== TD_WALLTIME
&& ti
->period
!= TT_NULL
);
153 if (ti
->period
== TT_TOTAL
) {
154 ti
->len
.t
.main_time
-= interval
;
155 if (ti
->len
.t
.main_time
>= 0)
157 if (ti
->len
.t
.byoyomi_time
<= 0) {
158 /* No byoyomi to save us. */
159 fprintf(stderr
, "*** LOST ON TIME internally! (%0.2f, spent %0.2fs on last move)\n",
160 ti
->len
.t
.main_time
, interval
);
161 /* What can we do? Pretend this didn't happen. */
162 ti
->len
.t
.main_time
= 1.0f
;
165 /* Fall-through to byoyomi. */
166 ti
->period
= TT_MOVE
;
167 interval
= -ti
->len
.t
.main_time
;
168 ti
->len
.t
.main_time
= 0;
171 ti
->len
.t
.byoyomi_time
-= interval
;
172 if (ti
->len
.t
.byoyomi_time
< 0) {
174 if (--ti
->len
.t
.byoyomi_periods
< 1) {
175 fprintf(stderr
, "*** LOST ON TIME internally! (%0.2f, spent %0.2fs on last move)\n",
176 ti
->len
.t
.byoyomi_time
, interval
);
177 /* Well, what can we do? Pretend this didn't happen. */
178 ti
->len
.t
.byoyomi_periods
= 1;
180 ti
->len
.t
.byoyomi_time
= ti
->len
.t
.byoyomi_time_max
;
181 ti
->len
.t
.byoyomi_stones
= ti
->len
.t
.byoyomi_stones_max
;
184 if (new_move
&& --ti
->len
.t
.byoyomi_stones
< 1) {
185 /* Finished a period. */
186 ti
->len
.t
.byoyomi_time
= ti
->len
.t
.byoyomi_time_max
;
187 ti
->len
.t
.byoyomi_stones
= ti
->len
.t
.byoyomi_stones_max
;
191 /* Returns the current time. */
195 #if _POSIX_TIMERS > 0
197 clock_gettime(CLOCK_REALTIME
, &now
);
198 return now
.tv_sec
+ now
.tv_nsec
/1000000000.0;
201 gettimeofday(&now
, NULL
);
202 return now
.tv_sec
+ now
.tv_usec
/1000000.0;
206 /* Sleep for a given interval (in seconds). Return immediately if interval < 0. */
208 time_sleep(double interval
)
212 ts
.tv_nsec
= (int)(modf(interval
, &sec
)*1000000000.0);
213 ts
.tv_sec
= (int)sec
;
214 nanosleep(&ts
, NULL
); /* ignore error if interval was < 0 */
218 /* Returns true if we are in byoyomi (or should play as if in byo yomi
219 * because remaining time per move in main time is less than byoyomi time
222 time_in_byoyomi(struct time_info
*ti
) {
223 assert(ti
->dim
== TD_WALLTIME
);
224 if (!ti
->len
.t
.byoyomi_time
)
225 return false; // there is no byoyomi!
226 assert(ti
->len
.t
.byoyomi_stones
> 0);
227 if (!ti
->len
.t
.main_time
)
228 return true; // we _are_ in byoyomi
229 if (ti
->len
.t
.main_time
<= ti
->len
.t
.byoyomi_time
/ ti
->len
.t
.byoyomi_stones
+ 0.001)
230 return true; // our basic time left is less than byoyomi time per move
234 /* Set worst.time to all available remaining time (main time plus usable
235 * byoyomi), to be spread over returned number of moves (expected game
236 * length minus moves to be played in final byoyomi - if we would not be
237 * able to spend more time on them in main time anyway). */
239 time_stop_set_remaining(struct time_info
*ti
, struct board
*b
, double net_lag
, struct time_stop
*stop
)
241 int moves_left
= board_estimated_moves_left(b
);
242 stop
->worst
.time
= ti
->len
.t
.main_time
;
244 if (!ti
->len
.t
.byoyomi_time
)
247 /* Time for one move in byoyomi. */
248 assert(ti
->len
.t
.byoyomi_stones
> 0);
249 double move_time
= ti
->len
.t
.byoyomi_time
/ ti
->len
.t
.byoyomi_stones
;
251 /* (i) Plan to extend our thinking time to make use of byoyom. */
253 /* For Japanese byoyomi with N>1 periods, we use N-1 periods
254 * as main time, keeping the last one as insurance against
255 * unexpected net lag. */
256 if (ti
->len
.t
.byoyomi_periods
> 2) {
257 stop
->worst
.time
+= (ti
->len
.t
.byoyomi_periods
- 2) * move_time
;
258 // Will add 1 more byoyomi_time just below
261 /* In case of Canadian byoyomi, include time that can be spent
262 * on its first move. */
263 stop
->worst
.time
+= move_time
;
265 /* (ii) Do not play faster in main time than we would in byoyomi. */
267 /* Maximize the number of moves played uniformly in main time,
268 * while not playing faster in main time than in byoyomi.
269 * At this point, the main time remaining is stop->worst.time and
270 * already includes the first (canadian) or N-1 byoyomi periods. */
271 double real_move_time
= move_time
- net_lag
;
272 if (real_move_time
> 0) {
273 int main_moves
= stop
->worst
.time
/ real_move_time
;
274 if (moves_left
> main_moves
) {
275 /* We plan to do too many moves in main time,
276 * do the rest in byoyomi. */
277 moves_left
= main_moves
;
279 if (moves_left
<= 0) // possible if too much lag
286 /* Adjust the recommended per-move time based on the current game phase.
287 * We expect stop->worst to be total time available, stop->desired the current
288 * per-move time allocation, and set stop->desired to adjusted per-move time. */
290 time_stop_phase_adjust(struct board
*b
, int fuseki_end
, int yose_start
, struct time_stop
*stop
)
292 int bsize
= (board_size(b
)-2)*(board_size(b
)-2);
293 fuseki_end
= fuseki_end
* bsize
/ 100; // move nb at fuseki end
294 yose_start
= yose_start
* bsize
/ 100; // move nb at yose start
295 assert(fuseki_end
< yose_start
);
297 /* No adjustments in yose. */
298 if (b
->moves
>= yose_start
)
300 int moves_to_yose
= (yose_start
- b
->moves
) / 2;
301 // ^- /2 because we only consider the moves we have to play ourselves
302 int left_at_yose_start
= board_estimated_moves_left(b
) - moves_to_yose
;
303 if (left_at_yose_start
< MIN_MOVES_LEFT
)
304 left_at_yose_start
= MIN_MOVES_LEFT
;
306 /* This particular value of middlegame_time will continuously converge
307 * to effective "yose_time" value as we approach yose_start. */
308 double middlegame_time
= stop
->worst
.time
/ left_at_yose_start
;
309 if (middlegame_time
< stop
->desired
.time
)
312 if (b
->moves
< fuseki_end
) {
313 assert(fuseki_end
> 0);
314 /* At the game start, use stop->desired.time (rather
315 * conservative estimate), then gradually prolong it. */
316 double beta
= b
->moves
/ fuseki_end
;
317 stop
->desired
.time
= middlegame_time
* beta
+ stop
->desired
.time
* (1 - beta
);
319 } else { assert(b
->moves
< yose_start
);
320 /* Middlegame, start with relatively large value, then
321 * converge to the uniform-timeslice yose value. */
322 stop
->desired
.time
= middlegame_time
;
327 lag_adjust(double *time
, double net_lag
)
329 double nolag_time
= *time
;
331 if (*time
< MIN_THINK_WITH_LAG
&& nolag_time
> MIN_THINK_WITH_LAG
)
332 *time
= MIN_THINK_WITH_LAG
;
335 /* Pre-process time_info for search control and sets the desired stopping conditions. */
337 time_stop_conditions(struct time_info
*ti
, struct board
*b
, int fuseki_end
, int yose_start
, struct time_stop
*stop
)
339 /* We must have _some_ limits by now, be it random default values! */
340 assert(ti
->period
!= TT_NULL
);
342 /* Special-case limit by number of simulations. */
343 if (ti
->dim
== TD_GAMES
) {
344 if (ti
->period
== TT_TOTAL
) {
345 ti
->period
= TT_MOVE
;
346 ti
->len
.games
/= board_estimated_moves_left(b
);
349 stop
->desired
.playouts
= ti
->len
.games
;
350 /* We force worst == desired, so note that we will NOT loop
351 * until best == winner. */
352 stop
->worst
.playouts
= ti
->len
.games
;
356 assert(ti
->dim
== TD_WALLTIME
);
359 /* Minimum net lag (seconds) to be reserved in the time for move. */
360 double net_lag
= MAX_NET_LAG
;
361 if (!ti
->len
.t
.timer_start
) {
362 ti
->len
.t
.timer_start
= time_now(); // we're playing the first game move
364 net_lag
+= time_now() - ti
->len
.t
.timer_start
;
365 // TODO: keep statistics to get good estimate of lag not just current move
369 if (ti
->period
== TT_TOTAL
&& time_in_byoyomi(ti
)) {
370 /* Technically, we are still in main time, but we can
371 * effectively switch to byoyomi scheduling since we
372 * have less time available than one byoyomi move takes. */
373 ti
->period
= TT_MOVE
;
377 if (ti
->period
== TT_MOVE
) {
378 /* We are in byoyomi, or almost! */
380 /* The period can still include some tiny remnant of main
381 * time if we are just switching to byoyomi. */
382 double period_len
= ti
->len
.t
.byoyomi_time
+ ti
->len
.t
.main_time
;
384 stop
->worst
.time
= period_len
;
385 assert(ti
->len
.t
.byoyomi_stones
> 0);
386 stop
->desired
.time
= period_len
/ ti
->len
.t
.byoyomi_stones
;
388 /* Use a larger safety margin if we risk losing on time on
389 * this move; it makes no sense to have 30s byoyomi and wait
390 * until 28s to play our move). */
391 if (stop
->desired
.time
>= period_len
- net_lag
) {
392 double safe_margin
= RESERVED_BYOYOMI_PERCENT
* stop
->desired
.time
/ 100;
393 if (safe_margin
> net_lag
)
394 net_lag
= safe_margin
;
397 /* Make recommended_old == average(recommended_new, max) */
398 double worst_time
= stop
->desired
.time
* MAX_BYOYOMI_TIME_EXTENSION
;
399 if (worst_time
< stop
->worst
.time
)
400 stop
->worst
.time
= worst_time
;
401 stop
->desired
.time
*= (2 - MAX_BYOYOMI_TIME_EXTENSION
);
403 } else { assert(ti
->period
== TT_TOTAL
);
404 /* We are in main time. */
406 assert(ti
->len
.t
.main_time
> 0);
407 /* Set worst.time to all available remaining time, to be spread
408 * over returned number of moves. */
409 int moves_left
= time_stop_set_remaining(ti
, b
, net_lag
, stop
);
411 /* Allocate even slice of the remaining time for next move. */
412 stop
->desired
.time
= stop
->worst
.time
/ moves_left
;
413 assert(stop
->desired
.time
> 0 && stop
->worst
.time
> 0);
414 assert(stop
->desired
.time
<= stop
->worst
.time
+ 0.001);
416 /* Furthermore, tweak the slice based on the game phase. */
417 time_stop_phase_adjust(b
, fuseki_end
, yose_start
, stop
);
419 /* Put final upper bound on maximal time spent on the move.
420 * Keep enough time for sudden death (or near SD) games. */
421 double worst_time
= stop
->desired
.time
;
422 if (ti
->len
.t
.byoyomi_time_max
> ti
->len
.t
.byoyomi_stones_max
) {
423 worst_time
*= MAX_MAIN_TIME_EXTENSION
;
425 worst_time
*= MAX_SUDDEN_DEATH_EXTENSION
;
427 if (worst_time
< stop
->worst
.time
)
428 stop
->worst
.time
= worst_time
;
429 if (stop
->desired
.time
> stop
->worst
.time
)
430 stop
->desired
.time
= stop
->worst
.time
;
434 fprintf(stderr
, "desired %0.2f, worst %0.2f, clock [%d] %0.2f + %0.2f/%d*%d, lag %0.2f\n",
435 stop
->desired
.time
, stop
->worst
.time
,
436 ti
->dim
, ti
->len
.t
.main_time
,
437 ti
->len
.t
.byoyomi_time
, ti
->len
.t
.byoyomi_stones
,
438 ti
->len
.t
.byoyomi_periods
, net_lag
);
440 /* Account for lag. */
441 lag_adjust(&stop
->desired
.time
, net_lag
);
442 lag_adjust(&stop
->worst
.time
, net_lag
);