UCT: bestr_ratio 0.02 by default, best2_ratio 2.5 by default
[pachi/t.git] / timeinfo.c
blobde484eca5c8e84cbd7f877f33d9898123ec540f5
1 #include <assert.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include <time.h>
8 #define DEBUG
10 #include "debug.h"
11 #include "tactics.h"
12 #include "timeinfo.h"
14 #define MAX_NET_LAG 2.0 /* Max net lag in seconds. TODO: estimate dynamically. */
15 #define RESERVED_BYOYOMI_PERCENT 15 /* Reserve 15% of byoyomi time as safety margin if risk of losing on time */
17 /* For safety, use at most 3 times the desired time on a single move
18 * in main time, and 1.1 times in byoyomi. */
19 #define MAX_MAIN_TIME_EXTENSION 3.0
20 #define MAX_BYOYOMI_TIME_EXTENSION 1.1
22 bool
23 time_parse(struct time_info *ti, char *s)
25 switch (s[0]) {
26 case '_': ti->period = TT_TOTAL; s++; break;
27 default: ti->period = TT_MOVE; break;
29 switch (s[0]) {
30 case '=':
31 ti->dim = TD_GAMES;
32 ti->len.games = atoi(++s);
33 break;
34 default:
35 if (!isdigit(s[0]))
36 return false;
37 ti->dim = TD_WALLTIME;
38 ti->len.t.timer_start = 0;
39 if (ti->period == TT_TOTAL) {
40 ti->len.t.main_time = atof(s);
41 ti->len.t.byoyomi_time = 0.0;
42 ti->len.t.byoyomi_time_max = 0.0;
43 ti->len.t.byoyomi_periods = 0;
44 ti->len.t.byoyomi_stones = 0;
45 ti->len.t.byoyomi_stones_max = 0;
46 } else { assert(ti->period == TT_MOVE);
47 ti->len.t.main_time = 0.0;
48 ti->len.t.byoyomi_time = atof(s);
49 ti->len.t.byoyomi_time_max = ti->len.t.byoyomi_time;
50 ti->len.t.byoyomi_periods = 1;
51 ti->len.t.byoyomi_stones = 1;
52 ti->len.t.byoyomi_stones_max = 1;
54 break;
56 return true;
59 /* Update time settings according to gtp time_settings or kgs-time_settings command. */
60 void
61 time_settings(struct time_info *ti, int main_time, int byoyomi_time, int byoyomi_stones, int byoyomi_periods)
63 if (main_time < 0) {
64 ti->period = TT_NULL; // no time limit, rely on engine default
65 } else {
66 ti->period = main_time > 0 ? TT_TOTAL : TT_MOVE;
67 ti->dim = TD_WALLTIME;
68 ti->len.t.timer_start = 0;
69 ti->len.t.main_time = (double) main_time;
70 ti->len.t.byoyomi_time = (double) byoyomi_time;
71 ti->len.t.byoyomi_periods = byoyomi_periods;
72 ti->len.t.byoyomi_stones = byoyomi_stones;
73 ti->len.t.canadian = byoyomi_stones > 0;
74 if (byoyomi_time > 0) {
75 /* Normally, only one of byoyomi_periods and
76 * byoyomi_stones arguments will be > 0. However,
77 * our data structure uses generalized byoyomi
78 * specification that will assume "1 byoyomi period
79 * of N stones" for Canadian byoyomi and "N byoyomi
80 * periods of 1 stone" for Japanese byoyomi. */
81 if (ti->len.t.byoyomi_periods < 1)
82 ti->len.t.byoyomi_periods = 1;
83 if (ti->len.t.byoyomi_stones < 1)
84 ti->len.t.byoyomi_stones = 1;
85 } else {
86 assert(!ti->len.t.byoyomi_periods && !ti->len.t.byoyomi_stones);
88 ti->len.t.byoyomi_time_max = ti->len.t.byoyomi_time;
89 ti->len.t.byoyomi_stones_max = ti->len.t.byoyomi_stones;
93 /* Update time information according to gtp time_left command.
94 * kgs doesn't give time_left for the first move, so make sure
95 * that just time_settings + time_stop_conditions still work. */
96 void
97 time_left(struct time_info *ti, int time_left, int stones_left)
99 assert(ti->period != TT_NULL);
100 ti->dim = TD_WALLTIME;
102 if (!time_left && !stones_left) {
103 /* Some GTP peers send time_left 0 0 at the end of main time. */
104 ti->period = TT_MOVE;
105 ti->len.t.main_time = 0;
106 /* byoyomi_time kept fully charged. */
108 } else if (!stones_left) {
109 /* Main time */
110 ti->period = TT_TOTAL;
111 ti->len.t.main_time = time_left;
112 /* byoyomi_time kept fully charged. */
114 } else {
115 /* Byoyomi */
116 ti->period = TT_MOVE;
117 ti->len.t.main_time = 0;
118 ti->len.t.byoyomi_time = time_left;
119 if (ti->len.t.canadian) {
120 ti->len.t.byoyomi_stones = stones_left;
121 } else {
122 // field misused by kgs
123 ti->len.t.byoyomi_periods = stones_left;
128 /* Start our timer. kgs does this (correctly) on "play" not "genmove"
129 * unless we are making the first move of the game. */
130 void
131 time_start_timer(struct time_info *ti)
133 if (ti->period != TT_NULL && ti->dim == TD_WALLTIME)
134 ti->len.t.timer_start = time_now();
137 void
138 time_sub(struct time_info *ti, double interval)
140 assert(ti->dim == TD_WALLTIME && ti->period != TT_NULL);
142 if (ti->period == TT_TOTAL) {
143 ti->len.t.main_time -= interval;
144 if (ti->len.t.main_time >= 0)
145 return;
146 /* Fall-through to byoyomi. */
147 ti->period = TT_MOVE;
148 interval = -ti->len.t.main_time;
149 ti->len.t.main_time = 0;
152 ti->len.t.byoyomi_time -= interval;
153 if (ti->len.t.byoyomi_time < 0) {
154 /* Lost a period. */
155 if (--ti->len.t.byoyomi_periods < 1) {
156 fprintf(stderr, "*** LOST ON TIME internally! (%0.2f, spent %0.2fs on last move)\n",
157 ti->len.t.byoyomi_time, interval);
158 /* Well, what can we do? Pretend this didn't happen. */
159 ti->len.t.byoyomi_periods = 1;
161 ti->len.t.byoyomi_time = ti->len.t.byoyomi_time_max;
162 ti->len.t.byoyomi_stones = ti->len.t.byoyomi_stones_max;
163 return;
165 if (--ti->len.t.byoyomi_stones < 1) {
166 /* Finished a period. */
167 ti->len.t.byoyomi_time = ti->len.t.byoyomi_time_max;
168 ti->len.t.byoyomi_stones = ti->len.t.byoyomi_stones_max;
172 /* Returns the current time. */
173 double
174 time_now(void)
176 struct timespec now;
177 clock_gettime(CLOCK_REALTIME, &now);
178 return now.tv_sec + now.tv_nsec/1000000000.0;
181 /* Sleep for a given interval (in seconds). Return immediately if interval < 0. */
182 void
183 time_sleep(double interval)
185 struct timespec ts;
186 double sec;
187 ts.tv_nsec = (int)(modf(interval, &sec)*1000000000.0);
188 ts.tv_sec = (int)sec;
189 nanosleep(&ts, NULL); /* ignore error if interval was < 0 */
193 /* Returns true if we are in byoyomi (or should play as if in byo yomi
194 * because remaining time per move in main time is less than byoyomi time
195 * per move). */
196 static bool
197 time_in_byoyomi(struct time_info *ti) {
198 assert(ti->dim == TD_WALLTIME);
199 if (!ti->len.t.byoyomi_time)
200 return false; // there is no byoyomi!
201 assert(ti->len.t.byoyomi_stones > 0);
202 if (!ti->len.t.main_time)
203 return true; // we _are_ in byoyomi
204 if (ti->len.t.main_time <= ti->len.t.byoyomi_time / ti->len.t.byoyomi_stones + 0.001)
205 return true; // our basic time left is less than byoyomi time per move
206 return false;
209 /* Set worst.time to all available remaining time (main time plus usable
210 * byoyomi), to be spread over returned number of moves (expected game
211 * length minus moves to be played in final byoyomi - if we would not be
212 * able to spend more time on them in main time anyway). */
213 static int
214 time_stop_set_remaining(struct time_info *ti, struct board *b, double net_lag, struct time_stop *stop)
216 int moves_left = board_estimated_moves_left(b);
217 stop->worst.time = ti->len.t.main_time;
219 if (!ti->len.t.byoyomi_time)
220 return moves_left;
222 /* Time for one move in byoyomi. */
223 assert(ti->len.t.byoyomi_stones > 0);
224 double move_time = ti->len.t.byoyomi_time / ti->len.t.byoyomi_stones;
226 /* (i) Plan to extend our thinking time to make use of byoyom. */
228 /* For Japanese byoyomi with N>1 periods, we use N-1 periods
229 * as main time, keeping the last one as insurance against
230 * unexpected net lag. */
231 if (ti->len.t.byoyomi_periods > 2) {
232 stop->worst.time += (ti->len.t.byoyomi_periods - 2) * move_time;
233 // Will add 1 more byoyomi_time just below
236 /* In case of Canadian byoyomi, include time that can be spent
237 * on its first move. */
238 stop->worst.time += move_time;
240 /* (ii) Do not play faster in main time than we would in byoyomi. */
242 /* Maximize the number of moves played uniformly in main time,
243 * while not playing faster in main time than in byoyomi.
244 * At this point, the main time remaining is stop->worst.time and
245 * already includes the first (canadian) or N-1 byoyomi periods. */
246 double real_move_time = move_time - net_lag;
247 if (real_move_time > 0) {
248 int main_moves = stop->worst.time / real_move_time;
249 if (moves_left > main_moves) {
250 /* We plan to do too many moves in main time,
251 * do the rest in byoyomi. */
252 moves_left = main_moves;
254 if (moves_left <= 0) // possible if too much lag
255 moves_left = 1;
258 return moves_left;
261 /* Adjust the recommended per-move time based on the current game phase.
262 * We expect stop->worst to be total time available, stop->desired the current
263 * per-move time allocation, and set stop->desired to adjusted per-move time. */
264 static void
265 time_stop_phase_adjust(struct board *b, int fuseki_end, int yose_start, struct time_stop *stop)
267 int bsize = (board_size(b)-2)*(board_size(b)-2);
268 fuseki_end = fuseki_end * bsize / 100; // move nb at fuseki end
269 yose_start = yose_start * bsize / 100; // move nb at yose start
270 assert(fuseki_end < yose_start);
272 /* No adjustments in yose. */
273 if (b->moves >= yose_start)
274 return;
275 int moves_to_yose = (yose_start - b->moves) / 2;
276 // ^- /2 because we only consider the moves we have to play ourselves
277 int left_at_yose_start = board_estimated_moves_left(b) - moves_to_yose;
278 if (left_at_yose_start < MIN_MOVES_LEFT)
279 left_at_yose_start = MIN_MOVES_LEFT;
281 /* This particular value of middlegame_time will continuously converge
282 * to effective "yose_time" value as we approach yose_start. */
283 double middlegame_time = stop->worst.time / left_at_yose_start;
284 if (middlegame_time < stop->desired.time)
285 return;
287 if (b->moves < fuseki_end) {
288 assert(fuseki_end > 0);
289 /* At the game start, use stop->desired.time (rather
290 * conservative estimate), then gradually prolong it. */
291 double beta = b->moves / fuseki_end;
292 stop->desired.time = middlegame_time * beta + stop->desired.time * (1 - beta);
294 } else { assert(b->moves < yose_start);
295 /* Middlegame, start with relatively large value, then
296 * converge to the uniform-timeslice yose value. */
297 stop->desired.time = middlegame_time;
301 /* Pre-process time_info for search control and sets the desired stopping conditions. */
302 void
303 time_stop_conditions(struct time_info *ti, struct board *b, int fuseki_end, int yose_start, struct time_stop *stop)
305 /* We must have _some_ limits by now, be it random default values! */
306 assert(ti->period != TT_NULL);
308 /* Special-case limit by number of simulations. */
309 if (ti->dim == TD_GAMES) {
310 if (ti->period == TT_TOTAL) {
311 ti->period = TT_MOVE;
312 ti->len.games /= board_estimated_moves_left(b);
315 stop->desired.playouts = ti->len.games;
316 /* We force worst == desired, so note that we will NOT loop
317 * until best == winner. */
318 stop->worst.playouts = ti->len.games;
319 return;
322 assert(ti->dim == TD_WALLTIME);
325 /* Minimum net lag (seconds) to be reserved in the time for move. */
326 double net_lag = MAX_NET_LAG;
327 if (!ti->len.t.timer_start) {
328 ti->len.t.timer_start = time_now(); // we're playing the first game move
329 } else {
330 net_lag += time_now() - ti->len.t.timer_start;
331 // TODO: keep statistics to get good estimate of lag not just current move
335 if (ti->period == TT_TOTAL && time_in_byoyomi(ti)) {
336 /* Technically, we are still in main time, but we can
337 * effectively switch to byoyomi scheduling since we
338 * have less time available than one byoyomi move takes. */
339 ti->period = TT_MOVE;
343 if (ti->period == TT_MOVE) {
344 /* We are in byoyomi, or almost! */
346 /* The period can still include some tiny remnant of main
347 * time if we are just switching to byoyomi. */
348 double period_len = ti->len.t.byoyomi_time + ti->len.t.main_time;
350 stop->worst.time = period_len;
351 assert(ti->len.t.byoyomi_stones > 0);
352 stop->desired.time = period_len / ti->len.t.byoyomi_stones;
354 /* Use a larger safety margin if we risk losing on time on
355 * this move; it makes no sense to have 30s byoyomi and wait
356 * until 28s to play our move). */
357 if (stop->desired.time >= period_len - net_lag) {
358 double safe_margin = RESERVED_BYOYOMI_PERCENT * stop->desired.time / 100;
359 if (safe_margin > net_lag)
360 net_lag = safe_margin;
363 /* Make recommended_old == average(recommended_new, max) */
364 double worst_time = stop->desired.time * MAX_BYOYOMI_TIME_EXTENSION;
365 if (worst_time < stop->worst.time)
366 stop->worst.time = worst_time;
367 stop->desired.time *= (2 - MAX_BYOYOMI_TIME_EXTENSION);
369 } else { assert(ti->period == TT_TOTAL);
370 /* We are in main time. */
372 assert(ti->len.t.main_time > 0);
373 /* Set worst.time to all available remaining time, to be spread
374 * over returned number of moves. */
375 int moves_left = time_stop_set_remaining(ti, b, net_lag, stop);
377 /* Allocate even slice of the remaining time for next move. */
378 stop->desired.time = stop->worst.time / moves_left;
379 assert(stop->desired.time > 0 && stop->worst.time > 0);
380 assert(stop->desired.time <= stop->worst.time + 0.001);
382 /* Furthermore, tweak the slice based on the game phase. */
383 time_stop_phase_adjust(b, fuseki_end, yose_start, stop);
385 /* Put final upper bound on maximal time spent on the move. */
386 double worst_time = stop->desired.time * MAX_MAIN_TIME_EXTENSION;
387 if (worst_time < stop->worst.time)
388 stop->worst.time = worst_time;
389 if (stop->desired.time > stop->worst.time)
390 stop->desired.time = stop->worst.time;
393 if (DEBUGL(1))
394 fprintf(stderr, "desired %0.2f, worst %0.2f, clock [%d] %0.2f + %0.2f/%d*%d, lag %0.2f\n",
395 stop->desired.time, stop->worst.time,
396 ti->dim, ti->len.t.main_time,
397 ti->len.t.byoyomi_time, ti->len.t.byoyomi_stones,
398 ti->len.t.byoyomi_periods, net_lag);
400 /* Account for lag. */
401 stop->desired.time -= net_lag;
402 stop->worst.time -= net_lag;