For non-canadian byoyomi with N>1 periods, keep last period as insurance.
[pachi/json.git] / timeinfo.c
blob5290654b22daa2e41550ecbe414692128b680534
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.max_time = ti->len.t.recommended_time;
34 ti->len.t.net_lag = MAX_NET_LAG;
35 ti->len.t.timer_start = 0;
36 ti->len.t.byoyomi_time = 0.0;
37 ti->len.t.byoyomi_periods = 0;
38 break;
40 return true;
43 /* Update time settings according to gtp time_settings or kgs-time_settings command. */
44 void
45 time_settings(struct time_info *ti, int main_time, int byoyomi_time, int byoyomi_stones, int byoyomi_periods)
47 if (byoyomi_time > 0 && byoyomi_stones == 0) {
48 ti->period = TT_NULL; // no time limit, rely on engine default
49 } else {
50 ti->period = TT_TOTAL;
51 ti->dim = TD_WALLTIME;
52 ti->len.t.max_time = (double) main_time; // byoyomi will be added at next genmove
53 ti->len.t.recommended_time = ti->len.t.max_time;
54 ti->len.t.timer_start = 0;
55 ti->len.t.net_lag = MAX_NET_LAG;
56 ti->len.t.byoyomi_time = (double) byoyomi_time;
57 if (byoyomi_stones > 0)
58 ti->len.t.byoyomi_time /= byoyomi_stones;
59 ti->len.t.byoyomi_periods = byoyomi_periods;
63 /* Update time information according to gtp time_left command.
64 * kgs doesn't give time_left for the first move, so make sure
65 * that just time_settings + time_select_best still work. */
66 void
67 time_left(struct time_info *ti, int time_left, int stones_left)
69 assert(ti->period != TT_NULL);
70 ti->dim = TD_WALLTIME;
71 ti->len.t.max_time = (double)time_left;
73 if (ti->len.t.byoyomi_periods > 0 && stones_left > 0) {
74 ti->len.t.byoyomi_periods = stones_left; // field misused by kgs
75 stones_left = 1;
77 if (stones_left == 0) {
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(1) && 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 with N>1 periods, we use N-1 periods as main time,
123 * keeping the last one as insurance against unexpected net lag. */
124 if (ti->len.t.byoyomi_periods > 2) {
125 ti->len.t.max_time += (ti->len.t.byoyomi_periods - 2) * 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 N-1 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;
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 true if we are in byoyomi (or should play as if in byo yomi
181 * because remaining time per move in main time is less than byoyomi time
182 * per move). */
183 bool
184 time_in_byoyomi(struct time_info *ti) {
185 return ti->period == TT_MOVE && ti->dim == TD_WALLTIME && ti->len.t.byoyomi_time > 0
186 && ti->len.t.recommended_time <= ti->len.t.byoyomi_time + 0.001;
189 /* Returns the current time. */
190 double
191 time_now(void)
193 struct timespec now;
194 clock_gettime(CLOCK_REALTIME, &now);
195 return now.tv_sec + now.tv_nsec/1000000000.0;
198 /* Sleep for a given interval (in seconds). Return immediately if interval < 0. */
199 void
200 time_sleep(double interval)
202 struct timespec ts;
203 double sec;
204 ts.tv_nsec = (int)(modf(interval, &sec)*1000000000.0);
205 ts.tv_sec = (int)sec;
206 nanosleep(&ts, NULL); /* ignore error if interval was < 0 */