Add support for time_left, time_setting and kgs-time_settings gtp commands.
[pachi/t.git] / timeinfo.c
blobfc497f455b5e17591ad1695463c60521dd9ada81
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 "timeinfo.h"
13 #define MAX_NET_LAG 2.0 /* Max net lag in seconds. TODO: estimate dynamically. */
14 #define RESERVED_BYOYOMI_PERCENT 15 /* Reserve 15% of byoyomi time as safety margin if risk of losing on time */
16 bool
17 time_parse(struct time_info *ti, char *s)
19 switch (s[0]) {
20 case '_': ti->period = TT_TOTAL; s++; break;
21 default: ti->period = TT_MOVE; break;
23 switch (s[0]) {
24 case '=':
25 ti->dim = TD_GAMES;
26 ti->len.games = atoi(++s);
27 break;
28 default:
29 if (!isdigit(s[0]))
30 return false;
31 ti->dim = TD_WALLTIME;
32 ti->len.t.recommended_time = atof(s);
33 ti->len.t.net_lag = MAX_NET_LAG;
34 ti->len.t.timer_start = 0;
35 ti->len.t.byoyomi_time = 0.0;
36 ti->len.t.byoyomi_periods = 0;
37 break;
39 return true;
42 /* Update time settings according to gtp time_settings or kgs-time_settings command. */
43 void
44 time_settings(struct time_info *ti, int main_time, int byoyomi_time, int byoyomi_stones, int byoyomi_periods)
46 if (byoyomi_time > 0 && byoyomi_stones == 0) {
47 ti->period = TT_NULL; // no time limit, rely on engine default
48 } else {
49 ti->period = TT_TOTAL;
50 ti->dim = TD_WALLTIME;
51 ti->len.t.max_time = (double) main_time; // byoyomi will be added at next genmove
52 ti->len.t.recommended_time = ti->len.t.max_time;
53 ti->len.t.timer_start = 0;
54 ti->len.t.net_lag = MAX_NET_LAG;
55 ti->len.t.byoyomi_time = (double) byoyomi_time;
56 if (byoyomi_stones > 0)
57 ti->len.t.byoyomi_time /= byoyomi_stones;
58 ti->len.t.byoyomi_periods = byoyomi_periods;
62 /* Update time information according to gtp time_left command.
63 * kgs doesn't give time_left for the first move, so make sure
64 * that just time_settings + time_select_best still work. */
65 void
66 time_left(struct time_info *ti, int time_left, int stones_left)
68 assert(ti->period != TT_NULL);
69 ti->dim = TD_WALLTIME;
70 ti->len.t.max_time = (double)time_left;
72 if (ti->len.t.byoyomi_periods > 0 && stones_left > 0) {
73 ti->len.t.byoyomi_periods = stones_left; // field misused by kgs
74 stones_left = 1;
76 /* For non-canadian byoyomi, we use all periods as main time. */
77 if (stones_left == 0 || ti->len.t.byoyomi_periods > 1) {
78 /* Main time */
79 ti->period = TT_TOTAL;
80 ti->len.t.recommended_time = ti->len.t.max_time;
81 /* byoyomi_time, net_lag & timer_start unchanged. */
82 } else {
83 ti->period = TT_MOVE;
84 ti->len.t.byoyomi_time = ((double)time_left)/stones_left;
85 ti->len.t.recommended_time = ti->len.t.byoyomi_time;
86 /* net_lag & timer_start unchanged. */
90 /* Set correct time information before making a move, and
91 * always make it time per move for the engine. */
92 void
93 time_prepare_move(struct time_info *ti, struct board *board)
95 int moves_left;
97 if (ti->period == TT_TOTAL) {
98 moves_left = board_estimated_moves_left(board);
99 assert(moves_left > 0);
100 if (ti->dim == TD_GAMES) {
101 ti->period = TT_MOVE;
102 ti->len.games /= moves_left;
105 if (ti->period == TT_NULL || ti->dim != TD_WALLTIME)
106 return;
108 double now = time_now();
109 double lag;
110 if (!ti->len.t.timer_start) {
111 ti->len.t.timer_start = now; // we're playing the first game move
112 lag = 0;
113 } else {
114 lag = now - ti->len.t.timer_start;
115 // TODO: keep statistics to get good estimate of lag not just current move
116 ti->len.t.max_time -= lag; // can become < 0, taken into account below
117 ti->len.t.recommended_time -= lag;
118 if (DEBUGL(2) && lag > MAX_NET_LAG)
119 fprintf(stderr, "lag %0.2f > max_net_lag %0.2f\n", lag, MAX_NET_LAG);
121 if (ti->period == TT_TOTAL) {
122 /* For non-canadian byoyomi, we use all periods as main time, just making sure
123 * to avoid running out of the last one. */
124 if (ti->len.t.byoyomi_periods > 1) {
125 ti->len.t.max_time += (ti->len.t.byoyomi_periods - 1) * ti->len.t.byoyomi_time;
126 // Will add 1 more byoyomi_time just below
128 if (ti->len.t.byoyomi_time > 0) {
129 ti->len.t.max_time += ti->len.t.byoyomi_time;
130 ti->len.t.recommended_time = ti->len.t.max_time;
132 /* Maximize the number of moves played uniformly in main time, while
133 * not playing faster in main time than in byoyomi. At this point,
134 * the main time remaining is ti->len.t.max_time and already includes
135 * the first (canadian) or all byoyomi periods.
136 * main_speed = max_time / main_moves >= byoyomi_time
137 * => main_moves <= max_time / byoyomi_time */
138 double actual_byoyomi = ti->len.t.byoyomi_time - MAX_NET_LAG;
139 if (actual_byoyomi > 0) {
140 int main_moves = (int)(ti->len.t.max_time / actual_byoyomi);
141 if (moves_left > main_moves)
142 moves_left = main_moves; // will do the rest in byoyomi
143 if (moves_left <= 0) // possible if too much lag
144 moves_left = 1;
147 ti->period = TT_MOVE;
148 ti->len.t.recommended_time /= moves_left; // may be < 0 if too much lag
150 // To simplify the engine code, do not leave negative times:
151 if (ti->len.t.recommended_time < 0)
152 ti->len.t.recommended_time = 0;
153 if (ti->len.t.max_time < 0)
154 ti->len.t.max_time = 0;
155 assert(ti->len.t.recommended_time <= ti->len.t.max_time + 0.001);
157 /* Use a larger safety margin if we risk losing on time on this move: */
158 double safe_margin = RESERVED_BYOYOMI_PERCENT * ti->len.t.byoyomi_time/100;
159 if (safe_margin > MAX_NET_LAG && ti->len.t.recommended_time >= ti->len.t.max_time - MAX_NET_LAG) {
160 ti->len.t.net_lag = safe_margin;
161 } else {
162 ti->len.t.net_lag = MAX_NET_LAG;
165 if (DEBUGL(1))
166 fprintf(stderr, "recommended_time %0.2f, max_time %0.2f, byoyomi %0.2f, lag %0.2f max %0.2f\n",
167 ti->len.t.recommended_time, ti->len.t.max_time, ti->len.t.byoyomi_time, lag,
168 ti->len.t.net_lag);
171 /* Start our timer. kgs does this (correctly) on "play" not "genmove"
172 * unless we are making the first move of the game. */
173 void
174 time_start_timer(struct time_info *ti)
176 if (ti->period != TT_NULL && ti->dim == TD_WALLTIME)
177 ti->len.t.timer_start = time_now();
180 /* Returns the current time. */
181 double
182 time_now(void)
184 struct timespec now;
185 clock_gettime(CLOCK_REALTIME, &now);
186 return now.tv_sec + now.tv_nsec/1000000000.0;
189 /* Sleep for a given interval (in seconds). Return immediately if interval < 0. */
190 void
191 time_sleep(double interval)
193 struct timespec ts;
194 double sec;
195 ts.tv_nsec = (int)(modf(interval, &sec)*1000000000.0);
196 ts.tv_sec = (int)sec;
197 nanosleep(&ts, NULL); /* ignore error if interval was < 0 */